aslakhellesoy-cucumber 0.1.16.4 → 0.1.16.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/History.txt +75 -1
  2. data/Manifest.txt +58 -65
  3. data/examples/cs/features/step_definitons/calculator_steps.rb +1 -1
  4. data/examples/i18n/Rakefile +3 -3
  5. data/examples/i18n/ar/features/step_definitons/calculator_steps.rb +1 -6
  6. data/examples/i18n/da/features/step_definitons/kalkulator_steps.rb +1 -1
  7. data/examples/i18n/de/features/addition.feature +6 -6
  8. data/examples/i18n/de/features/step_definitons/calculator_steps.rb +2 -2
  9. data/examples/i18n/en/features/addition.feature +6 -6
  10. data/examples/i18n/en/features/step_definitons/calculator_steps.rb +2 -2
  11. data/examples/i18n/es/features/step_definitons/calculador_steps.rb +1 -1
  12. data/examples/i18n/et/features/liitmine.feature +7 -6
  13. data/examples/i18n/et/features/step_definitions/kalkulaator_steps.rb +1 -1
  14. data/examples/i18n/fi/features/step_definitons/laskin_steps.rb +1 -1
  15. data/examples/i18n/fi/features/yhteenlasku.feature +2 -2
  16. data/examples/i18n/fr/features/addition.feature +2 -2
  17. data/examples/i18n/fr/features/step_definitions/calculatrice_steps.rb +1 -1
  18. data/examples/i18n/id/features/addition.feature +6 -6
  19. data/examples/i18n/id/features/step_definitons/calculator_steps.rb +2 -2
  20. data/examples/i18n/it/features/step_definitons/calcolatrice_steps.rb +1 -1
  21. data/examples/i18n/ja/features/step_definitons/calculator_steps.rb +2 -3
  22. data/examples/i18n/ko/features/step_definitons/calculator_steps.rb +1 -1
  23. data/examples/i18n/lt/features/addition.feature +7 -6
  24. data/examples/i18n/lt/features/step_definitons/calculator_steps.rb +2 -2
  25. data/examples/i18n/no/features/step_definitons/kalkulator_steps.rb +7 -7
  26. data/examples/i18n/no/features/summering.feature +1 -0
  27. data/examples/i18n/pt/features/step_definitions/calculadora_steps.rb +1 -1
  28. data/examples/i18n/ro/features/step_definitons/calculator_steps.rb +1 -1
  29. data/examples/i18n/se/features/step_definitons/kalkulator_steps.rb +1 -1
  30. data/examples/i18n/zh-CN/features/step_definitons/calculator_steps.rb +2 -2
  31. data/examples/jbehave/README.textile +17 -0
  32. data/examples/jbehave/features/support/env.rb +7 -0
  33. data/examples/jbehave/features/trading.feature +24 -0
  34. data/examples/jbehave/pom.xml +48 -0
  35. data/examples/self_test/README.textile +4 -1
  36. data/examples/self_test/features/call_undefined_step_from_step_def.feature +7 -0
  37. data/examples/self_test/features/lots_of_undefined.feature +8 -0
  38. data/examples/self_test/features/outline_sample.feature +8 -5
  39. data/examples/self_test/features/sample.feature +5 -3
  40. data/examples/self_test/features/step_definitions/sample_steps.rb +15 -3
  41. data/features/cucumber_cli.feature +199 -97
  42. data/features/cucumber_cli_outlines.feature +46 -38
  43. data/features/report_called_undefined_steps.feature +31 -0
  44. data/features/step_definitions/cucumber_steps.rb +7 -3
  45. data/features/step_definitions/extra_steps.rb +1 -1
  46. data/features/support/env.rb +1 -1
  47. data/gem_tasks/features.rake +1 -1
  48. data/gem_tasks/flog.rake +1 -1
  49. data/lib/autotest/cucumber_mixin.rb +16 -23
  50. data/lib/cucumber/ast/comment.rb +26 -0
  51. data/lib/cucumber/ast/examples.rb +22 -0
  52. data/lib/cucumber/ast/feature.rb +60 -0
  53. data/lib/cucumber/ast/features.rb +39 -0
  54. data/lib/cucumber/ast/filter.rb +22 -0
  55. data/lib/cucumber/ast/outline_table.rb +49 -0
  56. data/lib/cucumber/ast/py_string.rb +52 -0
  57. data/lib/cucumber/ast/scenario.rb +91 -0
  58. data/lib/cucumber/ast/scenario_outline.rb +83 -0
  59. data/lib/cucumber/ast/step.rb +130 -0
  60. data/lib/cucumber/ast/table.rb +214 -0
  61. data/lib/cucumber/ast/tags.rb +33 -0
  62. data/lib/cucumber/ast/visitor.rb +93 -0
  63. data/lib/cucumber/ast.rb +27 -0
  64. data/lib/cucumber/broadcaster.rb +1 -6
  65. data/lib/cucumber/cli.rb +178 -128
  66. data/lib/cucumber/core_ext/exception.rb +41 -8
  67. data/lib/cucumber/core_ext/instance_exec.rb +54 -0
  68. data/lib/cucumber/core_ext/proc.rb +29 -65
  69. data/lib/cucumber/core_ext/string.rb +19 -0
  70. data/lib/cucumber/{formatters → formatter}/ansicolor.rb +11 -10
  71. data/lib/cucumber/formatter/console.rb +116 -0
  72. data/lib/cucumber/formatter/pretty.rb +158 -0
  73. data/lib/cucumber/formatter/profile.rb +77 -0
  74. data/lib/cucumber/formatter/progress.rb +68 -0
  75. data/lib/cucumber/formatter.rb +1 -0
  76. data/lib/cucumber/formatters/autotest_formatter.rb +0 -2
  77. data/lib/cucumber/formatters/html_formatter.rb +4 -3
  78. data/lib/cucumber/formatters/pretty_formatter.rb +1 -1
  79. data/lib/cucumber/formatters/unicode.rb +3 -3
  80. data/lib/cucumber/jbehave.rb +104 -0
  81. data/lib/cucumber/languages.yml +100 -73
  82. data/lib/cucumber/parser/basic.rb +0 -0
  83. data/lib/cucumber/parser/feature.rb +1694 -0
  84. data/lib/cucumber/parser/feature.tt +206 -0
  85. data/lib/cucumber/parser/file_parser.rb +50 -0
  86. data/lib/cucumber/parser/i18n.tt +26 -0
  87. data/lib/cucumber/parser/treetop_ext.rb +9 -0
  88. data/lib/cucumber/parser.rb +27 -0
  89. data/lib/cucumber/platform.rb +3 -17
  90. data/lib/cucumber/step_definition.rb +83 -0
  91. data/lib/cucumber/step_mother.rb +128 -72
  92. data/lib/cucumber/version.rb +1 -1
  93. data/lib/cucumber.rb +56 -9
  94. data/spec/cucumber/ast/feature_factory.rb +54 -0
  95. data/spec/cucumber/ast/feature_spec.rb +60 -0
  96. data/spec/cucumber/ast/py_string_spec.rb +40 -0
  97. data/spec/cucumber/ast/scenario_outline_spec.rb +64 -0
  98. data/spec/cucumber/ast/scenario_spec.rb +82 -0
  99. data/spec/cucumber/ast/step_spec.rb +45 -0
  100. data/spec/cucumber/ast/table_spec.rb +81 -0
  101. data/spec/cucumber/broadcaster_spec.rb +4 -17
  102. data/spec/cucumber/cli_spec.rb +43 -148
  103. data/spec/cucumber/core_ext/proc_spec.rb +27 -35
  104. data/spec/cucumber/core_ext/string_spec.rb +8 -0
  105. data/spec/cucumber/{formatters → formatter}/ansicolor_spec.rb +2 -2
  106. data/spec/cucumber/formatter/html/cucumber.css +37 -0
  107. data/spec/cucumber/formatter/html/cucumber.js +11 -0
  108. data/spec/cucumber/formatter/html/index.html +45 -0
  109. data/spec/cucumber/formatter/html/jquery-1.3.min.js +19 -0
  110. data/spec/cucumber/formatter/html/jquery.uitableedit.js +100 -0
  111. data/spec/cucumber/formatters/autotest_formatter_spec.rb +1 -0
  112. data/spec/cucumber/formatters/profile_formatter_spec.rb +17 -16
  113. data/spec/cucumber/parser/feature_parser_spec.rb +247 -0
  114. data/spec/cucumber/parser/table_parser_spec.rb +48 -0
  115. data/spec/cucumber/step_definition_spec.rb +62 -0
  116. data/spec/cucumber/step_mom_spec.rb +49 -0
  117. data/spec/cucumber/treetop_parser/empty_feature.feature +1 -1
  118. data/spec/cucumber/treetop_parser/spaces.feature +3 -1
  119. data/spec/cucumber/treetop_parser/with_comments.feature +1 -1
  120. data/spec/cucumber/treetop_parser/with_tags.feature +18 -0
  121. data/spec/cucumber/world/pending_spec.rb +13 -12
  122. data/spec/spec_helper.rb +1 -1
  123. metadata +59 -67
  124. data/examples/calculator_ruby_features/Rakefile +0 -6
  125. data/examples/calculator_ruby_features/features/addition.rb +0 -39
  126. data/examples/calculator_ruby_features/features/step_definitons/calculator_steps.rb +0 -43
  127. data/gem_tasks/treetop.rake +0 -41
  128. data/lib/cucumber/executor.rb +0 -205
  129. data/lib/cucumber/formatters/profile_formatter.rb +0 -92
  130. data/lib/cucumber/formatters/progress_formatter.rb +0 -61
  131. data/lib/cucumber/formatters.rb +0 -1
  132. data/lib/cucumber/model/table.rb +0 -32
  133. data/lib/cucumber/model.rb +0 -1
  134. data/lib/cucumber/step_methods.rb +0 -49
  135. data/lib/cucumber/tree/feature.rb +0 -105
  136. data/lib/cucumber/tree/features.rb +0 -21
  137. data/lib/cucumber/tree/given_scenario.rb +0 -13
  138. data/lib/cucumber/tree/scenario.rb +0 -240
  139. data/lib/cucumber/tree/step.rb +0 -173
  140. data/lib/cucumber/tree/table.rb +0 -26
  141. data/lib/cucumber/tree/top_down_visitor.rb +0 -23
  142. data/lib/cucumber/tree.rb +0 -16
  143. data/lib/cucumber/treetop_parser/feature.treetop.erb +0 -254
  144. data/lib/cucumber/treetop_parser/feature_ar.rb +0 -1951
  145. data/lib/cucumber/treetop_parser/feature_cy.rb +0 -1951
  146. data/lib/cucumber/treetop_parser/feature_da.rb +0 -1951
  147. data/lib/cucumber/treetop_parser/feature_de.rb +0 -1951
  148. data/lib/cucumber/treetop_parser/feature_en-lol.rb +0 -1951
  149. data/lib/cucumber/treetop_parser/feature_en-tx.rb +0 -1951
  150. data/lib/cucumber/treetop_parser/feature_en.rb +0 -1951
  151. data/lib/cucumber/treetop_parser/feature_es.rb +0 -1951
  152. data/lib/cucumber/treetop_parser/feature_et.rb +0 -1951
  153. data/lib/cucumber/treetop_parser/feature_fr.rb +0 -1951
  154. data/lib/cucumber/treetop_parser/feature_id.rb +0 -1951
  155. data/lib/cucumber/treetop_parser/feature_it.rb +0 -1951
  156. data/lib/cucumber/treetop_parser/feature_ja.rb +0 -1951
  157. data/lib/cucumber/treetop_parser/feature_ko.rb +0 -1951
  158. data/lib/cucumber/treetop_parser/feature_lt.rb +0 -1951
  159. data/lib/cucumber/treetop_parser/feature_nl.rb +0 -1951
  160. data/lib/cucumber/treetop_parser/feature_no.rb +0 -1951
  161. data/lib/cucumber/treetop_parser/feature_parser.rb +0 -36
  162. data/lib/cucumber/treetop_parser/feature_pl.rb +0 -1951
  163. data/lib/cucumber/treetop_parser/feature_pt.rb +0 -1951
  164. data/lib/cucumber/treetop_parser/feature_ro.rb +0 -1951
  165. data/lib/cucumber/treetop_parser/feature_ro2.rb +0 -1951
  166. data/lib/cucumber/treetop_parser/feature_ru.rb +0 -1951
  167. data/lib/cucumber/treetop_parser/feature_se.rb +0 -1951
  168. data/lib/cucumber/treetop_parser/feature_zh-CN.rb +0 -1951
  169. data/lib/cucumber/world/pending.rb +0 -22
  170. data/lib/cucumber/world.rb +0 -1
  171. data/setup.rb +0 -1585
  172. data/spec/cucumber/executor_spec.rb +0 -382
  173. data/spec/cucumber/formatters/html_formatter_spec.rb +0 -104
  174. data/spec/cucumber/formatters/pretty_formatter_spec.rb +0 -410
  175. data/spec/cucumber/formatters/progress_formatter_spec.rb +0 -81
  176. data/spec/cucumber/model/table_spec.rb +0 -32
  177. data/spec/cucumber/step_mother_spec.rb +0 -74
  178. data/spec/cucumber/tree/feature_spec.rb +0 -122
  179. data/spec/cucumber/tree/row_scenario_outline_spec.rb +0 -73
  180. data/spec/cucumber/tree/row_scenario_spec.rb +0 -55
  181. data/spec/cucumber/tree/row_step_outline_spec.rb +0 -38
  182. data/spec/cucumber/tree/scenario_outline_spec.rb +0 -50
  183. data/spec/cucumber/tree/scenario_spec.rb +0 -134
  184. data/spec/cucumber/tree/step_outline_spec.rb +0 -17
  185. data/spec/cucumber/tree/step_spec.rb +0 -59
  186. data/spec/cucumber/treetop_parser/feature_parser_spec.rb +0 -120
