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 self.parse(text, uri, location)
def self.parse(text, uri, location) builder = Builder.new lexer = ::Gherkin::Lexer::I18nLexer.new(builder) lexer.scan(text) new(builder.rows, location) end
def ==(other)
def ==(other) other.class == self.class && raw == other.raw end
def build_hashes
def build_hashes cells_rows[1..-1].map do |row| row.to_hash end end
def cell_matrix #:nodoc:
def cell_matrix #:nodoc: @cell_matrix end
def cells_rows #:nodoc:
def cells_rows #:nodoc: cell_matrix.map do |cell_row| @cells_class.new(self, cell_row) end end
def col_width(col) #:nodoc:
def col_width(col) #:nodoc: columns[col].__send__(:width) end
def column_names #:nodoc:
def column_names #:nodoc: cell_matrix[0].map { |cell| cell.value } end
def columns #:nodoc:
def columns #:nodoc: cell_matrix.transpose.map do |cell_row| @cells_class.new(self, cell_row) end end
def create_cell_matrix(raw) #:nodoc:
def create_cell_matrix(raw) #:nodoc: @cell_matrix = raw.map do |raw_row| line = raw_row.line rescue -1 raw_row.map do |raw_cell| new_cell(raw_cell, line) end end end
def description_for_visitors
def description_for_visitors :data_table 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
[{'a' => '2', 'b' => '3', 'sum' => '5'}, {'a' => '7', 'b' => '9', 'sum' => '16'}]
Gets converted into the following:
| 7 | 9 | 16 |
| 2 | 3 | 5 |
| a | b | sum |
the following plain text:
Hash are the headers in the table. For example, a DataTable built from
Converts this table into an Array of Hash where the keys of each
def hashes build_hashes end
def hashes_to_array(hashes) #:nodoc:
def hashes_to_array(hashes) #:nodoc: header = hashes[0].keys.sort [header] + hashes.map{|hash| header.map{|key| hash[key]}} end
def headers #:nodoc:
def headers #:nodoc: raw.first end
def initialize(raw, 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(raw, location) @cells_class = Cells @cell_class = Cell raw = ensure_array_of_array(rubify(raw)) # Verify that it's square raw.transpose create_cell_matrix(raw) @location = location end
def inspect
def inspect raw.inspect 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 new_cell(raw_cell, line) #:nodoc:
def new_cell(raw_cell, line) #:nodoc: @cell_class.new(raw_cell, self, line) end
def raw
[['a', 'b'], ['c', 'd']]
gets converted into the following:
| c | d |
| a | b |
the following plain text:
Gets the raw data of this table. For example, a DataTable built from
def raw cell_matrix.map do |row| row.map do |cell| cell.value end end end
def rows
def rows hashes.map do |hash| hash.values_at(*headers) end end
def rows_hash
The table must be exactly two columns wide
{'a' => '2', 'b' => '3'}
Gets converted into the following:
| b | 3 |
| a | 2 |
used as keys and the second column is used as values
Converts this table into a Hash where the first column is
def rows_hash verify_table_width(2) self.transpose.hashes[0] end
def to_hash(cells) #:nodoc:
def to_hash(cells) #:nodoc: hash = Hash.new do |the_hash, key| the_hash[key.to_s] if key.is_a?(Symbol) end column_names.each_with_index do |column_name, column_index| hash[column_name] = cells.value(column_index) end hash end
def to_sexp #:nodoc:
For testing only
def to_sexp #:nodoc: [:table, *cells_rows.map{|row| row.to_sexp}] 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_table_width(width) #:nodoc:
def verify_table_width(width) #:nodoc: raise %{The table must have exactly #{width} columns} unless raw[0].size == width end