Module: BitGirder::Testing::AssertMethods

Includes:
Core::BitGirderMethods
Included in:
Event::Logger::Testing::EventAccumulator, AbstractPhaseClass, TestClassMixin
Defined in:
lib/bitgirder/testing.rb

Constant Summary

Constants included from Core::BitGirderMethods

Core::BitGirderMethods::PARAM_TYPE_ARG, Core::BitGirderMethods::PARAM_TYPE_ENVVAR, Core::BitGirderMethods::PARAM_TYPE_KEY

Instance Method Summary collapse

Methods included from Core::BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Instance Method Details

#assert(passed, msg = nil) ⇒ Object



32
33
34
# File 'lib/bitgirder/testing.rb', line 32

def assert( passed, msg = nil )
    fail_test( msg ) unless passed
end

#assert_equal(expct, actual, msg = nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/bitgirder/testing.rb', line 40

def assert_equal( expct, actual, msg = nil )
    
    unless expct == actual
        fail_test( msg || "expct != actual (#{expct} != #{actual})" )
    end
end

#assert_equal_i(expct, actual, msg = nil) ⇒ Object



53
54
55
# File 'lib/bitgirder/testing.rb', line 53

def assert_equal_i( expct, actual, msg = nil )
    assert_equal_meth( expct, actual, :to_i, msg )
end

#assert_equal_meth(expct_obj, actual_obj, meth, msg = nil) ⇒ Object



47
48
49
50
51
# File 'lib/bitgirder/testing.rb', line 47

def assert_equal_meth( expct_obj, actual_obj, meth, msg = nil )
    
    not_nil( meth, :meth )
    assert_equal( expct_obj.send( meth ), actual_obj.send( meth ), msg )
end

#assert_equal_s(expct, actual, msg = nil) ⇒ Object



57
58
59
# File 'lib/bitgirder/testing.rb', line 57

def assert_equal_s( expct, actual, msg = nil )
    assert_equal_meth( expct, actual, :to_s, msg )
end

#assert_false(val, *argv) ⇒ Object



36
37
38
# File 'lib/bitgirder/testing.rb', line 36

def assert_false( val, *argv )
    assert( ! val, *argv )
end

#assert_match(pat, str, msg = nil) ⇒ Object



65
66
67
68
69
# File 'lib/bitgirder/testing.rb', line 65

def assert_match( pat, str, msg = nil )

    msg ||= lambda { "#{str.inspect} does not match #{pat}" }
    assert( pat.match( str ), msg )
end

#assert_nil(val, msg = nil) ⇒ Object



61
62
63
# File 'lib/bitgirder/testing.rb', line 61

def assert_nil( val, msg = nil )
    assert( val == nil, msg ||= "Expected nil but got #{val}" )
end

#assert_raised(*excpts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bitgirder/testing.rb', line 80

def assert_raised( *excpts )
    
    res = nil

    pat = get_expect_raised_pat( excpts )

    begin
        yield
    rescue *excpts => e; # got an expected exception
        res = e
    rescue Exception => e
        raise e
    end

    if pat && res
        pat.match( res.message ) or 
            raise "Message #{res.message.inspect} does not match #{pat}"
    end

#        res or raise "No expected exception raised"
    res or fail_test( "Expected raise of one of #{excpts.inspect}" )
end

#fail_test(msg = nil) ⇒ Object

Raises:



26
27
28
29
30
# File 'lib/bitgirder/testing.rb', line 26

def fail_test( msg = nil )
    
    msg = msg.call if msg.is_a?( Proc )
    raise AssertionFailure.new( msg )
end

#get_expect_raised_pat(excpts) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/bitgirder/testing.rb', line 71

def get_expect_raised_pat( excpts )
        
    case excpts[ 0 ]
        when String then Regexp.new( /^#{Regexp.quote( excpts.shift )}$/ )
        when Regexp then excpts.shift
        else nil
    end
end