@@ -0,0 +1,206 @@
1
+ module Cucumber
2
+ module Parser
3
+ # TIP: When you hack on the grammar, just delete feature.rb in this directory.
4
+ # Treetop will then generate the parser in-memory. When you're happy, just generate
5
+ # the rb file with tt feature.tt
6
+ grammar Feature
7
+ include FileParser
8
+ include I18n
9
+
10
+ rule feature
11
+ white comment white tags white header:(!(scenario_outline / scenario) .)* feature_elements {
12
+ def build
13
+ Ast::Feature.new(comment.build, tags.build, header.text_value, feature_elements.build)
14
+ end
15
+ }
16
+ end
17
+
18
+ rule tags
19
+ white ts:(tag (space/eol)+)* {
20
+ def build
21
+ tag_names = ts.elements.map{|e| e.tag.tag_name.text_value}
22
+ Ast::Tags.new(ts.line, tag_names)
23
+ end
24
+ }
25
+ end
26
+
27
+ rule tag
28
+ '@' tag_name:([a-z0-9])+
29
+ end
30
+
31
+ rule comment
32
+ (comment_line eol+)* {
33
+ def build
34
+ Ast::Comment.new(text_value)
35
+ end
36
+ }
37
+ end
38
+
39
+ rule comment_line
40
+ '#' line_to_eol
41
+ end
42
+
43
+ rule feature_elements
44
+ (scenario / scenario_outline)* {
45
+ def build
46
+ elements.map{|s| s.build}
47
+ end
48
+ }
49
+ end
50
+
51
+ rule scenario
52
+ comment tags white scenario_keyword space* name:line_to_eol (eol+ / eof) steps {
53
+ def build
54
+ Ast::Scenario.new(
55
+ comment.build,
56
+ tags.build,
57
+ scenario_keyword.line,
58
+ scenario_keyword.text_value,
59
+ name.text_value,
60
+ steps.build
61
+ )
62
+ end
63
+ }
64
+ end
65
+
66
+ rule scenario_outline
67
+ comment tags white scenario_outline_keyword space* name:line_to_eol white steps examples_sections {
68
+ def build
69
+ Ast::ScenarioOutline.new(
70
+ comment.build,
71
+ tags.build,
72
+ scenario_outline_keyword.line,
73
+ scenario_outline_keyword.text_value,
74
+ name.text_value,
75
+ steps.build,
76
+ examples_sections.build
77
+ )
78
+ end
79
+ }
80
+ end
81
+
82
+ rule steps
83
+ step* {
84
+ def build
85
+ elements.map{|e| e.build}
86
+ end
87
+ }
88
+ end
89
+
90
+ rule step
91
+ space* step_keyword space* name:line_to_eol (eol+ / eof) multi:multiline_arg? {
92
+ def build
93
+ if multi.respond_to?(:build)
94
+ Ast::Step.new(step_keyword.line, step_keyword.text_value, name.text_value, multi.build)
95
+ else
96
+ Ast::Step.new(step_keyword.line, step_keyword.text_value, name.text_value)
97
+ end
98
+ end
99
+ }
100
+ end
101
+
102
+ rule examples_sections
103
+ examples+ {
104
+ def build
105
+ elements.map{|e| e.build}
106
+ end
107
+ }
108
+ end
109
+
110
+ rule examples
111
+ space* examples_keyword white table {
112
+ def build
113
+ [examples_keyword.line, examples_keyword.text_value, "", table.raw]
114
+ end
115
+ }
116
+ end
117
+
118
+ rule multiline_arg
119
+ table / py_string
120
+ end
121
+
122
+ rule table
123
+ table_row+ {
124
+ def build
125
+ Ast::Table.new(raw)
126
+ end
127
+
128
+ def raw
129
+ elements.map{|e| e.build}
130
+ end
131
+ }
132
+ end
133
+
134
+ rule table_row
135
+ space* '|' cells:(cell '|')+ space* (eol+ / eof) {
136
+ def build
137
+ row = cells.elements.map do |elt|
138
+ value = elt.cell.text_value.strip
139
+ value.empty? ? nil : value
140
+ end
141
+
142
+ class << row
143
+ attr_accessor :line
144
+ end
145
+ row.line = cells.line
146
+
147
+ row
148
+ end
149
+ }
150
+ end
151
+
152
+ rule cell
153
+ (!('|' / eol) .)*
154
+ end
155
+
156
+ rule line_to_eol
157
+ (!eol .)+
158
+ end
159
+
160
+ rule py_string
161
+ open_py_string s:(!close_py_string .)* close_py_string {
162
+ def build
163
+ Ast::PyString.new(open_py_string.line, close_py_string.line, s.text_value, open_py_string.indentation)
164
+ end
165
+ }
166
+ end
167
+
168
+ rule open_py_string
169
+ white '"""' space* eol {
170
+ def indentation
171
+ white.text_value.length
172
+ end
173
+
174
+ def line
175
+ white.line
176
+ end
177
+ }
178
+ end
179
+
180
+ rule close_py_string
181
+ eol space* quotes:'"""' white {
182
+ def line
183
+ quotes.line
184
+ end
185
+ }
186
+ end
187
+
188
+ rule white
189
+ (space / eol)*
190
+ end
191
+
192
+ rule space
193
+ [ \t]
194
+ end
195
+
196
+ rule eol
197
+ "\n" / ("\r" "\n"?)
198
+ end
199
+
200
+ rule eof
201
+ !.
202
+ end
203
+
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,50 @@
1
+ module Cucumber
2
+ module Parser
3
+ module FileParser
4
+ FILE_LINE_PATTERN = /^([\w\W]*?):([\d:]+)$/
5
+
6
+ # Parses a file and returns a Cucumber::Ast
7
+ def parse_file(file)
8
+ _, path, lines = *FILE_LINE_PATTERN.match(file)
9
+ if path
10
+ lines = lines.split(':').map { |line| line.to_i }
11
+ else
12
+ path = file
13
+ lines = []
14
+ end
15
+
16
+ feature = File.open(path, Cucumber.file_mode('r')) do |io|
17
+ parse_or_fail(io.read, path)
18
+ end
19
+ feature.lines = lines
20
+ feature
21
+ end
22
+
23
+ def parse_or_fail(s, file=nil)
24
+ parse_tree = parse(s)
25
+ if parse_tree.nil?
26
+ raise SyntaxError.new(file, self)
27
+ else
28
+ ast = parse_tree.build
29
+ ast.file = file
30
+ ast
31
+ end
32
+ end
33
+ end
34
+
35
+ class SyntaxError < StandardError
36
+ def initialize(file, parser)
37
+ tf = parser.terminal_failures
38
+ expected = tf.size == 1 ? tf[0].expected_string.inspect : "one of #{tf.map{|f| f.expected_string.inspect}.uniq*', '}"
39
+ after = parser.input[parser.index...parser.failure_index]
40
+ found = parser.input[parser.failure_index..parser.failure_index]
41
+ @message = "#{file}:#{parser.failure_line}:#{parser.failure_column}: " +
42
+ "Parse error, expected #{expected}. After #{after.inspect}. Found: #{found.inspect}"
43
+ end
44
+
45
+ def message
46
+ @message
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ module Cucumber
2
+ module Parser
3
+ grammar I18n
4
+
5
+ rule scenario_keyword
6
+ '<%= keywords['scenario'] %>:'
7
+ end
8
+
9
+ rule scenario_outline_keyword
10
+ '<%= keywords['scenario_outline'] %>:'
11
+ end
12
+
13
+ rule step_keyword
14
+ '<%= keywords['given'] %>' /
15
+ '<%= keywords['when'] %>' /
16
+ '<%= keywords['then'] %>' /
17
+ '<%= keywords['and'] %>' /
18
+ '<%= keywords['but'] %>'
19
+ end
20
+
21
+ rule examples_keyword
22
+ '<%= keywords['examples'] %>' ':'?
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module Treetop
2
+ module Runtime
3
+ class SyntaxNode
4
+ def line
5
+ input.line_of(interval.first)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'erb'
2
+ require 'treetop'
3
+ require 'treetop/runtime'
4
+ require 'treetop/ruby_extensions'
5
+ require 'cucumber/platform'
6
+ require 'cucumber/ast'
7
+ require 'cucumber/parser/file_parser'
8
+ require 'cucumber/parser/treetop_ext'
9
+
10
+ module Cucumber
11
+ # Classes in this module parse feature files and translate the parse tree
12
+ # (concrete syntax tree) into an abstract syntax tree (AST) using
13
+ # <a href="http://martinfowler.com/dslwip/EmbeddedTranslation.html">Embedded translation</a>.
14
+ #
15
+ # The AST is built by the various <tt>#build</tt> methods in the parse tree.
16
+ #
17
+ # The AST classes are defined in the Cucumber::Ast module.
18
+ module Parser
19
+ def self.load_parser(keywords)
20
+ template = File.open(File.dirname(__FILE__) + "/parser/i18n.tt", Cucumber.file_mode('r')).read
21
+ erb = ERB.new(template)
22
+ grammar = erb.result(binding)
23
+ Treetop.load_from_string(grammar)
24
+ require 'cucumber/parser/feature'
25
+ end
26
+ end
27
+ end
@@ -1,9 +1,11 @@
1
1
  # Detect the platform we're running on so we can tweak behaviour
