rubyunit 0.3.16 → 0.3.17

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/RubyUnit/Assertions/Basic.rb +111 -108
  4. data/lib/RubyUnit/Assertions/Classes.rb +216 -0
  5. data/lib/RubyUnit/Assertions/Collections.rb +105 -0
  6. data/lib/RubyUnit/Assertions/Comparisons.rb +205 -0
  7. data/lib/RubyUnit/Assertions/Exceptions.rb +122 -0
  8. data/lib/RubyUnit/Assertions/Methods.rb +162 -0
  9. data/lib/RubyUnit/Assertions/Root.rb +93 -0
  10. data/lib/RubyUnit/Assertions.rb +22 -97
  11. data/lib/RubyUnit.rb +16 -9
  12. data/tests/AssertionFailure/TC_Class.rb +8 -3
  13. data/tests/AssertionFailure/TC_Instance.rb +13 -26
  14. data/tests/AssertionFailure/data/Class.rb +13 -0
  15. data/tests/AssertionFailure/data/Instance.rb +9 -10
  16. data/tests/Assertions/{TC_Class.rb → TC_Classes.rb} +3 -4
  17. data/tests/Assertions/TC_Collections.rb +13 -0
  18. data/tests/Assertions/TC_Comparisons.rb +13 -0
  19. data/tests/Assertions/TC_Exceptions.rb +13 -0
  20. data/tests/Assertions/TC_Methods.rb +13 -0
  21. data/tests/Assertions/data/Basic.rb +2 -2
  22. data/tests/Assertions/data/{Class.rb → Classes.rb} +1 -1
  23. data/tests/Assertions/data/Collections.rb +13 -0
  24. data/tests/Assertions/data/Comparisons.rb +7 -0
  25. data/tests/Assertions/data/Exceptions.rb +7 -0
  26. data/tests/Assertions/data/Methods.rb +7 -0
  27. data/tests/RubyUnit/TC_GemInfo.rb +4 -3
  28. data/tests/RubyUnit/TC_RubyUnit.rb +8 -7
  29. data/tests/RubyUnit/data/GemInfo.rb +1 -1
  30. data/tests/RubyUnit/data/RubyUnit.rb +9 -2
  31. data/tests/TS_Assertions.rb +2 -4
  32. metadata +20 -12
  33. data/lib/RubyUnit/Assertions/Class.rb +0 -198
  34. data/lib/RubyUnit/Assertions/Collection.rb +0 -80
  35. data/lib/RubyUnit/Assertions/Comparison.rb +0 -200
  36. data/lib/RubyUnit/Assertions/Exception.rb +0 -105
  37. data/lib/RubyUnit/Assertions/Method.rb +0 -157
  38. data/tests/Assertions/TC_Comparison.rb +0 -13
  39. data/tests/Assertions/data/Comparison.rb +0 -7
@@ -0,0 +1,205 @@
1
+ require_relative 'Root'
2
+
3
+ module RubyUnit
4
+ module Assertions
5
+ module Comparisons
6
+ include RubyUnit::AssertionMessage
7
+ include Assertions::Root
8
+
9
+ #
10
+ # Assert that two values are equal.
11
+ # * raises RubyUnit::AssertionFailure unless _expected_ equals _actual_
12
+ #
13
+ # expected::
14
+ # The value that is forbidden by the assertion
15
+ #
16
+ # actual::
17
+ # The value that is being checked by the assertion
18
+ #
19
+ # message::
20
+ # The message provided to be reported for a failure
21
+ #
22
+ # assertEqual 42, 24, "This will fail" # => fail
23
+ #
24
+ def assertEqual expected, actual, message = nil
25
+ __assert (expected == actual), ASSERT_EQUAL_ERROR, message, {:expected=>expected, :actual=>actual}
26
+ end
27
+
28
+ #
29
+ # Assert that two values are NOT equal.
30
+ # * raises RubyUnit::AssertionFailure if _illegal_ equals _actual_
31
+ #
32
+ # illegal::
33
+ # The value that is not allowed by the assertion
34
+ #
35
+ # actual::
36
+ # The value that is being checked by the assertion
37
+ #
38
+ # message::
39
+ # The message provided to be reported for a failure
40
+ #
41
+ # assertNotEqual 3.14, 3.14, "This will fail" # => fail
42
+ #
43
+ def assertNotEqual illegal, actual, message = nil
44
+ __reject (illegal == actual), ASSERT_NOT_EQUAL_ERROR, message, {:illegal=>illegal, :actual=>actual}
45
+ end
46
+
47
+ #
48
+ # Assert that one value is greater than another.
49
+ # * raises RubyUnit::AssertionFailure unless _greater_ is greater than _value_
50
+ #
51
+ # greater::
52
+ # The value that should be greater
53
+ #
54
+ # value::
55
+ # The value that is being checked by the assertion
56
+ #
57
+ # message::
58
+ # The message provided to be reported for a failure
59
+ #
60
+ # assertGreaterThan 24, 42, "This will fail" # => fail
61
+ #
62
+ def assertGreaterThan greater, value, message = nil
63
+ __assert (greater > value), ASSERT_GREATERTHAN_ERROR, message, {:greater=>greater, :value=>value}
64
+ end
65
+
66
+ #
67
+ # Assert that one value is greater than another.
68
+ # * raises RubyUnit::AssertionFailure unless _greater_ is greater than or equal to _value_
69
+ #
70
+ # greater::
71
+ # The value that should be greater than or equal
72
+ #
73
+ # value::
74
+ # The value that is being checked by the assertion
75
+ #
76
+ # message::
77
+ # The message provided to be reported for a failure
78
+ #
79
+ # assertGreaterThanOrEqual 24, 42, "This will fail" # => fail
80
+ #
81
+ def assertGreaterThanOrEqual greater, value, message = nil
82
+ __assert (greater >= value), ASSERT_GREATERTHANOREQUAL_ERROR, message, {:greater=>greater, :value=>value}
83
+ end
84
+
85
+ #
86
+ # Assert that one value is less than another.
87
+ # * raises RubyUnit::AssertionFailure unless _less_ is less than _value_
88
+ #
89
+ # less::
90
+ # The value that should be less
91
+ #
92
+ # value::
93
+ # The value that is being checked by the assertion
94
+ #
95
+ # message::
96
+ # The message provided to be reported for a failure
97
+ #
98
+ # assertLessThan 42, 24, "This will fail" # => fail
99
+ #
100
+ def assertLessThan less, value, message = nil
101
+ __assert (less < value), ASSERT_LESSTHAN_ERROR, message, {:less=>less, :value=>value}
102
+ end
103
+
104
+ #
105
+ # Assert that one value is less than another.
106
+ # * raises RubyUnit::AssertionFailure unless _less_ is less than or equal to _value_
107
+ #
108
+ # less::
109
+ # The value that should be less than or equal
110
+ #
111
+ # value::
112
+ # The value that is being checked by the assertion
113
+ #
114
+ # message::
115
+ # The message provided to be reported for a failure
116
+ #
117
+ # assertLessThanOrEqual 42, 24, "This will fail" # => fail
118
+ #
119
+ def assertLessThanOrEqual less, value, message = nil
120
+ __assert (less <= value), ASSERT_LESSTHANOREQUAL_ERROR, message, {:less=>less, :value=>value}
121
+ end
122
+
123
+ #
124
+ # Assert that a value matches a Regexp pattern.
125
+ # * raises RubyUnit::AssertionFailure unless _value_ matches _pattern_
126
+ #
127
+ # pattern::
128
+ # A Regexp pattern expected by the assertion
129
+ #
130
+ # value::
131
+ # The value that is being checked for the assertion
132
+ #
133
+ # message::
134
+ # The message provided to be reported for a failure
135
+ #
136
+ # assertMatch /^Hello/, 'Goodbye!', "This will fail" # => fail
137
+ #
138
+ def assertMatch pattern, value, message = nil
139
+ pattern = [pattern] unless pattern.is_a? Array
140
+ pattern.each do |regex|
141
+ __assert (value =~ regex), ASSERT_MATCH_ERROR, message, {:pattern=>pattern, :value=>value}
142
+ end
143
+ end
144
+
145
+ #
146
+ # Assert that a value does not match a Regexp pattern.
147
+ # * raises RubyUnit::AssertionFailure if _value_ matches _pattern_
148
+ #
149
+ # pattern::
150
+ # A Regexp pattern excluded by the assertion
151
+ #
152
+ # value::
153
+ # The value that is being checked for the assertion
154
+ #
155
+ # message::
156
+ # The message provided to be reported for a failure
157
+ #
158
+ # assertMatch /^Good/, 'Goodbye!', "This will fail" # => fail
159
+ #
160
+ def assertNotMatch exclusion, value, message = nil
161
+ __reject (value =~ exclusion), ASSERT_NO_MATCH_ERROR, message, {:exclusion=>exclusion, :value=>value}
162
+ end
163
+
164
+ #
165
+ # Assert that two objects are the same object
166
+ # * raises RubyUnit::AssertionFailure unless _expected_ and _actual_ are
167
+ # the same object.
168
+ #
169
+ # expected::
170
+ # The expected object
171
+ #
172
+ # actual::
173
+ # The object that is being checked against _expected_
174
+ #
175
+ # message::
176
+ # The message provided to be reported for a failure
177
+ #
178
+ # assertSame '42', 42, 'Not even close.' # => fail
179
+ #
180
+ def assertSame expected, actual, message = nil
181
+ __assert (expected.equal? actual), ASSERT_SAME_ERROR, message, {:expected=>expected, :actual=>actual}
182
+ end
183
+
184
+ #
185
+ # Assert that two objects are not the same object
186
+ # * raises RubyUnit::AssertionFailure if _illegal_ and _actual_ are the
187
+ # same object.
188
+ #
189
+ # illegal::
190
+ # The expected that it shouldn't be
191
+ #
192
+ # actual::
193
+ # The object that is being checked against _illegal_
194
+ #
195
+ # message::
196
+ # The message provided to be reported for a failure
197
+ #
198
+ # assertNotSame value, value, 'Imagine that!' # => fail
199
+ #
200
+ def assertNotSame illegal, actual, message = nil
201
+ __reject (illegal.equal? actual), ASSERT_NOT_SAME_ERROR, message, {:illegal=>illegal, :actual=>actual}
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,122 @@
1
+ require_relative 'Root'
2
+
3
+ module RubyUnit
4
+ module Assertions
5
+ module Exceptions
6
+ include RubyUnit::AssertionMessage
7
+ include Root
8
+
9
+ ##
10
+ # Assert that no exception is raised.
11
+ # * raises RubyUnit::AssertionFailure if any exception is raised
12
+ #
13
+ # message::
14
+ # The message provided to be reported for a failure
15
+ #
16
+ # &block::
17
+ # The code block that is executed
18
+ #
19
+ # assertNothingRaised 'Not expecting an exception!' do
20
+ # # do something
21
+ # end
22
+ #
23
+ def assertNothingRaised message = nil, &block
24
+ __assert_block do
25
+ begin
26
+ yield
27
+ true # just in case the block yields 'false' or 'nil'
28
+ rescue Exception => e
29
+ __fail ASSERT_NOTHING_RAISED_ERROR, message, {:exception=>e.message}
30
+ end
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Assert that a specified exception message is raised.
36
+ # * raises RubyUnit::AssertionFailure unless the correct Exception message is raised
37
+ #
38
+ # pattern::
39
+ # The String or Regexp that will be used to validate the Exception message
40
+ #
41
+ # message::
42
+ # The message provided to be reported for a failure
43
+ #
44
+ # &block::
45
+ # The code block that is expected to throw the Exception
46
+ #
47
+ # assertRaiseMessage /^Invalid/, 'Expecting an exception!' do
48
+ # # do something
49
+ # end
50
+ #
51
+ def assertRaiseMessage pattern, message = nil, &block
52
+ assertRaiseExpected Exception, pattern, message, &block
53
+ end
54
+
55
+ ##
56
+ # Assert that a specified exception type is raised.
57
+ # * raises RubyUnit::AssertionFailure unless the correct Exception type is raised
58
+ #
59
+ # e::
60
+ # The Exception class that is expected.
61
+ #
62
+ # message::
63
+ # The message provided to be reported for a failure
64
+ #
65
+ # &block::
66
+ # The code block that is expected to throw the Exception
67
+ #
68
+ # assertRaiseKindOf StandardError, 'Expecting an exception!' do # => fail
69
+ # # do something
70
+ # end
71
+ #
72
+ def assertRaiseKindOf e, message = nil, &block
73
+ assertRaiseExpected e, '', message, &block
74
+ end
75
+
76
+ ##
77
+ # Assert that a specified exception is raised.
78
+ # * raises RubyUnit::AssertionFailure unless the correct Exception is raised
79
+ #
80
+ # exception::
81
+ # The Exception class that is expected.
82
+ #
83
+ # pattern::
84
+ # The String or Regexp that will be used to validate the Exception message
85
+ #
86
+ # message::
87
+ # The message provided to be reported for a failure
88
+ #
89
+ # &block::
90
+ # The code block that is expected to throw the Exception
91
+ #
92
+ # assertRaiseExpected StandardError, /^Invalid/, 'Expecting an exception!' do
93
+ # raise StandardError, 'Invalid Retroincabulator'
94
+ # end
95
+ #
96
+ def assertRaiseExpected exception, pattern, message = nil &block
97
+ __assert_exception pattern, exception
98
+ __assert_block ASSERT_RAISE_EXPECTED_ERROR, message, {:exception=>exception, :pattern=>pattern} do
99
+ e = false
100
+ begin
101
+ yield
102
+ rescue exception => e
103
+ assertEqual pattern, e.message if pattern.is_a? String and pattern.length > 0
104
+ assertMatch pattern, e.message if pattern.is_a? Regexp
105
+ end
106
+ e
107
+ end
108
+ end
109
+
110
+ private
111
+ ##
112
+ # Validate the parameters for exception assertions
113
+ # * raises ArgumentError if _pattern_ is not a String or Regexp
114
+ # * raises ArgumentError unless _e_ is a descendent of the Exception class
115
+ #
116
+ def __assert_exception pattern, e = Exception # :nodoc:
117
+ raise ArgumentError, "Message patter must be a Regexp or String, got #{pattern.class}" unless pattern.is_a? Regexp or pattern.is_a? String
118
+ raise ArgumentError, "Expected subclass of Exception, got #{e.class}" unless e < Exception
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,162 @@
1
+ require_relative 'Root'
2
+
3
+ module RubyUnit
4
+ module Assertions
5
+ module Methods
6
+ include RubyUnit::AssertionMessage
7
+ include Root
8
+
9
+ #
10
+ # Assert that an object responds to particular method
11
+ # * raises RubyUnit::AssertionFailure unless _object_ responds to _method_
12
+ #
13
+ # object::
14
+ # The object to check
15
+ #
16
+ # method::
17
+ # The method to assert on the object
18
+ #
19
+ # message::
20
+ # The message provided to be reported for a failure
21
+ #
22
+ # assertRespondTo /^Regexp/, :length, 'It does not, so... no' # => fail
23
+ #
24
+ def assertRespondTo object, method, message = nil
25
+ __assert (object.respond_to? method), ASSERT_RESPOND_TO_ERROR, message, {:object=>object, :method=>method}
26
+ end
27
+
28
+ #
29
+ # Assert that an object does not respond to a particular method
30
+ # * raises RubyUnit::AssertionFailure if _object_ responds to _method_
31
+ #
32
+ # object::
33
+ # The object to check
34
+ #
35
+ # method::
36
+ # The method to assert on the object
37
+ #
38
+ # message::
39
+ # The message provided to be reported for a failure
40
+ #
41
+ # assertNotRespondTo 25, :integer?, 'It does, so close' # => fail
42
+ #
43
+ def assertNotRespondTo object, method, message = nil
44
+ __assert (object.respond_to? method), ASSERT_NOT_RESPOND_TO_ERROR, message, {:object=>object, :method=>method}
45
+ end
46
+
47
+ #
48
+ # Assert that an object has defined the specified method.
49
+ # * raises RubyUnit::AssertionFailure unless _klass_ has defined _method_
50
+ #
51
+ # klass::
52
+ # The object to check for _method_
53
+ #
54
+ # method::
55
+ # The method to check
56
+ #
57
+ # message::
58
+ # The message provided to be reported for a failure
59
+ #
60
+ # assertMethod String, :integer?, 'Nope' # => fail
61
+ #
62
+ def assertMethod klass, method, message = nil
63
+ assertInclude klass.methods, method, message
64
+ end
65
+
66
+ #
67
+ # Assert that an object has not defined the specified method.
68
+ # * raises RubyUnit::AssertionFailure if _klass_ has defined _method_
69
+ #
70
+ # klass::
71
+ # The object to check for _method_
72
+ #
73
+ # method::
74
+ # The method to check
75
+ #
76
+ # message::
77
+ # The message provided to be reported for a failure
78
+ #
79
+ # assertNotMethod Integer, :integer?, 'Nope' # => fail
80
+ #
81
+ def assertNotMethod klass, not_method, message = nil
82
+ assertNotInclude klass.methods, not_method, message
83
+ end
84
+
85
+ #
86
+ # Assert that an object has defined the specified instance method.
87
+ # * raises RubyUnit::AssertionFailure unless _klass_ has defined _instance_method_
88
+ #
89
+ # klass::
90
+ # The object to check for _instance_method_
91
+ #
92
+ # method::
93
+ # The method to check
94
+ #
95
+ # message::
96
+ # The message provided to be reported for a failure
97
+ #
98
+ # assertInstanceMethod String, :integer?, 'Nope' # => fail
99
+ #
100
+ def assertInstanceMethod klass, instance_method, message = nil
101
+ assertInclude klass.instance_methods, instance_method, message
102
+ end
103
+
104
+ #
105
+ # Assert that an object has not defined the specified instance method.
106
+ # * raises RubyUnit::AssertionFailure unless _klass_ has defined _not_instance_method_
107
+ #
108
+ # klass::
109
+ # The object to check for _not_instance_method_
110
+ #
111
+ # method::
112
+ # The method to check
113
+ #
114
+ # message::
115
+ # The message provided to be reported for a failure
116
+ #
117
+ # assertNotInstanceMethod Integer, :integer?, 'Nope' # => fail
118
+ #
119
+ def assertNotInstanceMethod klass, not_instance_method, message = nil
120
+ assertNotInclude klass.instance_methods, not_instance_method, message
121
+ end
122
+
123
+ #
124
+ # Assert that an Class has defined the specified class method.
125
+ # * raises RubyUnit::AssertionFailure unless _klass_ has defined _class_method_
126
+ #
127
+ # klass::
128
+ # The object to check for _class_method_
129
+ #
130
+ # method::
131
+ # The method to check
132
+ #
133
+ # message::
134
+ # The message provided to be reported for a failure
135
+ #
136
+ # assertClassMethod String, :integer?, 'Nope' # => fail
137
+ #
138
+ def assertClassMethod klass, class_method, message = nil
139
+ assertInclude klass.singleton_methods, class_method, message
140
+ end
141
+
142
+ #
143
+ # Assert that an Class has not defined the specified class method.
144
+ # * raises RubyUnit::AssertionFailure unless _klass_ has defined _not_class_method_
145
+ #
146
+ # klass::
147
+ # The object to check for _not_class_method_
148
+ #
149
+ # method::
150
+ # The method to check
151
+ #
152
+ # message::
153
+ # The message provided to be reported for a failure
154
+ #
155
+ # assertNotClassMethod String, :new, 'Nope' # => fail
156
+ #
157
+ def assertNotClassMethod klass, not_class_method, message = nil
158
+ assertNotInclude klass.singleton_methods, not_class_method, message
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,93 @@
1
+ module RubyUnit
2
+ module Assertions
3
+ module Root
4
+ private
5
+ ##
6
+ # Builds the message that will be used with the assertion
7
+ # * raises RubyUnit::AssertionFailure
8
+ # * raises ArgumentError unless error is a String
9
+ # * raises ArgumentError unless message is nil or a String
10
+ # * raises ArgumentError unless data is a Hash
11
+ #
12
+ # error::
13
+ # The assertion description
14
+ #
15
+ # message::
16
+ # The message provided by the test for the assertion
17
+ #
18
+ # data::
19
+ # The data associated with assertion failure
20
+ #
21
+ # __fail 'Failing Test', message, {'expected' => expected, 'actual' => actual }
22
+ #
23
+ def __fail error, message, data = {} # :nodoc:
24
+ raise ArgumentError, 'Error description must be a String' unless error.is_a? String
25
+ raise ArgumentError, 'Failure message must be String' unless message.nil? or message.is_a? String
26
+ raise ArgumentError, 'Failure data must be a Hash' unless data.is_a? Hash
27
+ raise AssertionFailure.new({'Assertion Failure'=>message}.merge data), error
28
+ end
29
+
30
+ ##
31
+ # The assertion wrapper is responsible for doing everything that must be done
32
+ # on each assertion.
33
+ # * keep track of the total number of assertions
34
+ #
35
+ # &block::
36
+ # The assertion which is being wrapped
37
+ #
38
+ def __assert_block error = '', message = nil, data = {} # :nodoc:
39
+ Assertions.add_assertion
40
+ result = block_given? ? yield : false
41
+ __fail error, message, data unless result
42
+ result
43
+ end
44
+
45
+ ##
46
+ # This is now a wrapper for __assert_block so it can be called 'nicely' in one line
47
+ # * raises RubyUnit::AssertionFailure unless _value_ is true
48
+ #
49
+ # value::
50
+ # The value to be asserted
51
+ #
52
+ # error::
53
+ # The error associated with the assertion being checked
54
+ #
55
+ # message::
56
+ # The message provided to be reported for a failure
57
+ #
58
+ # data::
59
+ # The data associated with assertion
60
+ #
61
+ # __assert value, 'Failed to assert value is true', message, {:value=>value}
62
+ #
63
+ def __assert value, error, message, data # :nodoc:
64
+ __assert_block error, message, data do
65
+ # this will result in returning true if an Exception is not raised
66
+ value
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Internally validate that an assertion is false or nil
72
+ # * raises RubyUnit::AssertionFailure unless _value_ is false or nil
73
+ #
74
+ # value::
75
+ # The value to be asserted
76
+ #
77
+ # error::
78
+ # The error associated with the assertion being checked
79
+ #
80
+ # message::
81
+ # The message provided to be reported for a failure
82
+ #
83
+ # data::
84
+ # The data associated with assertion
85
+ #
86
+ # __reject value, 'Failed to assert value is not true', message, {:value=>value}
87
+ #
88
+ def __reject value, error, message, data # :nodoc:
89
+ __assert !value, error, message, data
90
+ end
91
+ end
92
+ end
93
+ end