minitest-should_syntax 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/HISTORY.md ADDED
@@ -0,0 +1,4 @@
1
+ v1.0.0 - Jan 13, 2013
2
+ ---------------------
3
+
4
+ First release.
data/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # minitest-should_syntax
2
+
3
+ Rspec-like matching for MiniTest.
4
+
5
+ *minitest-should_syntax* lets you use a syntax similar to RSpec on your MiniTest
6
+ tests. It monkey-patches Object to translate RSpec-like sugar into plain
7
+ MiniTest `assert` matchers.
8
+
9
+ ```
10
+ $ gem install minitest-should_syntax
11
+ ```
12
+
13
+ ## Basic usage
14
+
15
+ ``` ruby
16
+ require 'minitest/autorun'
17
+ require 'minitest/should_syntax'
18
+
19
+ describe "Books" do
20
+ it "should work" do
21
+ book = Book.new title: "Revolution"
22
+
23
+ book.title.should == "Revolution"
24
+ end
25
+ end
26
+
27
+ ```
28
+
29
+ ## Should
30
+
31
+ Then you may use it as so:
32
+
33
+ ```ruby
34
+ obj.should == 2 # => assert_equal 2, obj
35
+ obj.should =~ /regex/ # => assert_match /regex/, obj
36
+ obj.should != 3 # => assert_not_equal 3, obj
37
+ obj.should.nil # => assert_nil obj
38
+ obj.should.respond_to(:freeze) # => assert_respond_to obj, :freeze
39
+
40
+ # Note that .be, .a and .an are optional.
41
+ obj.should.nil # => assert_nil obj
42
+ obj.should.be.nil # => assert_nil obj
43
+ obj.should.be.a.nil # => assert_nil obj
44
+
45
+ # You can also use should_not, or should.not:
46
+ obj.should_not == 3
47
+ obj.should.not == 3
48
+ obj.should_not.be.nil
49
+
50
+ # Anything else will pass through with a ?:
51
+ obj.should.be.good_looking # => assert obj.good_looking?
52
+
53
+ should.raise(Error) { lol }
54
+ should_not.raise { puts "hi" }
55
+
56
+ # You may add messages to your asserts with #blaming or #messaging.
57
+ (2 + 2).should.blaming("weird math") == 4
58
+ ```
59
+
60
+ ## Wrapped assertions
61
+
62
+ These are based from MiniTest::Assertions.
63
+
64
+ | Test::Unit | MiniTest::ShouldSyntax |
65
+ |-----------------------------|---------------------------------------|
66
+ | assert_equal | should.equal, should == |
67
+ | assert_not_equal | should.not.equal, should.not == |
68
+ | assert_same | should.be |
69
+ | assert_not_same | should.not.be |
70
+ | assert_nil | should.be.nil |
71
+ | assert_not_nil | should.not.be.nil |
72
+ | assert_in_delta | should.be.close |
73
+ | assert_match | should.match, should =~ |
74
+ | assert_no_match | should.not.match, should.not =~ |
75
+ | assert_instance_of | should.be.an.instance_of |
76
+ | assert_kind_of | should.be.a.kind_of |
77
+ | assert_respond_to | should.respond_to |
78
+ | assert_raise | should.raise |
79
+ | assert_nothing_raised | should.not.raise |
80
+ | assert_throws | should.throw |
81
+ | assert_block | should.satisfy |
82
+
83
+ ## Messages
84
+
85
+ Use the `msg` helper:
86
+
87
+ ``` ruby
88
+ it "should work" do
89
+ book = Book.new title: "Pride & Prejudice"
90
+
91
+ msg "The title should've been set on constructor."
92
+ book.title.should == "Pride & Prejudice"
93
+ end
94
+ ```
95
+
96
+ Or you can use `.blaming` which does the same thing (with a more cumbersome
97
+ syntax):
98
+
99
+ ``` ruby
100
+ it "should work" do
101
+ book = Book.new title: "Pride & Prejudice"
102
+
103
+ message = "The title should've been set on constructor."
104
+ book.title.should.blaming(message) == "Pride & Prejudice"
105
+ end
106
+ ```
107
+
108
+ ## Extending
109
+
110
+ Need to create your own matchers? Create your new matcher in a module, then use
111
+ `MiniTest::ShouldSyntax.add`.
112
+
113
+ ```ruby
114
+ module DanceMatcher
115
+ def boogie_all_night!
116
+ if positive?
117
+ test.assert left.respond_to?(:dance)
118
+ else
119
+ test.assert ! left.respond_to?(:dance)
120
+ end
121
+ end
122
+ end
123
+
124
+ MiniTest::ShouldSyntax.add DanceMatcher
125
+
126
+ # Then in your tests, use:
127
+ dancer.should.boogie_all_night!
128
+ ```
129
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ desc "Test."
2
+ task(:test) { Dir['./test/*.rb'].each { |f| load f } }
3
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ module MiniTest
2
+ class ShouldSyntax
3
+ def self.version
4
+ "1.0.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,162 @@
1
+ require 'minitest/unit'
2
+
3
+ # Rspec-like matching for MiniTest.
4
+ #
5
+ # == Usage
6
+ #
7
+ # # All objects get .should:
8
+ # obj.should == 2
9
+ # obj.should ~= /regex/
10
+ # obj.should != 3
11
+ # obj.should.be.true # Truthy
12
+ # obj.should.be.false # Falsy
13
+ #
14
+ # # Anything else will just pass thru:
15
+ # obj.should.nil? # same as: assert obj.nil?
16
+ # obj.should.be.nil? # same as: assert obj.nil?
17
+ # obj.should.respond_to?(:freeze)
18
+ #
19
+ # # You can also use should_not:
20
+ # obj.should_not == 3
21
+ # obj.should_not.be.nil?
22
+ #
23
+ # # Errors and throws
24
+ # should.raise(Error) { lol }
25
+ # should.throw(:x) { lol }
26
+ #
27
+ # # Messages
28
+ # msg "Age must be set properly"
29
+ # age.should == 18
30
+ #
31
+ module MiniTest
32
+ class ShouldSyntax
33
+ require File.expand_path('../should_syntax/version', __FILE__)
34
+
35
+ attr_reader :left
36
+ attr_reader :msg
37
+
38
+ def self.init(test) # :nodoc:
39
+ @@test = test
40
+ end
41
+
42
+ # Includes a module to extend .should with more matchers.
43
+ def self.add(extension)
44
+ self.send :include, extension
45
+ end
46
+
47
+ def initialize(left, neg=false)
48
+ @left = left
49
+ @neg = neg
50
+ if test.msg
51
+ blaming test.msg
52
+ test.msg = nil
53
+ end
54
+ end
55
+
56
+ def be(right=nil) self.same(right) if right; self; end
57
+ def a() self; end
58
+ def an() self; end
59
+
60
+ def negative?() @neg; end
61
+ def positive?() !@neg; end
62
+ def test() @@test; end
63
+ def not() @neg = true; self; end
64
+
65
+ def true() true_or_false(true); end
66
+ def false() true_or_false(false); end
67
+
68
+ def true_or_false(bool)
69
+ val = !! left
70
+ val = !val if bool == false
71
+ method = (positive? ? :"assert" : :"refute")
72
+ test.send method, val, [msg, 'Expected to be falsy'].compact.join("\n")
73
+ end
74
+
75
+ def blaming(msg); @msg = msg; self; end
76
+ def messaging(msg); @msg = msg; self; end
77
+
78
+ def ==(right) assert_or_refute :equal, right, left; end
79
+ def !=(right) refute_or_assert :equal, right, left; end
80
+ def =~(right) assert_or_refute :match, right, left; end
81
+ def >(right) assert_or_refute :operator, left, :>, right; end
82
+ def <(right) assert_or_refute :operator, left, :<, right; end
83
+ def >=(right) assert_or_refute :operator, left, :>=, right; end
84
+ def <=(right) assert_or_refute :operator, left, :<=, right; end
85
+ def include(right) assert_or_refute :includes, left, right; end
86
+ def instance_of(right) assert_or_refute :instance_of, right, left; end
87
+ def kind_of(right) assert_or_refute :kind_of, right, left; end
88
+ def nil() assert_or_refute :nil, left; end
89
+ def same(right) assert_or_refute :same, right, left; end
90
+ def respond_to(right) assert_or_refute :respond_to, left, right; end
91
+ def empty() assert_or_refute :empty, left; end
92
+ def satisfy(&blk) assert_or_refute :block, &blk; end
93
+
94
+ def match(right) self =~ right; end
95
+ def equal(right) self == right; end
96
+
97
+ def close(right, d=0.001) assert_or_refute :in_delta, right, left, d; end
98
+ def in_epsilon(right, d=0.001) assert_or_refute :in_epsilon, right, left, d; end
99
+
100
+ def assert_or_refute(what, *args, &blk)
101
+ test.send (positive? ? :"assert_#{what}" : :"refute_#{what}"), *args, msg, &blk
102
+ end
103
+
104
+ def refute_or_assert(what, *args)
105
+ test.send (negative? ? :"assert_#{what}" : :"refute_#{what}"), *args, msg
106
+ end
107
+
108
+ def throw(what=nil, &blk)
109
+ if positive?
110
+ test.send :assert_throws, what, msg, &blk
111
+ else
112
+ warn "ShouldSyntax: should.not.throw is not supported"
113
+ end
114
+ end
115
+
116
+ def raise(ex=StandardError, &blk)
117
+ if positive?
118
+ test.send :assert_raises, ex, msg, &blk
119
+ else
120
+ warn "ShouldSyntax: should.not.raise is not supported"
121
+ end
122
+ end
123
+
124
+ def method_missing(meth, *args, &blk)
125
+ result = left.send(:"#{meth}?", *args, &blk)
126
+ method = positive? ? :assert : :refute
127
+
128
+ args = [result]
129
+ args << msg if msg
130
+
131
+ test.send method, *args
132
+ end
133
+ end
134
+ end
135
+
136
+ class Object
137
+ def should
138
+ MiniTest::ShouldSyntax.new(self)
139
+ end
140
+
141
+ def should_not
142
+ MiniTest::ShouldSyntax.new(self, true)
143
+ end
144
+ end
145
+
146
+ class MiniTest::Unit::TestCase
147
+ alias :mts_before_setup :before_setup
148
+
149
+ def before_setup(*a, &block)
150
+ MiniTest::ShouldSyntax.init self
151
+ mts_before_setup(*a, &block)
152
+ end
153
+
154
+ def msg(string=nil)
155
+ self.msg = string if string
156
+ @msg
157
+ end
158
+
159
+ def msg=(string)
160
+ @msg = string
161
+ end
162
+ end
@@ -0,0 +1,15 @@
1
+ require "./lib/minitest/should_syntax/version"
2
+ Gem::Specification.new do |s|
3
+ s.name = "minitest-should_syntax"
4
+ s.version = MiniTest::ShouldSyntax.version
5
+ s.summary = "RSpec-like syntax for MiniTest."
6
+ s.description = "Lets you use a syntax similar to RSpec on your MiniTest tests."
7
+ s.authors = ["Rico Sta. Cruz"]
8
+ s.email = ["rico@sinefunc.com"]
9
+ s.homepage = "http://github.com/rstacruz/minitest-should_syntax"
10
+ s.files = `git ls-files`.strip.split("\n")
11
+
12
+ s.add_dependency "minitest"
13
+ s.add_development_dependency "mocha"
14
+ s.add_development_dependency "rake"
15
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ module MiniTest::ArrayMatcher
4
+ def like(right)
5
+ super unless left.is_a?(Array) && right.is_a?(Array)
6
+ if positive?
7
+ test.assert_equal left.sort, right.sort
8
+ else
9
+ test.refute_equal left.sort, right.sort
10
+ end
11
+ end
12
+ end
13
+
14
+ MiniTest::ShouldSyntax.add MiniTest::ArrayMatcher
15
+
16
+ class ExtensionTest < UnitTest
17
+ def test_extension
18
+ a = %w(a b c)
19
+ b = %w(b c a)
20
+
21
+ a.should.be.like b
22
+ end
23
+
24
+ def test_extension_2
25
+ a = %w(a b c)
26
+ b = %w(b c A)
27
+
28
+ a.should_not.be.like b
29
+ end
30
+
31
+ def test_extension_3
32
+ a = %w(a b c)
33
+ b = 2
34
+
35
+ should.raise(NoMethodError) { a.should.be.like b }
36
+ end
37
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/should_syntax'
5
+ require 'mocha/setup'
6
+
7
+ class UnitTest < MiniTest::Unit::TestCase
8
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class IncludeTest < UnitTest
4
+ def test_should_include
5
+ "abc".should.include "b"
6
+ end
7
+
8
+ def test_should_include_fail
9
+ self.expects(:assert_includes).with { |a, b| a == "axxc" && b == "b" }
10
+ "axxc".should.include "b"
11
+ end
12
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class Foo
4
+ def get_true?
5
+ true
6
+ end
7
+
8
+ def get_false?
9
+ false
10
+ end
11
+ end
12
+
13
+ class ShouldTest < UnitTest
14
+ describe "should" do
15
+ it ".should ==" do
16
+ 2.should == 2
17
+ 2.should_not == 3
18
+ 2.should.not == 3
19
+ end
20
+
21
+ it ".should !=" do
22
+ 2.should != 3
23
+ 2.should_not != 2
24
+ 2.should.not != 2
25
+ end
26
+
27
+ it ".should.match" do
28
+ "hi".should =~ /hi/
29
+ "hi".should.match /hi/
30
+ "hi".should_not =~ /HI/
31
+ "hi".should.not.match /HI/
32
+ end
33
+
34
+ it ".should.be.nil?" do
35
+ @foo.should.be.nil?
36
+ 1000.should_not.be.nil?
37
+ end
38
+
39
+ it ".should.respond_to" do
40
+ "".should.respond_to(:empty?)
41
+ "".should_not.respond_to(:lolwhat)
42
+ end
43
+
44
+ it ".should.raise" do
45
+ should.raise(ZeroDivisionError) { 2 / 0 }
46
+ # should_not.raise { 2 + 2 }
47
+ end
48
+
49
+ it ".should.be.empty" do
50
+ [].should.be.empty
51
+ [].should.empty
52
+ end
53
+
54
+ it ".should.not.be.empty" do
55
+ [1].should_not.be.empty
56
+ [1].should.include(1)
57
+ end
58
+
59
+ it ".should <" do
60
+ 2.should < 3
61
+ 1.should < 2
62
+ 2.should <= 2
63
+ 2.should <= 4
64
+ 4.should >= 4
65
+ 4.should >= 3
66
+ end
67
+
68
+ it ".should.be.kind_of" do
69
+ Object.new.should.respond_to(:freeze)
70
+ Object.new.should.be.kind_of(Object)
71
+ Object.new.should.be.an.instance_of(Object)
72
+ end
73
+
74
+ it "should.be.equal again" do
75
+ a = Object.new
76
+ b = a
77
+ a.should.be.equal(b)
78
+ a.should.be(b)
79
+ a.should_not.be.equal(Object.new)
80
+ end
81
+
82
+
83
+ it ".should.be.close" do
84
+ Math::PI.should.be.close(22.0/7, 0.1)
85
+ end
86
+
87
+ it ".should.throw" do
88
+ should.throw(:x) { throw :x }
89
+ # should.not.throw { 2 + 2 }
90
+ end
91
+
92
+ it ".should.not.method_missing" do
93
+ Foo.new.should.not.get_false
94
+ Foo.new.should.get_true
95
+ end
96
+
97
+ it ".should.be" do
98
+ a = Object.new
99
+ b = a
100
+
101
+ expects(:assert_same).with(a, b, nil)
102
+ a.should.be(b)
103
+ end
104
+
105
+ it ".should.include" do
106
+ expects(:assert_includes).with([], 2, nil)
107
+ [].should.include 2
108
+ end
109
+
110
+ it ".should.not.include" do
111
+ expects(:refute_includes).with([], 2, nil)
112
+ [].should.not.include 2
113
+ end
114
+
115
+ it ".should.blaming" do
116
+ expects(:assert_equal).with(4, 3, 'lol')
117
+ 3.should.blaming('lol') == 4
118
+ end
119
+
120
+ it ".msg" do
121
+ expects(:assert_equal).with(4, 3, 'oh no')
122
+
123
+ msg "oh no"
124
+ 3.should == 4
125
+ end
126
+
127
+ it ".should.blaming again" do
128
+ object = Object.new
129
+
130
+ expects(:assert).with(true).at_least_once # Because minitest does this
131
+ expects(:assert).with(true, 'he cant dance')
132
+ object.expects(:dance?).returns(true)
133
+
134
+ object.should.blaming('he cant dance').dance
135
+ end
136
+
137
+ it ".should.satisfy" do
138
+ expects :assert_block
139
+ should.satisfy { false }
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-should_syntax
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Rico Sta. Cruz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
+ name: minitest
22
+ type: :runtime
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ name: mocha
38
+ type: :development
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ none: false
53
+ name: rake
54
+ type: :development
55
+ prerelease: false
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
62
+ description: Lets you use a syntax similar to RSpec on your MiniTest tests.
63
+ email:
64
+ - rico@sinefunc.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - HISTORY.md
72
+ - README.md
73
+ - Rakefile
74
+ - lib/minitest/should_syntax.rb
75
+ - lib/minitest/should_syntax/version.rb
76
+ - minitest-should_syntax.gemspec
77
+ - test/extension_test.rb
78
+ - test/helper.rb
79
+ - test/include_test.rb
80
+ - test/should_test.rb
81
+ homepage: http://github.com/rstacruz/minitest-should_syntax
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ none: false
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ none: false
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: RSpec-like syntax for MiniTest.
105
+ test_files: []