assay 0.2.0

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.
@@ -0,0 +1,75 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ class NilFailure < CompareFailure
6
+
7
+ def self.assertion_name
8
+ :nil
9
+ end
10
+
11
+ def self.assertion_operator
12
+ :nil?
13
+ end
14
+
15
+ # Check assertion.
16
+ def self.pass?(exp)
17
+ exp.nil?
18
+ end
19
+
20
+ #
21
+ def to_s
22
+ return super unless @arguments.size == 1
23
+
24
+ exp = @arguments[0].inspect
25
+
26
+ if @_negated
27
+ "Expected #{exp} to NOT be nil"
28
+ else
29
+ "Expected #{exp} to be nil"
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+
36
+ module Assertives
37
+ # Passed if object is +nil+.
38
+ #
39
+ def assert_nil(exp, opts={})
40
+ opts[:backtrace] ||= caller
41
+ NilFailure.assert(exp, opts)
42
+ end
43
+
44
+ # Passed if object is not +nil+.
45
+ #
46
+ # assert_not_nil(true)
47
+ #
48
+ def assert_not_nil(exp, opts={})
49
+ opts[:backtrace] ||= caller
50
+ NilFailure.refute(exp, opts)
51
+ end
52
+ end
53
+
54
+
55
+ module Matchers
56
+
57
+ #
58
+ #
59
+ # value.assert is_nil
60
+ #
61
+ def is_nil
62
+ NilFailure.to_matcher
63
+ end
64
+
65
+ #
66
+ #
67
+ # value.should be_nil
68
+ #
69
+ def be_nil
70
+ NilFailure.to_matcher
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,133 @@
1
+ require 'assay/assertions/execution_failure'
2
+
3
+ module Assay
4
+
5
+ #
6
+ class RaiseFailure < ExecutionFailure
7
+
8
+ def self.assertion_name
9
+ :raises
10
+ end
11
+
12
+ def self.assertion_name!
13
+ :not_raised
14
+ end
15
+
16
+ #
17
+ def self.assert(exp, msg=nil, call=nil) #:yeild:
18
+ begin
19
+ yield
20
+ #msg = msg || fail_message(exp)
21
+ test = false
22
+ args = [exp]
23
+ rescue Exception => err
24
+ #msg = msg || fail_message(exp, err)
25
+ test = (exp === err)
26
+ args = [exp, err]
27
+ end
28
+ if !test
29
+ err = new(msg, :backtrace=>(call || caller), :arguments=>args)
30
+ fail err
31
+ end
32
+ end
33
+
34
+ #
35
+ def self.assert!(exp, msg=nil, call=nil) #:yield:
36
+ begin
37
+ yield
38
+ test = true
39
+ args = [exp]
40
+ rescue Exception => err
41
+ #msg = msg || fail_message!(exp, err)
42
+ test = (exp === err)
43
+ args = [exp, err]
44
+ end
45
+ if !test
46
+ err = new(msg, :backtrace=>(call || caller), :arguments=>args)
47
+ fail err
48
+ end
49
+ end
50
+
51
+ # Check assertion.
52
+ #
53
+ # Note: This is not used by the #assert method.
54
+ def self.pass?(*exp)
55
+ begin
56
+ yield
57
+ false
58
+ rescue Exception => e
59
+ exp.any?{ |x| x === e }
60
+ end
61
+ end
62
+
63
+ # Check negated assertion.
64
+ #
65
+ # Note: This is not used by the #assert! method.
66
+ def self.fail?(*exp)
67
+ begin
68
+ yield
69
+ true
70
+ rescue Exception => e
71
+ !exp.any?{ |x| x === e }
72
+ end
73
+ end
74
+
75
+ # TODO: how to add `but got class` instead.
76
+ def to_s
77
+ return super unless @arguments.size == 1
78
+
79
+ exp = @arguments[0].inspect
80
+ #err = @_arguments[1].inspect
81
+
82
+ if @_negated
83
+ "Expected #{exp} NOT to be raised"
84
+ else
85
+ "Expected #{exp} to be raised" #, but was #{err} instead."
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+
92
+ module Assertives
93
+ # Passes if the block raises a given exceptions.
94
+ #
95
+ # assert_raises RuntimeError do
96
+ # raise 'Boom!!!'
97
+ # end
98
+ #
99
+ def assert_raises(exp, msg=nil, call=nil, &blk) #:yeild:
100
+ RaiseFailure.assert(exp, msg=nil, call=nil, &blk)
101
+ end
102
+
103
+ # Passes if the block *does not* raise a given exceptions.
104
+ #
105
+ # assert_not_raised IOError do
106
+ # raise 'Boom!!!'
107
+ # end
108
+ #
109
+ def assert_not_raised(exp, msg=nil, call=nil, &blk) #:yeild:
110
+ RaiseFailure.refute(exp, msg, call, &blk)
111
+ end
112
+ end
113
+
114
+
115
+ module Matchers
116
+ #
117
+ #
118
+ # Exception.assert is_raised{ ... }
119
+ #
120
+ def is_raised(&blk)
121
+ RaiseFailure.to_matcher(&blk)
122
+ end
123
+
124
+ #
125
+ #
126
+ # Exception.should be_raised{ ... }
127
+ #
128
+ def be_raised(&blk)
129
+ RaiseFailure.to_matcher(&blk)
130
+ end
131
+ end
132
+
133
+ end
@@ -0,0 +1,87 @@
1
+ require 'assay/assertion'
2
+
3
+ module Assay
4
+
5
+ # Does an object #respond_to? a method call.
6
+ class ResponseFailure < Assertion
7
+
8
+ def self.assertion_name
9
+ :respond_to
10
+ end
11
+
12
+ def self.assertion_name!
13
+ :not_respond_to
14
+ end
15
+
16
+ def self.operator_name
17
+ :respond_to?
18
+ end
19
+
20
+ # Check assertion.
21
+ def self.pass?(reciever, method)
22
+ #flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
23
+ #obj, meth = meth, obj if flip
24
+ reciever.respond_to?(method)
25
+ end
26
+
27
+ #
28
+ def to_s
29
+ return super unless @arguments.size == 2
30
+
31
+ reciever = @arguments[0].inspect
32
+ method = @arguments[1].inspect
33
+
34
+ if @_negated
35
+ "Expected #{reciever} (#{reciever.class}) to NOT respond to ##{method}"
36
+ else
37
+ "Expected #{reciever} (#{reciever.class}) to respond to ##{method}"
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+
44
+ module Assertives
45
+ # Passes if +object+ respond_to? +methods+.
46
+ #
47
+ # assert_respond_to 'bugbear', :slice
48
+ #
49
+ def assert_respond_to(reciever, method, opts={})
50
+ opts[:backtrace] ||= caller
51
+ ResponseFailure.assert(reciever, method, opts)
52
+ end
53
+ alias_method :assert_responds_to, :assert_respond_to
54
+
55
+ # Passes if +object+ does not respond_to? +methods+.
56
+ #
57
+ # assert_not_respond_to 'bugbear', :slice
58
+ #
59
+ def assert_not_respond_to(reciever, method, opts={})
60
+ opts[:backtrace] ||= caller
61
+ ResponseFailure.refute(reciever, method, opts)
62
+ end
63
+
64
+ alias_method :assert_not_responds_to, :assert_not_respond_to
65
+ end
66
+
67
+
68
+ module Matchers
69
+ #
70
+ #
71
+ # object.assert is_responsive_to(:method_symbol)
72
+ #
73
+ def is_responsive_to(method)
74
+ ResponseFailure.to_matcher(method)
75
+ end
76
+
77
+ #
78
+ #
79
+ # object.should be_responsive_to(:method_symbol)
80
+ #
81
+ def be_responsive_to(method)
82
+ ResponseFailure.to_matcher(method)
83
+ end
84
+ end
85
+
86
+ end
87
+
@@ -0,0 +1,83 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ # Comparison assertion for the #eql? method.
6
+ class SameFailure < CompareFailure
7
+
8
+ def self.assertion_name
9
+ :same
10
+ end
11
+
12
+ def self.assertion_operator
13
+ :eql?
14
+ end
15
+
16
+ # Check assertion.
17
+ def self.pass?(exp, act)
18
+ exp.eql?(act)
19
+ end
20
+
21
+ #
22
+ def to_s
23
+ return super unless @arguments.size == 2
24
+
25
+ exp = @arguments[0].inspect
26
+ act = @arguments[1].inspect
27
+
28
+ if @_negated
29
+ "Expected #{act} to NOT be the same as #{exp}"
30
+ else
31
+ "Expected #{act} to be the same as #{exp}"
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+
38
+ module Assertives
39
+ # Passes if +expected+ .eq? +actual+.
40
+ #
41
+ # Note that the ordering of arguments is important,
42
+ # since a helpful error message is generated when this
43
+ # one fails that tells you the values of expected and actual.
44
+ #
45
+ # assert_same 'MY STRING', 'my string'.upcase
46
+ #
47
+ def assert_same(exp, act, opts={})
48
+ opts[:backtrace] ||= caller
49
+ SameFailure.assert(exp, act, opts)
50
+ end
51
+
52
+ # Passes if not +expected+ .eq? +actual+.
53
+ #
54
+ # assert_not_the_same 'some string', 5
55
+ #
56
+ def refute_same(exp, act, opts={})
57
+ opts[:backtrace] ||= caller
58
+ SameFailure.refute(exp, act, opts)
59
+ end
60
+
61
+ alias_method :assert_not_same, :refute_same
62
+ end
63
+
64
+
65
+ module Matchers
66
+ #
67
+ #
68
+ # object1.assert is_same_as(object2)
69
+ #
70
+ def is_same_as(obj)
71
+ SameFailure.to_matcher(obj)
72
+ end
73
+
74
+ #
75
+ #
76
+ # object1.should be_same_as(object2)
77
+ #
78
+ def be_same_as(obj)
79
+ SameFailure.to_matcher(obj)
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,123 @@
1
+ require 'assay/assertions/execution_failure'
2
+
3
+ module Assay
4
+
5
+ #
6
+ class ThrowFailure < ExecutionFailure
7
+
8
+ def self.assertion_name
9
+ :throws
10
+ end
11
+
12
+ def self.assertion_name!
13
+ :not_thrown
14
+ end
15
+
16
+ # Passes if the block throws expected_symbol
17
+ #
18
+ # assert_throws :done do
19
+ # throw :done
20
+ # end
21
+ #
22
+ def self.pass?(sym, &block)
23
+ pass = true
24
+ catch(sym) do
25
+ begin
26
+ block.call
27
+ rescue ArgumentError => err # 1.9 exception
28
+ #msg += ", not #{err.message.split(/ /).last}"
29
+ pass = false
30
+ rescue NameError => err # 1.8 exception
31
+ #msg += ", not #{err.name.inspect}"
32
+ pass = false
33
+ end
34
+ end
35
+ pass
36
+ end
37
+
38
+ # Passes if the block throws expected_symbol
39
+ #
40
+ # assert_not_thrown :done do
41
+ # throw :chimp
42
+ # end
43
+ #
44
+ # FIXME: Is this correct?
45
+ def self.fail?(sym, &block)
46
+ pass = false
47
+ catch(sym) do
48
+ begin
49
+ block.call
50
+ rescue ArgumentError => err # 1.9 exception
51
+ #msg += ", not #{err.message.split(/ /).last}"
52
+ pass = true
53
+ rescue NameError => err # 1.8 exception
54
+ #msg += ", not #{err.name.inspect}"
55
+ pass = true
56
+ end
57
+ end
58
+ pass
59
+ end
60
+
61
+ #
62
+ def to_s
63
+ return super unless @arguments.size == 1
64
+
65
+ sym = @arguments[0].inspect
66
+
67
+ if @_negated
68
+ "Expected #{sym} to have been thrown"
69
+ else
70
+ "Expected #{sym} NOT to have been thrown"
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+
77
+ module Assertives
78
+ # Passes if the block throws expected_symbol
79
+ #
80
+ # assert_throws :done do
81
+ # throw :done
82
+ # end
83
+ #
84
+ def assert_throws(sym, opts={}, &blk)
85
+ opts[:backtrace] ||= caller
86
+ ThrowFailure.assert(sym, opts, &blk)
87
+ end
88
+
89
+ # Passes if the block throws expected_symbol
90
+ #
91
+ # refute_throws :done do
92
+ # throw :chimp
93
+ # end
94
+ #
95
+ def refute_throws(sym, opts={}, &blk)
96
+ opts[:backtrace] ||= caller
97
+ ThrowFailure.refute(sym, opts, &blk)
98
+ end
99
+
100
+ alias_method :assert_not_thrown, :refute_throws
101
+ end
102
+
103
+
104
+ module Matchers
105
+ #
106
+ #
107
+ # :symbol.assert is_thrown{ ... }
108
+ #
109
+ def is_thrown(&blk)
110
+ ThrowFailure.to_matcher(&blk)
111
+ end
112
+
113
+ #
114
+ #
115
+ # :symbol.should be_thrown{ ... }
116
+ #
117
+ def be_thrown(&blk)
118
+ ThrowFailure.to_matcher(&blk)
119
+ end
120
+ end
121
+
122
+ end
123
+