turnip 3.0.0.pre.beta.1 → 3.0.0.pre.beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8425f714759628ba119de6a3d1c4f8d7b8822c1
4
- data.tar.gz: 342a721605fd9f29e0812962c1abbd7f98d5eaa7
3
+ metadata.gz: c0b502b56a65a996d87ad7262b06c8f2766a6f76
4
+ data.tar.gz: 8d2895b87e0b01423729ae4cae984765325856f5
5
5
  SHA512:
6
- metadata.gz: 83c07693eb5d083086cbd570532566e3bbcdbce60b5114f57091612b2d4dc1ec25b46794d9c5603eee547a4167eaad555d6cc9969a8bac2e53941455b51ad649
7
- data.tar.gz: 2116eac308a84e0d4119f26fef894dafd9fcfbf11d34671297bf50920268e8b4a2102da61aeae8b43396a1c4a1fad0f16847b23aa45ccba2575367461059ba64
6
+ metadata.gz: 8f1b62c64c693630465be6b58f6997ef4a231356c3024a8a6c99f28ced4b11113610413e271e859f2e137cbb99e6517ee365a77b04e5d31d9b8200e0c80fb3f4
7
+ data.tar.gz: 8f818d8d209117f591738fc92e223039c029685fec002ddd7333e59e305229b7573fd47873061b5b6be60174bb98e0edbadf97ba6815757071b3e29285fb3e04
@@ -1,138 +1,8 @@
1
1
  require "gherkin/parser"
2
- require "gherkin/token_scanner"
2
+ require 'turnip/node/feature'
3
3
 
4
4
  module Turnip
5
5
  class Builder
6
- module Tags
7
- def tags
8
- @raw[:tags].map { |tag| tag[:name].sub(/^@/, '') }
9
- end
10
-
11
- def tags_hash
12
- Hash[tags.map { |t| [t.to_sym, true] }]
13
- end
14
-
15
- def metadata_hash
16
- tags_hash
17
- end
18
- end
19
-
20
- module Name
21
- def name
22
- @raw[:name]
23
- end
24
- end
25
-
26
- module Line
27
- def line
28
- @raw[:location][:line]
29
- end
30
- end
31
-
32
- module Steps
33
- def steps
34
- @steps ||= @raw[:steps].map do |step|
35
- extra_args = []
36
- if (arg = step[:argument])
37
- if arg[:type] == :DataTable
38
- table = Turnip::Table.new(arg[:rows].map {|r| r[:cells].map {|c| c[:value]}})
39
- extra_args.push(table)
40
- else
41
- extra_args.push arg[:content]
42
- end
43
- end
44
- Step.new(step[:text], extra_args, step[:location][:line], step[:keyword])
45
- end
46
- end
47
- end
48
-
49
- class Feature
50
- include Tags
51
- include Name
52
- include Line
53
-
54
- attr_reader :scenarios, :backgrounds
55
- attr_accessor :feature_tag
56
-
57
- def initialize(raw)
58
- @raw = raw
59
- @scenarios = []
60
- @backgrounds = []
61
- end
62
-
63
- def metadata_hash
64
- super.merge(:type => Turnip.type, :turnip => true)
65
- end
66
- end
67
-
68
- class Background
69
- include Steps
70
-
71
- def initialize(raw)
72
- @raw = raw
73
- end
74
- end
75
-
76
- class Scenario
77
- include Tags
78
- include Name
79
- include Line
80
- include Steps
81
-
82
- attr_writer :steps
83
-
84
- def initialize(raw)
85
- @raw = raw
86
- end
87
- end
88
-
89
- class ScenarioOutline
90
- include Tags
91
- include Name
92
- include Steps
93
-
94
- def initialize(raw)
95
- @raw = raw
96
- end
97
-
98
- def to_scenarios
99
- return [] unless @raw[:examples]
100
- @raw[:examples].map { |example|
101
- headers = example[:tableHeader][:cells].map {|c| c[:value]}
102
- rows = example[:tableBody].map {|r| r[:cells].map {|c| c[:value]}}
103
- rows.map do |row|
104
- Scenario.new(@raw).tap do |scenario|
105
- scenario.steps = steps.map do |step|
106
- new_description = substitute(step.description, headers, row)
107
- new_extra_args = step.extra_args.map do |ea|
108
- case ea
109
- when String
110
- substitute(ea, headers, row)
111
- when Turnip::Table
112
- Turnip::Table.new(ea.map {|t_row| t_row.map {|t_col| substitute(t_col, headers, row) } })
113
- else
114
- ea
115
- end
116
- end
117
- Step.new(new_description, new_extra_args, step.line, step.keyword)
118
- end
119
- end
120
- end
121
- }.flatten
122
- end
123
-
124
- private
125
-
126
- def substitute(text, headers, row)
127
- text.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
128
- end
129
- end
130
-
131
- class Step < Struct.new(:description, :extra_args, :line, :keyword)
132
- def to_s
133
- "#{keyword}#{description}"
134
- end
135
- end
136
6
 
