|
|
|
@ -22,8 +22,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer (bytes->list (bytes-append (bytes #x05) #"devon" (bytes #x15)))))]
|
|
|
|
|
[struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(check-equal? (send struct decode stream) (dictify 'name "devon"
|
|
|
|
|
'age 21)))
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht)
|
|
|
|
|
(mhasheq 'name "devon" 'age 21)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
@ -40,6 +40,15 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; name: 'devon'
|
|
|
|
|
; age: 32
|
|
|
|
|
; canDrink: true
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer (bytes->list (bytes-append (bytes #x05) #"devon" (bytes #x20)))))]
|
|
|
|
|
[struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(set-field! process struct (λ (o stream) (ref-set! o 'canDrink (>= (ref o 'age) 21))))
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht)
|
|
|
|
|
(mhasheq 'name "devon" 'age 32 'canDrink #t)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support function keys', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '\x05devon\x20'
|
|
|
|
@ -52,6 +61,15 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; name: 'devon'
|
|
|
|
|
; age: 32
|
|
|
|
|
; canDrink: true
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer (bytes->list (bytes-append (bytes #x05) #"devon" (bytes #x20)))))]
|
|
|
|
|
[struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'canDrink (λ (o) (>= (ref o 'age) 21))))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht)
|
|
|
|
|
(mhasheq 'name "devon" 'age 32 'canDrink #t)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'size', ->
|
|
|
|
|
; it 'should compute the correct size', ->
|
|
|
|
@ -60,7 +78,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; age: uint8
|
|
|
|
|
;
|
|
|
|
|
; struct.size(name: 'devon', age: 21).should.equal 7
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(let ([struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(check-equal? (send struct size (hasheq 'name "devon" 'age 32)) 7))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; todo: when pointers are ready
|
|
|
|
|
; it 'should compute the correct size with pointers', ->
|
|
|
|
|
; struct = new Struct
|
|
|
|
|
; name: new StringT uint8
|
|
|
|
@ -73,6 +97,11 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; ptr: 'hello'
|
|
|
|
|
;
|
|
|
|
|
; size.should.equal 14
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(displayln 'warning:pointer-not-done)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should get the correct size when no value is given', ->
|
|
|
|
|
; struct = new Struct
|
|
|
|
@ -80,6 +109,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; age: uint8
|
|
|
|
|
;
|
|
|
|
|
; struct.size().should.equal 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+Struct (dictify 'name (+StringT 4)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(check-equal? (send struct size) 5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should throw when getting non-fixed length size and no value is given', ->
|
|
|
|
|
; struct = new Struct
|
|
|
|
@ -89,6 +125,11 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; should.throw ->
|
|
|
|
|
; struct.size()
|
|
|
|
|
; , /not a fixed size/i
|
|
|
|
|
|
|
|
|
|
(let ([struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (send struct size))))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'encode', ->
|
|
|
|
|
; it 'should encode objects to buffers', (done) ->
|
|
|
|
@ -106,6 +147,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; age: 21
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer (bytes->list (bytes-append (bytes #x05) #"devon" (bytes #x15)))))]
|
|
|
|
|
[struct (+Struct (dictify 'name (+StringT uint8)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht)
|
|
|
|
|
(mhasheq 'name "devon" 'age 21)))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support preEncode hook', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
@ -126,7 +174,18 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; age: 21
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(let ([stream (+EncodeStream)]
|
|
|
|
|
[struct (+Struct (dictify 'nameLength uint8
|
|
|
|
|
'name (+StringT 'nameLength)
|
|
|
|
|
'age uint8))])
|
|
|
|
|
(set-field! preEncode struct (λ (val stream) (ref-set! val 'nameLength (length (ref val 'name)))))
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon" 'age 21))
|
|
|
|
|
(check-equal? (send stream dump)
|
|
|
|
|
(+Buffer (bytes->list (bytes-append (bytes #x05) #"devon" (bytes #x15))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; todo: when pointer is ready
|
|
|
|
|
; it 'should encode pointer data after structure', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
@ -143,4 +202,5 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|
|
|
|
|
; age: 21
|
|
|
|
|
; ptr: 'hello'
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
; stream.end()
|
|
|
|
|
(displayln 'warning:pointer-not-done)
|