jnunemaker-matchy 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-10-03
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -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.
@@ -0,0 +1,36 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.rdoc
6
+ Rakefile
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ countloc.rb
10
+ lib/matchy.rb
11
+ lib/matchy/built_in/change_expectations.rb
12
+ lib/matchy/built_in/enumerable_expectations.rb
13
+ lib/matchy/built_in/error_expectations.rb
14
+ lib/matchy/built_in/operator_expectations.rb
15
+ lib/matchy/built_in/truth_expectations.rb
16
+ lib/matchy/custom_matcher.rb
17
+ lib/matchy/expectation_builder.rb
18
+ lib/matchy/matcher_builder.rb
19
+ lib/matchy/modals.rb
20
+ lib/matchy/version.rb
21
+ matchy.gemspec
22
+ setup.rb
23
+ tasks/deployment.rake
24
+ tasks/environment.rake
25
+ test/all.rb
26
+ test/ruby1.9.compatibility_tests.rb
27
+ test/test_change_expectation.rb
28
+ test/test_custom_matcher.rb
29
+ test/test_enumerable_expectations.rb
30
+ test/test_error_expectations.rb
31
+ test/test_expectation_builder.rb
32
+ test/test_helper.rb
33
+ test/test_matcher_builder.rb
34
+ test/test_modals.rb
35
+ test/test_operator_expectations.rb
36
+ test/test_truth_expectations.rb
@@ -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
+
@@ -0,0 +1,162 @@
1
+ = matchy
2
+
3
+ * http://github.com/jeremymcanally/matchy
4
+
5
+ == DESCRIPTION:
6
+ Hate writing assertions? Need a little behavior-driven love in your tests? Then matchy is for you.
7
+
8
+ == FEATURES/PROBLEMS:
9
+
10
+ * Get the beauty of RSpec without all the overhead
11
+ * Create your own matchers with ease
12
+
13
+ == SYNOPSIS:
14
+
15
+ * Get BDD on your objects
16
+
17
+ x = 13 * 4
18
+ x.should == 42
19
+
20
+ y = "hello"
21
+ y.length.should_not be(4)
22
+
23
+ * Use familiar syntax to specify things
24
+
25
+ # RSpec
26
+ "my string".should =~ /string/
27
+ lambda { raise "FAIL" }.should raise_error
28
+
29
+ # matchy
30
+ "my string".should =~ /string/
31
+ lambda { raise "FAIL" }.should raise_error
32
+
33
+ * Most of familiar RSpec Matchers are built in
34
+
35
+ # raise_error matcher
36
+ lambda {raise}.should raise_error #pass
37
+ lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
38
+ lambda {raise "message"}.should raise_error("message") #pass
39
+ lambda {raise "message"}.should raise_error(/essa/) #pass
40
+
41
+ # change matcher
42
+ lambda {@var+=1}.should change {@var}
43
+ # passes
44
+ lambda { }.should change {@var}
45
+ # fails
46
+ @var = 1
47
+ lambda {@var+=1}.should change {@var}.from(1).to(2)
48
+ # passes
49
+
50
+ # be_something matcher
51
+ @obj.should be_something
52
+ # passes if @obj.something? is true
53
+
54
+ * a lot more ...
55
+
56
+ * Create your own custom matchers
57
+
58
+ # maybe in your test helper
59
+ class Test::Unit::TestCase
60
+ custom_matcher :be_nil do |receiver, matcher, args|
61
+ receiver.nil?
62
+ end
63
+
64
+ # also you can set positive (should) and negative (should not) failure messages
65
+ custom_matcher :be_nil do |receiver, matcher, args|
66
+ matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
67
+ matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
68
+ receiver.nil?
69
+ end
70
+ end
71
+
72
+ # your actual test
73
+ class NilTest < Test::Unit::TestCase
74
+ def test_nil_stuff
75
+ nil.should be_nil # pass
76
+ nil.should_not be_nil # fail
77
+ 'foo'.should_not be_nil # pass
78
+ 'foo'.should be_nil # fail
79
+ end
80
+ end
81
+
82
+ # Matchers can accept arguments
83
+ class Test::Unit::TestCase
84
+ custom_matcher :have_error_on do |receiver, matcher, args|
85
+ attribute = args[0]
86
+
87
+ receiver.valid?
88
+ receiver.errors.on(attribute).should_not == nil
89
+ end
90
+ end
91
+
92
+ class ArgumentTest < Test::Unit::TestCase
93
+ class Item < ActiveRecord::Base
94
+ validate_presence_of :title
95
+ end
96
+
97
+ def test_arguments
98
+ item = Item.new
99
+ item.should have_error_on(:title) # pass
100
+ item.title = 'Foo'
101
+ item.should_not have_error_on(:title) # pass
102
+ end
103
+ end
104
+
105
+ # Even more advanced, you can have messages on matchers
106
+ class Test::Unit::TestCase
107
+ custom_matcher :have do |receiver, matcher, args|
108
+ count = args[0]
109
+ something = matcher.chained_messages[0].name
110
+ actual = receiver.send(something).size
111
+ matcher.positive_failure_message = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
112
+ actual == count
113
+ end
114
+ end
115
+
116
+ class MoreAdvancedTest < Test::Unit::TestCase
117
+ class Item
118
+ def tags
119
+ %w(foo bar baz)
120
+ end
121
+ end
122
+
123
+ def test_item_has_tags
124
+ item = Item.new
125
+ item.should have(3).tags # pass
126
+ item.should have(2).tags # fail
127
+ end
128
+ end
129
+
130
+ == REQUIREMENTS:
131
+
132
+ * Test::Unit (you got it)
133
+
134
+ == INSTALL:
135
+
136
+ $ gem sources -a http://gems.github.com
137
+ $ sudo gem install jnunemaker-matchy
138
+
139
+ == LICENSE:
140
+
141
+ (The MIT License)
142
+
143
+ Copyright (c) 2008 Jeremy McAnally
144
+
145
+ Permission is hereby granted, free of charge, to any person obtaining
146
+ a copy of this software and associated documentation files (the
147
+ 'Software'), to deal in the Software without restriction, including
148
+ without limitation the rights to use, copy, modify, merge, publish,
149
+ distribute, sublicense, and/or sell copies of the Software, and to
150
+ permit persons to whom the Software is furnished to do so, subject to
151
+ the following conditions:
152
+
153
+ The above copyright notice and this permission notice shall be
154
+ included in all copies or substantial portions of the Software.
155
+
156
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
157
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
158
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
159
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
160
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
161
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
162
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -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 }
@@ -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]))
@@ -0,0 +1,67 @@
1
+ # This is here because OptionsParser is SO slow.
2
+ def extract_path(argv)
3
+ if argv[1].nil?
4
+ if argv[0] =~ /-a/
5
+ return "/**/*.rb"
6
+ elsif argv[0]
7
+ if argv[0] =~ /\.rb$/
8
+ return argv[0]
9
+ end
10
+ return argv[0] + "/**/*.rb"
11
+ else
12
+ return "/**/*.rb"
13
+ end
14
+ elsif argv[1] =~ /\.rb$/
15
+ return argv[1]
16
+ else
17
+ return argv[1] + "/**/*.rb"
18
+ end
19
+ end
20
+
21
+ def all?
22
+ ARGV.join =~ /-a/
23
+ end
24
+
25
+
26
+
27
+ def comment?(line)
28
+ line =~ /^\s*#/
29
+ end
30
+
31
+ def blank?(line)
32
+ line =~ /^\s*$/
33
+ end
34
+
35
+ def puke(header, locs, comments, blanks)
36
+ puts header + ":"
37
+ puts "#{locs} loc"
38
+ puts "#{comments} lines of comments"
39
+ puts "#{blanks} blank lines"
40
+ end
41
+
42
+ dir = File.dirname(__FILE__)
43
+ full_path = File.join(dir,extract_path(ARGV))
44
+ gloc = gcmts = gblanks = 0
45
+ Dir[File.expand_path("#{full_path}")].uniq.each do |file|
46
+ if file =~ /.*\.rb$/
47
+
48
+ loc = cmts = blanks = 0
49
+
50
+ File.open(file, "r") do |f|
51
+ while f.gets
52
+ if comment?($_)
53
+ cmts += 1
54
+ elsif blank?($_)
55
+ blanks += 1
56
+ else
57
+ loc += 1
58
+ end
59
+ end
60
+ end
61
+ gcmts += cmts
62
+ gloc += loc
63
+ gblanks += blanks
64
+ puke(file, loc, cmts, blanks) if all?
65
+ end
66
+ end
67
+ puke("Total", gloc, gcmts, gblanks)
@@ -0,0 +1,19 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'test/unit'
5
+
6
+ require 'matchy/expectation_builder'
7
+ require 'matchy/modals'
8
+ require 'matchy/matcher_builder'
9
+ require 'matchy/custom_matcher'
10
+ require 'matchy/version'
11
+
12
+ require 'matchy/built_in/enumerable_expectations'
13
+ require 'matchy/built_in/error_expectations'
14
+ require 'matchy/built_in/truth_expectations'
15
+ require 'matchy/built_in/operator_expectations'
16
+ require 'matchy/built_in/change_expectations'
17
+
18
+ Test::Unit::TestCase.send(:include, Matchy::Expectations::TestCaseExtensions)
19
+ include Matchy::CustomMatcher
@@ -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.chained_messages
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_failure_message = "given block shouldn't alter the block attached to change"
25
+ matcher.negative_failure_message = "given block should alter the block attached to change"
26
+ comparison
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end