Class: Cucumber::Core::Test::DataTable
- Inherits:
-
Object
- Object
- Cucumber::Core::Test::DataTable
- Defined in:
- lib/cucumber/core/test/data_table.rb
Overview
Step Definitions that match a plain text Step with a multiline argument table will receive it as an instance of DataTable. A DataTable object holds the data of a table parsed from a feature file and lets you access and manipulate the data in different ways.
For example:
Given I have:
| a | b |
| c | d |
And a matching StepDefinition:
Given /I have:/ do |table|
data = table.raw
end
This will store [['a', 'b'], ['c', 'd']]
in the data
variable.
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #data_table? ⇒ Boolean
- #describe_to(visitor, *args) ⇒ Object
- #doc_string? ⇒ Boolean
-
#dup ⇒ Object
Creates a copy of this table.
-
#initialize(rows) ⇒ DataTable
constructor
Creates a new instance.
- #inspect ⇒ Object
- #lines_count ⇒ Object
- #map(&block) ⇒ Object
- #to_step_definition_arg ⇒ Object
-
#transpose ⇒ Object
Returns a new, transposed table.
Constructor Details
#initialize(rows) ⇒ DataTable
Creates a new instance. raw
should be an Array of Array of String or an Array of Hash You don’t typically create your own DataTable objects - Cucumber will do it internally and pass them to your Step Definitions.
31 32 33 34 35 |
# File 'lib/cucumber/core/test/data_table.rb', line 31 def initialize(rows) raw = ensure_array_of_array(rows) verify_rows_are_same_length(raw) @raw = raw.freeze end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
36 37 38 |
# File 'lib/cucumber/core/test/data_table.rb', line 36 def raw @raw end |
Instance Method Details
#==(other) ⇒ Object
87 88 89 |
# File 'lib/cucumber/core/test/data_table.rb', line 87 def ==(other) other.class == self.class && raw == other.raw end |
#data_table? ⇒ Boolean
46 47 48 |
# File 'lib/cucumber/core/test/data_table.rb', line 46 def data_table? true end |
#describe_to(visitor, *args) ⇒ Object
38 39 40 |
# File 'lib/cucumber/core/test/data_table.rb', line 38 def describe_to(visitor, *args) visitor.data_table(self, *args) end |
#doc_string? ⇒ Boolean
50 51 52 |
# File 'lib/cucumber/core/test/data_table.rb', line 50 def doc_string? false end |
#dup ⇒ Object
Creates a copy of this table
56 57 58 |
# File 'lib/cucumber/core/test/data_table.rb', line 56 def dup self.class.new(raw.dup) end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/cucumber/core/test/data_table.rb', line 91 def inspect %{#<#{self.class} #{raw.inspect})>} end |
#lines_count ⇒ Object
83 84 85 |
# File 'lib/cucumber/core/test/data_table.rb', line 83 def lines_count raw.count end |
#map(&block) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/cucumber/core/test/data_table.rb', line 75 def map(&block) new_raw = raw.map do |row| row.map(&block) end self.class.new(new_raw) end |
#to_step_definition_arg ⇒ Object
42 43 44 |
# File 'lib/cucumber/core/test/data_table.rb', line 42 def to_step_definition_arg dup end |
#transpose ⇒ Object
Returns a new, transposed table. Example:
| a | 7 | 4 |
| b | 9 | 2 |
Gets converted into the following:
| a | b |
| 7 | 9 |
| 4 | 2 |
71 72 73 |
# File 'lib/cucumber/core/test/data_table.rb', line 71 def transpose self.class.new(raw.transpose) end |