assay 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ # Comparison assertion for TrueClass.
6
+ #
7
+ # TrueFailure.pass?(true) #=> true
8
+ # TrueFailure.fail?(true) #=> false
9
+ # TrueFailure.pass?(1) #=> false
10
+ #
11
+ class TrueFailure < CompareFailure
12
+
13
+ def self.assertion_name
14
+ :true
15
+ end
16
+
17
+ def self.assertion_operator
18
+ :true?
19
+ end
20
+
21
+ # Check assertion.
22
+ def self.pass?(exp)
23
+ TrueClass === exp
24
+ end
25
+
26
+ #
27
+ def to_s
28
+ return super unless @arguments.size == 1
29
+
30
+ exp = @arguments[0].inspect
31
+
32
+ if @_negated
33
+ "Expected #{exp} to NOT be true"
34
+ else
35
+ "Expected #{exp} to be true"
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+
42
+ module Assertives
43
+ # Passed if object is +true+.
44
+ #
45
+ def assert_true(exp, opts={})
46
+ opts[:backtrace] ||= caller
47
+ TrueFailure.assert(exp, opts)
48
+ end
49
+
50
+ # Passed if object is not +true+.
51
+ #
52
+ # assert_not_true(false)
53
+ #
54
+ def assert_not_true(exp, opts={})
55
+ opts[:backtrace] ||= caller
56
+ TrueFailure.refute(exp, opts)
57
+ end
58
+ end
59
+
60
+
61
+ module Matchers
62
+ # True?
63
+ #
64
+ # value.assert is_true
65
+ #
66
+ def is_true
67
+ TrueFailure.to_matcher
68
+ end
69
+
70
+ # True?
71
+ #
72
+ # value.should be_true
73
+ #
74
+ def be_true
75
+ TrueFailure.to_matcher
76
+ end
77
+ end
78
+
79
+ end
80
+
@@ -0,0 +1,48 @@
1
+ module Assay
2
+
3
+ #
4
+ class Matcher
5
+ def initialize(fail_class, *arguments, &block)
6
+ @fail_class = fail_class
7
+ @arguments = arguments
8
+ @block = block
9
+ end
10
+
11
+ #
12
+ def fail_class
13
+ @fail_class
14
+ end
15
+
16
+ #
17
+ def matches?(target)
18
+ @exception = nil
19
+ @target = target
20
+
21
+ @fail_class.pass?(target, *@arguments, &@block)
22
+ end
23
+
24
+ # Returns Exception class.
25
+ def exception(message=nil)
26
+ @exception ||= fail_class.new(message, @target, *@arguments, &@block)
27
+ # :negated => options[:negated],
28
+ # :backtrace => options[:backtrace] || caller,
29
+ end
30
+
31
+ # This is just for RSpec matcher compatability.
32
+ def fail_message
33
+ exception.to_s
34
+ end
35
+
36
+ # This is just for RSpec matcher compatability.
37
+ def negative_fail_message
38
+ #exception.set_negative(true)
39
+ exception.to_s
40
+ end
41
+
42
+ #
43
+ def fail(backtrace=nil)
44
+ super exception #(backtrace || caller)
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,75 @@
1
+ = Failure Classes
2
+
3
+ require 'assay'
4
+
5
+ == EqualityFailure
6
+
7
+ assert Assay::EqualityFailure.pass?(1,1)
8
+ refute Assay::EqualityFailure.pass?(1,2)
9
+
10
+ == TrueFailure
11
+
12
+ assert Assay::TrueFailure.pass?(true)
13
+ refute Assay::TrueFailure.pass?(false)
14
+
15
+ == FalseFailure
16
+
17
+ assert Assay::FalseFailure.pass?(false)
18
+ refute Assay::FalseFailure.pass?(true)
19
+
20
+ == NilFailure
21
+
22
+ assert Assay::NilFailure.pass?(nil)
23
+ refute Assay::NilFailure.pass?(true)
24
+ refute Assay::NilFailure.pass?(false)
25
+
26
+ == DeltaFailure
27
+
28
+ assert Assay::DeltaFailure.pass?(1, 1.5, 2)
29
+ refute Assay::DeltaFailure.pass?(1, 2.5, 1)
30
+
31
+ == MatchFailure
32
+
33
+ assert Assay::MatchFailure.pass?(/a/, "abc")
34
+ refute Assay::MatchFailure.pass?(/x/, "abc")
35
+
36
+ == SameFailure
37
+
38
+ assert Assay::SameFailure.pass?(1, 1)
39
+ refute Assay::SameFailure.pass?(1, 1.0)
40
+
41
+ == IdentityFailure
42
+
43
+ assert Assay::IdentityFailure.pass?(:a, :a)
44
+ refute Assay::IdentityFailure.pass?("a", "a")
45
+
46
+ == InstanceFailure
47
+
48
+ assert Assay::InstanceFailure.pass?(1, Fixnum)
49
+ refute Assay::InstanceFailure.pass?(1, String)
50
+
51
+ == KindFailure
52
+
53
+ assert Assay::KindFailure.pass?(1, Integer)
54
+ refute Assay::KindFailure.pass?(1, String)
55
+
56
+ == RaiseFailure
57
+
58
+ assert Assay::RaiseFailure.pass?(ArgumentError){ raise ArgumentError }
59
+ refute Assay::RaiseFailure.pass?(ArgumentError){ raise TypeError }
60
+
61
+ == ResponseFailure
62
+
63
+ assert Assay::ResponseFailure.pass?("string", :upcase)
64
+ refute Assay::ResponseFailure.pass?("string", :not_a_method)
65
+
66
+ == ExecutionFailure
67
+
68
+ assert Assay::ExecutionFailure.pass?(){ nil }
69
+ refute Assay::ExecutionFailure.pass?(){ raise }
70
+
71
+ == ThrowFailure
72
+
73
+ assert Assay::ThrowFailure.pass?(:foo){ throw :foo }
74
+ refute Assay::ThrowFailure.pass?(:foo){ throw :bar }
75
+
@@ -0,0 +1,118 @@
1
+ = Assertion Methods
2
+
3
+ require 'assay'
4
+
5
+ include Assay::Assertives
6
+
7
+ == EqualityFailure
8
+
9
+ assert_equal(1,1)
10
+
11
+ expect Assay::EqualityFailure do
12
+ assert_equal(1,2)
13
+ end
14
+
15
+ == TrueFailure
16
+
17
+ assert_true(true)
18
+
19
+ expect Assay::TrueFailure do
20
+ assert_true(false)
21
+ end
22
+
23
+ == FalseFailure
24
+
25
+ assert_false(false)
26
+
27
+ expect Assay::FalseFailure do
28
+ assert_false(true)
29
+ end
30
+
31
+ == NilFailure
32
+
33
+ assert_nil(nil)
34
+
35
+ expect Assay::NilFailure do
36
+ assert_nil(true)
37
+ end
38
+
39
+ == DeltaFailure
40
+
41
+ assert_in_delta(1, 1.5, 2)
42
+
43
+ expect Assay::DeltaFailure do
44
+ assert_in_delta(1, 2.5, 1)
45
+ end
46
+
47
+ == MatchFailure
48
+
49
+ assert_match(/a/, "abc")
50
+
51
+ expect Assay::MatchFailure do
52
+ assert_match(/x/, "abc")
53
+ end
54
+
55
+ == SameFailure
56
+
57
+ assert_same(1, 1)
58
+
59
+ expect Assay::SameFailure do
60
+ assert_same(1, 1.0)
61
+ end
62
+
63
+ == IdentityFailure
64
+
65
+ assert_identical(:a, :a)
66
+
67
+ expect Assay::IdentityFailure do
68
+ assert_identical("a", "a")
69
+ end
70
+
71
+ == InstanceFailure
72
+
73
+ assert_instance_of(1, Fixnum)
74
+
75
+ expect Assay::InstanceFailure do
76
+ assert_instance_of(1, String)
77
+ end
78
+
79
+ == KindFailure
80
+
81
+ assert_kind_of(1, Integer)
82
+
83
+ expect Assay::KindFailure do
84
+ assert_kind_of(1, String)
85
+ end
86
+
87
+ == RaiseFailure
88
+
89
+ assert_raises(ArgumentError){ raise ArgumentError }
90
+
91
+ expect Assay::RaiseFailure do
92
+ assert_raises(ArgumentError){ raise TypeError }
93
+ end
94
+
95
+ == ResponseFailure
96
+
97
+ assert_responds_to("string", :upcase)
98
+
99
+ expect Assay::ResponseFailure do
100
+ assert_responds_to("string", :not_a_method)
101
+ end
102
+
103
+ == Executionfailure
104
+
105
+ assert_executes{ :ok }
106
+
107
+ expect Assay::ExecutionFailure do
108
+ assert_executes{ raise }
109
+ end
110
+
111
+ == ThrowFailure
112
+
113
+ assert_throws(:foo){ throw :foo }
114
+
115
+ expect Assay::ThrowFailure do
116
+ assert_throws(:foo){ throw :bar }
117
+ end
118
+
@@ -0,0 +1,118 @@
1
+ = Matchers
2
+
3
+ require 'assay'
4
+
5
+ include Assay::Matchers
6
+
7
+ == EqualityFailure
8
+
9
+ 1.assert is_equal_to(1)
10
+
11
+ expect Assay::EqualityFailure do
12
+ 1.assert is_equal_to(2)
13
+ end
14
+
15
+ == TrueFailure
16
+
17
+ true.assert is_true
18
+
19
+ expect Assay::TrueFailure do
20
+ false.assert is_true
21
+ end
22
+
23
+ == FalseFailure
24
+
25
+ false.assert is_false
26
+
27
+ expect Assay::FalseFailure do
28
+ true.assert is_false
29
+ end
30
+
31
+ == NilFailure
32
+
33
+ nil.assert is_nil
34
+
35
+ expect Assay::NilFailure do
36
+ true.assert is_nil
37
+ end
38
+
39
+ == DeltaFailure
40
+
41
+ 1.assert is_within(1.5, 2)
42
+
43
+ expect Assay::DeltaFailure do
44
+ 1.assert is_within(0.5, 2)
45
+ end
46
+
47
+ == MatchFailure
48
+
49
+ "abc".assert is_match_for(/a/)
50
+
51
+ expect Assay::MatchFailure do
52
+ "abc".assert is_match_for(/x/)
53
+ end
54
+
55
+ == SameFailure
56
+
57
+ 1.assert is_same_as(1)
58
+
59
+ expect Assay::SameFailure do
60
+ 1.assert is_same_as(1.0)
61
+ end
62
+
63
+ == IdentityFailure
64
+
65
+ :a.assert is_identical_to(:a)
66
+
67
+ expect Assay::IdentityFailure do
68
+ "a".assert is_identical_to("a")
69
+ end
70
+
71
+ == InstanceFailure
72
+
73
+ 1.assert is_instance_of(Fixnum)
74
+
75
+ expect Assay::InstanceFailure do
76
+ 1.assert is_instance_of(String)
77
+ end
78
+
79
+ == KindFailure
80
+
81
+ 1.assert is_kind_of(Integer)
82
+
83
+ expect Assay::KindFailure do
84
+ 1.assert is_kind_of(String)
85
+ end
86
+
87
+ == RaiseFailure
88
+
89
+ ArgumentError.assert is_raised{ raise ArgumentError }
90
+
91
+ expect Assay::RaiseFailure do
92
+ ArgumentError.assert is_raised{ raise TypeError }
93
+ end
94
+
95
+ == ResponseFailure
96
+
97
+ "string".assert is_responsive_to(:upcase)
98
+
99
+ expect Assay::ResponseFailure do
100
+ "string".assert is_responsive_to(:not_a_method)
101
+ end
102
+
103
+ == ExecutionFailure
104
+
105
+ assert is_executed{ :foo }
106
+
107
+ expect Assay::ExecutionFailure do
108
+ assert is_executed{ raise }
109
+ end
110
+
111
+ == ThrowFailure
112
+
113
+ :foo.assert is_thrown{ throw :foo }
114
+
115
+ expect Assay::ThrowFailure do
116
+ :foo.assert is_thrown{ throw :bar }
117
+ end
118
+
@@ -0,0 +1,10 @@
1
+ = Lookup
2
+
3
+ Assay can be used to look up failure classes by associated opearators.
4
+
5
+ require 'assay'
6
+
7
+ Assay.lookup(:==) #=> ::Assay::EqualityFailure
8
+
9
+ Assay.lookup(:empty?) #=> ::Assay::EmptyFailure
10
+