Skip to content

Commit

Permalink
adapt to new implementation w/out direct and added nodes arg, add tes…
Browse files Browse the repository at this point in the history
…ts for coverage
  • Loading branch information
elidoran committed May 19, 2017
1 parent 1ced13a commit 43bbb64
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 41 deletions.
61 changes: 53 additions & 8 deletions test/lib/test-control.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe 'test control', ->

it 'should reset', ->

control = new Control {nodes:{}, _start:'noop'}, {}
noop = ->
control = new Control {nodes:{noop}, _start:'noop'}, {}

control._reset()

Expand All @@ -16,7 +17,7 @@ describe 'test control', ->
assert.equal control.waiting, null
assert.equal control._result, null
assert.deepEqual control._beforesAdded, []
assert.deepEqual control._next, ['noop']
assert.deepEqual control._next, [noop]


it 'should record failure', ->
Expand All @@ -25,28 +26,54 @@ describe 'test control', ->
control = new Control {nodes:{}, _start:'noop'}, context: context
control._node = 'testing2'
error = new Error 'cuz'
control.fail 'testing', error
control.fail error

assert.equal control._failed.reason, 'testing'
assert.equal control._failed.message, 'cuz'
assert.equal control._failed.node, 'testing2'
assert.equal control._failed.error, error
assert.notStrictEqual control._failed.context, context
assert.deepEqual control._failed.context, context


it 'should add details to failure error', ->

context = some: 'context'
control = new Control {nodes:{}, _start:'noop'}, context: context
control._node = 'testing2'
error = new Error 'cuz'
control.fail error, extra: 'details'

assert.equal control._failed.message, 'cuz'
assert.equal control._failed.node, 'testing2'
assert.notStrictEqual control._failed.context, context
assert.deepEqual control._failed.context, context
assert.equal control._failed.extra, 'details'

it 'should use string to create failure error', ->

control = new Control {nodes:{}, _start:'noop'}, context: {}
control.fail 'message'
assert.equal control._failed.message, 'message'

it 'should accept a failure error w/out details', ->

control = new Control {nodes:{}, _start:'noop'}, context: {}
control.fail new Error 'blah'
assert.equal control._failed.message, 'blah'


it 'should use context $copy', ->

context = some: 'context', $copy: -> some: 'copy'
control = new Control {nodes:{}, _start:'noop'}, context: context
control._node = 'testing2'
error = new Error 'cuz'
control.fail 'testing', error
control.fail error, extra: 'testing'

assert.equal control._failed.reason, 'testing'
assert.equal control._failed.message, 'cuz'
assert.equal control._failed.node, 'testing2'
assert.equal control._failed.error, error
assert.notStrictEqual control._failed.context, context
assert.deepEqual control._failed.context, some:'copy'
assert.equal control._failed.extra, 'testing'


it 'should function as done instead of data', ->
Expand All @@ -71,3 +98,21 @@ describe 'test control', ->
assert.deepEqual control._result, {} # NIL is an empty object
result = control._process()
assert.strictEqual result, null

it 'should error in _call() w/out node', ->

control = new Control {nodes:{}, _start:'noop'}, context: {}
control._call()
assert.equal control._failed?.message, 'no next node to call'

it 'should catch error in _call() when node throws', ->

control = new Control {nodes:{}, _start:'noop'}, context: {}
control._call -> throw 'error'
assert.equal control._failed?.message, 'error'

it 'should error in _prepareNext() w/out next node', ->

control = new Control {nodes:{}, _start:'noop'}, context: {}
control._prepareNext()
assert control._failed?.message, 'no next node'
1 change: 0 additions & 1 deletion test/lib/test-examples.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ describe 'run examples during', ->

it 'testing', ->
require '../../examples/strings/counter'
require '../../examples/strings/counter-direct'
require '../../examples/buffers/json-payload'
require '../../examples/transforms/math'
# require '../../examples/objects/messages'
Expand Down
191 changes: 159 additions & 32 deletions test/lib/test.coffee
Original file line number Diff line number Diff line change
@@ -1,26 +1,132 @@
{Transform} = require 'stream'
assert = require 'assert'
buildBuilder = require '../../lib'
build = require '../../lib'

