Top Level Namespace

Defined Under Namespace

Classes: String

Instance Method Summary collapse

Instance Method Details

#assert(name, object1, object2) ⇒ Bool Also known as: test

Test the subject against it’s expected return value.

Examples:

assert('String test', '1'.class, String)
assert('Fixnum test', 1.class, Fixnum) 

Parameters:

  • name (String)

    The name of the test, its used when reporting if it failed.

  • object1 (Object)

    An expression to test.

  • object2 (Object)

    The expected return value.

Returns:

  • (Bool)

    True if test passed.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_test.rb', line 35

def assert(name, object1, object2)
  ret = object1.eql?(object2)
  if ret
    $ok += 1
  else
    $fails += 1
    $fail_names.push(name)
  end
  $total += 1

  ret
end

#assert_block(name = "") ⇒ Object

Provides a block for tests that needs to be grouped togther. When the tests report is printed if the global variable $sep_switch is set to true(it is true by default), then bounddires are printed around the test, with the name of the test printed prominently.

Examples:

assert_block('Foo tests') do
  foo = Foo.new

  puts('Foo test start')
  assert('Foo test #1', foo.get, true)
  assert('Foo test #2', foo.destroy, false)
  assert('Foo test #3', foo.destroyed?, true)
  puts('Foo test end')
end

The result you may get if $sep_switch is true.

--------- 
Foo tests
--------- 
Foo test start
Foo test end
---------

Parameters:

  • name (String) (defaults to: "")

    The of the group of the tests.

See Also:



87
88
89
90
91
92
93
# File 'lib/ruby_test.rb', line 87

def assert_block(name="")
  if $sep_switch
    print_bounds(name) { yield } if $sep_switch
  else
    yield
  end
end


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_test.rb', line 50

def print_bounds(str, plus=0)
  sep_str = '-'
  str_size = str.size + plus

  str_size.times { print(sep_str.color_mode(:normal)) }
  puts(' ') # Printing newlines
  puts(str.color_mode(:normal))
  str_size.times { print(sep_str.color_mode(:normal)) }
  puts(' ')
  yield
  str_size.times { print(sep_str.color_mode(:normal)) }
  puts(' ')
end

#report(str) ⇒ Object

Prints a report of the tests above it.

Parameters:

  • str (String)

    A string to be printed as the name of the report.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby_test.rb', line 97

def report(str)
  print_bounds("Summary for: #{str}") do
    $fail_names.each { |n| puts("Failed test => #{n}".color_mode(:fail)) }
    puts("Total: #{$total}".color_mode(:normal))
    puts("Ok: #{$ok}".color_mode(:ok))
    puts("Fails: #{$fails}".color_mode(:fail))
  end

  $total = 0
  $ok = 0
  $fails = 0
end