2
2
  # in various places.
3
3
  require 'rbconfig'
4
+ require 'yaml'
4
5
 
5
6
  module Cucumber
6
7
  LANGUAGE_FILE = File.expand_path(File.dirname(__FILE__) + '/languages.yml')
8
+ LANGUAGES = YAML.load_file(LANGUAGE_FILE)
7
9
  BINARY = File.expand_path(File.dirname(__FILE__) + '/../../bin/cucumber')
8
10
  JRUBY = defined?(JRUBY_VERSION)
9
11
  IRONRUBY = Config::CONFIG['sitedir'] =~ /IronRuby/
@@ -12,21 +14,5 @@ module Cucumber
12
14
  RAILS = defined?(Rails)
13
15
  RUBY_BINARY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
14
16
  RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
15
-
16
- class << self
17
- attr_reader :language
18
-
19
- def load_language(lang)
20
- @language = config[lang]
21
- end
22
-
23
- def languages
24
- config.keys.sort
25
- end
26
-
27
- def config
28
- require 'yaml'
29
- @config ||= YAML.load_file(LANGUAGE_FILE)
30
- end
31
- end
17
+ EXCEPTION_STATUS = Hash.new(:failed)
32
18
  end
@@ -0,0 +1,83 @@
1
+ require 'cucumber/core_ext/string'
2
+ require 'cucumber/core_ext/proc'
3
+
4
+ module Cucumber
5
+ # A Step Definition holds a Regexp and a Proc, and is created
6
+ # by calling <tt>Given</tt>, <tt>When</tt> or <tt>Then</tt>
7
+ # in the <tt>step_definitions</tt> ruby files - for example:
8
+ #
9
+ # Given /I have (\d+) cucumbers in my belly/ do
10
+ # # some code here
11
+ # end
12
+ #
13
+ class StepDefinition
14
+ def self.snippet_text(step_keyword, step_name)
15
+ escaped = Regexp.escape(step_name).gsub('\ ', ' ').gsub('/', '\/')
16
+ "#{step_keyword} /^#{escaped}$/ do\nend"
17
+ end
18
+
19
+ class MissingProc < StandardError
20
+ def message
21
+ "Step definitions must always have a proc"
22
+ end
23
+ end
24
+
25
+ attr_reader :regexp
26
+
27
+ def initialize(regexp, &proc)
28
+ raise MissingProc if proc.nil?
29
+ @regexp, @proc = regexp, proc
30
+ end
31
+
32
+ #:stopdoc:
33
+
34
+ def match(step_name)
35
+ case step_name
36
+ when String then @regexp.match(step_name)
37
+ when Regexp then @regexp == step_name
38
+ end
39
+ end
40
+
41
+ # Formats the matched arguments of the associated Step. This method
42
+ # is usually called from visitors, which render output.
43
+ #
44
+ # The +format+ either be a String or a Proc.
45
+ #
46
+ # If it is a String it should be a format string according to
47
+ # <tt>Kernel#sprinf</tt>, for example:
48
+ #
49
+ # '<span class="param">%s</span></tt>'
50
+ #
51
+ # If it is a Proc, it should take one argument and return the formatted
52
+ # argument, for example:
53
+ #
54
+ # lambda { |param| "[#{param}]" }
55
+ #
56
+ def format_args(step_name, format)
57
+ step_name.gzub(@regexp, format)
58
+ end
59
+
60
+ def matched_args(step_name)
61
+ step_name.match(@regexp).captures
62
+ end
63
+
64
+ def execute(step_name, world, *args)
65
+ args = args.map{|arg| Ast::PyString === arg ? arg.to_s : arg}
66
+ begin
67
+ world.cucumber_instance_exec(true, @regexp.inspect, *args, &@proc)
68
+ rescue Cucumber::ArityMismatchError => e
69
+ e.backtrace.unshift(self.to_backtrace_line)
70
+ raise e
71
+ end
72
+ end
73
+
74
+ def to_backtrace_line
75
+ "#{file_colon_line}:in `#{@regexp.inspect}'"
76
+ end
77
+
78
+ def file_colon_line
79
+ @proc.file_colon_line
80
+ end
81
+
82
+ end
83
+ end
@@ -1,98 +1,154 @@
1
- require 'cucumber/tree/top_down_visitor'
1
+ require 'cucumber/step_definition'
2
+ require 'cucumber/core_ext/instance_exec'
2
3
 
