minitest-should_syntax 1.0.0 → 1.0.2

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/HISTORY.md CHANGED
@@ -1,3 +1,21 @@
1
+ v1.0.2 - Jan 31, 2013
2
+ ---------------------
3
+
4
+ * Ruby 1.8 compatibility fixes.
5
+
6
+ v1.0.1 - Jan 31, 2013
7
+ ---------------------
8
+
9
+ * Implement the `#otherwise` helper.
10
+ * Fixed: Make MiniTest show the correct file paths to errors.
11
+
12
+ ### Other changes:
13
+
14
+ * Clarify should.not vs should_not in README.
15
+ * Make the extension test give a more proper example.
16
+ * More readable tests.
17
+ * Refactor should_not.
18
+
1
19
  v1.0.0 - Jan 13, 2013
2
20
  ---------------------
3
21
 
data/README.md CHANGED
@@ -6,6 +6,9 @@ Rspec-like matching for MiniTest.
6
6
  tests. It monkey-patches Object to translate RSpec-like sugar into plain
7
7
  MiniTest `assert` matchers.
8
8
 
9
+ (*Ouch! Monkey patch?* Yes! But it only defines `Object#should` and
10
+ `#should_not`.)
11
+
9
12
  ```
10
13
  $ gem install minitest-should_syntax
11
14
  ```
@@ -31,68 +34,84 @@ end
31
34
  Then you may use it as so:
32
35
 
33
36
  ```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
37
+ obj.should == 2 # => assert_equal 2, obj
38
+ obj.should =~ /regex/ # => assert_match /regex/, obj
39
+ obj.should != 3 # => assert_not_equal 3, obj
40
+ obj.should.nil # => assert_nil obj
41
+ obj.should.respond_to(:freeze) # => assert_respond_to obj, :freeze
39
42
 
40
43
  # 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
+ obj.should.nil # => assert_nil obj
45
+ obj.should.be.nil # => assert_nil obj
46
+ obj.should.be.a.nil # => assert_nil obj
44
47
 
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
48
+ # You can also use should.not:
49
+ obj.should.not == 3
50
+ obj.should.not.be.nil
49
51
 
50
52
  # Anything else will pass through with a ?:
51
- obj.should.be.good_looking # => assert obj.good_looking?
53
+ obj.should.be.good_looking # => assert obj.good_looking?
52
54
 
53
- should.raise(Error) { lol }
54
- should_not.raise { puts "hi" }
55
+ # Testing exceptions:
56
+ should.raise(ZeroDivisionError) { 2/0 }
55
57
 
56
- # You may add messages to your asserts with #blaming or #messaging.
57
- (2 + 2).should.blaming("weird math") == 4
58
+ # should_not is an alias for should.not:
59
+ obj.should_not == 3
58
60
  ```
59
61
 
60
62
  ## Wrapped assertions
61
63
 
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 |
64
+ These are based on [MiniTest::Assertions].
65
+
66
+ | MiniTest::ShouldSyntax | [MiniTest::Assertions] |
67
+ |-----------------------------------------|-----------------------------|
68
+ | x.should.equal y | assert_equal x, y |
69
+ | x.should == y | assert_equal x, y |
70
+ | x.should.not.equal | refute_equal x, y |
71
+ | x.should != | refute_equal x, y |
72
+ | x.should.be | assert_same x, y |
73
+ | x.should.not.be | refute_same x, y |
74
+ | x.should >= *(and others)* | assert_operator x, :>=, y |
75
+ | x.should.not >= *(and others)* | refute_operator x, :>=, y |
76
+ | x.should.be.nil | assert_nil x |
77
+ | x.should.not.be.nil | refute_nil x |
78
+ | x.should.be.close y | assert_in_delta y |
79
+ | x.should.be.in_epsilon y | assert_in_epsilon y |
80
+ | x.should.match /y/ | assert_match x, /y/ |
81
+ | x.should =~ /y/ | assert_match x, /y/ |
82
+ | x.should.not.match, should.not =~ | refute_match |
83
+ | x.should.be.an.instance_of y | assert_instance_of x, y |
84
+ | x.should.be.a.kind_of x, y | assert_kind_of x, y |
85
+ | x.should.respond_to :y | assert_respond_to x, :y |
86
+ | should.raise(x) { ... } | assert_raise(x) { ... } |
87
+ | should.throw(x) { ... } | assert_throws(x) { ... } |
88
+ | should.satisfy { ... } | assert_block { ... } |
89
+
90
+ [MiniTest::Assertions]: https://github.com/seattlerb/minitest/blob/master/lib/minitest/unit.rb
82
91
 
