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. 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

#rawObject (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

Returns:

  • (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

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



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

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

#inspectObject



91
92
93
# File 'lib/cucumber/core/test/data_table.rb', line 91

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

#lines_countObject



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_argObject



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

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 |


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

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