WatersOfOblivion-easy-matchers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.0 2009-06-19
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/easy-matchers.rb
6
+ script/console
7
+ script/destroy
8
+ script/generate
9
+ spec/easy-matchers_spec.rb
10
+ spec/spec.opts
11
+ spec/spec_helper.rb
12
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = easy-matchers
2
+
3
+ * http://github.com/WatersOfOblivion/easy-matchers
4
+
5
+ == DESCRIPTION:
6
+
7
+ A DSL for easily writing RSpec matchers
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Simple matcher syntax
12
+ * Automatically handles error messages
13
+
14
+ == SYNOPSIS:
15
+
16
+ should :be_named do
17
+ match do
18
+ unless @options[:dont_do_it]
19
+ if @target.respond_to? :name
20
+ if @target.name == @expected
21
+ fail "it is actually named #{@target.name.inspect}"
22
+ end
23
+ else
24
+ fail "it is not named"
25
+ end
26
+ end
27
+ end
28
+
29
+ failure do
30
+ "#{@target.class} should be named #{@expected.inspect}"
31
+ end
32
+ end
33
+
34
+ class Foo
35
+ def name
36
+ "Foo"
37
+ end
38
+ end
39
+
40
+ class Bar
41
+ end
42
+
43
+ describe "My specs" do
44
+ before do
45
+ @foo = Foo.new
46
+ end
47
+
48
+ it "should pass" do
49
+ @foo.should :be_named "Foo"
50
+ @foo.should :be_named "Bar", :dont_do_it => true
51
+ end
52
+
53
+ it "should not pass" do
54
+ @foo.should :be_named "Bar" # => 'Foo should be named "Bar": it is actually named "Foo"'
55
+ @bar.should :be_named "Bar" # => 'Bar should be named "Bar": it is not named'
56
+ end
57
+ end
58
+
59
+ == REQUIREMENTS:
60
+
61
+ * RSpec
62
+
63
+ == INSTALL:
64
+
65
+ * sudo gem install WatersOfOblivion-easy-matchers -s http://gems.github.com
66
+
67
+ == LICENSE:
68
+
69
+ (The MIT License)
70
+
71
+ Copyright (c) 2009 Jonathan Bryant
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining
74
+ a copy of this software and associated documentation files (the
75
+ 'Software'), to deal in the Software without restriction, including
76
+ without limitation the rights to use, copy, modify, merge, publish,
77
+ distribute, sublicense, and/or sell copies of the Software, and to
78
+ permit persons to whom the Software is furnished to do so, subject to
79
+ the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be
82
+ included in all copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
85
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
87
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
88
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
89
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
90
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
2
+ %w[rake rake/clean fileutils newgem rubigen].each { |f| require f }
3
+ require File.dirname(__FILE__) + '/lib/easy-matchers'
4
+
5
+ # Generate all the Rake tasks
6
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
7
+ $hoe = Hoe.new('easy-matchers', EasyMatchers::VERSION) do |p|
8
+ p.developer('Jonathan Bryant', 'jonathan@watersofoblivion.com')
9
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
10
+ # p.extra_deps = [
11
+ # ['activesupport','>= 2.0.2'],
12
+ # ]
13
+ p.extra_dev_deps = [
14
+ ['newgem', ">= #{::Newgem::VERSION}"]
15
+ ]
16
+
17
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
18
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
19
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
20
+ p.rsync_args = '-av --delete --ignore-errors'
21
+ end
22
+
23
+ require 'newgem/tasks' # load /tasks/*.rake
24
+ Dir['tasks/**/*.rake'].each { |t| load t }
25
+
26
+ task :default => :spec
@@ -0,0 +1,61 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module EasyMatchers
5
+ VERSION = '0.0.1'
6
+
7
+ def should name, &block
8
+ matcher = Class.new AbstractMatcher
9
+ matcher.instance_eval &block if block_given?
10
+
11
+ define_method name do |*expected|
12
+ matcher.new *expected
13
+ end
14
+ end
15
+
16
+ class MatchFailure < Exception
17
+ end
18
+
19
+ class AbstractMatcher
20
+ class << self
21
+ def match &block
22
+ define_method :match, &block
23
+ end
24
+
25
+ def failure &block
26
+ define_method :failure_message do
27
+ "#{block.call}" + (@failure && !@failure.empty? ? ": #{@failure}" : "")
28
+ end
29
+ end
30
+ end
31
+
32
+ def initialize *args
33
+ @options = args.last.is_a?(Hash) ? args.pop : {}
34
+ @expected = args.shift
35
+ end
36
+
37
+ def matches? target
38
+ @target = target
39
+ begin
40
+ result = match
41
+ (result.is_a?(TrueClass) || result.is_a?(FalseClass)) ? result : true
42
+ rescue MatchFailure => ex
43
+ @failure = ex.message
44
+ false
45
+ end
46
+ end
47
+
48
+ def failure_message
49
+ "#{@target} did not match #{@expected}"
50
+ end
51
+
52
+ def fail *args
53
+ raise MatchFailure.new *args
54
+ end
55
+
56
+ protected
57
+
58
+ def match
59
+ end
60
+ end
61
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/easy-matchers.rb'}"
9
+ puts "Loading easy-matchers gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,149 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ module EasyMatchers
4
+ describe AbstractMatcher do
5
+ describe "class method" do
6
+ before do
7
+ @block = Proc.new { }
8
+ end
9
+
10
+ describe :match do
11
+ it "should redefine the :match method" do
12
+ AbstractMatcher.should_receive(:define_method).with(:match)
13
+ AbstractMatcher.match &@block
14
+ end
15
+ end
16
+
17
+ describe :failure do
18
+ it "should redefine the :failure_message method" do
19
+ AbstractMatcher.should_receive(:define_method).with(:failure_message)
20
+ AbstractMatcher.failure &@block
21
+ end
22
+ end
23
+ end
24
+
25
+ describe :initialize do
26
+ it "should put the first argument into @expected" do
27
+ AbstractMatcher.new.instance_variable_get("@expected").should be_nil
28
+ AbstractMatcher.new(:foo, :bar).instance_variable_get("@expected").should == :foo
29
+ end
30
+
31
+ it "should put the options hash in @options" do
32
+ AbstractMatcher.new.instance_variable_get("@options").should == {}
33
+ AbstractMatcher.new(:foo => :bar).instance_variable_get("@options").should == { :foo => :bar }
34
+ end
35
+ end
36
+
37
+ describe :matches? do
38
+ before do
39
+ @matcher = AbstractMatcher.new
40
+ end
41
+
42
+ it "should put the argument in @target" do
43
+ @matcher.instance_variable_get("@target").should be_nil
44
+ @matcher.matches?(:foo)
45
+ @matcher.instance_variable_get("@target").should == :foo
46
+ end
47
+
48
+ it "should call the match method if it exists" do
49
+ @matcher.should_receive(:match)
50
+ @matcher.matches?(:foo)
51
+ end
52
+
53
+ it "should return the result if the match method returns a boolean" do
54
+ @matcher.stub!(:match).and_return(true, false)
55
+ @matcher.matches?(:foo).should be_true
56
+ @matcher.matches?(:foo).should be_false
57
+ end
58
+
59
+ it "should return true otherwise" do
60
+ @matcher.stub! :match => nil
61
+ @matcher.matches?(:foo).should be_true
62
+ end
63
+
64
+ it "should return false if a MatchFailure was raised" do
65
+ @matcher.stub!(:match).and_raise(MatchFailure)
66
+ @matcher.matches?(:foo).should be_false
67
+ end
68
+
69
+ it "should set the error message to @failure" do
70
+ @matcher.stub!(:match).and_raise(MatchFailure.new "the error message")
71
+ @matcher.matches?(:foo)
72
+ @matcher.instance_variable_get("@failure").should == "the error message"
73
+ end
74
+ end
75
+
76
+ describe :failure_message do
77
+ before do
78
+ matcher = Class.new(AbstractMatcher)
79
+ matcher.failure do
80
+ "the message"
81
+ end
82
+ @matcher = matcher.new
83
+ end
84
+
85
+ it "should have a basic default" do
86
+ AbstractMatcher.new.failure_message.should match(/.* did not match .*/)
87
+ end
88
+
89
+ it "should display the given message" do
90
+ @matcher.failure_message.should == "the message"
91
+ end
92
+
93
+ it "should append the failure message if present" do
94
+ @matcher.instance_variable_set("@failure", "the failure")
95
+ @matcher.failure_message.should == "the message: the failure"
96
+ end
97
+ end
98
+
99
+ describe :fail do
100
+ it "should raise a MatchFailure exception" do
101
+ lambda {
102
+ AbstractMatcher.new.fail
103
+ }.should raise_error MatchFailure
104
+ end
105
+
106
+ it "should set the message of the exception to the parameter" do
107
+ begin
108
+ AbstractMatcher.new.fail "the message"
109
+ rescue MatchFailure => ex
110
+ ex.message.should == "the message"
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "class method" do
117
+ describe :should do
118
+ =begin
119
+
120
+ # Testing :should is difficult to test using RSpec
121
+
122
+ before do
123
+ @module = Module.new
124
+ @module.send :include, EasyMatchers
125
+ end
126
+
127
+ it "should subclass AbstractMatcher" do
128
+ matcher = mock "Matcher"
129
+ Class.should_receive(:new).and_return matcher
130
+ @module.should :foo
131
+ end
132
+
133
+ it "should instance_eval the block in the new subclass" do
134
+ lambda {
135
+ @module.should :foo do
136
+ fail if self.superclass == AbstractMatcher
137
+ end
138
+ }.should raise_error RuntimeError
139
+ end
140
+
141
+ it "should define a method with the name" do
142
+ @module.should_not respond_to? :foo
143
+ @module.should :foo
144
+ @module.should respond_to? :foo
145
+ end
146
+ =end
147
+ end
148
+ end
149
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'easy-matchers'
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: WatersOfOblivion-easy-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Bryant
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: A DSL for easily writing RSpec matchers
36
+ email:
37
+ - jonathan@watersofoblivion.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - README.rdoc
46
+ files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/easy-matchers.rb
52
+ - script/console
53
+ - script/destroy
54
+ - script/generate
55
+ - spec/easy-matchers_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ - tasks/rspec.rake
59
+ has_rdoc: true
60
+ homepage: http://github.com/WatersOfOblivion/easy-matchers
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: easy-matchers
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: A DSL for easily writing RSpec matchers
86
+ test_files: []
87
+