3
4
  module Cucumber
4
- class Pending < StandardError
5
- end
5
+ # This is the main interface for registering step definitions, which is done
6
+ # from <tt>*_steps.rb</tt> files. This module is included right at the top-level
7
+ # so #register_step_definition (and more interestingly - its aliases) are
8
+ # available from the top-level.
9
+ module StepMother
10
+ attr_writer :snippet_generator
6
11
 
7
- class ForcedPending < Pending
8
- end
12
+ class Undefined < StandardError
13
+ attr_reader :step_name
9
14
 
10
- class Duplicate < StandardError
11
- end
15
+ def initialize(step_name)
16
+ super %{Undefined step: "#{step_name}"}
17
+ @step_name = step_name
18
+ end
19
+ Cucumber::EXCEPTION_STATUS[self] = :undefined
20
+ end
12
21
 
13
- class Multiple < StandardError
14
- end
22
+ class Pending < StandardError
23
+ Cucumber::EXCEPTION_STATUS[self] = :pending
24
+ end
15
25
 
16
- class MissingProc < StandardError
17
- def message
18
- "Step definitions must always have a proc"
26
+ # Raised when a step matches 2 or more StepDefinition
27
+ class Ambiguous < StandardError
28
+ def initialize(step_name, step_definitions)
29
+ message = "Ambiguous match of \"#{step_name}\":\n\n"
30
+ message << step_definitions.map{|sd| sd.to_backtrace_line}.join("\n")
31
+ message << "\n\n"
32
+ super(message)
33
+ end
19
34
  end
