spy/stub: Add support for lastCall

Also added missing license file
master
Jonatan Nilsson 2021-10-10 03:24:41 +00:00
parent c3c8169aeb
commit 7746b52cfe
4 changed files with 26 additions and 0 deletions

13
LICENSE Normal file
View File

@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

View File

@ -17,6 +17,7 @@ export function stub(returnFunc = null) {
let func = function(...args) { let func = function(...args) {
func.called = true func.called = true
calls.push(args) calls.push(args)
func.lastCall = args
if (func.callCount < indexMap.length) { if (func.callCount < indexMap.length) {
func[indexMap[func.callCount]] = args func[indexMap[func.callCount]] = args
} }
@ -25,12 +26,14 @@ export function stub(returnFunc = null) {
return returner(...args) return returner(...args)
} }
} }
func.lastCall = null
func.called = false func.called = false
func.callCount = 0 func.callCount = 0
func.onCall = function(i) { func.onCall = function(i) {
return calls[i] return calls[i]
} }
func.reset = function() { func.reset = function() {
func.lastCall = null
func.called = false func.called = false
func.callCount = 0 func.callCount = 0
for (let i = 0; i < indexMap.length; i++) { for (let i = 0; i < indexMap.length; i++) {

View File

@ -30,6 +30,7 @@
"index.mjs", "index.mjs",
"cli.mjs", "cli.mjs",
"README.md", "README.md",
"LICENSE",
"lib" "lib"
] ]
} }

View File

@ -11,6 +11,7 @@ import { spy, stub } from '../index.mjs'
assert.strictEqual(spyer.firstCall, null) assert.strictEqual(spyer.firstCall, null)
assert.strictEqual(spyer.secondCall, null) assert.strictEqual(spyer.secondCall, null)
assert.strictEqual(spyer.thirdCall, null) assert.strictEqual(spyer.thirdCall, null)
assert.strictEqual(spyer.lastCall, null)
spyer(1) spyer(1)
spyer(2) spyer(2)
@ -23,6 +24,8 @@ import { spy, stub } from '../index.mjs'
assert.notStrictEqual(spyer.firstCall, null) assert.notStrictEqual(spyer.firstCall, null)
assert.notStrictEqual(spyer.secondCall, null) assert.notStrictEqual(spyer.secondCall, null)
assert.notStrictEqual(spyer.thirdCall, null) assert.notStrictEqual(spyer.thirdCall, null)
assert.notStrictEqual(spyer.lastCall, null)
assert.strictEqual(spyer.lastCall[0], 5)
spyer.reset() spyer.reset()
@ -31,6 +34,7 @@ import { spy, stub } from '../index.mjs'
assert.strictEqual(spyer.firstCall, null) assert.strictEqual(spyer.firstCall, null)
assert.strictEqual(spyer.secondCall, null) assert.strictEqual(spyer.secondCall, null)
assert.strictEqual(spyer.thirdCall, null) assert.strictEqual(spyer.thirdCall, null)
assert.strictEqual(spyer.lastCall, null)
}) })
t.test('should keep track of call count', function() { t.test('should keep track of call count', function() {
@ -74,8 +78,12 @@ import { spy, stub } from '../index.mjs'
assert.notOk(spyer.called) assert.notOk(spyer.called)
spyer(assertFirstArgs) spyer(assertFirstArgs)
assert.strictEqual(spyer.lastCall[0], assertFirstArgs)
spyer(assertSecondArgs) spyer(assertSecondArgs)
assert.strictEqual(spyer.lastCall[0], assertSecondArgs)
spyer(assertThirdArgs[0], assertThirdArgs[1]) spyer(assertThirdArgs[0], assertThirdArgs[1])
assert.strictEqual(spyer.lastCall[0], assertThirdArgs[0])
assert.strictEqual(spyer.lastCall[1], assertThirdArgs[1])
assert.strictEqual(spyer.callCount, 3) assert.strictEqual(spyer.callCount, 3)
assert.strictEqual(spyer.onCall(0)[0], assertFirstArgs) assert.strictEqual(spyer.onCall(0)[0], assertFirstArgs)
@ -226,6 +234,7 @@ t.describe('#stub()', function() {
assert.strictEqual(s(3), 'test') assert.strictEqual(s(3), 'test')
assert.strictEqual(s(4), 'test') assert.strictEqual(s(4), 'test')
assert.strictEqual(s(5), 'test') assert.strictEqual(s(5), 'test')
assert.strictEqual(s.lastCall[0], 5)
assert.notStrictEqual(s.callCount, 0) assert.notStrictEqual(s.callCount, 0)
assert.ok(s.called) assert.ok(s.called)