Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Add Data Tables #14

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,39 @@ If your regular expression captures any matches, you should provide a list of va
puts "$quantity cucumbers bought. Price was $price"
}

You can use basic tables in your scenarios. The data from the table s made available to your step definition, via the last variable name you pass into the capture list. For example, if you had the following step in your feature:

Given I have added the following products to my shopping cart:
| apple | £2.00 |
| orange | £3.00 |
| banana | £1.50 |

I could write the step definition for the Given step as:

Given {^I have added the following products to my shopping cart:$} {table_data} {
puts "$table_data"
}

The data in the table is provided as a list of lists, so in the above example, table data would look like:

{"apple" "£2.00"} {"orange" "£3.00"} {"banana" "£1.50"}

meaning you can access each element using `lindex`, e.g.

puts [lindex [lindex $table_data 0] 0]
apple

If your step definition captures a match in the step definition as well as has a table, the table variable will always be last, e.g.

When I buy the following items from Tesco:
| cabbage |
| potato |
| onion |

and in your step definition, you might have

Given {^I buy the following items from (\w+):$} {store items} {
puts "I've been shopping at $store"
puts "The first item I bought was [lindex $items 0]"
}

27 changes: 27 additions & 0 deletions features/data_tables.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Feature: DataTables

@wip
Scenario: Match a step with a DataTable
Given a file named "features/test.feature" with:
"""
Feature:
Scenario:
Given passing with a DataTable:
| a | b |
| c | d |
"""
And a file named "features/support/env.rb" with:
"""
require 'cucumber/tcl'
"""
And a file named "features/step_defintions/steps.tcl" with:
"""
Given {^passing with a DataTable:$} {content} {
puts $content
}
"""
When I run `cucumber`
Then it should pass with:
"""
{{a} {b}} {{c} {d}}
"""
24 changes: 24 additions & 0 deletions lib/cucumber/tcl/data_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Cucumber
module Tcl

# Wraps the Cucumber DataTable so that when passed through to tcl, its
# string representation is easy to parse into a tcl list.
class DataTable
def initialize(original)
@raw = original.raw
end

def to_s
to_tcl_list(@raw.map { |row| to_tcl_list(row) })
end

private

def to_tcl_list(array)
array.map { |element| "{" + element + "}" }.join(" ")
end
end

end
end

1 change: 0 additions & 1 deletion lib/cucumber/tcl/framework.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ proc step_definition_exists { step_name } {


proc execute_step_definition { step_name {multiline_args {}} } {
# TODO: handle parameters in the regexp
set res [_search_steps $step_name 1 $multiline_args]
return $res

Expand Down
5 changes: 5 additions & 0 deletions lib/cucumber/tcl/step_definitions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'tcl'
require 'cucumber/tcl/data_table'

module Cucumber
module Tcl
Expand Down Expand Up @@ -30,6 +31,10 @@ def doc_string(arg)
@arguments << arg.content
end

def data_table(arg)
@arguments << DataTable.new(arg)
end

def to_a
@arguments
end
Expand Down
13 changes: 13 additions & 0 deletions spec/cucumber/tcl/data_table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'cucumber/tcl/data_table'
require 'cucumber/core/ast/data_table'

module Cucumber::Tcl
describe DataTable do
it "#to_s converts to Tcl list of lists" do
raw = [["a", "b"], ["c", "d"]]
original = Cucumber::Core::Ast::DataTable.new(raw, Cucumber::Core::Ast::Location.new(__FILE__, 8))
table = DataTable.new(original)
expect(table.to_s).to eq '{{a} {b}} {{c} {d}}'
end
end
end