Exercise-3-1
### Assertions found in the test/unit library
### originally found here http://manuals.rubyonrails.com/read/chapter/24
### modified with examples

require 'test/unit'

class BigTestOfAssertions < Test::Unit::TestCase
  # assert( boolean, [msg] )
  # ensures the object/expression is true

  def test_boolean
    assert( 1+1 == 2 ) # => true
  end

  # assert_equal( obj1, obj2, [msg] )
  # ensures obj1 == obj2 is true 

  def test_equal
    assert_equal( "hello", "HELLO".downcase ) # => true
  end
  
  # assert_same( obj1, obj2, [msg] ) 
  # ensures obj1.equal?(obj2) is true
  # assert_not_same( obj1, obj2, [msg] )
  # ensures obj1.equal?(obj2) is false

  def test_sameness
    a = "test"
    b = a
    c = "test"

    assert_same(a, b) # => true
    assert_not_same(a, c) # => true
  end

  # assert_nil( obj, [msg] )
  # ensures obj.nil? is true
  # assert_not_nil( obj, [msg] )
  # ensures obj.nil? is false

  def test_nilness
    var = "not nil"

    assert_nil(nil) # => true
    assert_not_nil(var) # => true
  end

  # assert_match( regexp, string, [msg] )
  # ensures a string matches the regular expression
  # assert_no_match( regexp, string, [msg] )
  # ensures a string doesn’t matches the regular expression

  def test_regexp
    assert_match(/fish/, "dog cat fish hamster") # => true
    assert_no_match(/baby/,"dog cat fish hamster") # => false
  end

  # assert_in_delta( expecting, actual, delta, [msg] )
  # ensures the numbers expecting and actual are within delta of each other

  def test_delta
    result = 7
    assert_in_delta(5, result, 3) # => true
  end

  # assert_raises( exceptions ){ block }
  # ensures a block raises one of the comma-separated exceptions
  # assert_nothing_raised( exceptions ){ block }
  # ensures a block doesn’t raise one of the comma-separated exceptions

  def test_exceptions
    assert_raises(Errno::ENOENT) { f = File.open('not-exists.txt') }  # => true
  end

  # assert_instance_of( class, obj, [msg] )
  # ensures obj is the class type
  # assert_kind_of( class, obj, [msg] )
  # ensures obj is or descends from class

  def test_classes
    assert_instance_of(Fixnum, 1) # => true
    assert_kind_of(Object, 1) # => true
    assert_kind_of(Numeric, 1) # => true
  end

  def test_methods
    # assert_respond_to( obj, symbol, [msg] )
    # ensures obj has a method called symbol

    assert_respond_to(1, :zero?) # => true

    # assert_operator( obj1, operator, obj2, [msg] )
    # ensures obj1.operator(obj2) is true

    assert_operator(1, :equal?, 1) # => true

    # assert_send( array, [msg] )
    # ensures that executing the method listed in array[1] on the object in array[0] 
    # with the parameters of array[ 2 and up ] is true.

    array = [0, :zero?]
    assert_send(array) # => true
    array = [1, :==, 1]
    assert_send(array) # => true
  end
  
  # flunk ( [msg] )
  # ensures failure
  def test_failure
    # flunk("This will always fail.  Kind of sad.")
  end
end

link download