class Cucumber::Core::Ast::DataTable
This will store [['a', 'b'], ['c', 'd']]
in the data
variable.
end
data = table.raw
Given /I have:/ do |table|
And a matching StepDefinition:
| c | d |
| a | b |
Given I have:
For example:
in different ways.
table parsed from a feature file and lets you access and manipulate the data
will receive it as an instance of DataTable. A DataTable object holds the data of a
Step Definitions that match a plain text Step with a multiline argument table
def ==(other)
def ==(other) other.class == self.class && raw == other.raw end
def data_table?
def data_table? true end
def description_for_visitors
def description_for_visitors :data_table end
def doc_string?
def doc_string? false end
def dup
Creates a copy of this table
def dup self.class.new(raw.dup, location) end
def ensure_array_of_array(array)
def ensure_array_of_array(array) Hash === array[0] ? hashes_to_array(array) : array end
def hashes_to_array(hashes)
def hashes_to_array(hashes) header = hashes[0].keys.sort [header] + hashes.map{|hash| header.map{|key| hash[key]}} end
def initialize(rows, location)
it internally and pass them to your Step Definitions.
You don't typically create your own DataTable objects - Cucumber will do
or an Array of Hash
Creates a new instance. +raw+ should be an Array of Array of String
def initialize(rows, location) raw = ensure_array_of_array(rows) verify_rows_are_same_length(raw) @raw = raw.freeze @location = location end
def inspect
def inspect %{#<#{self.class} #{raw.inspect} (#{location})>} end
def map(&block)
def map(&block) new_raw = raw.map do |row| row.map(&block) end self.class.new(new_raw, location) end
def to_step_definition_arg
def to_step_definition_arg dup end
def transpose
| 4 | 2 |
| 7 | 9 |
| a | b |
Gets converted into the following:
| b | 9 | 2 |
| a | 7 | 4 |
Returns a new, transposed table. Example:
def transpose self.class.new(raw.transpose, location) end
def verify_rows_are_same_length(raw)
def verify_rows_are_same_length(raw) begin raw.transpose rescue IndexError raise ArgumentError, "Rows must all be the same length" end end