cuke-patterns 0.1.1 → 0.1.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.
- data/VERSION +1 -1
- data/cuke-patterns.gemspec +1 -1
- data/features/step_definitions/simple_pattern_steps.rb +12 -3
- data/lib/cuke-patterns.rb +32 -5
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/cuke-patterns.gemspec
CHANGED
@@ -14,9 +14,6 @@ Then /^@x should evaluate to (.*)$/ do |expression|
|
|
14
14
|
@x.should == eval(expression)
|
15
15
|
end
|
16
16
|
|
17
|
-
Pattern 'a', /a|an/
|
18
|
-
Pattern 'an', /a|an/
|
19
|
-
|
20
17
|
Pattern :class, /([A-Z][a-z]+)/ do |class_name|
|
21
18
|
Object.const_get(class_name)
|
22
19
|
end
|
@@ -28,3 +25,15 @@ end
|
|
28
25
|
Pattern :list_of_integers, /(-?\d+(?: *(?:,|and) *-?\d+)*)/ do |list|
|
29
26
|
list.split(/ *(?:,|and) */).map{|number| number.to_i}
|
30
27
|
end
|
28
|
+
|
29
|
+
PatternGenerator do |key|
|
30
|
+
%w[a an].include?(key) and /a|an/
|
31
|
+
end
|
32
|
+
|
33
|
+
## This PatternGenerator would enable automatic singularize/pluralize of ALL words
|
34
|
+
## so you don't have to resort to regexp construction for steps just do deal with
|
35
|
+
## pluralization related nonsense.
|
36
|
+
#
|
37
|
+
# PatternGenerator do |key|
|
38
|
+
# Regexp.new([Regexp.escape(key.singularize), Regexp.escape(key.pluralize)].join('|'))
|
39
|
+
# end
|
data/lib/cuke-patterns.rb
CHANGED
@@ -6,6 +6,10 @@ module CukePatterns
|
|
6
6
|
@rb_language.register_rb_cuke_pattern(name, regexp, &proc)
|
7
7
|
end
|
8
8
|
|
9
|
+
def register_rb_cuke_pattern_generator(&proc)
|
10
|
+
@rb_language.register_rb_cuke_pattern_generator(&proc)
|
11
|
+
end
|
12
|
+
|
9
13
|
end
|
10
14
|
|
11
15
|
# For extending the Cucumber::RbSupport::RbLanguage class
|
@@ -33,7 +37,21 @@ module CukePatterns
|
|
33
37
|
end
|
34
38
|
|
35
39
|
def cuke_patterns
|
36
|
-
@cuke_patterns ||= {}
|
40
|
+
@cuke_patterns ||= Hash.new {|hash, key| default_cuke_pattern(key)}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Hook this method to automatically generate regexp matches for words in step
|
44
|
+
# definition strings that *don't* match to registered patterns.
|
45
|
+
def default_cuke_pattern(key)
|
46
|
+
default_cuke_pattern_generators.each do |generator|
|
47
|
+
regexp, proc = generator[key]
|
48
|
+
return [regexp, proc] if regexp
|
49
|
+
end
|
50
|
+
return nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def default_cuke_pattern_generators
|
54
|
+
@default_cuke_pattern_generators ||= []
|
37
55
|
end
|
38
56
|
|
39
57
|
def register_rb_cuke_pattern(name, regexp, &conversion_proc)
|
@@ -51,6 +69,10 @@ module CukePatterns
|
|
51
69
|
return cuke_patterns[name] = [regexp, conversion_proc]
|
52
70
|
end
|
53
71
|
|
72
|
+
def register_rb_cuke_pattern_generator(&proc)
|
73
|
+
default_cuke_pattern_generators << proc
|
74
|
+
end
|
75
|
+
|
54
76
|
def register_rb_step_definition_with_cuke_patterns(matcher, proc)
|
55
77
|
return register_rb_step_definition_without_cuke_patterns(matcher, proc) unless matcher.is_a?(String)
|
56
78
|
cuke_pattern_delayed_step_definition_registrations << [matcher, proc]
|
@@ -73,13 +95,13 @@ module CukePatterns
|
|
73
95
|
# Split the string by non-alphanumeric, underscore or leading-colon characters
|
74
96
|
matcher.scan(/(:?\w+)|([^:\w]+|:)/) do |candidate, non_candidate|
|
75
97
|
|
76
|
-
|
98
|
+
regexp, conversion_proc = cuke_patterns[candidate] if candidate
|
99
|
+
|
100
|
+
if non_candidate or not regexp
|
77
101
|
matcher_regexp << Regexp.escape(candidate || non_candidate)
|
78
102
|
next
|
79
103
|
end
|
80
104
|
|
81
|
-
regexp, conversion_proc = cuke_patterns[candidate]
|
82
|
-
|
83
105
|
pattern_capture_count = capture_count_for(regexp)
|
84
106
|
|
85
107
|
proc = convert_cuke_pattern_proc_arguments(
|
@@ -89,7 +111,7 @@ module CukePatterns
|
|
89
111
|
pattern_counter += 1 unless pattern_capture_count == 0
|
90
112
|
capture_counter += pattern_capture_count
|
91
113
|
|
92
|
-
matcher_regexp << regexp
|
114
|
+
matcher_regexp << "(?:#{regexp})"
|
93
115
|
|
94
116
|
end
|
95
117
|
|
@@ -162,6 +184,11 @@ Cucumber::RbSupport::RbDsl.module_eval do
|
|
162
184
|
def Pattern(name, regexp, &proc)
|
163
185
|
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern(name, regexp, &proc)
|
164
186
|
end
|
187
|
+
|
188
|
+
def PatternGenerator(&proc)
|
189
|
+
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern_generator(&proc)
|
190
|
+
end
|
191
|
+
|
165
192
|
end
|
166
193
|
|
167
194
|
Cucumber::RbSupport::RbLanguage.class_eval do
|