Skip to content

Commit

Permalink
chore: add kotlin folder
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Jan 8, 2018
1 parent f703a59 commit 40b3d93
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions kotlin/spy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package spy
interface Error {
var name: String
var message: String
}
interface CallRecord {
var arguments: Array<Any>
var result: Any
var error: Error
}

interface Spy {
var calls: Array<CallRecord>
}

/**
* Spy on function that uses callback.
*/
fun spy<T super Function>(fn: T): T & Spy {
const calls: CallRecord[] = []
const spiedFn: T = function (...args) {
const spiedArgs = args.map(a => {
return typeof a === 'function' ? spy(a) : a
})
const call = { arguments: spiedArgs } as CallRecord
calls.push(call)
try {
const result = fn(...spiedArgs)
call.result = result
return result
}
catch (err) {
call.error = err
throw err
}
} as any

return Object.assign(spiedFn, {
calls
})
}


interface AsyncCallRecord extends Promise<any> {
arguments: any[],
throws(errback: any): Promise<any>
}

interface AsyncSpy {
calls: ReadonlyArray<AsyncCallRecord>
}

/**
* Spy on function that returns a promise.
*/
fun spyAsync<T extends Function>(fn: T): T & AsyncSpy {
const calls: AsyncCallRecord[] = []
const spiedFn: T = function (...args) {
const call = { arguments: args } as AsyncCallRecord
calls.push(call)
const result = fn(...args)
call.then = (cb, eb) => result.then(cb, eb)
call.catch = cb => (result.catch(cb))
call.throws = call.catch
return result
} as any

return Object.assign(spiedFn, {
calls
})
}

0 comments on commit 40b3d93

Please sign in to comment.