minitest-chain 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ require 'minitest/unit'
2
+
3
+ module MiniTest::Chain
4
+ class AssertionChain
5
+ def initialize(scope, obj)
6
+ @scope = scope
7
+ @obj = obj
8
+ end
9
+
10
+ # Flippers:
11
+ [
12
+ [:equal, :equal],
13
+ [:match, :match],
14
+ [:close_to, :in_delta],
15
+ [:within_epsilon, :in_epsilon],
16
+ [:instance_of, :instance_of],
17
+ [:same, :same],
18
+ ].each do |name, assertion|
19
+ class_eval <<-RUBY, __FILE__, __LINE__+1
20
+ def #{name}(other, *args)
21
+ @scope.assert_#{assertion}(other, @obj, *args)
22
+ self
23
+ end
24
+
25
+ def not_#{name}(other, *args)
26
+ @scope.refute_#{assertion}(other, @obj, *args)
27
+ self
28
+ end
29
+ RUBY
30
+ end
31
+
32
+ # Unaries and reversers:
33
+ [
34
+ [:empty, :empty],
35
+ [:include, :includes],
36
+ [:is, :operator],
37
+ [:respond_to, :respond_to],
38
+ ].each do |name, assertion|
39
+ class_eval <<-RUBY, __FILE__, __LINE__+1
40
+ def #{name}(*args)
41
+ @scope.assert_#{assertion}(@obj, *args)
42
+ self
43
+ end
44
+
45
+ def not_#{name}(*args)
46
+ @scope.refute_#{assertion}(@obj, *args)
47
+ self
48
+ end
49
+ RUBY
50
+ end
51
+
52
+ alias is_not not_is
53
+ remove_method :not_is
54
+ end
55
+
56
+ def assert(obj, *args)
57
+ super
58
+ AssertionChain.new(self, obj)
59
+ end
60
+ end
61
+
data/test/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ $: << File.expand_path('../../lib', __FILE__)
2
+ require 'minitest/autorun'
3
+ require 'minitest-chain'
4
+
@@ -0,0 +1,116 @@
1
+ require_relative 'helper'
2
+
3
+ class TestChain < MiniTest::Unit::TestCase
4
+ include MiniTest::Chain
5
+
6
+ def assert_refutes
7
+ assert_raises(MiniTest::Assertion) do
8
+ yield
9
+ end
10
+ end
11
+
12
+ def test_equal
13
+ assert("Hello").equal("Hello")
14
+ assert("World").not_equal("Hello")
15
+
16
+ assert_refutes { assert("World").equal("Hello") }
17
+ assert_refutes { assert("Hello").not_equal("Hello") }
18
+ end
19
+
20
+ def test_match
21
+ assert("World").match(/World/)
22
+ assert("Hello").not_match(/World/)
23
+
24
+ assert_refutes { assert("Hello").match(/World/) }
25
+ assert_refutes { assert("World").not_match(/World/) }
26
+ end
27
+
28
+ def test_empty
29
+ assert([]).empty
30
+ assert([1]).not_empty
31
+
32
+ assert_refutes { assert([1]).empty }
33
+ assert_refutes { assert([]).not_empty }
34
+ end
35
+
36
+ def test_includes
37
+ assert([1]).include(1)
38
+ assert([2]).not_include(1)
39
+
40
+ assert_refutes { assert([2]).include(1) }
41
+ assert_refutes { assert([1]).not_include(1) }
42
+ end
43
+
44
+ def test_close_to
45
+ assert(1.5).close_to(1.5)
46
+ assert(2.0).not_close_to(1.5)
47
+
48
+ assert_refutes { assert(2.0).close_to(1.5) }
49
+ assert_refutes { assert(1.5).not_close_to(1.5) }
50
+ end
51
+
52
+ def test_within_epsilon
53
+ assert(1.6).within_epsilon(1.5, 0.10)
54
+
55
+ assert_refutes do
56
+ assert(1.9).within_epsilon(1.5, 0.10)
57
+ end
58
+ end
59
+
60
+ def test_instance_of
61
+ assert(1 ).instance_of(Fixnum)
62
+ assert(1.5).not_instance_of(Fixnum)
63
+
64
+ assert_refutes { assert(1.5).instance_of(Fixnum) }
65
+ assert_refutes { assert(1 ).not_instance_of(Fixnum) }
66
+ end
67
+
68
+ def test_is
69
+ assert(1).is(:<, 2)
70
+ assert(3).is_not(:<, 2)
71
+
72
+ assert_refutes { assert(3).is(:<, 2) }
73
+ assert_refutes { assert(1).is_not(:<, 2) }
74
+ end
75
+
76
+ def test_not_is
77
+ assert_raises(NoMethodError) do
78
+ assert(1).not_is(:zero?)
79
+ end
80
+ end
81
+
82
+ def test_is_predicate
83
+ assert([]).is(:empty?)
84
+ assert([1]).is_not(:empty?)
85
+
86
+ assert_refutes { assert([1]).is(:empty?) }
87
+ assert_refutes { assert([]).is_not(:empty?) }
88
+ end
89
+
90
+ def test_respond_to
91
+ assert([]).respond_to(:empty?)
92
+ assert(1).not_respond_to(:empty?)
93
+
94
+ assert_refutes { assert(1).respond_to(:empty?) }
95
+ assert_refutes { assert([]).not_respond_to(:empty?) }
96
+ end
97
+
98
+ def test_same
99
+ a = Object.new
100
+ b = Object.new
101
+
102
+ assert(a).same(a)
103
+ assert(a).not_same(b)
104
+
105
+ assert_refutes { assert(a).same(b) }
106
+ assert_refutes { assert(a).not_same(a) }
107
+ end
108
+
109
+ def test_chain
110
+ assert(1)
111
+ .is(:>, 0)
112
+ .equal(1)
113
+ .instance_of(Fixnum)
114
+ end
115
+ end
116
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Magnus Holm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ description: Chainable assertions for MiniTest
31
+ email: judofyr@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - test/helper.rb
37
+ - test/test_chain.rb
38
+ - lib/minitest-chain.rb
39
+ homepage: https://github.com/judofyr/minitest-chain
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.25
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Chainable assertions for MiniTest
63
+ test_files:
64
+ - test/helper.rb
65
+ - test/test_chain.rb