Module: Test::Unit::Assertions
- Defined in:
- lib/assertions.rb
Overview
Some useful extra assertions. Require ‘assertions’, and Test::Unit::Assertions will have the additional assertions.
Instance Method Summary collapse
-
#assert_between(lhs1, lhs2, rhs, message = "") ⇒ Object
Description: Find out if a something is between two other things when compared.
-
#assert_fail(message = "", &block) ⇒ Object
Description: This assertion passes if and only if block contains an assertion that fails (but which is suppressed from propagating outside of block and so will not cause the test to fail).
-
#assert_greater_than(lhs, rhs, message = "") ⇒ Object
(also: #assert_gt)
Description: This is a convenience wrapper around assert_operator.
-
#assert_greater_than_or_equal_to(lhs, rhs, message = "") ⇒ Object
(also: #assert_ge)
Description: This is a convenience wrapper around assert_operator.
-
#assert_less_than(lhs, rhs, message = "") ⇒ Object
(also: #assert_lt)
Description: This is a convenience wrapper around assert_operator.
-
#assert_less_than_or_equal_to(lhs, rhs, message = "") ⇒ Object
(also: #assert_le)
Description: This is a convenience wrapper around assert_operator.
-
#assert_negative(value, message = "") ⇒ Object
Description: This is a convenience wrapper around assert_operator.
-
#assert_not(boolean, message = "") ⇒ Object
Description: This assertion passes if and only if boolean is false or nil.
-
#assert_positive(value, message = "") ⇒ Object
Description: This is a convenience wrapper around assert_operator.
-
#assert_raise_message(expected_exception_message, expected_exception_class, message = "", &block) ⇒ Object
Description: This assertion passes if and only if block throws an exception of class (or descended from class) expected_exception_class with message expected_exception_message.
-
#assert_sorted(enum, message = "") ⇒ Object
Description: Will tell you if the elements in an Enum are sorted (ascending).
-
#assert_sorted_by(key, enum, message = "") ⇒ Object
Description: Will tell you if the elements in an Enum are sorted (ascending) based on a key corresponding to a key in a hash or method on a class.
-
#assert_sorted_by_desc(key, enum, message = "") ⇒ Object
Description: Will tell you if the elements in an Enum are sorted (descending) based on a key corresponding to a key in a hash or method on a class.
-
#assert_sorted_desc(enum, message = "") ⇒ Object
Description: Will tell you if the elements in an Enum are sorted (ascending).
Instance Method Details
#assert_between(lhs1, lhs2, rhs, message = "") ⇒ Object
Description:
Find out if a something is between two other things when compared. It asserts that lhs1 < rhs < lhs2 or lhs1 > rhs > lhs2
Example:
assert_between(Date.yesterday, Date.tomorrow, Date.today)
Parameters:
- lhs1
-
One of the things to compare against
- lhs2
-
The other thing to compare against
- rhs
-
The thing being tested for between-ness
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/assertions.rb', line 257 def assert_between(lhs1, lhs2, rhs, ="") if lhs1 == lhs2 = (, "Gave the same value for both sides of range. <?> was not equal to <?>", rhs, lhs1) assert_block() { lhs1 == rhs } else lower = lhs1 < lhs2 ? lhs1 : lhs2 higher = lhs1 < lhs2 ? lhs2 : lhs1 = (, "<?> was not between <?> and <?>", rhs, lower, higher) assert_block() { lower < rhs && rhs < higher } end end |
#assert_fail(message = "", &block) ⇒ Object
Description:
This assertion passes if and only if block contains an assertion that fails (but which is suppressed from propagating outside of block and so will not cause the test to fail). If the assertion passes, the failed assertion is written to stdout. This method is (only?) useful when testing other assertions.
Example:
assert_fail do
assert_equal(5, 4)
end
Parameters:
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
- &block
-
This block should contain an assertion that fails.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/assertions.rb', line 33 def assert_fail( = "", &block) = (, "Failed assertion was expected, but it did not occur.") assert_block() do begin yield false rescue AssertionFailedError => e true end end end |
#assert_greater_than(lhs, rhs, message = "") ⇒ Object Also known as: assert_gt
Description:
This is a convenience wrapper around assert_operator. It asserts that lhs > rhs.
Example:
assert_greater_than(5, 4)
Parameters:
- lhs
-
The left-hand side of the comparison.
- rhs
-
The right-hand side of the comparison.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
122 123 124 |
# File 'lib/assertions.rb', line 122 def assert_greater_than(lhs, rhs, = "") assert_operator(lhs, :>, rhs, ) end |
#assert_greater_than_or_equal_to(lhs, rhs, message = "") ⇒ Object Also known as: assert_ge
Description:
This is a convenience wrapper around assert_operator. It asserts that lhs >= rhs.
Example:
assert_greater_than_or_equal_to(5, 5)
Parameters:
- lhs
-
The left-hand side of the comparison.
- rhs
-
The right-hand side of the comparison.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
144 145 146 |
# File 'lib/assertions.rb', line 144 def assert_greater_than_or_equal_to(lhs, rhs, = "") assert_operator(lhs, :>=, rhs, ) end |
#assert_less_than(lhs, rhs, message = "") ⇒ Object Also known as: assert_lt
Description:
This is a convenience wrapper around assert_operator. It asserts that lhs < rhs.
Example:
assert_less_than(4, 5)
Parameters:
- lhs
-
The left-hand side of the comparison.
- rhs
-
The right-hand side of the comparison.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
287 288 289 |
# File 'lib/assertions.rb', line 287 def assert_less_than(lhs, rhs, = "")\ assert_operator(lhs, :<, rhs, ) end |
#assert_less_than_or_equal_to(lhs, rhs, message = "") ⇒ Object Also known as: assert_le
Description:
This is a convenience wrapper around assert_operator. It asserts that lhs <= rhs.
Example:
assert_less_than_or_equal_to(4, 4)
Parameters:
- lhs
-
The left-hand side of the comparison.
- rhs
-
The right-hand side of the comparison.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
309 310 311 |
# File 'lib/assertions.rb', line 309 def assert_less_than_or_equal_to(lhs, rhs, = "") assert_operator(lhs, :<=, rhs, ) end |
#assert_negative(value, message = "") ⇒ Object
Description:
This is a convenience wrapper around assert_operator. It asserts that the value is less than zero
Example:
assert_negative(-5)
Parameters:
- value
-
The number whose negativeness we wish to determine
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
101 102 103 |
# File 'lib/assertions.rb', line 101 def assert_negative(value, = "") assert_operator(value, :<, 0, ) end |
#assert_not(boolean, message = "") ⇒ Object
Description:
This assertion passes if and only if boolean is false or nil.
Parameters:
- boolean
-
This must be false or nil or the assertion to pass
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
58 59 60 61 62 63 64 65 |
# File 'lib/assertions.rb', line 58 def assert_not(boolean, = "") = (, "<?> incorrectly is true.", boolean) assert_block() do !boolean end end |
#assert_positive(value, message = "") ⇒ Object
Description:
This is a convenience wrapper around assert_operator. It asserts that the value is greater than zero
Example:
assert_positive(12)
Parameters:
- value
-
The number whose positiveness we wish to determine
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
82 83 84 |
# File 'lib/assertions.rb', line 82 def assert_positive(value, = "") assert_operator(value, :>, 0, ) end |
#assert_raise_message(expected_exception_message, expected_exception_class, message = "", &block) ⇒ Object
Description:
This assertion passes if and only if block throws an exception of class (or descended from class) expected_exception_class with message expected_exception_message.
Example:
("Hello, exception!", RuntimeError) do
raise "Hello, exception!"
end
Parameters:
- expected_exception_message
-
The message expected to be contained in the exception thrown by block.
- expected_exception_class
-
The expected class (or parent class) of the exception thrown by block.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
- &block
-
The block that is supposed to throw the specified exception.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/assertions.rb', line 338 def (, expected_exception_class, = "", &block) = (, "<?> exception expected but none was thrown.", expected_exception_class) actual_exception = nil assert_block() do begin yield false rescue Exception => e actual_exception = e true end end = (, "<?> exception expected but was\n?", expected_exception_class, actual_exception) assert_block() do actual_exception.is_a?(expected_exception_class) end = (, "<?> exception message expected but was\n<?>", , actual_exception.) assert_block() do == actual_exception. end end |
#assert_sorted(enum, message = "") ⇒ Object
Description:
Will tell you if the elements in an Enum are sorted (ascending)
Example:
assert_sorted([1,2,3,4])
Parameters:
- enum
-
The enumeration to check for sortedness.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
162 163 164 165 |
# File 'lib/assertions.rb', line 162 def assert_sorted(enum, = "") raise ArgumentError.new("enum must be enumerable") unless enum.is_a?(Enumerable) assert_equal enum.clone.sort{|a,b| a <=> b }, enum end |
#assert_sorted_by(key, enum, message = "") ⇒ Object
Description:
Will tell you if the elements in an Enum are sorted (ascending) based on a key corresponding to a key in a hash or method on a class.
Example:
assert_sorted_by :bar, [{:bar => 1}, {:bar => 2}]
Parameters:
- key
-
A string or symbol to use to get a value on an object in ‘enum’
- enum
-
The enumeration to check for sortedness.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
211 212 213 214 |
# File 'lib/assertions.rb', line 211 def assert_sorted_by(key, enum, = "") check_enum_for_key_sortability(key, enum) assert_sorted enum.map{ |x| x.is_a?(Hash) ? x[key] : x.send(key) } end |
#assert_sorted_by_desc(key, enum, message = "") ⇒ Object
Description:
Will tell you if the elements in an Enum are sorted (descending) based on a key corresponding to a key in a hash or method on a class.
Example:
assert_sorted_by_desc :bar, [{:bar => 2}, {:bar => 1}]
Parameters:
- key
-
A string or symbol to use to get a value on an object in ‘enum’
- enum
-
The enumeration to check for sortedness.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
233 234 235 236 |
# File 'lib/assertions.rb', line 233 def assert_sorted_by_desc(key, enum, = "") check_enum_for_key_sortability(key, enum) assert_sorted_desc enum.map{ |x| x.is_a?(Hash) ? x[key] : x.send(key) } end |
#assert_sorted_desc(enum, message = "") ⇒ Object
Description:
Will tell you if the elements in an Enum are sorted (ascending)
Example:
assert_sorted_desc([4,3,2,1])
Parameters:
- enum
-
The enumeration to check for sortedness.
- message = “”
-
An optional additional message that will be displayed if the assertion fails.
180 181 182 183 |
# File 'lib/assertions.rb', line 180 def assert_sorted_desc(enum, = "") raise ArgumentError.new("enum must be enumerable") unless enum.is_a?(Enumerable) assert_equal enum.clone.sort{|a,b| b <=> a }, enum end |