20
- end
21
35
 
22
- class StepMother
23
- PENDING = lambda do |*_|
24
- raise Pending
36
+ # Raised when 2 or more StepDefinition have the same Regexp
37
+ class Redundant < StandardError
38
+ def initialize(step_def_1, step_def_2)
39
+ message = "Multiple step definitions have the same Regexp:\n\n"
40
+ message << step_def_1.to_backtrace_line << "\n"
41
+ message << step_def_2.to_backtrace_line << "\n\n"
42
+ super(message)
43
+ end
25
44
  end
26
- require 'cucumber/core_ext/proc'
27
- PENDING.extend(CoreExt::CallIn)
28
- PENDING.name = "PENDING"
29
45
 
30
- def initialize
31
- @step_procs = Hash.new(PENDING)
46
+ # Registers a new StepDefinition. This method is aliased
47
+ # to <tt>Given</tt>, <tt>When</tt> and <tt>Then</tt>.
48
+ #
49
+ # See Cucumber#alias_steps for details on how to
50
+ # create your own aliases.
51
+ #
52
+ # The +&proc+ gets executed in the context of a <tt>world</tt>
53
+ # object, which is defined by #World. A new <tt>world</tt>
54
+ # object is created for each scenario and is shared across
55
+ # step definitions within that scenario.
56
+ def register_step_definition(regexp, &proc)
57
+ step_definition = StepDefinition.new(regexp, &proc)
58
+ step_definitions.each do |already|
59
+ raise Redundant.new(already, step_definition) if already.match(regexp)
60
+ end
61
+ step_definitions << step_definition
62
+ step_definition
32
63
  end