83
92
  ## Messages
84
93
 
85
- Use the `msg` helper:
94
+ Use the `otherwise` helper:
86
95
 
87
96
  ``` ruby
88
97
  it "should work" do
89
98
  book = Book.new title: "Pride & Prejudice"
90
99
 
91
- msg "The title should've been set on constructor."
100
+ otherwise "The title should've been set on constructor"
92
101
  book.title.should == "Pride & Prejudice"
93
102
  end
94
103
  ```
95
104
 
105
+ Result:
106
+
107
+ ```
108
+ 1) Failure:
109
+ should work(Test) [your_test.rb:77]:
110
+ The title should've been set on constructor.
111
+ Expected: "Pride & Prejudice"
112
+ Actual: nil
113
+ ```
114
+
96
115
  Or you can use `.blaming` which does the same thing (with a more cumbersome
97
116
  syntax):
98
117
 
@@ -100,7 +119,7 @@ Or you can use `.blaming` which does the same thing (with a more cumbersome
100
119
  it "should work" do
101
120
  book = Book.new title: "Pride & Prejudice"
102
121
 
103
- message = "The title should've been set on constructor."
122
+ message = "The title should've been set on constructor"
104
123
  book.title.should.blaming(message) == "Pride & Prejudice"
105
124
  end
106
125
  ```
@@ -113,10 +132,16 @@ Need to create your own matchers? Create your new matcher in a module, then use
113
132
  ```ruby
114
133
  module DanceMatcher
115
134
  def boogie_all_night!
135
+ # Delegates to `assert(condition, message)`.
136
+ #
137
+ # positive? - returns `true` if .should, or `false` if .should.not
138
+ # test - the MiniTest object
139
+ # msg - the failure message. `nil` if not set
140
+ #
116
141
  if positive?
117
- test.assert left.respond_to?(:dance)
142
+ test.assert left.respond_to?(:dance), msg
118
143
  else
119
- test.assert ! left.respond_to?(:dance)
144
+ test.refute left.respond_to?(:dance), msg
120
145
  end
121
146
  end
122
147
  end
@@ -44,9 +44,8 @@ module MiniTest
44
44
  self.send :include, extension
45
45
  end
46
46
 
47
- def initialize(left, neg=false)
47
+ def initialize(left)
48
48
  @left = left
49
- @neg = neg
50
49
  if test.msg
51
50
  blaming test.msg
52
51
  test.msg = nil
@@ -76,7 +75,6 @@ module MiniTest
76
75
  def messaging(msg); @msg = msg; self; end
77
76
 
78
77
  def ==(right) assert_or_refute :equal, right, left; end
79
- def !=(right) refute_or_assert :equal, right, left; end
80
78
  def =~(right) assert_or_refute :match, right, left; end
81
79
  def >(right) assert_or_refute :operator, left, :>, right; end
82
80
  def <(right) assert_or_refute :operator, left, :<, right; end
@@ -91,6 +89,13 @@ module MiniTest
91
89
  def empty() assert_or_refute :empty, left; end
92
90
  def satisfy(&blk) assert_or_refute :block, &blk; end
93
91
 
92
+ # Ruby 1.8 doesn't support overriding !=.
93
+ if RUBY_VERSION >= "1.9"
94
+ class_eval %[
95
+ def !=(right) refute_or_assert :equal, right, left; end
96
+ ]
97
+ end
98
+
94
99
  def match(right) self =~ right; end
95
100
  def equal(right) self == right; end
96
101
 
@@ -98,11 +103,13 @@ module MiniTest
98
103
  def in_epsilon(right, d=0.001) assert_or_refute :in_epsilon, right, left, d; end
99
104
 
100
105
  def assert_or_refute(what, *args, &blk)
101
- test.send (positive? ? :"assert_#{what}" : :"refute_#{what}"), *args, msg, &blk
106
+ args << msg
107
+ test.send((positive? ? :"assert_#{what}" : :"refute_#{what}"), *args, &blk)
102
108
  end
103
109
 
104
110
  def refute_or_assert(what, *args)
105
- test.send (negative? ? :"assert_#{what}" : :"refute_#{what}"), *args, msg
111
+ args << msg
112
+ test.send((negative? ? :"assert_#{what}" : :"refute_#{what}"), *args)
106
113
  end
107
114
 
108
115
  def throw(what=nil, &blk)
@@ -139,10 +146,12 @@ class Object
139
146
  end
140
147
 
141
148
  def should_not
142
- MiniTest::ShouldSyntax.new(self, true)
149
+ should.not
143
150
  end
144
151
  end
145
152
 
153
+ # Patch TestCase::before_setup to invoke ShouldSyntax,
154
+ # and inject the #msg and #otherwise helper.
146
155
  class MiniTest::Unit::TestCase
147
156
  alias :mts_before_setup :before_setup
148
157
 
@@ -159,4 +168,21 @@ class MiniTest::Unit::TestCase
159
168
  def msg=(string)
160
169
  @msg = string
161
170
  end
171
+
172
+ def otherwise(message)
173
+ msg message
174
+ end
162
175
  end
176
+
177
+ # Patch #location() to ignore should_syntax.
178
+ class MiniTest::Unit
179
+ def location(e) # :nodoc:
180
+ last_before_assertion = ""
181
+ e.backtrace.reverse_each do |s|
182
+ break if s =~ /(in .(assert|refute|flunk|pass|fail|raise|must|wont))|(minitest\/should_syntax)/
183
+ last_before_assertion = s
184
+ end
185
+ last_before_assertion.sub(/:in .*$/, '')
186
+ end
187
+ end
188
+
@@ -1,7 +1,7 @@
1
1
  module MiniTest
2
2
  class ShouldSyntax
3
3
  def self.version
4
- "1.0.0"
4
+ "1.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -4,31 +4,31 @@ module MiniTest::ArrayMatcher
4
4
  def like(right)
5
5
  super unless left.is_a?(Array) && right.is_a?(Array)
6
6
  if positive?
7
- test.assert_equal left.sort, right.sort
7
+ test.assert_equal left.sort, right.sort, msg
8
8
  else
9
- test.refute_equal left.sort, right.sort
9
+ test.refute_equal left.sort, right.sort, msg
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
14
  MiniTest::ShouldSyntax.add MiniTest::ArrayMatcher
15
15
 
16
- class ExtensionTest < UnitTest
17
- def test_extension
16
+ describe "Extensions" do
17
+ it ".should.be.like" do
18
18
  a = %w(a b c)
19
19
  b = %w(b c a)
20
20
 
21
21
  a.should.be.like b
22
22
  end
23
23
 
24
- def test_extension_2
24
+ it ".should.not.be.like" do
25
25
  a = %w(a b c)
26
26
  b = %w(b c A)
27
27
 
28
- a.should_not.be.like b
28
+ a.should.not.be.like b
29
29
  end
30
30
 
31
- def test_extension_3
31
+ it "super" do
32
32
  a = %w(a b c)
33
33
  b = 2
34
34
 
@@ -4,5 +4,7 @@ require 'minitest/autorun'
4
4
  require 'minitest/should_syntax'
5
5
  require 'mocha/setup'
6
6
 
7
- class UnitTest < MiniTest::Unit::TestCase
7
+ class MiniTest::Unit::TestCase
8
+ # helpers
8
9
  end
10
+
@@ -1,11 +1,11 @@
1
1
  require File.expand_path('../helper', __FILE__)
2
2
 
3
- class IncludeTest < UnitTest
4
- def test_should_include
3
+ describe "Includes" do
4
+ it ".should.include" do
5
5
  "abc".should.include "b"
6
6
  end
7
7
 
8
- def test_should_include_fail
8
+ it ".should.include failure" do
9
9
  self.expects(:assert_includes).with { |a, b| a == "axxc" && b == "b" }
10
10
  "axxc".should.include "b"
11
11
  end
@@ -10,133 +10,131 @@ class Foo
10
10
  end
11
11
  end
12
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
13
+ describe "should" do
14
+ it ".should ==" do
15
+ 2.should == 2
16
+ 2.should_not == 3
17
+ 2.should.not == 3
18
+ end
19
+
20
+ it ".should !=" do
21
+ 2.should != 3
22
+ 2.should_not != 2
23
+ 2.should.not != 2
24
+ end
25
+
26
+ it ".should.match" do
27
+ "hi".should =~ /hi/
28
+ "hi".should.match /hi/
29
+ "hi".should_not =~ /HI/
30
+ "hi".should.not.match /HI/
31
+ end
32
+
33
+ it ".should.be.nil?" do
34
+ @foo.should.be.nil?
35
+ 1000.should_not.be.nil?
36
+ end
37
+
38
+ it ".should.respond_to" do
39
+ "".should.respond_to(:empty?)
40
+ "".should_not.respond_to(:lolwhat)
41
+ end
42
+
43
+ it ".should.raise" do
44
+ should.raise(ZeroDivisionError) { 2 / 0 }
45
+ # should_not.raise { 2 + 2 }
46
+ end
47
+
48
+ it ".should.be.empty" do
49
+ [].should.be.empty
50
+ [].should.empty
51
+ end
52
+
53
+ it ".should.not.be.empty" do
54
+ [1].should_not.be.empty
55
+ [1].should.include(1)
56
+ end
57
+
58
+ it ".should <" do
59
+ 2.should < 3
60
+ 1.should < 2
61
+ 2.should <= 2
62
+ 2.should <= 4
63
+ 4.should >= 4
64
+ 4.should >= 3
65
+ end
66
+
67
+ it ".should.be.kind_of" do
68
+ Object.new.should.respond_to(:freeze)
69
+ Object.new.should.be.kind_of(Object)
70
+ Object.new.should.be.an.instance_of(Object)
71
+ end
72
+
73
+ it "should.be.equal again" do
74
+ a = Object.new
75
+ b = a
76
+ a.should.be.equal(b)
77
+ a.should.be(b)
78
+ a.should_not.be.equal(Object.new)
79
+ end
80
+
81
+
82
+ it ".should.be.close" do
83
+ Math::PI.should.be.close(22.0/7, 0.1)
84
+ end
85
+
86
+ it ".should.throw" do
87
+ should.throw(:x) { throw :x }
88
+ # should.not.throw { 2 + 2 }
89
+ end
90
+
91
+ it ".should.not.method_missing" do
92
+ Foo.new.should.not.get_false
93
+ Foo.new.should.get_true
94
+ end
95
+
96
+ it ".should.be" do
97
+ a = Object.new
98
+ b = a
99
+
100
+ expects(:assert_same).with(a, b, nil)
101
+ a.should.be(b)
102
+ end
103
+
104
+ it ".should.include" do
105
+ expects(:assert_includes).with([], 2, nil)
106
+ [].should.include 2
107
+ end
108
+
109
+ it ".should.not.include" do
110
+ expects(:refute_includes).with([], 2, nil)
111
+ [].should.not.include 2
112
+ end
113
+
114
+ it ".should.blaming" do
115
+ expects(:assert_equal).with(4, 3, 'lol')
116
+ 3.should.blaming('lol') == 4
117
+ end
118
+
119
+ it ".msg" do
120
+ expects(:assert_equal).with(4, 3, 'oh no')
121
+
122
+ msg "oh no"
123
+ 3.should == 4
124
+ end
125
+
126
+ it ".should.blaming again" do
127
+ object = Object.new
128
+
129
+ expects(:assert).with(true).at_least_once # Because minitest does this
130
+ expects(:assert).with(true, 'he cant dance')
131
+ object.expects(:dance?).returns(true)
132
+
133
+ object.should.blaming('he cant dance').dance
134
+ end
135
+
136
+ it ".should.satisfy" do
137
+ expects :assert_block
138
+ should.satisfy { false }
141
139
  end
142
140
  end
metadata CHANGED
@@ -2,25 +2,25 @@
2
2
  name: minitest-should_syntax
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rico Sta. Cruz
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-12 00:00:00.000000000 Z
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
+ prerelease: false
15
16
  version_requirements: !ruby/object:Gem::Requirement
16
17
  requirements:
17
18
  - - ! '>='
18
19
  - !ruby/object:Gem::Version
19
20
  version: '0'
20
21
  none: false
21
- name: minitest
22
22
  type: :runtime
23
- prerelease: false
23
+ name: minitest
24
24
  requirement: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ! '>='
@@ -28,15 +28,15 @@ dependencies:
28
28
  version: '0'
29
29
  none: false
30
30
  - !ruby/object:Gem::Dependency
31
+ prerelease: false
31
32
  version_requirements: !ruby/object:Gem::Requirement
32
33
  requirements:
33
34
  - - ! '>='
34
35
  - !ruby/object:Gem::Version
35
36
  version: '0'
36
37
  none: false
37
- name: mocha
38
38
  type: :development
39
- prerelease: false
39
+ name: mocha
40
40
  requirement: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,15 +44,15 @@ dependencies:
44
44
  version: '0'
45
45
  none: false
46
46
  - !ruby/object:Gem::Dependency
47
+ prerelease: false
47
48
  version_requirements: !ruby/object:Gem::Requirement
48
49
  requirements:
49
50
  - - ! '>='
50
51
  - !ruby/object:Gem::Version
51
52
  version: '0'
52
53
  none: false
53
- name: rake
54
54
  type: :development
55
- prerelease: false
55
+ name: rake
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - ! '>='
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 1.8.23
101
+ rubygems_version: 1.8.25
102
102
  signing_key:
103
103
  specification_version: 3
104
104
  summary: RSpec-like syntax for MiniTest.