mhennemeyer-matchy 0.2.0

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.1 2008-10-03
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jeremy McAnally
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/Manifest.txt ADDED
@@ -0,0 +1,25 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ matchy.gemspec
6
+ History.txt
7
+ License.txt
8
+ Manifest.txt
9
+ PostInstall.txt
10
+ README.rdoc
11
+ Rakefile
12
+ config/hoe.rb
13
+ config/requirements.rb
14
+ lib/matchy.rb
15
+ lib/matchy/version.rb
16
+ lib/matchy/expectation_builder
17
+ lib/matchy/modals.rb
18
+ lib/matchy/def_matcher.rb
19
+ lib/matchy/matcher_builder.rb
20
+ lib/matchy/built_in/enumerable_expectations.rb
21
+ lib/matchy/built_in/error_expectations.rb
22
+ lib/matchy/built_in/operator_expectations.rb
23
+ lib/matchy/built_in/truth_expectations.rb
24
+ lib/matchy/built_in/change_expectations.rb
25
+ setup.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on matchy, see http://matchy.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,119 @@
1
+ = matchy
2
+
3
+ * http://github.com/mhennemeyer/matchy
4
+
5
+ == DESCRIPTION:
6
+ A 300loc refactoring of Jeremy Mcanally's Matchy. Original Description:
7
+ Hate writing assertions? Need a little behavior-driven love in your tests? Then matchy is for you.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Get the beauty of RSpec without all the overhead
12
+ * Create your own matchers with ease and def_matcher()
13
+
14
+ == SYNOPSIS:
15
+
16
+ * Get BDD on your objects
17
+
18
+ x = 13 * 4
19
+ x.should == 42
20
+
21
+ y = "hello"
22
+ y.length.should_not be(4)
23
+
24
+ * Use familiar syntax to specify things
25
+
26
+ # RSpec
27
+ "my string".should =~ /string/
28
+ lambda { raise "FAIL" }.should raise_error
29
+
30
+ # matchy
31
+ "my string".should =~ /string/
32
+ lambda { raise "FAIL" }.should raise_error
33
+
34
+ * Most of familiar RSpec Matchers are built in
35
+
36
+ # raise_error matcher
37
+ lambda {raise}.should raise_error #pass
38
+ lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
39
+ lambda {raise "message"}.should raise_error("message") #pass
40
+ lambda {raise "message"}.should raise_error(/essa/) #pass
41
+
42
+ # change matcher
43
+ lambda {@var+=1}.should change {@var}
44
+ # passes
45
+ lambda { }.should change {@var}
46
+ # fails
47
+ @var = 1
48
+ lambda {@var+=1}.should change {@var}.from(1).to(2)
49
+ # passes
50
+
51
+ # be_something matcher
52
+ @obj.should be_something
53
+ # passes if @obj.something? is true
54
+
55
+ * a lot more ...
56
+
57
+ * Get the speed of Test:Unit with the syntax of RSpec
58
+
59
+ * Create your own matchers
60
+
61
+ # nil_matcher (As a simple and quick example)
62
+ def_matcher :be_nil do |receiver, matcher, args|
63
+ receiver.nil?
64
+ end
65
+
66
+ nil.should be_nil # pass
67
+ nil.should_not be_nil # fails
68
+ 'notnil'.should be_nil # fails
69
+ 'notnil'.should_not be_nil # pass
70
+
71
+ # have(n).somethings matcher (A little bit more complex)
72
+ def_matcher :have do |receiver, matcher, args|
73
+ count = args[0]
74
+ something = matcher.msgs[0].name
75
+ actual = receiver.send(something).length
76
+ matcher.positive_msg = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
77
+ actual == count
78
+ end
79
+
80
+ class Thing
81
+ def widgets
82
+ @widgets ||= []
83
+ end
84
+ end
85
+ @thing.should have(3).widgets
86
+
87
+ == REQUIREMENTS:
88
+
89
+ * Test::Unit (you got it)
90
+
91
+ == INSTALL:
92
+
93
+ $ gem sources -a http://gems.github.com
94
+ $ sudo gem install mhennemeyer-matchy
95
+
96
+ == LICENSE:
97
+
98
+ (The MIT License)
99
+
100
+ Copyright (c) 2008 Jeremy McAnally
101
+
102
+ Permission is hereby granted, free of charge, to any person obtaining
103
+ a copy of this software and associated documentation files (the
104
+ 'Software'), to deal in the Software without restriction, including
105
+ without limitation the rights to use, copy, modify, merge, publish,
106
+ distribute, sublicense, and/or sell copies of the Software, and to
107
+ permit persons to whom the Software is furnished to do so, subject to
108
+ the following conditions:
109
+
110
+ The above copyright notice and this permission notice shall be
111
+ included in all copies or substantial portions of the Software.
112
+
113
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
114
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
115
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
116
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
117
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
118
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
119
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'matchy/version'
2
+
3
+ AUTHOR = 'Jeremy McAnally' # can also be an array of Authors
4
+ EMAIL = "jeremy@entp.com"
5
+ DESCRIPTION = "RSpec-esque matchers for use in Test::Unit"
6
+ GEM_NAME = 'matchy' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'matchy' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+ EXTRA_DEPENDENCIES = [
11
+ # ['activesupport', '>= 1.3.1']
12
+ ] # An array of rubygem dependencies [name, version]
13
+
14
+ @config_file = "~/.rubyforge/user-config.yml"
15
+ @config = nil
16
+ RUBYFORGE_USERNAME = "unknown"
17
+ def rubyforge_username
18
+ unless @config
19
+ begin
20
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
21
+ rescue
22
+ puts <<-EOS
23
+ ERROR: No rubyforge config file found: #{@config_file}
24
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
25
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
26
+ EOS
27
+ exit
28
+ end
29
+ end
30
+ RUBYFORGE_USERNAME.replace @config["username"]
31
+ end
32
+
33
+
34
+ REV = nil
35
+ # UNCOMMENT IF REQUIRED:
36
+ # REV = YAML.load(`svn info`)['Revision']
37
+ VERS = Matchy::VERSION::STRING + (REV ? ".#{REV}" : "")
38
+ RDOC_OPTS = ['--quiet', '--title', 'matchy documentation',
39
+ "--opname", "index.html",
40
+ "--line-numbers",
41
+ "--main", "README.rdoc",
42
+ "--inline-source"]
43
+
44
+ class Hoe
45
+ def extra_deps
46
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
47
+ @extra_deps
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.developer(AUTHOR, EMAIL)
55
+ p.description = DESCRIPTION
56
+ p.summary = DESCRIPTION
57
+ p.url = HOMEPATH
58
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
59
+ p.test_globs = ["test/**/test_*.rb"]
60
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
61
+
62
+ # == Optional
63
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
64
+ #p.extra_deps = EXTRA_DEPENDENCIES
65
+
66
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
67
+ end
68
+
69
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ $hoe.rsync_args = '-av --delete --ignore-errors'
73
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
data/lib/matchy.rb ADDED
@@ -0,0 +1,54 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ # Matchy should work with either test/unit
5
+ # or minitest
6
+ module Matchy
7
+ def self.minitest?
8
+ # This needs to be better.
9
+ # How can we decide if we really have a
10
+ # suite of MiniTest Tests?
11
+ # Rails for example defines MiniTest, so only check for
12
+ # defined?(MiniTest) would be malicious
13
+ defined?(MiniTest) && defined?(MiniTest::Assertions) &&
14
+ (!defined?(Test::Unit::TestCase) || !(Test::Unit::TestCase < MiniTest::Assertions))
15
+ end
16
+ def self.assertions_module
17
+ minitest? ? MiniTest::Assertions : Test::Unit::Assertions
18
+ end
19
+ def self.test_case_class
20
+ minitest? ? MiniTest::Unit::TestCase : Test::Unit::TestCase
21
+ end
22
+ end
23
+
24
+ require 'rubygems'
25
+ require 'test/unit' unless Matchy.minitest?
26
+
27
+ require 'matchy/expectation_builder'
28
+ require 'matchy/modals'
29
+ require 'matchy/version'
30
+ require 'matchy/matcher_builder'
31
+ require 'matchy/def_matcher'
32
+
33
+ require 'matchy/built_in/enumerable_expectations'
34
+ require 'matchy/built_in/error_expectations'
35
+ require 'matchy/built_in/truth_expectations'
36
+ require 'matchy/built_in/operator_expectations'
37
+ require 'matchy/built_in/change_expectations'
38
+
39
+
40
+ # Hack of Evil.
41
+ # Track the current testcase and
42
+ # provide it to the operator matchers.
43
+ Matchy.test_case_class.class_eval do
44
+ alias_method :old_run_method_aliased_by_matchy, :run
45
+ def run(whatever, *args, &block)
46
+ $current_test_case = self
47
+ old_run_method_aliased_by_matchy(whatever, *args, &block)
48
+ end
49
+ end
50
+
51
+ Matchy.test_case_class.send(:include, Matchy::Expectations::TestCaseExtensions)
52
+
53
+ include Matchy::DefMatcher
54
+
@@ -0,0 +1,31 @@
1
+ module Matchy
2
+ module Expectations
3
+ module TestCaseExtensions
4
+ # Checks if the given block alters the value of the block attached to change
5
+ #
6
+ # ==== Examples
7
+ # lambda {var += 1}.should change {var}.by(1)
8
+ # lambda {var += 2}.should change {var}.by_at_least(1)
9
+ # lambda {var += 1}.should change {var}.by_at_most(1)
10
+ # lambda {var += 2}.should change {var}.from(1).to(3) if var = 1
11
+ def change(&block)
12
+ build_matcher(:change) do |receiver, matcher, args|
13
+ before, done, after = block.call, receiver.call, block.call
14
+ comparison = after != before
15
+ if list = matcher.msgs
16
+ comparison = case list[0].name
17
+ # todo: provide meaningful messages
18
+ when :by then (after == before + list[0].args[0] || after == before - list[0].args[0])
19
+ when :by_at_least then (after >= before + list[0].args[0] || after <= before - list[0].args[0])
20
+ when :by_at_most then (after <= before + list[0].args[0] && after >= before - list[0].args[0])
21
+ when :from then (before == list[0].args[0]) && (after == list[1].args[0])
22
+ end
23
+ end
24
+ matcher.positive_msg = "given block shouldn't alter the block attached to change"
25
+ matcher.negative_msg = "given block should alter the block attached to change"
26
+ comparison
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ module Matchy
2
+ module Expectations
3
+ module TestCaseExtensions
4
+
5
+ # Calls +include?+ on the receiver for any object. You can also provide
6
+ # multiple arguments to see if all of them are included.
7
+ #
8
+ # ==== Examples
9
+ #
10
+ # [1,2,3].should include(1)
11
+ # [7,8,8].should_not include(3)
12
+ # ['a', 'b', 'c'].should include('a', 'c')
13
+ #
14
+ def include(*obj)
15
+ _clude(:include, obj)
16
+ end
17
+
18
+ # Expects the receiver to exclude the given object(s). You can provide
19
+ # multiple arguments to see if all of them are included.
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # [1,2,3].should exclude(16)
24
+ # [7,8,8].should_not exclude(7)
25
+ # ['a', 'b', 'c'].should exclude('e', 'f', 'g')
26
+ #
27
+ def exclude(*obj)
28
+ _clude(:exclude, obj)
29
+ end
30
+
31
+ private
32
+ def _clude(sym, obj)
33
+ build_matcher(sym, obj) do |given, matcher, args|
34
+ matcher.positive_msg = "Expected #{given.inspect} to #{sym} #{args.inspect}."
35
+ matcher.negative_msg = "Expected #{given.inspect} to not #{sym} #{args.inspect}."
36
+ args.inject(true) {|m,o| m && (given.include?(o) == (sym == :include)) }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,66 @@
1
+ module Matchy
2
+ module Expectations
3
+ module TestCaseExtensions
4
+ # Expects a lambda to raise an error. You can specify the error or leave it blank to encompass
5
+ # any error.
6
+ #
7
+ # ==== Examples
8
+ #
9
+ # lambda { raise "FAILURE." }.should raise_error
10
+ # lambda { puts i_dont_exist }.should raise_error(NameError)
11
+ #
12
+ def raise_error(*obj)
13
+ build_matcher(:raise_error, obj) do |receiver, matcher, args|
14
+ expected = args[0] || Exception
15
+ raised = false
16
+ error = nil
17
+ begin
18
+ receiver.call
19
+ rescue Exception => e
20
+ raised = true
21
+ error = e
22
+ end
23
+ if expected.respond_to?(:ancestors) && expected.ancestors.include?(Exception)
24
+ matcher.positive_msg = "Expected #{receiver.inspect} to raise #{expected.name}, " +
25
+ (error ? "but #{error.class.name} was raised instead." : "but none was raised.")
26
+ matcher.negative_msg = "Expected #{receiver.inspect} to not raise #{expected.name}."
27
+ comparison = (raised && error.class.ancestors.include?(expected))
28
+ else
29
+ message = error ? error.message : "none"
30
+ matcher.positive_msg = "Expected #{receiver.inspect} to raise error with message matching '#{expected}', but '#{message}' was raised."
31
+ matcher.negative_msg = "Expected #{receiver.inspect} to raise error with message not matching '#{expected}', but '#{message}' was raised."
32
+ comparison = (raised && (expected.kind_of?(Regexp) ? ((error.message =~ expected) ? true : false) : expected == error.message))
33
+ end
34
+ comparison
35
+ end
36
+ end
37
+
38
+ # Expects a lambda to throw an error.
39
+ #
40
+ # ==== Examples
41
+ #
42
+ # lambda { throw :thing }.should throw_symbol(:thing)
43
+ # lambda { "not this time" }.should_not throw_symbol(:hello)
44
+ #
45
+ def throw_symbol(*obj)
46
+ build_matcher(:throw_symbol, obj) do |receiver, matcher, args|
47
+ raised, thrown_symbol, expected = false, nil, args[0]
48
+ begin
49
+ receiver.call
50
+ rescue NameError => e
51
+ raise e unless e.message =~ /uncaught throw/
52
+ raised = true
53
+ thrown_symbol = e.name.to_sym if e.respond_to?(:name)
54
+ rescue ArgumentError => e
55
+ raise e unless e.message =~ /uncaught throw/
56
+ thrown_symbol = e.message.match(/uncaught throw :(.+)/)[1].to_sym
57
+ end
58
+ matcher.positive_msg = "Expected #{receiver.inspect} to throw :#{expected}, but " +
59
+ "#{thrown_symbol ? ':' + thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}."
60
+ matcher.negative_msg = "Expected #{receiver.inspect} to not throw :#{expected}."
61
+ expected == thrown_symbol
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end