describe 'test add', ->

it 'should add a node', ->

stating = build()
fn = ->
stating.add 'name', fn
assert stating.nodes.name
assert.equal stating.nodes.name, fn

it 'should get a node id from sub-property', ->

stating = build()
fn = ->
fn.id = 'name'
stating.add fn
assert stating.nodes.name
assert.equal stating.nodes.name, fn

it 'should get a node id from options sub-property', ->

stating = build()
fn = ->
fn.options = id: 'name'
stating.add fn
assert stating.nodes.name
assert.equal stating.nodes.name, fn

it 'should error without an id', ->

stating = build()
result = stating.add null, ->
assert result?.error

it 'should error without a function', ->

stating = build()
result = stating.add 'name'
assert result?.error

it 'should error when not a function (addAll)', ->

stating = build()
result = stating.addAll name: 'not a function'
assert result?.error

describe 'test before/after', ->

it 'should error for invalid "before" node (unknown name)', ->

stating = build()
result = stating.before('it').run('other')
assert result?.error

it 'should error for "after" invalid node (unknown name)', ->

stating = build()
result = stating.after('it').run('other')
assert result?.error

it 'should combine before array and new after nodes', ->

stating = build()
stating.addAll it: (->), other1: (->), other2: (->)
result = stating.before('it').run('other1')
assert.equal result, null
result = stating.before('it').run('other2')
assert.equal result, null
assert.equal stating.nodes.it.before.length, 2

it 'should combine after array and new after nodes', ->

stating = build()
stating.addAll it: (->), other1: (->), other2: (->)
result = stating.after('it').run('other1')
assert.equal result, null
result = stating.after('it').run('other2')
assert.equal result, null
assert.equal stating.nodes.it.after.length, 2


describe 'test events', ->

it 'should add events to context when set to true', ->

stating = build()
stating.add 'only', ->
executor = stating.objects events:true
assert executor?.control.events

it 'should NOT add events to context when set to false', ->

stating = build()
stating.add 'only', ->
executor = stating.objects events:false
assert.equal executor?.control.events, null

it 'should add custom events to context', ->

events = {}
stating = build()
stating.add 'only', ->
executor = stating.objects {events}
assert.equal executor?.control.events, events


describe 'test objects()', ->

greetings = []

builder = buildBuilder()
builder = build()

builder.add 'exists', (control) ->
if @input? then control.next 'has-props'
builder.add 'exists', (control, N) ->
if @input? then control.next N.hasProps
else control.fail 'input missing'

builder.add 'has-props', (control) ->
builder.add 'hasProps', (control, N) ->
if not @input.greeting? then control.fail 'greeting missing'
else if not @input.name? then control.fail 'name missing'
else control.next 'greet'
else control.next N.greet

builder.add 'greet', (control) ->
builder.add 'greet', (control, N) ->
greetings.push @input.greeting + ' ' + @input.name
control.wait()
control.next 'exists'
control.next N.exists

stator = builder.objects()

Expand All @@ -32,9 +138,9 @@ describe 'test objects()', ->
[ 'Hola', 'amigo' ]
]

it 'should not error in process 1', -> assert.equal results[0]?.error, undefined
it 'should not error in process 2', -> assert.equal results[1]?.error, undefined
it 'should not error in process 3', -> assert.equal results[2]?.error, undefined
it 'should not error in process 1', -> assert.equal results[0]?.message, undefined
it 'should not error in process 2', -> assert.equal results[1]?.message, undefined
it 'should not error in process 3', -> assert.equal results[2]?.message, undefined

it 'should receive greeting 1', -> assert.equal greetings[0], 'Hiya there'
it 'should receive greeting 2', -> assert.equal greetings[1], 'Hello John'
Expand All @@ -51,27 +157,24 @@ describe 'test strings()', ->
# it as... or, 'key' always gets @string and stores it as @key instead
# then, value,

