dream_test/gherkin/types
Gherkin types for dream_test.
This module defines the data structures representing parsed Gherkin
.feature files. These types are used by the parser and converted
to dream_test’s TestCase and TestSuite types for execution.
Type Overview
| Type | Purpose |
|---|---|
Feature | A parsed .feature file |
Scenario | A single scenario or scenario outline |
Step | A Given/When/Then step with text |
StepKeyword | The keyword type (Given, When, Then, And, But) |
StepArgument | Optional DocString or DataTable |
Background | Steps to run before each scenario |
ExamplesTable | Data table for Scenario Outline expansion |
Gherkin Syntax Reference
@tag1 @tag2
Feature: Shopping Cart
As a customer
I want to manage my cart
Background:
Given I am logged in
Scenario: Adding items
Given I have an empty cart
When I add 3 items of "Widget"
Then my cart should have 3 items
Scenario Outline: Multiple products
Given I have an empty cart
When I add <quantity> items of "<product>"
Then my cart should have <quantity> items
Examples:
| quantity | product |
| 1 | Widget |
| 5 | Gadget |
Types
Background steps that run before each scenario.
A Background defines common setup steps that are executed before every scenario in the feature. It’s useful for shared preconditions.
Example
Background:
Given I am logged in as "admin"
And I am on the dashboard
These steps run before each scenario, as if they were prepended.
pub type Background {
Background(steps: List(Step))
}
Constructors
-
Background(steps: List(Step))
Examples table for Scenario Outline.
Provides data for parameterized scenarios. Each row generates a separate scenario instance with placeholders replaced by values.
Example
Examples:
| quantity | product |
| 1 | Widget |
| 5 | Gadget |
With a scenario outline:
When I add <quantity> items of "<product>"
This expands to two scenarios:
- “When I add 1 items of "Widget"”
- “When I add 5 items of "Gadget"”
pub type ExamplesTable {
ExamplesTable(headers: List(String), rows: List(List(String)))
}
Constructors
-
ExamplesTable(headers: List(String), rows: List(List(String)))Arguments
- headers
-
Column headers (e.g., [“quantity”, “product”])
- rows
-
Data rows, each containing values for all columns
A parsed Gherkin feature file.
A feature represents a complete .feature file containing:
- Metadata (name, description, tags)
- Optional background steps
- One or more scenarios
Example
@shopping
Feature: Shopping Cart
As a customer
I want to manage items in my cart
So that I can purchase what I need
Background:
Given I am logged in
Scenario: Empty cart
Given I have an empty cart
Then my cart count should be 0
@slow
Scenario: Adding items
Given I have an empty cart
When I add 3 items of "Widget"
Then my cart should have 3 items
pub type Feature {
Feature(
name: String,
description: option.Option(String),
tags: List(String),
background: option.Option(Background),
scenarios: List(Scenario),
)
}
Constructors
-
Feature( name: String, description: option.Option(String), tags: List(String), background: option.Option(Background), scenarios: List(Scenario), )Arguments
- name
-
The feature name (from the Feature: line)
- description
-
Optional description text below the feature name
- tags
-
Tags applied to the feature (inherited by all scenarios)
- background
-
Optional background steps run before each scenario
- scenarios
-
The scenarios in this feature
A scenario within a feature.
There are two variants:
Scenario
A single test case with fixed steps:
Scenario: Adding items to cart
Given I have an empty cart
When I add 3 items
Then my cart should have 3 items
ScenarioOutline
A parameterized test with examples table:
Scenario Outline: Adding products
When I add <qty> items of "<name>"
Then my cart should have <qty> items
Examples:
| qty | name |
| 1 | Widget |
| 5 | Gadget |
pub type Scenario {
Scenario(name: String, tags: List(String), steps: List(Step))
ScenarioOutline(
name: String,
tags: List(String),
steps: List(Step),
examples: ExamplesTable,
)
}
Constructors
-
Scenario(name: String, tags: List(String), steps: List(Step))A standard scenario with fixed step values.
Arguments
- name
-
The scenario name
- tags
-
Tags applied to this scenario (e.g., @slow, @wip)
- steps
-
The steps to execute
-
ScenarioOutline( name: String, tags: List(String), steps: List(Step), examples: ExamplesTable, )A parameterized scenario expanded from examples.
Arguments
- name
-
The scenario outline name
- tags
-
Tags applied to this scenario outline
- steps
-
The step templates with
syntax - examples
-
The examples table for parameterization
A single step in a scenario.
Steps are the executable units of a scenario. Each step has:
- A keyword (Given/When/Then/And/But)
- Text describing the action
- Optional argument (DocString or DataTable)
Example
Given I have 5 items in my cart
When I remove 2 items
Then I should have 3 items
pub type Step {
Step(
keyword: StepKeyword,
text: String,
argument: option.Option(StepArgument),
)
}
Constructors
-
Step( keyword: StepKeyword, text: String, argument: option.Option(StepArgument), )Arguments
- keyword
-
The step keyword (Given, When, Then, And, But)
- text
-
The step text after the keyword
- argument
-
Optional DocString or DataTable argument
Optional argument attached to a step.
Steps can have additional structured data:
DocString
Multi-line text enclosed in triple quotes:
Given a document with content:
"""json
{"name": "example"}
"""
DataTable
Tabular data with pipe-delimited rows:
Given the following users:
| name | email |
| Alice | alice@test.com |
| Bob | bob@test.com |
pub type StepArgument {
DocString(content: String, content_type: option.Option(String))
DataTable(rows: List(List(String)))
}
Constructors
-
DocString(content: String, content_type: option.Option(String))Multi-line text content.
content- The text between the triple quotescontent_type- Optional media type hint (e.g., “json”, “xml”)
-
DataTable(rows: List(List(String)))Tabular data as a list of rows. Each row is a list of cell values. The first row is typically headers.
Keywords that can start a step.
In Gherkin, each step begins with one of these keywords:
Given- Describes initial context/preconditionsWhen- Describes an actionThen- Describes an expected outcomeAnd/But- Continues the previous step type
When matching step definitions, And and But inherit the keyword
type from the previous step (e.g., And after Given is treated as Given).
pub type StepKeyword {
Given
When
Then
And
But
}
Constructors
-
Given -
When -
Then -
And -
But
Values
pub fn empty_background() -> Background
Create an empty background.
Useful as a default value or for testing.
pub fn empty_examples() -> ExamplesTable
Create an empty examples table.
Useful as a default value or for testing.
pub fn keyword_from_string(
text: String,
) -> option.Option(StepKeyword)
Parse a string to a StepKeyword.
Returns the keyword if recognized, or None for unknown strings.
Example
keyword_from_string("Given") // -> Some(Given)
keyword_from_string("Hello") // -> None
pub fn keyword_to_string(keyword: StepKeyword) -> String
Convert a StepKeyword to its string representation.
Example
keyword_to_string(Given) // -> "Given"
keyword_to_string(And) // -> "And"
pub fn resolve_keyword(
keyword: StepKeyword,
previous: StepKeyword,
) -> StepKeyword
Resolve And/But to the effective keyword based on previous step.
In Gherkin, And and But inherit meaning from the previous step.
This function resolves them to their effective keyword type.
Example
resolve_keyword(And, Given) // -> Given
resolve_keyword(But, Then) // -> Then
resolve_keyword(When, Given) // -> When (non-And/But unchanged)