137
7
  attr_reader :features
138
8
 
@@ -152,19 +22,8 @@ module Turnip
152
22
 
153
23
  def build(attributes)
154
24
  return unless attributes[:feature]
155
- attr = attributes[:feature]
156
- feature = Feature.new(attr)
157
- attr[:children].each do |child|
158
- case child[:type]
159
- when :Background
160
- feature.backgrounds << Background.new(child)
161
- when :Scenario
162
- feature.scenarios << Scenario.new(child)
163
- else
164
- feature.scenarios.push(*ScenarioOutline.new(child).to_scenarios)
165
- end
166
- end
167
- @features << feature
25
+
26
+ @features << Node::Feature.new(attributes[:feature])
168
27
  end
169
28
  end
170
29
  end
@@ -1,10 +1,13 @@
1
1
  module Turnip
2
2
  module Execute
3
3
  def step(step_or_description, *extra_args)
4
- if step_or_description.respond_to?(:extra_args)
4
+
5
+ if step_or_description.respond_to?(:argument) # Turnip::Node::Step
5
6
  description = step_or_description.description
6
- extra_args.concat(step_or_description.extra_args)
7
- else
7
+ if step_or_description.argument
8
+ extra_args << step_or_description.argument
9
+ end
10
+ else # String
8
11
  description = step_or_description
9
12
  end
10
13
 