builder = buildBuilder()
builder = build()

builder.add 'key', (control) ->
builder.add 'key', (control, N) ->
# it's possible to use a reusable node which ensures there is content.
# also, possible to have one which gets all alpha possible
# then, 'key' can just take @input[@start..@index] and do its thing.
# or, it could store it into @text with @index after it. hmm.
# console.log 'key node',@alpha
@key = @alpha
control.next 'equal-sign'
control.next N.equalSign

builder.add 'equal-sign', (control) -> # TODO: allow a colon as well
# console.log 'equal-sign node'
builder.add 'equalSign', (control, N) -> # TODO: allow a colon as well
if @index >= @input.length then return control.wait()
if @input[@index] is '='
control.next 'value'
control.next N.value
@index++
else control.fail 'not equal sign'

builder.add 'alpha', (control) ->
# console.log 'alpha node',@index,@input
builder.add 'alpha', (control, N) ->
if @index >= @input.length then return control.wait()
start = @index
end = start
Expand All @@ -93,12 +196,12 @@ describe 'test strings()', ->

objects = []

builder.add 'value', (control) ->
builder.add 'value', (control, N) ->
objects.push key:@key, value:@number
@key = null
@alpha = null
@number = null
control.next 'separator'
control.next N.separator

builder.add 'number', (control) ->
if @index >= @input.length then return control.wait()
Expand All @@ -121,10 +224,10 @@ describe 'test strings()', ->
builder.before('value').run('number')

allowed = [',',' ','\n','\r','\t']
builder.add 'separator', (control) ->
builder.add 'separator', (control, N) ->
if @index >= @input.length then return control.wait()
if @input[@index] in allowed
control.next 'key'
control.next N.key
@index++
else control.fail('comma or whitespace required')

Expand Down Expand Up @@ -153,15 +256,15 @@ describe 'test strings()', ->
# console.dir results, colors:true
# console.dir objects, colors:true

it 'should not error', -> assert.equal result?.error, undefined
it 'should not error', -> assert.equal result?.message, undefined

it 'should not error process 1', -> assert.equal results[0]?.error, undefined
it 'should not error process 2', -> assert.equal results[1]?.error, undefined
it 'should not error process 3', -> assert.equal results[2]?.error, undefined
it 'should not error process 4', -> assert.equal results[3]?.error, undefined
it 'should not error process 5', -> assert.equal results[4]?.error, undefined
it 'should not error process 6', -> assert.equal results[5]?.error, undefined
it 'should not error process 7', -> assert.equal results[6]?.error, undefined
it 'should not error process 1', -> assert.equal results[0]?.message, undefined
it 'should not error process 2', -> assert.equal results[1]?.message, undefined
it 'should not error process 3', -> assert.equal results[2]?.message, undefined
it 'should not error process 4', -> assert.equal results[3]?.message, undefined
it 'should not error process 5', -> assert.equal results[4]?.message, undefined
it 'should not error process 6', -> assert.equal results[5]?.message, undefined
it 'should not error process 7', -> assert.equal results[6]?.message, undefined

it 'should provide object 1', ->
assert.deepEqual objects[0], key:'num', value:1
Expand All @@ -186,3 +289,27 @@ describe 'test strings()', ->

it 'should provide object 8', ->
assert.deepEqual objects[7], key:'numberFour', value:7890


describe 'test transform()', ->

it 'should build with decodeStrings set to true', ->

stating = build()
stating.add 'only', ->
transform = stating.transform
transform:
decodeStrings: true
assert transform
assert.equal transform.error, null

it 'should build with decodeStrings:false and a custom context', ->

stating = build()
stating.add 'only', ->
transform = stating.transform
transform:
decodeStrings: false
context: {}
assert transform
assert.equal transform.error, null

0 comments on commit 43bbb64

Please sign in to comment.