-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controls): Adding wrap controls
Wrap controls allows function calls wrapping to ease testing generators
- Loading branch information
1 parent
67876b7
commit 74e4020
Showing
5 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import is from '../utils/is' | ||
|
||
export const call = (value, next, iterate, yieldNext, raiseNext) => { | ||
if (!is.call(value)) return false | ||
try { | ||
next(value.func.apply(value.context, value.args)) | ||
} catch (err) { | ||
raiseNext(err) | ||
} | ||
return true | ||
} | ||
|
||
export const cps = (value, next, iterate, yieldNext, raiseNext) => { | ||
if (!is.cps(value)) return false | ||
value.func.call(null, ...value.args, (err, result) => { | ||
if (err) raiseNext(err) | ||
else next(result) | ||
}) | ||
return true | ||
} | ||
|
||
export default [call, cps] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'babel-polyfill' | ||
import expect from 'expect' | ||
import create from '../../src/create' | ||
import * as wrap from '../../src/controls/wrap' | ||
import {invoke, call, apply, cps} from '../../src/utils/helpers' | ||
|
||
describe('Wrap Controls', () => { | ||
it('call control using invoke', () => { | ||
let output | ||
const func = input => input | ||
const generator = function* () { | ||
output = yield invoke(func, 'invoked') | ||
} | ||
const runtime = create([wrap.call]) | ||
const expected = 'invoked' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
|
||
it('call control using call', () => { | ||
let output | ||
const context = { a: 'b' } | ||
const func = function(input) { | ||
return this.a + ' ' + input | ||
} | ||
const generator = function* () { | ||
output = yield call(func, context, 'invoked') | ||
} | ||
const runtime = create([wrap.call]) | ||
const expected = 'b invoked' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
|
||
it('call control using apply', () => { | ||
let output | ||
const context = { a: 'b' } | ||
const func = function(input) { | ||
return this.a + ' ' + input | ||
} | ||
const generator = function* () { | ||
output = yield apply(func, context, ['invoked']) | ||
} | ||
const runtime = create([wrap.call]) | ||
const expected = 'b invoked' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
|
||
it('call control with error', () => { | ||
let output | ||
const func = () => { throw 'error' } | ||
const generator = function* () { | ||
try { | ||
output = yield invoke(func) | ||
} catch (err) { | ||
output = err | ||
} | ||
} | ||
const runtime = create([wrap.call]) | ||
const expected = 'error' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
|
||
it('cps control', () => { | ||
let output | ||
const func = (input, callback) => { | ||
callback(false, input) | ||
} | ||
const generator = function* () { | ||
output = yield cps(func, 'invoked') | ||
} | ||
const runtime = create([wrap.cps]) | ||
const expected = 'invoked' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
|
||
it('cps control with error', () => { | ||
let output | ||
const func = (input, callback) => { | ||
callback('error', input) | ||
} | ||
const generator = function* () { | ||
try { | ||
output = yield cps(func, 'invoked') | ||
} catch (err) { | ||
output = err | ||
} | ||
} | ||
const runtime = create([wrap.cps]) | ||
const expected = 'error' | ||
|
||
runtime(generator) | ||
expect(output).toEqual(expected) | ||
}) | ||
}) |