import { should } from 'chai' ; should ( ) ; import source from '../../pin/source' ; import seq , { Sequence } from '../sequence' ; describe ( 'Sequence' , ( ) => { it ( 'should fill up a sequence of values based on given number from each source.' , done => { let s = seq ( 1 , 2 , 1 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( val => { val . should . eql ( [ 'b' , [ 'b' , 'b' ] , 'b' ] ) ; done ( ) ; } ) ; a . send ( 'a' ) ; b . send ( 'a' ) ; c . send ( 'a' ) ; a . send ( 'b' ) ; b . send ( 'b' ) ; b . send ( 'b' ) ; c . send ( 'b' ) ; } ) ; it ( 'should ensure the order of incoming values from each source.' , done => { let s = seq ( 1 , 2 , 1 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( val => { val . should . eql ( [ 'b' , [ 'b' , 'b' ] , 'b' ] ) ; done ( ) ; } ) ; a . send ( 'a' ) ; b . send ( 'a' ) ; c . send ( 'a' ) ; b . send ( 'a' ) ; a . send ( 'b' ) ; b . send ( 'b' ) ; b . send ( 'b' ) ; c . send ( 'b' ) ; } ) ; it ( 'should work properly with 0 items.' , done => { let s = seq ( 1 , 0 , 1 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( ( ) => done ( ) ) ; a . send ( ) ; b . send ( ) ; c . send ( ) ; a . send ( ) ; c . send ( ) ; } ) ; it ( 'should properly handle leading 0s.' , done => { let s = seq ( 0 , 0 , 2 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( val => { val . should . eql ( [ [ ] , [ ] , [ 'c' , 'c' ] ] ) ; done ( ) ; } ) ; a . send ( 'a' ) ; b . send ( 'b' ) ; c . send ( 'c' ) ; a . send ( 'a' ) ; c . send ( 'c' ) ; b . send ( 'b' ) ; c . send ( 'c' ) ; c . send ( 'c' ) ; } ) ; it ( 'should properly handle trailing 0s.' , done => { let s = seq ( 1 , 2 , 0 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( ( ) => done ( ) ) ; a . send ( ) ; b . send ( ) ; c . send ( ) ; a . send ( ) ; b . send ( ) ; b . send ( ) ; } ) ; it ( 'should properly treat \'+\' as "one or more"' , ( ) => { let res = < number [ ] > [ ] ; let s = seq ( 1 , '+' , 1 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( v => res . push ( v ) ) ; a . send ( 1 ) ; b . send ( 1 ) ; c . send ( 1 ) ; a . send ( 2 ) ; c . send ( 2 ) ; a . send ( 3 ) ; b . send ( 3 ) ; b . send ( 3 ) ; b . send ( 3 ) ; c . send ( 3 ) ; res [ 0 ] . should . eql ( [ 1 , 1 , 1 ] ) ; res [ 1 ] . should . eql ( [ 3 , [ 3 , 3 , 3 ] , 3 ] ) ; } ) ; it ( 'should properly treat \'*\' as "zero or more".' , ( ) => { let res = < number [ ] > [ ] ; let s = seq ( 1 , '*' , 2 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( v => res . push ( v ) ) ; a . send ( 1 ) ; c . send ( 1 ) ; b . send ( 1 ) ; a . send ( 2 ) ; c . send ( 2 ) ; c . send ( 2 ) ; a . send ( 3 ) ; b . send ( 3 ) ; b . send ( 3 ) ; c . send ( 3 ) ; c . send ( 3 ) ; res [ 0 ] . should . eql ( [ 2 , [ ] , [ 2 , 2 ] ] ) ; res [ 1 ] . should . eql ( [ 3 , [ 3 , 3 ] , [ 3 , 3 ] ] ) ; } ) ; it ( 'should properly handle leading *s.' , ( ) => { let res = < number [ ] > [ ] ; let s = seq ( '*' , '*' , 2 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( v => res . push ( v ) ) ; a . send ( 1 ) ; c . send ( 1 ) ; c . send ( 1 ) ; b . send ( 2 ) ; a . send ( 2 ) ; c . send ( 2 ) ; b . send ( 3 ) ; b . send ( 3 ) ; c . send ( 3 ) ; c . send ( 3 ) ; res [ 0 ] . should . eql ( [ 1 , [ ] , [ 1 , 1 ] ] ) ; res [ 1 ] . should . eql ( [ [ ] , [ 3 , 3 ] , [ 3 , 3 ] ] ) ; } ) ; it ( 'should properly handle trailing *s.' , ( ) => { let res = < number [ ] > [ ] ; let s = seq ( 2 , '*' , '*' ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( v => res . push ( v ) ) ; a . send ( 1 ) ; c . send ( 1 ) ; c . send ( 1 ) ; a . send ( 2 ) ; a . send ( 2 ) ; b . send ( 2 ) ; c . send ( 2 ) ; b . send ( 2 ) ; a . send ( 3 ) ; a . send ( 3 ) ; c . send ( 3 ) ; res [ 0 ] . should . eql ( [ [ 2 , 2 ] , [ ] , [ ] ] ) ; res [ 1 ] . should . eql ( [ [ 2 , 2 ] , 2 , [ ] ] ) ; res [ 2 ] . should . eql ( [ [ 2 , 2 ] , 2 , 2 ] ) ; res [ 3 ] . should . eql ( [ [ 3 , 3 ] , [ ] , [ ] ] ) ; res [ 4 ] . should . eql ( [ [ 3 , 3 ] , [ ] , 3 ] ) ; } ) ; it ( 'should properly treat \'+\' as "one or more".' , ( ) => { let r = 0 ; let s = seq ( 1 , '+' , 2 ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . in ( 2 ) ) ; s . output . subscribe ( ( ) => r ++ ) ; a . send ( ) ; b . send ( ) ; b . send ( ) ; c . send ( ) ; c . send ( ) ; a . send ( ) ; c . send ( ) ; c . send ( ) ; a . send ( ) ; b . send ( ) ; c . send ( ) ; c . send ( ) ; r . should . equal ( 2 ) ; } ) ; describe ( '.control' , ( ) => { it ( 'should reset the sequence on signal.' , ( ) => { let r = 0 ; let s = seq ( 1 , '+' ) ; let a = source ( ) ; a . to ( s . in ( 0 ) ) ; let b = source ( ) ; b . to ( s . in ( 1 ) ) ; let c = source ( ) ; c . to ( s . control ) ; s . output . subscribe ( ( ) => r ++ ) ; a . send ( 1 ) ; b . send ( 1 ) ; b . send ( 1 ) ; c . send ( ) ; b . send ( 1 ) ; a . send ( 1 ) ; b . send ( 1 ) ; r . should . equal ( 3 ) ; } ) ; } ) ; } ) ; describe ( 'sequence()' , ( ) => { it ( 'should give out a sequence based on given sequence tokens.' , ( ) => { let s = seq ( 1 , 2 , '+' , '*' ) ; s . should . be . instanceof ( Sequence ) ; s . tokens . length . should . equal ( 4 ) ; } ) ; } ) ;