Commit 520b0f73 authored by Daniel Bovensiepen's avatar Daniel Bovensiepen

Implement fallback print method for testing environment in case mruby-print GEM wasn't included.

parent 8e456308
...@@ -4,6 +4,20 @@ $kill_test = 0 ...@@ -4,6 +4,20 @@ $kill_test = 0
$asserts = [] $asserts = []
$test_start = Time.now if Object.const_defined?(:Time) $test_start = Time.now if Object.const_defined?(:Time)
# Implementation of print due to the reason that there might be no print
def t_print(*args)
i = 0
len = args.size
while i < len
begin
__printstr__ args[i].to_s
rescue NoMethodError
__t_printstr__ args[i].to_s
end
i += 1
end
end
## ##
# Create the assertion in a readable way # Create the assertion in a readable way
def assertion_string(err, str, iso=nil, e=nil) def assertion_string(err, str, iso=nil, e=nil)
...@@ -28,31 +42,31 @@ end ...@@ -28,31 +42,31 @@ end
# which will be tested by this # which will be tested by this
# assertion # assertion
def assert(str = 'Assertion failed', iso = '') def assert(str = 'Assertion failed', iso = '')
print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose t_print(str, (iso != '' ? " [#{iso}]" : ''), ' : ') if $mrbtest_verbose
begin begin
$mrbtest_assert = [] $mrbtest_assert = []
$mrbtest_assert_idx = 0 $mrbtest_assert_idx = 0
if(!yield || $mrbtest_assert.size > 0) if(!yield || $mrbtest_assert.size > 0)
$asserts.push(assertion_string('Fail: ', str, iso, nil)) $asserts.push(assertion_string('Fail: ', str, iso, nil))
$ko_test += 1 $ko_test += 1
print('F') t_print('F')
else else
$ok_test += 1 $ok_test += 1
print('.') t_print('.')
end end
rescue Exception => e rescue Exception => e
if e.class.to_s == 'MRubyTestSkip' if e.class.to_s == 'MRubyTestSkip'
$asserts.push "Skip: #{str} #{iso} #{e.cause}" $asserts.push "Skip: #{str} #{iso} #{e.cause}"
print('?') t_print('?')
else else
$asserts.push(assertion_string('Error: ', str, iso, e)) $asserts.push(assertion_string('Error: ', str, iso, e))
$kill_test += 1 $kill_test += 1
print('X') t_print('X')
end end
ensure ensure
$mrbtest_assert = nil $mrbtest_assert = nil
end end
print("\n") if $mrbtest_verbose t_print("\n") if $mrbtest_verbose
end end
def assertion_diff(exp, act) def assertion_diff(exp, act)
...@@ -125,31 +139,21 @@ end ...@@ -125,31 +139,21 @@ end
# Report the test result and print all assertions # Report the test result and print all assertions
# which were reported broken. # which were reported broken.
def report() def report()
print "\n" t_print("\n")
$asserts.each do |msg| $asserts.each do |msg|
puts msg puts msg
end end
$total_test = $ok_test.+($ko_test) $total_test = $ok_test.+($ko_test)
print('Total: ') t_print("Total: #{$total_test}\n")
print($total_test)
print("\n") t_print(" OK: #{$ok_test}\n")
t_print(" KO: #{$ko_test}\n")
print(' OK: ') t_print("Crash: #{$kill_test}\n")
print($ok_test)
print("\n")
print(' KO: ')
print($ko_test)
print("\n")
print('Crash: ')
print($kill_test)
print("\n")
if Object.const_defined?(:Time) if Object.const_defined?(:Time)
print(' Time: ') t_print(" Time: #{Time.now - $test_start} seconds\n")
print(Time.now - $test_start)
print(" seconds\n")
end end
end end
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <mruby/proc.h> #include <mruby/proc.h>
#include <mruby/data.h> #include <mruby/data.h>
#include <mruby/compile.h> #include <mruby/compile.h>
#include <mruby/string.h>
#include <mruby/variable.h> #include <mruby/variable.h>
void void
...@@ -59,6 +60,32 @@ eval_test(mrb_state *mrb) ...@@ -59,6 +60,32 @@ eval_test(mrb_state *mrb)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
static void
t_printstr(mrb_state *mrb, mrb_value obj)
{
struct RString *str;
char *s;
int len;
if (mrb_string_p(obj)) {
str = mrb_str_ptr(obj);
s = str->ptr;
len = str->len;
fwrite(s, len, 1, stdout);
}
}
mrb_value
mrb_t_printstr(mrb_state *mrb, mrb_value self)
{
mrb_value argv;
mrb_get_args(mrb, "o", &argv);
t_printstr(mrb, argv);
return argv;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
...@@ -79,6 +106,10 @@ main(int argc, char **argv) ...@@ -79,6 +106,10 @@ main(int argc, char **argv)
mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_verbose"), mrb_true_value()); mrb_gv_set(mrb, mrb_intern(mrb, "$mrbtest_verbose"), mrb_true_value());
} }
struct RClass *krn;
krn = mrb->kernel_module;
mrb_define_method(mrb, krn, "__t_printstr__", mrb_t_printstr, MRB_ARGS_REQ(1));
mrb_init_mrbtest(mrb); mrb_init_mrbtest(mrb);
ret = eval_test(mrb); ret = eval_test(mrb);
mrb_close(mrb); mrb_close(mrb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment