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

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, message="")
  if lhs1 == lhs2
    full_message = build_message(message, "Gave the same value for both sides of range. <?> was not equal to <?>", rhs, lhs1)

    assert_block(full_message) { lhs1 == rhs }
  else
    lower  = lhs1 < lhs2 ? lhs1 : lhs2
    higher = lhs1 < lhs2 ? lhs2 : lhs1
    full_message = build_message(message, "<?> was not between <?> and <?>", rhs, lower, higher)
    assert_block(full_message) { 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(message = "", &block)
  full_message = build_message(message,
                               "Failed assertion was expected, but it did not occur.")

  assert_block(full_message) 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, message = "")
  assert_operator(lhs, :>, rhs, message)
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, message = "")
  assert_operator(lhs, :>=, rhs, message)
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, message = "")\
  assert_operator(lhs, :<, rhs, message)
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, message = "")
  assert_operator(lhs, :<=, rhs, message)
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, message = "")
  assert_operator(value, :<, 0, message)
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, message = "")
  full_message = build_message(message,
                               "<?> incorrectly is true.",
                               boolean)
  assert_block(full_message) 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, message = "")
  assert_operator(value, :>, 0, message)
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: assert_raise_message("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 assert_raise_message(expected_exception_message,
                         expected_exception_class,
                         message = "",
                         &block)
  full_message = build_message(message,
                               "<?> exception expected but none was thrown.",
                               expected_exception_class)
  actual_exception = nil
  assert_block(full_message) do
    begin
      yield
      false
    rescue Exception => e
      actual_exception = e
      true
    end
  end

  full_message = build_message(message,
                               "<?> exception expected but was\n?",
                               expected_exception_class,
                               actual_exception)

  assert_block(full_message) do
    actual_exception.is_a?(expected_exception_class)
  end

  full_message = build_message(message,
                               "<?> exception message expected but was\n<?>",
                               expected_exception_message,
                               actual_exception.message)
  assert_block(full_message) do
    expected_exception_message == actual_exception.message
  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.

Raises:

  • (ArgumentError)


162
163
164
165
# File 'lib/assertions.rb', line 162

def assert_sorted(enum, message = "")
  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, [=> 1, => 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, message = "")
  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, [=> 2, => 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, message = "")
  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.

Raises:

  • (ArgumentError)


180
181
182
183
# File 'lib/assertions.rb', line 180

def assert_sorted_desc(enum, message = "")
  raise ArgumentError.new("enum must be enumerable") unless enum.is_a?(Enumerable)
  assert_equal enum.clone.sort{|a,b| b <=> a }, enum
end