33
64
 
34
- def register_step_proc(key, &proc)
35
- raise MissingProc if proc.nil?
36
- regexp = case(key)
37
- when String
38
- # Replace the $foo and $bar style parameters
39
- pattern = key.gsub(/\$\w+/, '(.*)')
40
- Regexp.new("^#{pattern}$")
41
- when Regexp
42
- key
43
- else
44
- raise "Step patterns must be Regexp or String, but was: #{key.inspect}"
65
+ def world(scenario, &proc)
66
+ world = new_world
67
+ begin
68
+ (@before_procs ||= []).each do |proc|
69
+ world.cucumber_instance_exec(false, 'Before', scenario, &proc)
70
+ end
71
+ yield world
72
+ ensure
73
+ (@after_procs ||= []).each do |proc|
74
+ world.cucumber_instance_exec(false, 'After', scenario, &proc) rescue nil
75
+ end
45
76
  end
46
- proc.extend(CoreExt::CallIn)
47
- proc.name = key.inspect
77
+ end
78
+
79
+ # Registers a Before proc. You can call this method as many times as you
80
+ # want (typically from ruby scripts under <tt>support</tt>).
81
+ def Before(&proc)
82
+ (@before_procs ||= []) << proc
83
+ end
48
84
 
49
- if @step_procs.has_key?(regexp)
50
- first_proc = @step_procs[regexp]
51
- message = %{Duplicate step definitions:
85
+ def After(&proc)
86
+ (@after_procs ||= []) << proc
87
+ end
52
88
 
53
- #{first_proc.to_backtrace_line}
54
- #{proc.to_backtrace_line}
89
+ # Registers a World proc. You can call this method as many times as you
90
+ # want (typically from ruby scripts under <tt>support</tt>).
91
+ def World(&proc)
92
+ (@world_procs ||= []) << proc
93
+ end
55
94
 
56
- }
57
- raise Duplicate.new(message)
95
+ # Creates a new world instance
96
+ def new_world #:nodoc:
97
+ world = Object.new
98
+ (@world_procs ||= []).each do |proc|
99
+ world = proc.call(world)
58
100
  end
