translated specs to busted

This commit is contained in:
kikito 2013-09-25 15:18:50 +02:00
parent 4e973f7a60
commit 3e1d11a95e
2 changed files with 44 additions and 43 deletions

View File

@ -91,8 +91,9 @@ Also, make sure to read the license file; the text of that license file must app
Specs Specs
===== =====
This project uses "telescope":https://github.com/norman/telescope for its specs. If you want to run the specs, you will have to install telescope first. Then run: This project uses [busted](https://olivinelabs.com/busted) for its specs. If you want to run the specs, you will have to install it first. Then run:
tsc spec/* cd path/where/the/spec/folder/is
busted

View File

@ -9,16 +9,16 @@ describe( 'cron', function()
end end
local countable = setmetatable({}, {__call = count}) local countable = setmetatable({}, {__call = count})
before(function() before_each(function()
counter = 0 counter = 0
cron.reset() cron.reset()
end) end)
describe('.update', function() describe('.update', function()
it('throws an error if dt is a negative number', function() it('throws an error if dt is a negative number', function()
assert_error(function() cron.update() end) assert.error(function() cron.update() end)
assert_error(function() cron.update(-1) end) assert.error(function() cron.update(-1) end)
assert_not_error(function() cron.update(1) end) assert.not_error(function() cron.update(1) end)
end) end)
end) end)
@ -27,85 +27,85 @@ describe( 'cron', function()
cron.after(1, count) cron.after(1, count)
cron.after(2, count) cron.after(2, count)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.reset() cron.reset()
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
end) end)
it('Cancels all periodical actions', function() it('Cancels all periodical actions', function()
cron.every(1, count) cron.every(1, count)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.reset() cron.reset()
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
end) end)
end) end)
describe('.after', function() describe('.after', function()
it('Throws error if time is not a positive number, or callback is not callable', function() it('Throws error if time is not a positive number, or callback is not callable', function()
assert_error(function() cron.after('error', count) end) assert.error(function() cron.after('error', count) end)
assert_error(function() cron.after(2, 'error') end) assert.error(function() cron.after(2, 'error') end)
assert_error(function() cron.after(-2, count) end) assert.error(function() cron.after(-2, count) end)
assert_error(function() cron.after(2, {}) end) assert.error(function() cron.after(2, {}) end)
assert_not_error(function() cron.after(2, count) end) assert.not_error(function() cron.after(2, count) end)
assert_not_error(function() cron.after(2, countable) end) assert.not_error(function() cron.after(2, countable) end)
end) end)
it('Executes timed actions only once, at the right time', function() it('Executes timed actions only once, at the right time', function()
cron.after(2, count) cron.after(2, count)
cron.after(4, count) cron.after(4, count)
cron.update(1) cron.update(1)
assert_equal(counter, 0) assert.equal(counter, 0)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.update(1) cron.update(1)
assert_equal(counter, 2) assert.equal(counter, 2)
end) end)
it('Passes on parameters to the function, if specified', function() it('Passes on parameters to the function, if specified', function()
cron.after(1, count, 2) cron.after(1, count, 2)
cron.update(1) cron.update(1)
assert_equal(counter, 2) assert.equal(counter, 2)
end) end)
end) end)
describe('.every', function() describe('.every', function()
it('Throws errors if time is not a positive number, or callback is not function', function() it('Throws errors if time is not a positive number, or callback is not function', function()
assert_error(function() cron.every('error', count) end) assert.error(function() cron.every('error', count) end)
assert_error(function() cron.every(2, 'error') end) assert.error(function() cron.every(2, 'error') end)
assert_error(function() cron.every(-2, count) end) assert.error(function() cron.every(-2, count) end)
assert_error(function() cron.every(-2, {}) end) assert.error(function() cron.every(-2, {}) end)
assert_not_error(function() cron.every(2, count) end) assert.not_error(function() cron.every(2, count) end)
assert_not_error(function() cron.every(2, countable) end) assert.not_error(function() cron.every(2, countable) end)
end) end)
it('Executes periodical actions periodically', function() it('Executes periodical actions periodically', function()
cron.every(3, count) cron.every(3, count)
cron.update(1) cron.update(1)
assert_equal(counter, 0) assert.equal(counter, 0)
cron.update(2) cron.update(2)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.update(2) cron.update(2)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.update(1) cron.update(1)
assert_equal(counter, 2) assert.equal(counter, 2)
end) end)
it('Executes the same action multiple times on a single update if appropiate', function() it('Executes the same action multiple times on a single update if appropiate', function()
cron.every(1, count) cron.every(1, count)
cron.update(2) cron.update(2)
assert_equal(counter, 2) assert.equal(counter, 2)
end) end)
it('Respects parameters', function() it('Respects parameters', function()
cron.every(1, count, 2) cron.every(1, count, 2)
cron.update(2) cron.update(2)
assert_equal(counter, 4) assert.equal(counter, 4)
end) end)
end) end)
@ -113,50 +113,50 @@ describe( 'cron', function()
it('Cancels timed entries', function() it('Cancels timed entries', function()
local id = cron.after(1, count) local id = cron.after(1, count)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.cancel(id) cron.cancel(id)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
end) end)
it('Cancels periodical entries', function() it('Cancels periodical entries', function()
local id = cron.every(1, count) local id = cron.every(1, count)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
cron.cancel(id) cron.cancel(id)
cron.update(1) cron.update(1)
assert_equal(counter, 1) assert.equal(counter, 1)
end) end)
end) end)
describe('.tagged', function() describe('.tagged', function()
before(function() before_each(function()
cron.tagged('hello').every(5, count) -- A cron.tagged('hello').every(5, count) -- A
cron.tagged('hello').after(2, count) -- B cron.tagged('hello').after(2, count) -- B
cron.every(1, count) -- C cron.every(1, count) -- C
end) end)
it('requires at least one tag', function() it('requires at least one tag', function()
assert_error(cron.tagged) assert.error(cron.tagged)
end) end)
it('filters update', function() it('filters update', function()
cron.tagged('hello').update(5) cron.tagged('hello').update(5)
assert_equal(counter, 2) -- A + B, but not C assert.equal(counter, 2) -- A + B, but not C
end) end)
it('filters cancel', function() it('filters cancel', function()
cron.tagged('hello', 'girl').every(5, count) -- D cron.tagged('hello', 'girl').every(5, count) -- D
cron.tagged('hello').update(5) -- A + B + D - C cron.tagged('hello').update(5) -- A + B + D - C
assert_equal(counter, 3) assert.equal(counter, 3)
cron.tagged('girl').cancel() cron.tagged('girl').cancel()
cron.tagged('hello').update(5) -- A + B - C cron.tagged('hello').update(5) -- A + B - C
assert_equal(counter, 4) assert.equal(counter, 4)
cron.tagged('girl').update(5) -- nothing (D is cancelled) cron.tagged('girl').update(5) -- nothing (D is cancelled)
assert_equal(counter, 4) assert.equal(counter, 4)
end) end)
end) end)