Class: Cucumber::Core::Test::DataTable

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(rows) ⇒ DataTable

Creates a new instance. rows should be a square (2d), array of strings or an array of hashes

You don’t typically create your own DataTable objects - Cucumber will do it internally and pass them to your Step Definitions.



32
33
34
35
36
# File 'lib/cucumber/core/test/data_table.rb', line 32

def initialize(rows)
  raw = ensure_array_of_array(rows)
  verify_rows_are_same_length(raw)
  @raw = raw.freeze
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



26
27
28
# File 'lib/cucumber/core/test/data_table.rb', line 26

def raw
  @raw
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
# File 'lib/cucumber/core/test/data_table.rb', line 38

def ==(other)
  other.class == self.class && raw == other.raw
end

#data_table?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cucumber/core/test/data_table.rb', line 42

def data_table?
  true
end

#describe_to(visitor) ⇒ Object



46
47
48
# File 'lib/cucumber/core/test/data_table.rb', line 46

def describe_to(visitor, *)
  visitor.data_table(self, *)
end

#doc_string?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cucumber/core/test/data_table.rb', line 50

def doc_string?
  false
end

#dupObject

Creates a copy of this table



55
56
57
# File 'lib/cucumber/core/test/data_table.rb', line 55

def dup
  self.class.new(raw.dup)
end

#inspectObject



59
60
61
# File 'lib/cucumber/core/test/data_table.rb', line 59

def inspect
  %{#<#{self.class} #{raw.inspect}>}
end

#lines_countObject



63
64
65
# File 'lib/cucumber/core/test/data_table.rb', line 63

def lines_count
  raw.count
end

#map(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/cucumber/core/test/data_table.rb', line 67

def map(&block)
  new_raw = raw.map do |row|
    row.map(&block)
  end

  self.class.new(new_raw)
end

#to_step_definition_argObject



75
76
77
# File 'lib/cucumber/core/test/data_table.rb', line 75

def to_step_definition_arg
  dup
end

#transposeObject

Returns a new, transposed table. Example:

| a | 7 | 4 |
| b | 9 | 2 |

Gets converted into the following:

| a | b |
| 7 | 9 |
| 4 | 2 |


89
90
91
# File 'lib/cucumber/core/test/data_table.rb', line 89

def transpose
  self.class.new(raw.transpose)
end