59
101
 
60
- @step_procs[regexp] = proc
102
+ world.extend(WorldMethods)
103
+ world.__cucumber_step_mother = self
104
+
105
+ world.extend(::Spec::Matchers) if defined?(::Spec::Matchers)
106
+ world
61
107
  end
62
108
 
63
- def regexp_args_proc(step_name)
64
- candidates = @step_procs.map do |regexp, proc|
65
- if step_name =~ regexp
66
- [regexp, $~.captures, proc]
67
- end
68
- end.compact
69
-
70
- case(candidates.length)
71
- when 0
72
- [nil, [], PENDING]
73
- when 1
74
- candidates[0]
75
- else
76
- message = %{Multiple step definitions match #{step_name.inspect}:
77
-
78
- #{candidates.map{|regexp, args, proc| proc.to_backtrace_line}.join("\n")}
79
-
80
- }
81
- raise Multiple.new(message)
109
+ # Looks up the StepDefinition that matches +step_name+
110
+ def step_definition(step_name) #:nodoc:
111
+ found = step_definitions.select do |step_definition|
112
+ step_definition.match(step_name)
82
113
  end
83
- end
84
-
85
- def proc_for(regexp)
86
- @step_procs[regexp]
114
+ raise Undefined.new(step_name) if found.empty?
115
+ raise Ambiguous.new(step_name, found) if found.size > 1
116
+ found[0]
117
+ end
118
+
119
+ def step_definitions
120
+ @step_definitions ||= []
87
121
  end