@@ -0,0 +1,19 @@
1
+ require 'turnip/node/scenario_definition'
2
+
3
+ module Turnip
4
+ module Node
5
+ #
6
+ # @note Background metadata generated by Gherkin
7
+ #
8
+ # {
9
+ # type: :Background,
10
+ # location: { line: 10, column: 3 },
11
+ # keyword: "Background",
12
+ # name: "Background Description",
13
+ # steps: []
14
+ # }
15
+ #
16
+ class Background < ScenarioDefinition
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ require 'turnip/node/location'
2
+
3
+ module Turnip
4
+ module Node
5
+ class Base
6
+ include HasLocation
7
+
8
+ attr_reader :raw
9
+
10
+ def initialize(raw)
11
+ @raw = raw
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,65 @@
1
+ require 'turnip/node/base'
2
+ require 'turnip/node/tag'
3
+
4
+ module Turnip
5
+ module Node
6
+ #
7
+ # @note Example metadata generated by Gherkin
8
+ #
9
+ # {
10
+ # type: :Examples,
11
+ # tags: [], # Array of Tag
12
+ # location: { line: 10, column: 3 },
13
+ # keyword: "Examples",
14
+ # name: "Example Description",
15
+ # tableHeader: {},
16
+ # tableBody: {}
17
+ # }
18
+ #
19
+ class Example < Base
20
+ include HasTags
21
+
22
+ def keyword
23
+ @raw[:keyword]
24
+ end
25
+
26
+ def name
27
+ @raw[:name]
28
+ end
29
+
30
+ def description
31
+ @raw[:description]
32
+ end
33
+
34
+ #
35
+ # @note
36
+ #
37
+ # Examples:
38
+ # | monster | hp |
39
+ # | slime | 10 | => [ 'monster', 'hp' ]
40
+ # | daemon | 70 |
41
+ #
42
+ # @return [Array]
43
+ #
44
+ def header
45
+ @header ||= @raw[:tableHeader][:cells].map { |c| c[:value] }
46
+ end
47
+
48
+ #
49
+ # @note
50
+ #
51
+ # Examples:
52
+ # | monster | hp |
53
+ # | slime | 10 | => [ ['slime', '10'], ['daemon', '70'] ]
54
+ # | daemon | 70 |
55
+ #
56
+ # @return [Array]
57
+ #
58
+ def rows
59
+ @rows ||= @raw[:tableBody].map do |row|
60
+ row[:cells].map { |c| c[:value] }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,77 @@
1
+ require 'turnip/node/base'
2
+ require 'turnip/node/tag'
3
+ require 'turnip/node/scenario'
4
+ require 'turnip/node/scenario_outline'
5
+ require 'turnip/node/background'
6
+
7
+ module Turnip
8
+ module Node
9
+ #
10
+ # @note Feature metadata generated by Gherkin
11
+ #
12
+ # {
13
+ # type: :Feature,
14
+ # tags: [], # Array of Tag
15
+ # location: { line: 10, column: 3 },
16
+ # language: 'en',
17
+ # keyword: 'Feature',
18
+ # name: 'Feature name',
19
+ # description: 'Feature description',
20
+ # children: [], # Array of Background, Scenario and Scenario Outline
21
+ # }
22
+ #
23
+ class Feature < Base
24
+ include HasTags
25
+
26
+ def name
27
+ @raw[:name]
28
+ end
29
+
30
+ def language
31
+ @raw[:language]
32
+ end
33
+
34
+ def keyword
35
+ @raw[:keyword]
36
+ end
37
+
38
+ def description
39
+ @raw[:description]
40
+ end
41
+
42
+ def children
43
+ @children ||= @raw[:children].map do |c|
44
+ case c[:type]
45
+ when :Background
46
+ Background.new(c)
47
+ when :Scenario
48
+ Scenario.new(c)
49
+ when :ScenarioOutline
50
+ ScenarioOutline.new(c)
51
+ end
52
+ end.compact
53
+ end
54
+
55
+ def backgrounds
56
+ @backgrounds ||= children.select do |c|
57
+ c.is_a?(Background)
58
+ end
59
+ end
60
+
61
+ def scenarios
62
+ @scenarios ||= children.map do |c|
63
+ case c
64
+ when Scenario
65
+ c
66
+ when ScenarioOutline
67
+ c.to_scenarios
68
+ end
69
+ end.flatten.compact
70
+ end
71
+
72
+ def metadata_hash
73
+ super.merge(:type => Turnip.type, :turnip => true)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,34 @@
1
+ module Turnip
2
+ module Node
3
+ #
4
+ # @note Location metadata generated by Gherkin
5
+ #
6
+ # {
7
+ # line: 10,
8
+ # column: 3
9
+ # }
10
+ #
11
+ class Location
12
+ attr_reader :line,
13
+ :column
14
+
15
+ def initialize(line, column)
16
+ @line = line
17
+ @column = column
18
+ end
19
+ end
20
+
21
+ module HasLocation
22
+ #
23
+ # @return [Location]
24
+ #
25
+ def location
26
+ @location ||= Location.new(@raw[:location][:line], @raw[:location][:column])
27
+ end
28
+
29
+ def line
30
+ location.line
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ require 'turnip/node/scenario_definition'
2
+ require 'turnip/node/tag'
3
+
4
+ module Turnip
5
+ module Node
6
+ #
7
+ # @note Scenario metadata generated by Gherkin
8
+ #
9
+ # {
10
+ # type: :Scenario,
11
+ # tags: [], # Array of Tag
12
+ # location: { line: 10, column: 3 },
13
+ # keyword: "Scenario",
14
+ # name: "Scenario Description",
15
+ # steps: []
16
+ # }
17
+ #
18
+ class Scenario < ScenarioDefinition
19
+ include HasTags
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'turnip/node/base'
2
+ require 'turnip/node/step'
3
+
4
+ module Turnip
5
+ module Node
6
+ class ScenarioDefinition < Base
7
+ def name
8
+ @raw[:name]
9
+ end
10
+
11
+ def keyword
12
+ @raw[:keyword]
13
+ end
14
+
15
+ def description
16
+ @raw[:description]
17
+ end
18
+
19
+ def steps
20
+ @steps ||= @raw[:steps].map do |step|
21
+ Step.new(step)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,151 @@
1
+ require 'turnip/node/scenario_definition'
2
+ require 'turnip/node/tag'
3
+ require 'turnip/node/example'
4
+
5
+ module Turnip
6
+ module Node
7
+ #
8
+ # @note ScenarioOutline metadata generated by Gherkin
9
+ #
10
+ # {
11
+ # type: :Scenario,
12
+ # tags: [], # Array of Tag
13
+ # location: { line: 10, column: 3 },
14
+ # keyword: "Scenario Outline",
15
+ # name: "Scenario Outline Description",
16
+ # steps: []
17
+ # }
18
+ #
19
+ class ScenarioOutline < ScenarioDefinition
20
+ include HasTags
21
+
22
+ def examples
23
+ @examples ||= @raw[:examples].map do |raw|
24
+ Example.new(raw)
25
+ end
26
+ end
27
+
28
+ #
29
+ # Return array of Scenario
30
+ #
31
+ # @note
32
+ #
33
+ # example:
34
+ #
35
+ # Scenario Outline: Test
36
+ # Then I go to <where>
37
+ #
38
+ # Examples:
39
+ # | where |
40
+ # | bank |
41
+ # | airport |
42
+ #
43
+ # to
44
+ #
45
+ # Scenario: Test
46
+ # Then I go to bank
47
+ #
48
+ # Scenario: Test
49
+ # Then I go to airport
50
+ #
51
+ # @return [Array]
52
+ #
53
+ def to_scenarios
54
+ return [] if examples.nil?
55
+
56
+ @scenarios ||= examples.map do |example|
57
+ header = example.header
58
+
59
+ example.rows.map do |row|
60
+ metadata = convert_metadata_to_scenario
61
+
62
+ #
63
+ # Replace <placeholder> using Example values
64
+ #
65
+ metadata[:steps].each do |step|
66
+ step[:text] = substitute(step[:text], header, row)
67
+
68
+ next if step[:argument].nil?
69
+
70
+ case step[:argument][:type]
71
+ when :DocString
72
+ step[:argument][:content] = substitute(step[:argument][:content], header, row)
73
+ when :DataTable
74
+ step[:argument][:rows].map do |table_row|
75
+ table_row[:cells].map do |cell|
76
+ cell[:value] = substitute(cell[:value], header, row)
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ Scenario.new(metadata)
83
+ end
84
+ end.flatten.compact
85
+ end
86
+
87
+ private
88
+
89
+ #
90
+ # Convert ScenariOutline metadata for Scenario
91
+ #
92
+ # @example:
93
+ #
94
+ # {
95
+ # "type": "ScenarioOutline",
96
+ # "tags": ['tag'],
97
+ # "location": {'loc'},
98
+ # "keyword": "Scenario Outline",
99
+ # "name": "...",
100
+ # "steps": [],
101
+ # "examples": []
102
+ # }
103
+ #
104
+ # to
105
+ #
106
+ # {
107
+ # "type": "Scenario",
108
+ # "tags": ['tag'],
109
+ # "location": {'loc'},
110
+ # "keyword": "Scenario",
111
+ # "name": "...",
112
+ # "steps": []
113
+ # }
114
+ #
115
+ # @note At this method, placeholder of step text is not replaced yet
116
+ #
117
+ # @todo :keyword is not considered a language (en only)
118
+ # @return [Hash]
119
+ #
120
+ def convert_metadata_to_scenario()
121
+ # deep copy
122
+ Marshal.load(Marshal.dump(raw)).tap do |new_raw|
123
+ new_raw.delete(:examples)
124
+ new_raw[:type] = :Scenario
125
+ new_raw[:keyword] = 'Scenario'
126
+ end
127
+ end
128
+
129
+ #
130
+ # Replace placeholder `<..>`
131
+ #
132
+ # @example:
133
+ #
134
+ # text = 'There is a <monster> that has <hp> hitpoints.'
135
+ # header = ['monster', 'hp']
136
+ # row = ['slime', '10']
137
+ #
138
+ # substitute(text, header, row)
139
+ # # => 'There is a slime that has 10 hitpoints.'
140
+ #
141
+ # @param text [String]
142
+ # @param header [Array]
143
+ # @param row [Array]
144
+ # @return [String]
145
+ #
146
+ def substitute(text, header, row)
147
+ text.gsub(/<([^>]*)>/) { |_| Hash[header.zip(row)][$1] }
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,64 @@
1
+ require 'turnip/node/base'
2
+ require 'turnip/table'
3
+
4
+ module Turnip
5
+ module Node
6
+ #
7
+ # @note Step metadata generated by Gherkin
8
+ #
9
+ # {
10
+ # type: :Step,
11
+ # location: { line: 10, column: 3 },
12
+ # keyword: 'Step',
13
+ # text: 'Step description',
14
+ # argument: {}, # DocString or DataTable
15
+ # }
16
+ #
17
+ class Step < Base
18
+ def keyword
19
+ @raw[:keyword]
20
+ end
21
+
22
+ def text
23
+ @raw[:text]
24
+ end
25
+
26
+ #
27
+ # Backward compatibility
28
+ #
29
+ def description
30
+ text
31
+ end
32
+
33
+ def argument
34
+ return nil if @raw[:argument].nil?
35
+
36
+ @argument ||= case @raw[:argument][:type]
37
+ when :DocString
38
+ doc_string(@raw[:argument])
39
+ when :DataTable
40
+ data_table(@raw[:argument])
41
+ end
42
+ end
43
+
44
+ def to_s
45
+ "#{keyword}#{text}"
46
+ end
47
+
48
+ private
49
+
50
+ def doc_string(doc)
51
+ doc[:content]
52
+ end
53
+
54
+ def data_table(table)
55
+ rows = table[:rows].map do |row|
56
+ row[:cells].map do |cell|
57
+ cell[:value]
58
+ end
59
+ end
60
+ Turnip::Table.new(rows)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,40 @@
1
+ require 'turnip/node/base'
2
+
3
+ module Turnip
4
+ module Node
5
+ #
6
+ # @note Location metadata generated by Gherkin
7
+ #
8
+ # {
9
+ # type: :Tag,
10
+ # location: { line: 10, column: 3 },
11
+ # name: '@tag'
12
+ # }
13
+ #
14
+ class Tag < Base
15
+ def name
16
+ @name ||= @raw[:name].gsub(/^@/, '')
17
+ end
18
+ end
19
+
20
+ module HasTags
21
+ #
22
+ # @return [Array] Array of Tag
23
+ #
24
+ def tags
25
+ @tags ||= @raw[:tags].map do |tag|
26
+ Tag.new(tag)
27
+ end
28
+ end
29
+
30
+ #
31
+ # Convert for hash to be used in RSpec
32
+ #
33
+ # @return [Hash]
34
+ #
35
+ def metadata_hash
36
+ Hash[tags.map { |t| [t.name.to_sym, true] }]
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Turnip
2
- VERSION = '3.0.0-beta.1'
2
+ VERSION = '3.0.0-beta.2'
3
3
  end
