cuke-patterns 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/cuke-patterns.gemspec +6 -3
- data/lib/cuke-patterns.rb +3 -196
- data/lib/cuke-patterns/rb_dsl_ext.rb +25 -0
- data/lib/cuke-patterns/rb_language_ext.rb +150 -0
- data/lib/cuke-patterns/step_mother_ext.rb +29 -0
- metadata +6 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/cuke-patterns.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{cuke-patterns}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brendan Baldwin"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-16}
|
13
13
|
s.description = %q{Makes cucumber step definitions more focused, understandable, searchable and awesomeable.}
|
14
14
|
s.email = %q{brendan@usergenic.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,7 +28,10 @@ Gem::Specification.new do |s|
|
|
28
28
|
"features/step_definitions/no_regression_steps.rb",
|
29
29
|
"features/step_definitions/simple_pattern_steps.rb",
|
30
30
|
"features/support/env.rb",
|
31
|
-
"lib/cuke-patterns.rb"
|
31
|
+
"lib/cuke-patterns.rb",
|
32
|
+
"lib/cuke-patterns/rb_dsl_ext.rb",
|
33
|
+
"lib/cuke-patterns/rb_language_ext.rb",
|
34
|
+
"lib/cuke-patterns/step_mother_ext.rb"
|
32
35
|
]
|
33
36
|
s.homepage = %q{http://github.com/brendan/cuke-patterns}
|
34
37
|
s.rdoc_options = ["--charset=UTF-8"]
|
data/lib/cuke-patterns.rb
CHANGED
@@ -1,200 +1,7 @@
|
|
1
1
|
module CukePatterns
|
2
|
-
|
3
|
-
module RbDslExt
|
4
|
-
|
5
|
-
def register_rb_cuke_pattern(name, regexp, &proc)
|
6
|
-
@rb_language.register_rb_cuke_pattern(name, regexp, &proc)
|
7
|
-
end
|
8
|
-
|
9
|
-
def register_rb_cuke_pattern_generator(&proc)
|
10
|
-
@rb_language.register_rb_cuke_pattern_generator(&proc)
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
# For extending the Cucumber::RbSupport::RbLanguage class
|
16
|
-
module RbLanguageExt
|
17
|
-
|
18
|
-
def self.included(rb_language_class)
|
19
|
-
rb_language_class.class_eval do
|
20
|
-
alias_method :register_rb_step_definition_without_cuke_patterns, :register_rb_step_definition
|
21
|
-
alias_method :register_rb_step_definition, :register_rb_step_definition_with_cuke_patterns
|
22
|
-
end
|
23
|
-
|
24
|
-
super
|
25
|
-
end
|
26
|
-
|
27
|
-
def apply_cuke_patterns_to_delayed_step_definition_registrations!
|
28
|
-
count = 0
|
29
|
-
cuke_pattern_delayed_step_definition_registrations.each do |matcher, proc|
|
30
|
-
regexp, converted_proc = convert_cuke_patterns_and_proc(matcher, proc)
|
31
|
-
register_rb_step_definition(regexp, converted_proc)
|
32
|
-
count += 1
|
33
|
-
end
|
34
|
-
|
35
|
-
# Clean up our mess!
|
36
|
-
remove_instance_variable('@cuke_pattern_delayed_step_definition_registrations')
|
37
|
-
end
|
38
|
-
|
39
|
-
def 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 ||= []
|
55
|
-
end
|
56
|
-
|
57
|
-
def register_rb_cuke_pattern(name, regexp, &conversion_proc)
|
58
|
-
name = ":#{name}" if name.is_a?(Symbol) # so :user becomes ':user'
|
59
|
-
|
60
|
-
if conversion_proc
|
61
|
-
# Count the capturing '(' characters to get the pattern arity
|
62
|
-
pattern_capture_count = capture_count_for(regexp)
|
63
|
-
|
64
|
-
if pattern_capture_count != conversion_proc.arity
|
65
|
-
raise "There are #{pattern_capture_count} of capture(s) in Pattern #{name} but block arity is #{conversion_proc.arity}"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
return cuke_patterns[name] = [regexp, conversion_proc]
|
70
|
-
end
|
71
|
-
|
72
|
-
def register_rb_cuke_pattern_generator(&proc)
|
73
|
-
default_cuke_pattern_generators << proc
|
74
|
-
end
|
75
|
-
|
76
|
-
def register_rb_step_definition_with_cuke_patterns(matcher, proc)
|
77
|
-
return register_rb_step_definition_without_cuke_patterns(matcher, proc) unless matcher.is_a?(String)
|
78
|
-
cuke_pattern_delayed_step_definition_registrations << [matcher, proc]
|
79
|
-
return :registration_delayed_by_cuke_patterns
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
|
84
|
-
def capture_count_for(regexp)
|
85
|
-
regexp.to_s.gsub(/\\\\|\\\(|\(\?/,'').scan(/\(/).length
|
86
|
-
end
|
87
|
-
|
88
|
-
def convert_cuke_patterns_and_proc(matcher, proc)
|
89
|
-
|
90
|
-
matcher_regexp = "^"
|
91
|
-
|
92
|
-
pattern_counter = 0
|
93
|
-
capture_counter = 0
|
94
|
-
|
95
|
-
# Split the string by non-alphanumeric, underscore or leading-colon characters
|
96
|
-
matcher.scan(/(:?\w+)|([^:\w]+|:)/) do |candidate, non_candidate|
|
97
|
-
|
98
|
-
regexp, conversion_proc = cuke_patterns[candidate] if candidate
|
99
|
-
|
100
|
-
if non_candidate or not regexp
|
101
|
-
matcher_regexp << Regexp.escape(candidate || non_candidate)
|
102
|
-
next
|
103
|
-
end
|
104
|
-
|
105
|
-
pattern_capture_count = capture_count_for(regexp)
|
106
|
-
|
107
|
-
proc = convert_cuke_pattern_proc_arguments(
|
108
|
-
proc, conversion_proc,
|
109
|
-
pattern_counter, pattern_capture_count) if conversion_proc
|
110
|
-
|
111
|
-
pattern_counter += 1 unless pattern_capture_count == 0
|
112
|
-
capture_counter += pattern_capture_count
|
113
|
-
|
114
|
-
matcher_regexp << "(?:#{regexp})"
|
115
|
-
|
116
|
-
end
|
117
|
-
|
118
|
-
matcher_regexp << "$"
|
119
|
-
|
120
|
-
return [Regexp.new(matcher_regexp), proc]
|
121
|
-
end
|
122
|
-
|
123
|
-
# Returns a new Proc/block made by applying the conversion_block to a number of
|
124
|
-
# the arguments yielded to the original_block (arg_count) at the offset.
|
125
|
-
def convert_cuke_pattern_proc_arguments(original_proc, conversion_proc, offset, arg_count)
|
126
|
-
|
127
|
-
arity = original_proc.arity + arg_count - 1
|
128
|
-
arg_list = (1..arity).map{|n| "arg#{n}"}.join(",")
|
129
|
-
|
130
|
-
file, line = original_proc.file_colon_line.split(/:/,2)
|
131
|
-
line = line.to_i
|
132
|
-
|
133
|
-
new_proc = instance_eval(<<-ruby, file, line)
|
134
|
-
Proc.new do |#{arg_list}|
|
135
|
-
args = [#{arg_list}]
|
136
|
-
arg_range = offset..(offset + arg_count - 1)
|
137
|
-
converted_args = instance_exec(*args[arg_range], &conversion_proc)
|
138
|
-
args.slice!(arg_range)
|
139
|
-
args.insert(offset, converted_args)
|
140
|
-
instance_exec(*args, &original_proc)
|
141
|
-
end
|
142
|
-
ruby
|
143
|
-
|
144
|
-
class << new_proc; self end.module_eval do
|
145
|
-
define_method(:file_colon_line){ original_proc.file_colon_line }
|
146
|
-
end
|
147
|
-
|
148
|
-
return new_proc
|
149
|
-
end
|
150
|
-
|
151
|
-
def cuke_pattern_delayed_step_definition_registrations
|
152
|
-
@cuke_pattern_delayed_step_definition_registrations ||= []
|
153
|
-
end
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
# For extending the Cucumber::StepMother class
|
158
|
-
module StepMotherExt
|
159
|
-
|
160
|
-
def self.included(step_mother_class)
|
161
|
-
step_mother_class.class_eval do
|
162
|
-
alias_method :load_code_files_without_cuke_patterns, :load_code_files
|
163
|
-
alias_method :load_code_files, :load_code_files_with_cuke_patterns
|
164
|
-
end
|
165
|
-
|
166
|
-
super
|
167
|
-
end
|
168
|
-
|
169
|
-
def load_code_files_with_cuke_patterns(step_def_files)
|
170
|
-
result = load_code_files_without_cuke_patterns(step_def_files)
|
171
|
-
@programming_languages.each do |programming_language|
|
172
|
-
programming_language.apply_cuke_patterns_to_delayed_step_definition_registrations!
|
173
|
-
end
|
174
|
-
return result
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
2
|
end
|
180
3
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
def Pattern(name, regexp, &proc)
|
185
|
-
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern(name, regexp, &proc)
|
186
|
-
end
|
4
|
+
require 'cuke-patterns/rb_dsl_ext'
|
5
|
+
require 'cuke-patterns/rb_language_ext'
|
6
|
+
require 'cuke-patterns/step_mother_ext'
|
187
7
|
|
188
|
-
def PatternGenerator(&proc)
|
189
|
-
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern_generator(&proc)
|
190
|
-
end
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
Cucumber::RbSupport::RbLanguage.class_eval do
|
195
|
-
include CukePatterns::RbLanguageExt
|
196
|
-
end
|
197
|
-
|
198
|
-
Cucumber::StepMother.class_eval do
|
199
|
-
include CukePatterns::StepMotherExt
|
200
|
-
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CukePatterns
|
2
|
+
module RbDslExt
|
3
|
+
|
4
|
+
def register_rb_cuke_pattern(name, regexp, &proc)
|
5
|
+
@rb_language.register_rb_cuke_pattern(name, regexp, &proc)
|
6
|
+
end
|
7
|
+
|
8
|
+
def register_rb_cuke_pattern_generator(&proc)
|
9
|
+
@rb_language.register_rb_cuke_pattern_generator(&proc)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Cucumber::RbSupport::RbDsl.module_eval do
|
16
|
+
extend CukePatterns::RbDslExt
|
17
|
+
|
18
|
+
def Pattern(name, regexp, &proc)
|
19
|
+
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern(name, regexp, &proc)
|
20
|
+
end
|
21
|
+
|
22
|
+
def PatternGenerator(&proc)
|
23
|
+
Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern_generator(&proc)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
module CukePatterns
|
2
|
+
|
3
|
+
# For extending the Cucumber::RbSupport::RbLanguage class
|
4
|
+
module RbLanguageExt
|
5
|
+
|
6
|
+
def self.included(rb_language_class)
|
7
|
+
rb_language_class.class_eval do
|
8
|
+
alias_method :register_rb_step_definition_without_cuke_patterns, :register_rb_step_definition
|
9
|
+
alias_method :register_rb_step_definition, :register_rb_step_definition_with_cuke_patterns
|
10
|
+
end
|
11
|
+
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply_cuke_patterns_to_delayed_step_definition_registrations!
|
16
|
+
count = 0
|
17
|
+
cuke_pattern_delayed_step_definition_registrations.each do |matcher, proc|
|
18
|
+
regexp, converted_proc = convert_cuke_patterns_and_proc(matcher, proc)
|
19
|
+
register_rb_step_definition(regexp, converted_proc)
|
20
|
+
count += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
# Clean up our mess!
|
24
|
+
remove_instance_variable('@cuke_pattern_delayed_step_definition_registrations')
|
25
|
+
end
|
26
|
+
|
27
|
+
def cuke_patterns
|
28
|
+
@cuke_patterns ||= Hash.new {|hash, key| default_cuke_pattern(key)}
|
29
|
+
end
|
30
|
+
|
31
|
+
# Hook this method to automatically generate regexp matches for words in step
|
32
|
+
# definition strings that *don't* match to registered patterns.
|
33
|
+
def default_cuke_pattern(key)
|
34
|
+
default_cuke_pattern_generators.each do |generator|
|
35
|
+
regexp, proc = generator[key]
|
36
|
+
return [regexp, proc] if regexp
|
37
|
+
end
|
38
|
+
return nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def default_cuke_pattern_generators
|
42
|
+
@default_cuke_pattern_generators ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
def register_rb_cuke_pattern(name, regexp, &conversion_proc)
|
46
|
+
name = ":#{name}" if name.is_a?(Symbol) # so :user becomes ':user'
|
47
|
+
|
48
|
+
if conversion_proc
|
49
|
+
# Count the capturing '(' characters to get the pattern arity
|
50
|
+
pattern_capture_count = capture_count_for(regexp)
|
51
|
+
|
52
|
+
if pattern_capture_count != conversion_proc.arity
|
53
|
+
raise "There are #{pattern_capture_count} of capture(s) in Pattern #{name} but block arity is #{conversion_proc.arity}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
return cuke_patterns[name] = [regexp, conversion_proc]
|
58
|
+
end
|
59
|
+
|
60
|
+
def register_rb_cuke_pattern_generator(&proc)
|
61
|
+
default_cuke_pattern_generators << proc
|
62
|
+
end
|
63
|
+
|
64
|
+
def register_rb_step_definition_with_cuke_patterns(matcher, proc)
|
65
|
+
return register_rb_step_definition_without_cuke_patterns(matcher, proc) unless matcher.is_a?(String)
|
66
|
+
cuke_pattern_delayed_step_definition_registrations << [matcher, proc]
|
67
|
+
return :registration_delayed_by_cuke_patterns
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def capture_count_for(regexp)
|
73
|
+
regexp.to_s.gsub(/\\\\|\\\(|\(\?/,'').scan(/\(/).length
|
74
|
+
end
|
75
|
+
|
76
|
+
def convert_cuke_patterns_and_proc(matcher, proc)
|
77
|
+
|
78
|
+
matcher_regexp = "^"
|
79
|
+
|
80
|
+
pattern_counter = 0
|
81
|
+
capture_counter = 0
|
82
|
+
|
83
|
+
# Split the string by non-alphanumeric, underscore or leading-colon characters
|
84
|
+
matcher.scan(/(:?\w+)|([^:\w]+|:)/) do |candidate, non_candidate|
|
85
|
+
|
86
|
+
regexp, conversion_proc = cuke_patterns[candidate] if candidate
|
87
|
+
|
88
|
+
if non_candidate or not regexp
|
89
|
+
matcher_regexp << Regexp.escape(candidate || non_candidate)
|
90
|
+
next
|
91
|
+
end
|
92
|
+
|
93
|
+
pattern_capture_count = capture_count_for(regexp)
|
94
|
+
|
95
|
+
proc = convert_cuke_pattern_proc_arguments(
|
96
|
+
proc, conversion_proc,
|
97
|
+
pattern_counter, pattern_capture_count) if conversion_proc
|
98
|
+
|
99
|
+
pattern_counter += 1 unless pattern_capture_count == 0
|
100
|
+
capture_counter += pattern_capture_count
|
101
|
+
|
102
|
+
matcher_regexp << "(?:#{regexp})"
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
matcher_regexp << "$"
|
107
|
+
|
108
|
+
return [Regexp.new(matcher_regexp), proc]
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns a new Proc/block made by applying the conversion_block to a number of
|
112
|
+
# the arguments yielded to the original_block (arg_count) at the offset.
|
113
|
+
def convert_cuke_pattern_proc_arguments(original_proc, conversion_proc, offset, arg_count)
|
114
|
+
|
115
|
+
arity = original_proc.arity + arg_count - 1
|
116
|
+
arg_list = (1..arity).map{|n| "arg#{n}"}.join(",")
|
117
|
+
|
118
|
+
file, line = original_proc.file_colon_line.split(/:/,2)
|
119
|
+
line = line.to_i
|
120
|
+
|
121
|
+
new_proc = instance_eval(<<-ruby, file, line)
|
122
|
+
Proc.new do |#{arg_list}|
|
123
|
+
args = [#{arg_list}]
|
124
|
+
arg_range = offset..(offset + arg_count - 1)
|
125
|
+
converted_args = instance_exec(*args[arg_range], &conversion_proc)
|
126
|
+
args.slice!(arg_range)
|
127
|
+
args.insert(offset, converted_args)
|
128
|
+
instance_exec(*args, &original_proc)
|
129
|
+
end
|
130
|
+
ruby
|
131
|
+
|
132
|
+
class << new_proc; self end.module_eval do
|
133
|
+
define_method(:file_colon_line){ original_proc.file_colon_line }
|
134
|
+
end
|
135
|
+
|
136
|
+
return new_proc
|
137
|
+
end
|
138
|
+
|
139
|
+
def cuke_pattern_delayed_step_definition_registrations
|
140
|
+
@cuke_pattern_delayed_step_definition_registrations ||= []
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
Cucumber::RbSupport::RbLanguage.class_eval do
|
148
|
+
include CukePatterns::RbLanguageExt
|
149
|
+
end
|
150
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module CukePatterns
|
2
|
+
|
3
|
+
# For extending the Cucumber::StepMother class
|
4
|
+
module StepMotherExt
|
5
|
+
|
6
|
+
def self.included(step_mother_class)
|
7
|
+
step_mother_class.class_eval do
|
8
|
+
alias_method :load_code_files_without_cuke_patterns, :load_code_files
|
9
|
+
alias_method :load_code_files, :load_code_files_with_cuke_patterns
|
10
|
+
end
|
11
|
+
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_code_files_with_cuke_patterns(step_def_files)
|
16
|
+
result = load_code_files_without_cuke_patterns(step_def_files)
|
17
|
+
@programming_languages.each do |programming_language|
|
18
|
+
programming_language.apply_cuke_patterns_to_delayed_step_definition_registrations!
|
19
|
+
end
|
20
|
+
return result
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
Cucumber::StepMother.class_eval do
|
28
|
+
include CukePatterns::StepMotherExt
|
29
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brendan Baldwin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-16 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -40,6 +40,9 @@ files:
|
|
40
40
|
- features/step_definitions/simple_pattern_steps.rb
|
41
41
|
- features/support/env.rb
|
42
42
|
- lib/cuke-patterns.rb
|
43
|
+
- lib/cuke-patterns/rb_dsl_ext.rb
|
44
|
+
- lib/cuke-patterns/rb_language_ext.rb
|
45
|
+
- lib/cuke-patterns/step_mother_ext.rb
|
43
46
|
has_rdoc: true
|
44
47
|
homepage: http://github.com/brendan/cuke-patterns
|
45
48
|
licenses: []
|