88
-
89
- def has_step_definition?(step_name)
90
- _, _, proc = regexp_args_proc(step_name)
91
- proc != PENDING
122
+
123
+ def snippet_text(step_keyword, step_name)
124
+ @snippet_generator.snippet_text(step_keyword, step_name)
92
125
  end
93
-
94
- # TODO - move execute here?
95
- def execute(step)
126
+
127
+ module WorldMethods #:nodoc:
128
+ attr_writer :__cucumber_step_mother, :__cucumber_current_step
129
+
130
+ # Call a step from within a step definition
131
+ def __cucumber_invoke(name, *multiline_arguments)
132
+ begin
133
+ @__cucumber_step_mother.step_definition(name).execute(name, self, *multiline_arguments)
134
+ rescue Exception => e
135
+ @__cucumber_current_step.exception = e
136
+ raise e
137
+ end
138
+ end
139
+
140
+ def pending(message = "TODO")
141
+ if block_given?
142
+ begin
143
+ yield
144
+ rescue Exception => e
145
+ raise Pending.new(message)
146
+ end
147
+ raise Pending.new("Expected pending '#{message}' to fail. No Error was raised. No longer pending?")
148
+ else
149
+ raise Pending.new(message)
150
+ end
151
+ end
96
152
  end
97
153
  end
98
154
  end
@@ -3,7 +3,7 @@ module Cucumber #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
5
  TINY = 16
6
- PATCH = 4 # Set to nil for official release
6
+ PATCH = 5 # Set to nil for official release
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PATCH].compact.join('.')
9
9
  end