micro_test 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/micro_test.rb +38 -6
- metadata +1 -1
data/lib/micro_test.rb
CHANGED
@@ -14,11 +14,18 @@ module MicroTest
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.log(value)
|
17
|
+
file_path = caller[1][0, caller[1].index(/:[0-9]+:/)]
|
18
|
+
line_num = caller[1].scan(/:[0-9]+:/).first.gsub(/:/, "").to_i
|
19
|
+
@files ||= {}
|
20
|
+
@files[file_path] ||= File.open(file_path, "r").readlines.map { |l| l.gsub(/\n/, "") }
|
21
|
+
line = @files[file_path][line_num - 1].strip
|
22
|
+
|
17
23
|
if value
|
18
|
-
puts " #{green :PASS} #{
|
24
|
+
puts " #{green :PASS}: #{line}"
|
19
25
|
@passed += 1
|
20
26
|
else
|
21
|
-
puts " #{red :FAIL} #{
|
27
|
+
puts " #{red :FAIL}: #{red line}"
|
28
|
+
puts " #{file_path}:#{line_num}"
|
22
29
|
@failed += 1
|
23
30
|
end
|
24
31
|
end
|
@@ -26,13 +33,22 @@ module MicroTest
|
|
26
33
|
def self.run
|
27
34
|
@passed = @failed = 0
|
28
35
|
test_classes.each do |test_class|
|
36
|
+
before = test_class.callbacks[:before]
|
37
|
+
after = test_class.callbacks[:after]
|
38
|
+
|
29
39
|
puts test_class.name
|
40
|
+
before[:all].call if before[:all]
|
30
41
|
test_class.tests.each do |desc, block|
|
31
|
-
|
42
|
+
before[:each].call if before[:each]
|
43
|
+
puts "- test #{desc}"
|
32
44
|
block.call
|
45
|
+
after[:each].call if after[:each]
|
33
46
|
end
|
47
|
+
after[:all].call if after[:all]
|
34
48
|
end
|
35
|
-
puts "
|
49
|
+
puts "---"
|
50
|
+
puts "Passed: #{green @passed}, Failed: #{red @failed}"
|
51
|
+
puts "---"
|
36
52
|
end
|
37
53
|
end
|
38
54
|
|
@@ -41,6 +57,18 @@ module MicroTest
|
|
41
57
|
MicroTest::Runner.test_classes << subclass
|
42
58
|
end
|
43
59
|
|
60
|
+
def self.callbacks
|
61
|
+
@callbacks ||= { :before => {}, :after => {} }
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.before(what, &block)
|
65
|
+
callbacks[:before][what] = block
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.after(what, &block)
|
69
|
+
callbacks[:after][what] = block
|
70
|
+
end
|
71
|
+
|
44
72
|
def self.tests
|
45
73
|
@tests ||= {}
|
46
74
|
end
|
@@ -57,10 +85,14 @@ end
|
|
57
85
|
|
58
86
|
# class ExampleTest < MicroTest::Test
|
59
87
|
# test "booleans" do
|
60
|
-
# assert
|
61
|
-
# assert true
|
88
|
+
# assert 1 == 1
|
89
|
+
# assert true # should be true
|
62
90
|
# assert false
|
63
91
|
# end
|
92
|
+
|
93
|
+
# test "foobar" do
|
94
|
+
# assert 5 - 2 == 10 # math works
|
95
|
+
# end
|
64
96
|
# end
|
65
97
|
|
66
98
|
# MicroTest::Runner.run
|