-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrotest.rb
54 lines (44 loc) · 1.07 KB
/
microtest.rb
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
# Implementation of test run and assertions
class Microtest
TESTS = []
def self.inherited(klass)
TESTS << klass
end
def self.run_all_tests
TESTS.each(&:run)
end
def self.run
public_instance_methods.grep(/_test$/).each do |name|
new.run name
end
end
def run(name)
send name
end
# Assertions
def assert(test, message = 'Failure')
if test
puts 'Success!'
return
end
fail RuntimeError, message, caller
end
def assert_equal(expected, actual)
assert expected == actual, "Failed! assert_equal: #{expected} vs. #{actual}."
end
def assert_in_delta(expected, actual)
delta = (expected - actual).abs
assert delta <= 0.001, "Failed! assert_in_delta: #{expected} vs. #{actual}."
end
def assert_includes(collection, actual)
assert collection.include?(actual),
"Failed! assert_includes: #{actual} in #{collection}."
end
def assert_empty(object)
assert object.empty?, "Failed! assert_empty: #{object}."
end
def test(description = '')
puts "#{description}:"
yield
end
end