Permalink
Browse files

Add key/value pairs (hashes) data table conversion

  • Loading branch information...
1 parent 7ef49e8 commit 29bd62886a0197924e1fcd48a6c55b6750c017ea @jbpros jbpros committed Oct 19, 2011
Showing with 47 additions and 5 deletions.
  1. +16 −0 data_tables.feature
  2. +8 −0 step_definitions/cucumber_mappings.rb
  3. +23 −5 step_definitions/cucumber_stepdefs.rb
View
16 data_tables.feature
@@ -17,3 +17,19 @@ Feature: Data Tables
[ "Cucumis anguria", "Burr Gherkin" ]
]
"""
+
+ Scenario: a data table can be read as an array of hashes
+ Given the following data table in a step:
+ """
+ | Latin | English |
+ | Cucumis sativus | Cucumber |
+ | Cucumis anguria | Burr Gherkin |
+ """
+ When the data table is passed to a step mapping that converts it to key/value pairs
+ Then the data table is converted to the following:
+ """
+ [
+ { "Latin":"Cucumis sativus", "English":"Cucumber" },
+ { "Latin":"Cucumis anguria", "English":"Burr Gherkin" }
+ ]
+ """
View
8 step_definitions/cucumber_mappings.rb
@@ -32,4 +32,12 @@ def step_file(pattern)
def pattern_exists?(pattern)
File.exist?(File.join(current_dir, step_file(pattern)))
end
+
+ protected
+
+ def indent_code(code, levels = 1)
+ indented = ''
+ code.each_line { |line| indented += "#{' ' * levels}#{line}" }
+ indented
+ end
end
View
28 step_definitions/cucumber_stepdefs.rb
@@ -1,6 +1,7 @@
require File.dirname(__FILE__) + '/cucumber_mappings'
-DEFAULT_SCENARIO_NAME = "scenario"
+DEFAULT_SCENARIO_NAME = "scenario"
+DATA_TABLE_RECEIVING_STEP_NAME = "a table:"
World(CucumberMappings)
@@ -30,7 +31,16 @@
end
Given /^the step "([^"]*)" has a passing mapping that receives a data table$/ do |step_name|
- write_mapping_receiving_data_table(step_name)
+ write_mapping_receiving_data_table_as_raw(step_name)
+end
+
+Given /^the following data table in a step:$/ do |data_table_string|
+ write_feature <<-EOF
+Feature:
+ Scenario:
+ Given #{DATA_TABLE_RECEIVING_STEP_NAME}
+#{indent_code(data_table_string, 3)}
+EOF
end
Given /^a World variable initialized to (\d+)$/ do |value|
@@ -87,7 +97,6 @@
run_feature
end
-#"
When /^Cucumber executes a scenario that calls the World function$/ do
write_mapping_calling_world_function("I call the world function")
write_feature <<-EOF
@@ -98,6 +107,11 @@
run_feature
end
+When /^the data table is passed to a step mapping that converts it to key\/value pairs$/ do
+ write_mapping_receiving_data_table_as_hashes(DATA_TABLE_RECEIVING_STEP_NAME)
+ run_feature
+end
+
Then /^the scenario passes$/ do
assert_passing_scenario
end
@@ -149,6 +163,10 @@
assert_world_function_called
end
-Then /^the received data table array equals the following:$/ do |array_source|
- assert_data_table_equals_array_source(array_source)
+Then /^the received data table array equals the following:$/ do |json|
+ assert_data_table_equals_json(json)
+end
+
+Then /^the data table is converted to the following:$/ do |json|
+ assert_data_table_equals_json(json)
end

0 comments on commit 29bd628

Please sign in to comment.