Permalink
Browse files

Separate results logic and avoid mixin pollution

Extracting results building logic out of the at_exit block leaves at
least some chance of extention.

Also, constants were removed since they are unnecessarily included in
the base class when NanoTest is mixed in. The include should only add
the #assert method.
  • Loading branch information...
1 parent d9ae669 commit 475396220992155b8a54e6c634de6ab0c5ba2967 @mynyml committed Nov 27, 2009
Showing with 71 additions and 37 deletions.
  1. +9 −6 README.rdoc
  2. +9 −5 lib/nanotest.rb
  3. +53 −26 test/test_nanotest.rb
View
@@ -10,17 +10,20 @@ the bare mynymum needed; for everything else, there's ruby.
=== Examples
require 'nanotest'
- include NanoTest
+ include NanoTest
- assert { 1 == 1 }
- assert { 1 >= 1 }
- assert { 1 == 2 } #line 12
+ assert { 1 == 1 }
+ assert { 2 > 1 }
+ assert { not 1 > 2 }
+ assert { 1 == 2 } #line 12
outputs:
- ..F
+ ...F
(examples.rb:012) assertion failed
+There's also a real life example at http://github.com/mynyml/phocus/blob/master/test/test_phocus.rb
+
=== API
NanoTest has a single method: #assert. You can either include NanoTest as
@@ -42,7 +45,7 @@ fancy, check out the wiki for a few tips and tricks.
=== Stats
$ rake -s loc
- lib files contain 15 SLOCs
+ lib files contain 18 SLOCs
=== Links
View
@@ -1,18 +1,22 @@
module NanoTest
extend self
- FAILURES = []
+ @@failures, @@dots = [], []
def assert(msg="assertion failed", file=nil, line=nil, &block)
unless block.call
file ||= caller.first.split(':')[0]
line ||= caller.first.split(':')[1]
- FAILURES << "(%s:%0.3d) %s" % [file,line,msg]
- print 'F'
+ @@failures << "(%s:%0.3d) %s" % [file,line,msg]
+ @@dots << 'F'
else
- print '.'
+ @@dots << '.'
end
end
- at_exit { puts; FAILURES.each {|f| puts f } }
+ def self.results
+ @@dots.join + "\n" + @@failures.join("\n")
+ end
+
+ at_exit { puts results unless results.strip.empty? }
end
View
@@ -1,53 +1,80 @@
+require 'minitest/autorun'
require 'test/test_helper'
+module NanoTest
+ class << self
+ def failures() @@failures end
+ def dots() @@dots end
+ end
+
+ # don't autorun
+ def at_exit() end
+end
+require 'nanotest'
+
+# fixture class for nanotest mixin
+class Foo
+ include NanoTest
+end
+
class TestNanoTest < MiniTest::Unit::TestCase
+ def self.test(name, &block)
+ define_method("test_#{name.gsub(/\s/,'_')}", &block)
+ end
def teardown
- NanoTest::FAILURES.clear
+ NanoTest::dots.clear
+ NanoTest::failures.clear
end
test "api" do
- class Foo; include NanoTest end
assert_respond_to Foo.new, :assert
assert_respond_to NanoTest, :assert
end
test "assertion passes" do
- out, err = capture_io do
- NanoTest.assert { true }
- end
- assert_equal '.', out
- assert_empty NanoTest::FAILURES
+ NanoTest.assert { true }
+ assert_equal '.', NanoTest::dots.last
+ assert_empty NanoTest::failures
end
test "assertion fails (false)" do
- out, err = capture_io do
- NanoTest.assert { false }
- end
- assert_equal 'F', out
- refute_empty NanoTest::FAILURES
+ NanoTest.assert { false }
+ assert_equal 'F', NanoTest::dots.last
+ refute_empty NanoTest::failures
end
test "assertion fails (nil)" do
- out, err = capture_io do
- NanoTest.assert { nil }
- end
- assert_equal 'F', out
- refute_empty NanoTest::FAILURES
+ NanoTest.assert { nil }
+ assert_equal 'F', NanoTest::dots.last
+ refute_empty NanoTest::failures
end
test "failure message" do
- capture_io do
- @line = __LINE__; NanoTest.assert { false }
- end
- assert_equal 1, NanoTest::FAILURES.size
- assert_includes NanoTest::FAILURES, "(%s:%0.3d) assertion failed" % [__FILE__, @line]
+ @line = __LINE__; NanoTest.assert { false }
+ assert_equal 1, NanoTest::failures.size
+ assert_includes NanoTest::failures, "(%s:%0.3d) assertion failed" % [__FILE__, @line]
end
test "custom failure message, file, line" do
- capture_io do
- NanoTest.assert('foo','bar',2) { false }
- end
- assert_includes NanoTest::FAILURES, "(bar:002) foo"
+ NanoTest.assert('foo','bar',2) { false }
+ assert_includes NanoTest::failures, "(bar:002) foo"
+ end
+
+ test "displays results" do
+ NanoTest.assert { true }
+ NanoTest.assert { false }; line1 = __LINE__
+ NanoTest.assert { false }; line2 = __LINE__
+ expected = <<-OUT.gsub(/^\s*/,'').strip % [__FILE__, line1, __FILE__, line2]
+ .FF
+ (%s:%0.3d) assertion failed
+ (%s:%0.3d) assertion failed
+ OUT
+ assert_equal expected, NanoTest.results
+ end
+
+ test "displays results with no assertions" do
+ assert_empty NanoTest.results.strip
end
end
+

0 comments on commit 4753962

Please sign in to comment.