assay 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ require 'assay/assertion'
2
+
3
+ module Assay
4
+
5
+ # Assert that a block of coded executes without error.
6
+ #
7
+ # ExecutionFailure is also the base class of the other
8
+ # block-executing assertion classes.
9
+ class ExecutionFailure < Assertion
10
+ #
11
+ def self.assertion_name
12
+ :execution
13
+ end
14
+
15
+ # Check assertion.
16
+ def self.pass?(_=nil, &block)
17
+ begin
18
+ block.call
19
+ true
20
+ rescue Exception
21
+ false
22
+ end
23
+ end
24
+
25
+ # Check negated assertion.
26
+ def self.fail?(_=nil, &block)
27
+ begin
28
+ block.call
29
+ false
30
+ rescue Exception
31
+ true
32
+ end
33
+ end
34
+
35
+ #
36
+ def to_s
37
+ if @_negated
38
+ "Expected procedure to raise an exception"
39
+ else
40
+ "Expected procedure to execute successfully"
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+
47
+ module Assertives
48
+ # Passes if the block yields successfully.
49
+ #
50
+ # assert_executes "Couldn't do the thing" do
51
+ # do_the_thing
52
+ # end
53
+ #
54
+ def assert_executes(opts={}, &blk)
55
+ opts[:backtrace] ||= caller
56
+ ExecutionFailure.assert(opts, &blk)
57
+ end
58
+
59
+ # Passes if the block does not yield successfully.
60
+ #
61
+ # assert_not_executes "Couldn't do the thing" do
62
+ # do_the_thing
63
+ # end
64
+ #
65
+ def assert_not_executes(opts={}, &blk)
66
+ opts[:backtrace] ||= caller
67
+ ExecutionFailure.refute(opts, &blk)
68
+ end
69
+ end
70
+
71
+
72
+ module Matchers
73
+ # TODO: Reasonable matcher for ExectuionFailure ?
74
+ #
75
+ #
76
+ # proc.assert is_executed(*args)
77
+ #
78
+ def is_executed(&block)
79
+ ExecutionFailure.to_matcher(&block)
80
+ end
81
+
82
+ #
83
+ #
84
+ # proc.should be_executed(*args)
85
+ #
86
+ def be_extecuted(&block)
87
+ ExecutionFailure.to_matcher(&block)
88
+ end
89
+ end
90
+
91
+ end
@@ -0,0 +1,73 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ class FalseFailure < CompareFailure
6
+
7
+ def self.assertion_name
8
+ :false
9
+ end
10
+
11
+ def self.assertion_operator
12
+ :false?
13
+ end
14
+
15
+ # Check assertion.
16
+ def self.pass?(exp)
17
+ FalseClass === exp
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 false"
28
+ else
29
+ "Expected #{exp} to be false"
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+
36
+ module Assertives
37
+ # Passed if object is +false+.
38
+ #
39
+ def assert_false(exp, opts={})
40
+ opts[:backtrace] ||= caller
41
+ FalseFailure.assert(exp, opts)
42
+ end
43
+
44
+ # Passed if object is not +false+.
45
+ #
46
+ # assert_not_false(false)
47
+ #
48
+ def assert_not_false(exp, opts={})
49
+ opts[:backtrace] ||= caller
50
+ FalseFailure.refute(exp, opts)
51
+ end
52
+ end
53
+
54
+
55
+ module Matchers
56
+ #
57
+ #
58
+ # value.assert is_false
59
+ #
60
+ def is_false
61
+ FalseFailure.to_matcher
62
+ end
63
+
64
+ #
65
+ #
66
+ # value.should be_false
67
+ #
68
+ def be_false
69
+ FalseFailure.to_matcher
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,86 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ class IdentityFailure < CompareFailure
6
+
7
+ def self.assertion_name
8
+ :identical
9
+ end
10
+
11
+ def self.assertion_operator
12
+ :equal?
13
+ end
14
+
15
+ #def self.fail_message(exp, act)
16
+ # "Expected #{act.inspect} to be identical to #{exp.inspect}"
17
+ #end
18
+
19
+ #def self.fail_message!(exp, act)
20
+ # "Expected #{act.inspect} not to be identical to #{exp.inspect}"
21
+ #end
22
+
23
+ # Check assertion.
24
+ def self.pass?(exp, act)
25
+ exp.object_id == act.object_id
26
+ end
27
+
28
+ #
29
+ def to_s
30
+ return super unless @arguments.size == 2
31
+
32
+ iexp = @arguments[0].inspect
33
+ iact = @arguments[1].inspect
34
+
35
+ if @_negated
36
+ "Expected #{iact} not to be identical to #{iexp}"
37
+ else
38
+ "Expected #{iact} to be identical to #{iexp}"
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+
45
+ module Assertives
46
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same instance).
47
+ #
48
+ # o = Object.new
49
+ # assert_identical(o, o)
50
+ #
51
+ def assert_identical(exp, act, opts={})
52
+ opts[:backtrace] ||= caller
53
+ IdentityFailure.assert(exp, act, opts)
54
+ end
55
+
56
+ # Passes if ! actual .equal? expected
57
+ #
58
+ # assert_not_identical(Object.new, Object.new)
59
+ #
60
+ def assert_not_identical(exp, act, opts={})
61
+ opts[:backtrace] ||= caller
62
+ IdentityFailure.refute(exp, act, opts)
63
+ end
64
+ end
65
+
66
+
67
+ module Matchers
68
+ #
69
+ #
70
+ # object1.assert is_identical_to(object2)
71
+ #
72
+ def is_identical_to(obj)
73
+ IdentityFailure.to_matcher(obj)
74
+ end
75
+
76
+ #
77
+ #
78
+ # object1.should be_identical_to(object2)
79
+ #
80
+ def be_identical_to(obj)
81
+ IdentityFailure.to_matcher(obj)
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,77 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ class InstanceFailure < CompareFailure
6
+
7
+ def self.assertion_name
8
+ :instance
9
+ end
10
+
11
+ def self.assertion_operator
12
+ :instance_of?
13
+ end
14
+
15
+ # Check assertion.
16
+ def self.pass?(exp, act)
17
+ exp.instance_of?(act)
18
+ end
19
+
20
+ #
21
+ def to_s
22
+ return super unless @arguments.size == 2
23
+
24
+ exp = @arguments[0].inspect
25
+ act = @arguments[1].inspect
26
+
27
+ if @_negated
28
+ "Expected #{act} to NOT be an instance of #{exp}"
29
+ else
30
+ "Expected #{act} to be an instance of #{exp}"
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ module Assertives
37
+ # Passes if object .instance_of? klass
38
+ #
39
+ # assert_instance_of(String, 'foo')
40
+ #
41
+ def assert_instance_of(cls, obj, opts={})
42
+ opts[:backtrace] ||= caller
43
+ InstanceFailure.assert(cls, obj, opts)
44
+ end
45
+
46
+ # Passes if object .instance_of? klass
47
+ #
48
+ # assert_instance_of(String, 'foo')
49
+ #
50
+ def refute_instance_of(cls, obj, opts={})
51
+ opts[:backtrace] ||= caller
52
+ InstanceFailure.refute(cls, obj, opts)
53
+ end
54
+
55
+ alias_method :assert_not_instance_of, :refute_instance_of
56
+ end
57
+
58
+
59
+ module Matchers
60
+ #
61
+ #
62
+ # object.assert is_instance_of(class)
63
+ #
64
+ def is_instance_of(cls)
65
+ InstanceFailure.to_matcher(cls)
66
+ end
67
+
68
+ #
69
+ #
70
+ # object.should be_instance_of(class)
71
+ #
72
+ def be_instance_of(cls)
73
+ InstanceFailure.to_matcher(cls)
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,80 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ # Comparison assertion for #kind_of?
6
+ #
7
+ # KindFailure.pass?(1, Integer) #=> true
8
+ # KindFailure.fail?(1, String) #=> true
9
+ #
10
+ class KindFailure < CompareFailure
11
+
12
+ def self.assertion_name
13
+ :kind_of
14
+ end
15
+
16
+ def self.assertion_operator
17
+ :kind_of?
18
+ end
19
+
20
+ # Check assertion.
21
+ def self.pass?(exp, act)
22
+ exp.kind_of? act
23
+ end
24
+
25
+ #
26
+ def to_s
27
+ return super unless @arguments.size == 2
28
+
29
+ exp = @arguments[0].inspect
30
+ act = @arguments[1].inspect
31
+
32
+ if @_negated
33
+ "Expected #{act} to be kind of #{exp}"
34
+ else
35
+ "Expected #{act} to be kind of #{exp}"
36
+ end
37
+ end
38
+ end
39
+
40
+
41
+ module Assertives
42
+ # Passes if object .kind_of? klass
43
+ #
44
+ # assert_kind_of(Object, 'foo')
45
+ #
46
+ def assert_kind_of(cls, obj, opts={})
47
+ opts[:backtrace] ||= caller
48
+ KindFailure.assert(cls, obj, opts)
49
+ end
50
+
51
+ # Passes if object .kind_of? klass
52
+ #
53
+ # assert_not_kind_of(Object, 'foo')
54
+ #
55
+ def refute_kind_of(cls, obj, opts={})
56
+ KindFailure.refute(cls, obj, opts)
57
+ end
58
+ alias_method :assert_not_kind_of, :refute_kind_of
59
+ end
60
+
61
+
62
+ module Matchers
63
+ #
64
+ #
65
+ # object.assert is_a_kind_of(class)
66
+ #
67
+ def is_kind_of(cls)
68
+ KindFailure.to_matcher(cls)
69
+ end
70
+
71
+ #
72
+ #
73
+ # object.should be_a_kind_of(class)
74
+ #
75
+ def be_kind_of(cls)
76
+ KindFailure.to_matcher(cls)
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,86 @@
1
+ require 'assay/assertions/compare_failure'
2
+
3
+ module Assay
4
+
5
+ class MatchFailure < CompareFailure
6
+
7
+ def self.assertion_name
8
+ :match
9
+ end
10
+
11
+ def self.assertion_operator
12
+ :=~
13
+ end
14
+
15
+ # Check assertion.
16
+ def self.pass?(exp, act)
17
+ exp =~ act
18
+ end
19
+
20
+ # Check negated assertion.
21
+ def self.fail?(exp, act)
22
+ exp !~ act
23
+ end
24
+
25
+ #
26
+ def self.assertable_method
27
+ :assert_match
28
+ end
29
+
30
+ #
31
+ def to_s
32
+ return super unless @arguments.size == 2
33
+
34
+ exp = @arguments[0].inspect
35
+ act = @arguments[1].inspect
36
+
37
+ if @_negated
38
+ "Expected #{act} !~ #{exp}"
39
+ else
40
+ "Expected #{act} =~ #{exp}"
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+
47
+ module Assertives
48
+ # Passes if string =~ pattern.
49
+ #
50
+ # assert_match(/\d+/, 'five, 6, seven')
51
+ #
52
+ def assert_match(exp, act, opts={})
53
+ opts[:backtrace] ||= caller
54
+ MatchFailure.assert(exp, act, opts)
55
+ end
56
+
57
+ # Passes if regexp !~ string
58
+ #
59
+ # refute_match(/two/, 'one 2 three')
60
+ #
61
+ def refute_match(exp, act, opts={})
62
+ opts[:backtrace] ||= caller
63
+ MatchFailure.refute(exp, act, opts)
64
+ end
65
+ end
66
+
67
+
68
+ module Matchers
69
+ #
70
+ #
71
+ # object.assert is_a_match_for(regexp)
72
+ #
73
+ def is_match_for(regexp)
74
+ MatchFailure.to_matcher(regexp)
75
+ end
76
+
77
+ #
78
+ #
79
+ # object.should be_a_match_for(regexp)
80
+ #
81
+ def be_match_for(regexp)
82
+ MatchFailure.to_matcher(regexp)
83
+ end
84
+ end
85
+
86
+ end