data/spec/builder_spec.rb CHANGED
@@ -66,10 +66,10 @@ describe Turnip::Builder do
66
66
  "I attack the monster and do 13 points damage",
67
67
  "the monster should be dead"
68
68
  ])
69
- table = feature.scenarios[0].steps[0].extra_args.find {|a| a.instance_of?(Turnip::Table)}
70
- table.hashes[0]['hit_points'].should == '10'
71
- table = feature.scenarios[1].steps[0].extra_args.find {|a| a.instance_of?(Turnip::Table)}
72
- table.hashes[0]['hit_points'].should == '8'
69
+ table = feature.scenarios[0].steps[0].argument
70
+ table.hashes[0]['hit_points'].should eq '10'
71
+ table = feature.scenarios[1].steps[0].argument
72
+ table.hashes[0]['hit_points'].should eq '8'
73
73
  end
74
74
  end
75
75
 
@@ -83,7 +83,7 @@ describe Turnip::Builder do
83
83
  'the monster introduced himself:'
84
84
  ])
85
85
 
86
- multiline = steps[1].extra_args.first
86
+ multiline = steps[1].argument
87
87
  multiline.should eq %q(Ahhhhhhh! i'm "John Smith"!)
88
88
  end
89
89
  end
@@ -41,13 +41,13 @@ describe Turnip::Execute do
41
41
  end
42
42
 
43
43
  it "can be executed with a builder step" do
44
- builder_step = double(:description => "a cool step", :extra_args => [])
44
+ builder_step = double(:description => "a cool step", :argument => nil)
45
45
  mod.step("a :test step") { |test| test.upcase }
46
46
  obj.step(builder_step).should == "COOL"
47
47
  end
48
48
 
49
49
  it "sends in extra arg from a builder step" do
50
- builder_step = double(:description => "a cool step", :extra_args => ["foo"])
50
+ builder_step = double(:description => "a cool step", :argument => "foo")
51
51
  mod.step("a :test step") { |test, foo| test.upcase + foo }
52
52
  obj.step(builder_step).should == "COOLfoo"
53
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turnip
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.pre.beta.1
4
+ version: 3.0.0.pre.beta.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -122,6 +122,16 @@ files:
122
122
  - lib/turnip/define.rb
123
123
  - lib/turnip/dsl.rb
124
124
  - lib/turnip/execute.rb
125
+ - lib/turnip/node/background.rb
126
+ - lib/turnip/node/base.rb
127
+ - lib/turnip/node/example.rb
128
+ - lib/turnip/node/feature.rb
129
+ - lib/turnip/node/location.rb
130
+ - lib/turnip/node/scenario.rb
131
+ - lib/turnip/node/scenario_definition.rb
132
+ - lib/turnip/node/scenario_outline.rb
133
+ - lib/turnip/node/step.rb
134
+ - lib/turnip/node/tag.rb
125
135
  - lib/turnip/placeholder.rb
126
136
  - lib/turnip/rspec.rb
127
137
  - lib/turnip/step_definition.rb