micro_test 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -22,7 +22,7 @@ gem install micro_test
22
22
 
23
23
  ## API
24
24
 
25
- * Tests subclass `MicroTest::Test`
25
+ * Tests should subclass `MicroTest::Test`
26
26
  * Define tests with `test "description" do ...`
27
27
  * Setup with `before [:all|:each] do ...`
28
28
  * Teardown with `after [:all|:each] do ...`
@@ -80,7 +80,7 @@ class MathTest < MicroTest::Test
80
80
  end
81
81
  ```
82
82
 
83
- Run all tests in the test dir, relative to the current dir.
83
+ Run all tests in the "test" directory _(relative to the current directory)_.
84
84
 
85
85
  ```bash
86
86
  $ mt
@@ -0,0 +1,53 @@
1
+ module MicroTest
2
+ class Formatter
3
+ def initialize
4
+ @total = 0
5
+ @passed = 0
6
+ @failed = 0
7
+ end
8
+
9
+ def header
10
+ puts "Testing in progress...\n\n"
11
+ end
12
+
13
+ def group(name)
14
+ puts name
15
+ end
16
+
17
+ def test(info)
18
+ @total += 1
19
+ info[:passed] ? @passed += 1 : @failed += 1
20
+ print "- #{info[:name]} "
21
+ info[:asserts].each do |assert|
22
+ if assert[:pass]
23
+ puts green(:PASS)
24
+ else
25
+ puts red(:FAIL)
26
+ puts " #{red assert[:line]}"
27
+ puts " #{red assert[:path]}:#{yellow assert[:line_num]}"
28
+ end
29
+ end
30
+ end
31
+
32
+ def footer
33
+ puts "\n---"
34
+ puts "Total: #{@total}, Passed: #{green @passed}, Failed: #{red @failed}"
35
+ puts "---"
36
+ end
37
+
38
+ private
39
+
40
+ def red(text)
41
+ "\e[31m#{text}\e[0m"
42
+ end
43
+
44
+ def yellow(text)
45
+ "\e[33m#{text}\e[0m"
46
+ end
47
+
48
+ def green(text)
49
+ "\e[32m#{text}\e[0m"
50
+ end
51
+
52
+ end
53
+ end
@@ -1,19 +1,17 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "formatter"))
2
+
1
3
  module MicroTest
2
4
  module Runner
3
5
  class << self
4
6
 
5
- def red(text)
6
- "\e[31m#{text}\e[0m"
7
- end
8
-
9
- def green(text)
10
- "\e[32m#{text}\e[0m"
11
- end
12
-
13
7
  def update(event, arg)
14
8
  send event, arg
15
9
  end
16
10
 
11
+ def formatter
12
+ @formatter ||= MicroTest::Formatter.new
13
+ end
14
+
17
15
  def add_test_class(klass)
18
16
  test_classes << klass
19
17
  end
@@ -41,36 +39,31 @@ module MicroTest
41
39
  end
42
40
 
43
41
  def assert(value)
44
- info = file_info(caller[6])
45
- if value
46
- puts " #{green :PASS}: #{info[:line]}"
47
- @passed += 1
48
- else
49
- puts " #{red :FAIL}: #{red info[:line]}"
50
- puts " #{info[:path]}:#{info[:line_num]}"
51
- @failed += 1
52
- end
42
+ @failed ||= !value
43
+ @asserts << file_info(caller[6]).merge(:pass => value)
53
44
  end
54
45
 
55
- def run
56
- @passed = @failed = 0
57
- @test_classes.shuffle.each do |test_class|
58
- before = test_class.callbacks[:before]
59
- after = test_class.callbacks[:after]
46
+ def run(f=nil)
47
+ @formatter = f
48
+ formatter.header
49
+
50
+ test_classes.shuffle.each do |test_class|
51
+ formatter.group test_class
52
+ test_class.invoke :before, :all
60
53
 
61
- puts test_class.name
62
- before[:all].call if before[:all]
63
54
  test_class.tests.keys.shuffle.each do |desc|
64
- before[:each].call if before[:each]
65
- puts "- test #{desc}"
55
+ @failed = false
56
+ @asserts = []
57
+ test_class.invoke :before, :each
66
58
  test_class.tests[desc].call
67
- after[:each].call if after[:each]
59
+ formatter.test(:name => desc, :passed => !@failed, :asserts => @asserts)
60
+ test_class.invoke :after, :each
68
61
  end
69
- after[:all].call if after[:all]
62
+
63
+ test_class.invoke :after, :all
70
64
  end
71
- puts "---"
72
- puts "Passed: #{green @passed}, Failed: #{red @failed}"
73
- puts "---"
65
+
66
+ formatter.footer
74
67
  end
75
68
 
76
69
  end
@@ -18,6 +18,10 @@ module MicroTest
18
18
  @callbacks ||= { :before => {}, :after => {} }
19
19
  end
20
20
 
21
+ def invoke(which, what)
22
+ callbacks[which][what].call if callbacks[which][what]
23
+ end
24
+
21
25
  def before(what, &block)
22
26
  callbacks[:before][what] = block
23
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! ' A tiny testing framework.
15
15
 
@@ -21,6 +21,7 @@ executables:
21
21
  extensions: []
22
22
  extra_rdoc_files: []
23
23
  files:
24
+ - lib/micro_test/formatter.rb
24
25
  - lib/micro_test/runner.rb
25
26
  - lib/micro_test/test.rb
26
27
  - lib/micro_test.rb