cuke-patterns 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Brendan Baldwin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,54 @@
1
+ # cuke-patterns
2
+
3
+ Makes cucumber step definitions more focused, understandable, searchable and awesomeable.
4
+
5
+ * Author: Brendan Baldwin <brendan@usergenic.com>
6
+ * Github: http://github.com/brendan/cuke-patterns
7
+
8
+ ## What does it do?
9
+
10
+ (This gem is only relevant if you're using cucumber already.)
11
+
12
+ Change your step definition files from this:
13
+
14
+ When /^(\w+) withdraws \$(\d+\(?:\.\d+)?)$/ do |name, amount|
15
+ customer = Bank::Customer.find_by_name(name)
16
+ amount = BigDecimal.new(amount)
17
+ Bank::Teller.withdraw_for(customer, amount)
18
+ end
19
+
20
+ Into this:
21
+
22
+ When ":customer withdraws :dollar_amount" do |customer, amount|
23
+ Bank::Teller.withdraw_for(customer, amount)
24
+ end
25
+
26
+ Pattern :customer, /(\w+)/ do |name|
27
+ Bank::Customer.find_by_name(name)
28
+ end
29
+
30
+ Pattern :dollar_amount, /\$(\d+\(?:.\d+)?)/ do |amount|
31
+ BigDecimal.new(amount)
32
+ end
33
+
34
+ Patterns are kind of like Transforms in cucumber, except Patterns are designed to be
35
+ referenced in Step definitions by name as opposed to recognized by form automatically.
36
+
37
+ To use it, just pop this in your app's support/env.rb which gets loaded by cucumber:
38
+
39
+ require 'rubygems'
40
+ require 'cuke-patterns'
41
+
42
+ ## Notes on Patches/Pull Requests
43
+
44
+ * Fork the project.
45
+ * Make your feature addition or bug fix.
46
+ * Add tests for it. This is important so I don't break it in a
47
+ future version unintentionally.
48
+ * Commit, do not mess with rakefile, version, or history.
49
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
50
+ * Send me a pull request. Bonus points for topic branches.
51
+
52
+ ## Copyright
53
+
54
+ Copyright (c) 2010 Brendan Baldwin. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'cucumber'
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:features) do |t|
7
+ t.cucumber_opts = "features --format pretty"
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gem|
13
+ gem.name = "cuke-patterns"
14
+ gem.summary = %Q{Makes cucumber step definitions more focused, understandable, searchable and awesomeable.}
15
+ gem.description = %Q{Makes cucumber step definitions more focused, understandable, searchable and awesomeable.}
16
+ gem.email = "brendan@usergenic.com"
17
+ gem.homepage = "http://github.com/brendan/cuke-patterns"
18
+ gem.authors = ["Brendan Baldwin"]
19
+ # gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
20
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :rcov do
43
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ end
45
+ end
46
+
47
+ task :test => :check_dependencies
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "cuke-patterns #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cuke-patterns}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brendan Baldwin"]
12
+ s.date = %q{2010-08-14}
13
+ s.description = %q{Makes cucumber step definitions more focused, understandable, searchable and awesomeable.}
14
+ s.email = %q{brendan@usergenic.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.markdown",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "cuke-patterns.gemspec",
25
+ "features/no_regressions_in_normal_cukes.feature",
26
+ "features/simple_cuke_patterns.feature",
27
+ "features/step_definitions/no_regression_steps.rb",
28
+ "features/step_definitions/simple_pattern_steps.rb",
29
+ "features/support/env.rb",
30
+ "lib/cuke-patterns.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/brendan/cuke-patterns}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Makes cucumber step definitions more focused, understandable, searchable and awesomeable.}
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
48
+
@@ -0,0 +1,9 @@
1
+ Feature: No regressions in normal cukes
2
+ In order for anyone to want to use this cuke-patterns gem
3
+ As a developer of cuke-patterns
4
+ I want to ensure this doesn't screw anything up
5
+
6
+ Scenario: Simple regexp matched scenario
7
+ Given a typical given step
8
+ When a typical when step
9
+ Then a typical then step
@@ -0,0 +1,16 @@
1
+ Feature: Simple Cuke Patterns
2
+
3
+ Scenario: Transform string of digits into Fixnum
4
+ When @x is 1234
5
+ Then @x should be a Fixnum
6
+ And @x should evaluate to 1234
7
+
8
+ Scenario: Transform digits with negative sign into Fixnum
9
+ When @x is -1234
10
+ Then @x should be a Fixnum
11
+ And @x should evaluate to -1234
12
+
13
+ Scenario: Transform a list to an array of Fixnums
14
+ When @x contains 1, 2, 3 and 4
15
+ Then @x should be an Array
16
+ And @x should evaluate to [1, 2, 3, 4]
@@ -0,0 +1,6 @@
1
+ Given /^a typical given step$/ do
2
+ end
3
+ When /^a typical when step$/ do
4
+ end
5
+ Then /^a typical then step$/ do
6
+ end
@@ -0,0 +1,30 @@
1
+ When "@x is :integer" do |integer|
2
+ @x = integer
3
+ end
4
+
5
+ When "@x contains :list_of_integers" do |list|
6
+ @x = list
7
+ end
8
+
9
+ Then "@x should be a :class" do |klass|
10
+ @x.class.should == klass
11
+ end
12
+
13
+ Then /^@x should evaluate to (.*)$/ do |expression|
14
+ @x.should == eval(expression)
15
+ end
16
+
17
+ Pattern 'a', /a|an/
18
+ Pattern 'an', /a|an/
19
+
20
+ Pattern :class, /([A-Z][a-z]+)/ do |class_name|
21
+ Object.const_get(class_name)
22
+ end
23
+
24
+ Pattern :integer, /(-?\d+)/ do |number|
25
+ number.to_i
26
+ end
27
+
28
+ Pattern :list_of_integers, /(-?\d+(?: *(?:,|and) *-?\d+)*)/ do |list|
29
+ list.split(/ *(?:,|and) */).map{|number| number.to_i}
30
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.push File.expand_path(File.join(File.dirname(__FILE__),'..','..','lib'))
2
+
3
+ require 'cuke-patterns'
4
+
@@ -0,0 +1,173 @@
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
+ end
10
+
11
+ # For extending the Cucumber::RbSupport::RbLanguage class
12
+ module RbLanguageExt
13
+
14
+ def self.included(rb_language_class)
15
+ rb_language_class.class_eval do
16
+ alias_method :register_rb_step_definition_without_cuke_patterns, :register_rb_step_definition
17
+ alias_method :register_rb_step_definition, :register_rb_step_definition_with_cuke_patterns
18
+ end
19
+
20
+ super
21
+ end
22
+
23
+ def apply_cuke_patterns_to_delayed_step_definition_registrations!
24
+ count = 0
25
+ cuke_pattern_delayed_step_definition_registrations.each do |matcher, proc|
26
+ regexp, converted_proc = convert_cuke_patterns_and_proc(matcher, proc)
27
+ register_rb_step_definition(regexp, converted_proc)
28
+ count += 1
29
+ end
30
+
31
+ # Clean up our mess!
32
+ remove_instance_variable('@cuke_pattern_delayed_step_definition_registrations')
33
+ end
34
+
35
+ def cuke_patterns
36
+ @cuke_patterns ||= {}
37
+ end
38
+
39
+ def register_rb_cuke_pattern(name, regexp, &conversion_proc)
40
+ name = ":#{name}" if name.is_a?(Symbol) # so :user becomes ':user'
41
+
42
+ if conversion_proc
43
+ # Count the capturing '(' characters to get the pattern arity
44
+ pattern_capture_count = capture_count_for(regexp)
45
+
46
+ if pattern_capture_count != conversion_proc.arity
47
+ raise "There are #{pattern_capture_count} of capture(s) in Pattern #{name} but block arity is #{conversion_proc.arity}"
48
+ end
49
+ end
50
+
51
+ return cuke_patterns[name] = [regexp, conversion_proc]
52
+ end
53
+
54
+ def register_rb_step_definition_with_cuke_patterns(matcher, proc)
55
+ return register_rb_step_definition_without_cuke_patterns(matcher, proc) unless matcher.is_a?(String)
56
+ cuke_pattern_delayed_step_definition_registrations << [matcher, proc]
57
+ return :registration_delayed_by_cuke_patterns
58
+ end
59
+
60
+ private
61
+
62
+ def capture_count_for(regexp)
63
+ regexp.to_s.gsub(/\\\\|\\\(|\(\?/,'').scan(/\(/).length
64
+ end
65
+
66
+ def convert_cuke_patterns_and_proc(matcher, proc)
67
+
68
+ matcher_regexp = "^"
69
+
70
+ pattern_counter = 0
71
+ capture_counter = 0
72
+
73
+ # Split the string by non-alphanumeric, underscore or leading-colon characters
74
+ matcher.scan(/(:?\w+)|([^:\w]+|:)/) do |candidate, non_candidate|
75
+
76
+ if non_candidate or not cuke_patterns.include?(candidate)
77
+ matcher_regexp << Regexp.escape(candidate || non_candidate)
78
+ next
79
+ end
80
+
81
+ regexp, conversion_proc = cuke_patterns[candidate]
82
+
83
+ pattern_capture_count = capture_count_for(regexp)
84
+
85
+ proc = convert_cuke_pattern_proc_arguments(
86
+ proc, conversion_proc,
87
+ pattern_counter, pattern_capture_count) if conversion_proc
88
+
89
+ pattern_counter += 1 unless pattern_capture_count == 0
90
+ capture_counter += pattern_capture_count
91
+
92
+ matcher_regexp << regexp.to_s
93
+
94
+ end
95
+
96
+ matcher_regexp << "$"
97
+
98
+ return [Regexp.new(matcher_regexp), proc]
99
+ end
100
+
101
+ # Returns a new Proc/block made by applying the conversion_block to a number of
102
+ # the arguments yielded to the original_block (arg_count) at the offset.
103
+ def convert_cuke_pattern_proc_arguments(original_proc, conversion_proc, offset, arg_count)
104
+
105
+ arity = original_proc.arity + arg_count - 1
106
+ arg_list = (1..arity).map{|n| "arg#{n}"}.join(",")
107
+
108
+ file, line = original_proc.file_colon_line.split(/:/,2)
109
+ line = line.to_i
110
+
111
+ new_proc = instance_eval(<<-ruby, file, line)
112
+ Proc.new do |#{arg_list}|
113
+ args = [#{arg_list}]
114
+ arg_range = offset..(offset + arg_count - 1)
115
+ converted_args = instance_exec(*args[arg_range], &conversion_proc)
116
+ args.slice!(arg_range)
117
+ args.insert(offset, converted_args)
118
+ instance_exec(*args, &original_proc)
119
+ end
120
+ ruby
121
+
122
+ class << new_proc; self end.module_eval do
123
+ define_method(:file_colon_line){ original_proc.file_colon_line }
124
+ end
125
+
126
+ return new_proc
127
+ end
128
+
129
+ def cuke_pattern_delayed_step_definition_registrations
130
+ @cuke_pattern_delayed_step_definition_registrations ||= []
131
+ end
132
+
133
+ end
134
+
135
+ # For extending the Cucumber::StepMother class
136
+ module StepMotherExt
137
+
138
+ def self.included(step_mother_class)
139
+ step_mother_class.class_eval do
140
+ alias_method :load_code_files_without_cuke_patterns, :load_code_files
141
+ alias_method :load_code_files, :load_code_files_with_cuke_patterns
142
+ end
143
+
144
+ super
145
+ end
146
+
147
+ def load_code_files_with_cuke_patterns(step_def_files)
148
+ result = load_code_files_without_cuke_patterns(step_def_files)
149
+ @programming_languages.each do |programming_language|
150
+ programming_language.apply_cuke_patterns_to_delayed_step_definition_registrations!
151
+ end
152
+ return result
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+
159
+ Cucumber::RbSupport::RbDsl.module_eval do
160
+ extend CukePatterns::RbDslExt
161
+
162
+ def Pattern(name, regexp, &proc)
163
+ Cucumber::RbSupport::RbDsl.register_rb_cuke_pattern(name, regexp, &proc)
164
+ end
165
+ end
166
+
167
+ Cucumber::RbSupport::RbLanguage.class_eval do
168
+ include CukePatterns::RbLanguageExt
169
+ end
170
+
171
+ Cucumber::StepMother.class_eval do
172
+ include CukePatterns::StepMotherExt
173
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuke-patterns
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Brendan Baldwin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-14 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Makes cucumber step definitions more focused, understandable, searchable and awesomeable.
22
+ email: brendan@usergenic.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.markdown
30
+ files:
31
+ - LICENSE
32
+ - README.markdown
33
+ - Rakefile
34
+ - VERSION
35
+ - cuke-patterns.gemspec
36
+ - features/no_regressions_in_normal_cukes.feature
37
+ - features/simple_cuke_patterns.feature
38
+ - features/step_definitions/no_regression_steps.rb
39
+ - features/step_definitions/simple_pattern_steps.rb
40
+ - features/support/env.rb
41
+ - lib/cuke-patterns.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/brendan/cuke-patterns
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Makes cucumber step definitions more focused, understandable, searchable and awesomeable.
72
+ test_files: []
73
+