Skip to content

Instantly share code, notes, and snippets.

@weakish
Created July 22, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weakish/2fa4aadaeebb2d726c977131ada2a5be to your computer and use it in GitHub Desktop.
Save weakish/2fa4aadaeebb2d726c977131ada2a5be to your computer and use it in GitHub Desktop.
An attempt to #port #gambiarra / #klud.js #minimal #unit-testing to #Ruby
# An attempt to port [gambiarra/klud.js][t] to Ruby.
#
# [t]: http://zserge.com/blog/minimal-testing.html
currentTest = nil
expected = []
actual = []
define_method(:tesuto, ->(e, test_function, msg) do
currentTest = {}
if e == 'begin'
currentTest = {
name: test_function,
pass: [],
fail: [],
}
elsif == 'end'
actual.push currentTest
elsif == 'pass'
currentTest[:pass].push msg
elsif == 'fail'
currentTest.[:fail].push msg
elsif == 'except'
puts "PANIC: #{test_function} #{msg}"
elsif == 'finalize'
nil
end
end)
define_method(:metatest,
->(name, f, expectedPassed, expectedFailed, async=false) do
tesuto name, f, async
expected.push({
name: name
pass: expectedPassed
fail: expectedFailed
})
end
)
#
# ok() tests
#
metatest(
'simple assert passes',
->() do
ok 2 == 2, '2==2'
end,
[ '2==2' ],
[]
)
metatest('simple assert fails', ->() do
ok 1 == 2, '1!=2'
end, [], [ '1!=2' ]
)
metatest('eq nil', ->() do
ok nil == nil, 'nil == nil'
end, ['nil == nil'], []
)
metatest('eq strings and numbers', ->() do
ok 'foo' == 'foo', 'str == str'
ok '2' == "#{1 + 1}", 'single-quoted == double-quoted'
ok 'foo' != 'bar', 'str != str'
ok 123 == 123, 'num == num'
ok 123 != 12, 'num != num'
end, [
'str == str',
'single-quoted == double-quoted',
'str != str',
'num == num',
'num != num'
], []
)
metatest('eq arrays', ->() do
ok [] == [], 'empty'
ok [1, 2] == [1, 2], 'equal'
ok [1, 2] != [2, 1], 'swapped'
ok [1, 2] != [1, 2, 3], 'shorter'
end, %w(empty equal swapped shorter), []
)
metatest('eq hash tables', ->() do
ok({} == {}, 'empty')
ok({a:1,b:2} == {a:1,b:2}, 'equal')
ok({b:2,a:1} == {a:1,b:2}, 'swapped')
ok({a:1,b:2} == {a:1,b:3}, 'not equal')
end, ['empty', 'equal', 'swapped', 'not equal'], []
)
metatest('eq nested tables', ->() do
ok({1 => {name:'mhc',age:28},2 => {name:'arb',age:26}} == {1 => {name:'mhc',age:28},2 => {name:'arb',age:26}}, 'equal')
ok({1 => {name:'mhc',age:28},2 => {name:'arb',age:26}} == {1 => {name:'mhc',age:28},2 => {name:'arb',age:27}}, 'not equal')
end, ['equal', 'not equal'], []
)
#
# spy() tests
#
metatest(
'spy called',
->() do
f = spy()
ok f.called.length == 0, 'not called'
f()
ok f.called.length > 0, 'called'
ok f.called.length == 1, 'called once'
f()
ok f.called.length == 2, 'called twice'
end,
[
'not called',
'called',
'called once'
'called twice'
],
[]
)
metatest('spy with arguments', ->() do
x = 0
f = spy(setX)
setX = ->(n) do
x = n
end
f 1
ok x == 1, 'x == 1'
ok f.called == [ [ 1 ] ], 'called with 1'
f 42
ok x == 42, 'x == 42'
ok f.called == [[1], [42]], 'called with 42'
end, [
'x == 1',
'called with 1',
'x == 42',
'called with 42'
], []
)
metatest('spy with exception', ->() {
throwSomething = ->(s)
if s != 'nopanic'
throw 'panic: ' + s
f = spy(throwSomething)
f 'nopanic'
f 'foo'
ok f.called == [['nopanic'], ['foo']], 'args ok'
ok f.thrown == [nil, 'panic: foo'], 'thrown ok'
}, [
'args ok'
'thrown ok'
], []
)
metatest('another spy with exception', ->() {
f = spy(->() {
a = unknownVariable
})
f()
ok f.thrown.length == 1, 'exception thrown'
}, [ 'exception thrown' ], []
)
metatest('spy should return a value', ->() {
f = spy(->() {
5
})
ok f() == 5, 'spy returns a value'
g = spy()
ok g() == nil, 'default spy returns nil'
}, [
'spy returns a value',
'default spy returns undefined'
], []
)
metatest('async test', (->(done) do
fiber = new Fiber do
ok true 'foo'
Fiber.yield
ok true, 'foobar'
done
end
fiber.resume
ok true, 'bar'
fiber.resume
end, %w(foo bar foobar), [], true
)
metatest('code after done will be skipped', (->))
metatest 'async test without actually async', ((next) ->
ok true, 'bar'
next()
return
), [ 'bar' ], [], true
metatest 'finalize', ((next) ->
# Analyze test results, length-1, since current expected results are ignored
i = 0
while i < expected.length - 1
if !expected[i].pass.compare(actual[i].pass)
console.log '✘', expected[i].name, expected[i].pass, actual[i].pass
elsif!expected[i].fail.compare(actual[i].fail)
console.log '✘', expected[i].name, expected[i].fail, actual[i].fail
else
console.log '✔', expected[i].name
i++
next()
return
), [], [], true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment