Browse files

first test framework for noscript

  • Loading branch information...
1 parent 82d2e6b commit 5522b25906d532eb0f39f158b62a7475360e0f8c @txus committed Sep 20, 2011
Showing with 76 additions and 39 deletions.
  1. +52 −14 kernel/test.ns
  2. +24 −25 test/kernel/test_test.rb
View
66 kernel/test.ns
@@ -1,17 +1,55 @@
-Test = Object.clone()
+TestCase = Object.clone({
+ setup: ->; end,
+ teardown: ->; end,
-Test.assert = -> value
- if value != true
- raise('Expected true, got #{value}.')
- else
- true
- end
-end
+ errors: [],
+
+ tests: 0,
+ assertions: 0,
+ failures: 0,
+
+ assert: -> value
+ if value != false
+ print('\e[32m.')
+ else
+ print('\e[31mF')
+ @errors.push('Expected #{value} to be truthy.')
+ @failures = @failures + 1
+ end
+ @assertions = @assertions + 1
+ end,
+
+ assert equal: -> expected, actual
+ if expected == actual
+ print('\e[32m.')
+ else
+ print('\e[31mF')
+ @errors.push('Expected #{expected}, got #{actual}.')
+ @failures = @failures + 1
+ end
+ @assertions = @assertions + 1
+ end,
+
+ print report: ->
+ puts('\n\n\e[0m#{@tests} tests, #{@assertions} assertions, #{@failures} failures')
+ puts(' Failure report:')
+ @errors.each(-> error
+ puts(' * #{error}')
+ end)
+ end,
+
+ run: ->
+ @setup()
+
+ @each(-> name, test
+ if name.starts with('it')
+ test()
+ @tests = @tests + 1
+ end
+ end)
+
+ @teardown()
-Test.assert equal = -> expected, actual
- if expected != actual
- raise('Expected #{expected}, got #{actual}.')
- else
- true
+ @print report()
end
-end
+})
View
49 test/kernel/test_test.rb
@@ -2,41 +2,40 @@
class TestTest < MiniTest::Unit::TestCase
- def test_assert_returns_true
+ def test_test
code = <<-CODE
- foo = true
- Test.assert(foo)
-CODE
- assert_equal Noscript::AST::True.new, compiles(code)
- end
+ MyTestCase = TestCase.clone({
- def test_assert_raises
- code = <<-CODE
- foo = false
- Test.assert(foo)
-CODE
+ setup: ->
+ @foo = 'bar'
+ end,
- assert_raises Noscript::AST::Exception do
- compiles(code)
- end
- end
+ it does foo things: ->
+ @assert equal(@foo, 'bar')
+ end,
- def test_assert_equal_returns_true
- code = <<-CODE
- Test.assert equal(1, 1)
-CODE
+ it does bar things: ->
+ @assert(true)
+ end,
- assert_equal Noscript::AST::True.new, compiles(code)
- end
+ it can fail too: ->
+ @assert(false)
+ end,
- def test_assert_equal_raises
- code = <<-CODE
- Test.assert equal(3, 2)
+ teardown: ->
+ @foo = nil
+ end
+
+ })
+
+ MyTestCase.run()
CODE
- assert_raises Noscript::AST::Exception do
+ expected = "\e[32m.\e[32m.\e[31mF\n\n\e[0m3 tests, 3 assertions, 1 failures\n Failure report:\n * Expected false to be truthy.\n"
+ assert_output expected do
compiles(code)
end
end
+
end

0 comments on commit 5522b25

Please sign in to comment.