1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#!/usr/bin/env ruby
#
# A simple function to create useful asserts
#
#
#
#
#
#
# Exception raised when some swig binding test fails
#
class SwigRubyError < RuntimeError
end
#
# Asserts whether a and b are equal.
#
# scope - optional Binding where to run the code
# msg - optional additional message to print
#
def swig_assert_equal( a, b, scope = nil, msg = nil )
a = 'nil' if a == nil
b = 'nil' if b == nil
begin
check = "#{a} == #{b}"
if scope.kind_of? Binding
ok = eval(check.to_s, scope)
else
ok = eval(check.to_s)
if !msg
msg = scope
scope = nil
end
end
rescue => e
raise
end
unless ok
valA = eval(a, scope)
valB = eval(b, scope)
raise SwigRubyError.new("FAILED EQUALITY: #{check} was #{valA} not #{valB}")
end
if $VERBOSE
$stdout.puts "\tPASSED EQUALITY #{check} #{msg}"
end
return ok
rescue => e
trace = e.backtrace[1..-1]
$stderr.puts "#{trace[0,1]}: #{e}"
if trace.size > 1
$stderr.puts "\tfrom #{trace[1..-1].join("\n\t ")}"
end
exit(1)
end
#
# Asserts whether an expression runs properly and is true
#
# scope - optional Binding where to run the code
# msg - optional additional message to print
#
def swig_assert( expr, scope = nil, msg = nil )
begin
if scope.kind_of? Binding
ok = eval(expr.to_s, scope)
else
ok = eval(expr.to_s)
msg = scope if !msg
end
rescue
raise
end
raise SwigRubyError.new("FAILED: #{expr.to_s} - #{msg}") unless ok
if $VERBOSE
$stdout.puts "\tPASSED #{expr} #{msg}"
end
rescue => e
trace = e.backtrace[1..-1]
$stderr.puts "#{trace[0,1]}: #{e}"
if trace.size > 1
$stderr.puts "\tfrom #{trace[1..-1].join("\n\t ")}"
end
exit(1)
end
#
# Asserts whether an expression runs properly
#
# scope - optional Binding where to run the code
# msg - optional additional message to print
#
def swig_eval( expr, scope = nil, msg = nil )
begin
if scope.kind_of? Binding
eval(expr.to_s, scope)
else
eval(expr.to_s)
msg = scope if !msg
end
rescue => e
raise SwigRubyError.new("Wrong assert: #{expr.to_s} - #{e}")
end
if $VERBOSE
$stdout.puts "\tPASSED #{expr} #{msg}"
end
rescue => e
trace = e.backtrace[1..-1]
$stderr.puts "#{trace[0,1]}: #{e}"
if trace.size > 1
$stderr.puts "\tfrom #{trace[1..-1].join("\n\t ")}"
end
exit(1)
end
#
# Given a set of lines as text, runs each of them, asserting them.
# Lines that are of the form:
# a == b are run with swig_assert_equal
# others are run with swig_eval.
#
# scope - optional Binding where to run the code
# msg - optional additional message to print
#
def swig_assert_each_line( lines, scope = nil, msg = nil )
lines.split("\n").each do |line|
next if line.empty? or line =~ /^\s*#.*/
if line =~ /^\s*([^\s]*)\s*==\s*(.*)\s*$/
swig_assert_equal($1, $2, scope, msg)
else
swig_eval(line, scope, msg)
end
end
end
|