Module: ASE::Test

Defined in:
lib/as-extensions/test.rb

Constant Summary collapse

CONFIG =
{ :verbose => (ARGV.include?('verbose')),
:fatal => (ARGV.include?('fatal')),
:nb_ok => 0,
:nb_nok => 0,
:mute_stats => false }

Class Method Summary collapse

Class Method Details

.assert(success = nil, msg = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/as-extensions/test.rb', line 34

def assert(success=nil, msg=nil)
  if success
    CONFIG[:nb_ok] += 1
    debug "Test \##{CONFIG[:nb_ok]+CONFIG[:nb_nok]} OK"
    return true
  else
    CONFIG[:nb_nok] += 1
    debug "=== TEST NOT OK ==="
    debug caller.join("\n")
    puts "ERROR ==> #{msg}" if msg
    raise "test failure" if CONFIG[:fatal]
    return false
  end
end

.assert_blank(x = 'a', msg = nil) ⇒ Object



49
50
51
# File 'lib/as-extensions/test.rb', line 49

def assert_blank(x='a', msg=nil)
  assert x.blank?, msg
end

.assert_boolean(b = nil, msg = nil) ⇒ Object



53
54
55
# File 'lib/as-extensions/test.rb', line 53

def assert_boolean(b=nil, msg=nil)
  assert (b == true || b == false), msg_stack(msg, "Expected a boolean, found a #{b.class}.")
end

.assert_empty(x = 1, msg = nil) ⇒ Object



57
58
59
# File 'lib/as-extensions/test.rb', line 57

def assert_empty(x=1, msg=nil)
  assert x.empty?, msg
end

.assert_equal(a, b, msg = nil) ⇒ Object



61
62
63
# File 'lib/as-extensions/test.rb', line 61

def assert_equal(a, b, msg=nil)
  assert a == b, msg_stack(msg, "#{b.inspect} found, expected #{a.inspect}")
end

.assert_fail(msg = nil) ⇒ Object



65
66
67
# File 'lib/as-extensions/test.rb', line 65

def assert_fail(msg=nil)
  assert false, msg
end

.assert_false(b = nil, msg = nil) ⇒ Object



69
70
71
# File 'lib/as-extensions/test.rb', line 69

def assert_false(b=nil, msg=nil)
  assert b == false, msg
end

.assert_include(a, b, msg = nil) ⇒ Object



73
74
75
# File 'lib/as-extensions/test.rb', line 73

def assert_include(a, b, msg=nil)
  assert b.include?(a), msg_stack(msg, "#{b.inspect} does not include #{a.inspect}")
end

.assert_instance_of(k = nil, x = nil, msg = nil) ⇒ Object



77
78
79
# File 'lib/as-extensions/test.rb', line 77

def assert_instance_of(k=nil, x=nil, msg=nil)
  assert x.instance_of?(k), msg
end

.assert_kind_of(k = nil, x = nil, msg = nil) ⇒ Object



81
82
83
# File 'lib/as-extensions/test.rb', line 81

def assert_kind_of(k=nil, x=nil, msg=nil)
  assert x.kind_of?(k), msg
end

.assert_looksame(a, b, msg = nil) ⇒ Object



85
86
87
# File 'lib/as-extensions/test.rb', line 85

def assert_looksame(a, b, msg=nil)
  assert_equal(a.inspect, b.inspect, msg)
end

.assert_nil(x = 1, msg = nil) ⇒ Object



89
90
91
# File 'lib/as-extensions/test.rb', line 89

def assert_nil(x=1, msg=nil)
  assert x.nil?, msg
end

.assert_not_blank(x = '', msg = nil) ⇒ Object



93
94
95
# File 'lib/as-extensions/test.rb', line 93

def assert_not_blank(x='', msg=nil)
  assert !x.blank?, msg
end

.assert_not_empty(x = [], msg = nil) ⇒ Object



97
98
99
# File 'lib/as-extensions/test.rb', line 97

def assert_not_empty(x=[], msg=nil)
  assert !x.empty?, msg
end

.assert_not_equal(a, b, msg = nil) ⇒ Object



101
102
103
# File 'lib/as-extensions/test.rb', line 101

def assert_not_equal(a, b, msg=nil)
  assert a != b, msg_stack(msg, "#{b.inspect} and #{a.inspect} are equal")
end

.assert_not_include(a, b, msg = nil) ⇒ Object



105
106
107
# File 'lib/as-extensions/test.rb', line 105

def assert_not_include(a, b, msg=nil)
  assert !b.include?(a), msg_stack(msg, "#{b.inspect} includes #{a.inspect}")
end

.assert_not_nil(x = nil, msg = nil) ⇒ Object



109
110
111
# File 'lib/as-extensions/test.rb', line 109

def assert_not_nil(x=nil, msg=nil)
  assert !x.nil?, msg
end

.assert_raise(kind = Exception, msg = nil, &blk) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/as-extensions/test.rb', line 113

def assert_raise(kind=Exception, msg=nil, &blk)
  begin
    yield
  rescue Exception => e
    assert_kind_of kind, e, "Expected #{kind.to_s} but #{e.to_s} was raised instead"
    return true
  end
  assert_fail msg_stack(msg, 'No exception raised.')
end

.assert_true(b = nil, msg = nil) ⇒ Object



123
124
125
# File 'lib/as-extensions/test.rb', line 123

def assert_true(b=nil, msg=nil)
  assert b == true, msg
end

.debug(s) ⇒ Object



127
128
129
# File 'lib/as-extensions/test.rb', line 127

def debug(s)
  puts s if CONFIG[:verbose]
end

.isolate(msg = nil, &blk) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/as-extensions/test.rb', line 131

def isolate(msg=nil, &blk)
  fork do
    yield
    exit!(47) if Test::CONFIG[:nb_nok] > 0 # propagate failure
  end
  child_pid = Process.wait
  msg = msg.is_a?(::String) ? "Failure in isolation jail \"#{msg}\"" : nil
  assert_equal 0, $?.exitstatus, msg_stack(msg, "Process in isolation returned #{$?.exitstatus}")
end

.msg_stack(msg1 = nil, msg2 = nil) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/as-extensions/test.rb', line 141

def msg_stack(msg1=nil, msg2=nil)
  if msg1
    if msg2 then "#{msg1}\nERROR ==> #{msg2}"
    else msg1 end
  elsif msg2 then msg2
  else nil end
end

.stats(mute = nil) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/as-extensions/test.rb', line 149

def stats(mute=nil)
  CONFIG[:mute_stats] = mute if !mute.nil?
  return if CONFIG[:mute_stats]
  puts "=== TEST STATS ==="
  puts "OK: #{CONFIG[:nb_ok]}"
  puts "NOK: #{CONFIG[:nb_nok]}"
end