|
|
|
@ -39,12 +39,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0000\u0005devon\u0015"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x00\x05devon\x15"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht) (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0)))
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0001\u000adevon 👍\u0015\u0000"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x01\x0adevon 👍\x15\x00"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht) (mhasheq 'name "devon 👍"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 1
|
|
|
|
@ -76,7 +76,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0005\u0005devon\u0015"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x05\x05devon\x15"))])
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (send struct decode stream)))))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
@ -115,13 +115,13 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0000\u0015\u0001\u0005devon"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x00\x15\x01\x05devon"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht) (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'alive 1
|
|
|
|
|
'version 0)))
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0001\u0015\u0001\u000adevon 👍\u0000"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x01\x15\x01\x0adevon 👍\x00"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht) (mhasheq 'name "devon 👍"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 1
|
|
|
|
@ -160,12 +160,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u0005devon\u0015"))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x05devon\x15"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream (mhash 'version 0)) ht) (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0)))
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\u000adevon 👍\u0015\u0000" 'utf8))])
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x0adevon 👍\x15\x00" 'utf8))])
|
|
|
|
|
(check-equal? (send (send struct decode stream (mhash 'version 1)) ht) (mhasheq 'name "devon 👍"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 1
|
|
|
|
@ -200,6 +200,33 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; version: 1
|
|
|
|
|
; name: 'ice cream'
|
|
|
|
|
; isDesert: 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8))
|
|
|
|
|
1 (dictify 'name (+StringT uint8)
|
|
|
|
|
'isDessert uint8)))))])
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x00\x05devon\x15"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream (mhash 'version 0)) ht) (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0)))
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x01\x00\x05pasta"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream (mhash 'version 0)) ht) (mhasheq 'name "pasta"
|
|
|
|
|
'version 0)))
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x01\x01\x09ice cream\x01"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream (mhash 'version 0)) ht) (mhasheq 'name "ice cream"
|
|
|
|
|
'isDessert 1
|
|
|
|
|
'version 1))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support process hook', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -220,6 +247,22 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; name: 'devon'
|
|
|
|
|
; age: 21
|
|
|
|
|
; processed: true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
(set-field! process struct (λ (o stream) (ref-set! o 'processed "true")))
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x00\x05devon\x15"))])
|
|
|
|
|
(check-equal? (send (send struct decode stream) ht) (mhasheq 'name "devon"
|
|
|
|
|
'processed "true"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0))))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'size', ->
|
|
|
|
|
; it 'should compute the correct size', ->
|
|
|
|
@ -246,6 +289,25 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; gender: 0
|
|
|
|
|
;
|
|
|
|
|
; size.should.equal 14
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(check-equal? (send struct size (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0)) 8)
|
|
|
|
|
|
|
|
|
|
(check-equal? (send struct size (mhasheq 'name "devon 👍"
|
|
|
|
|
'gender 0
|
|
|
|
|
'age 21
|
|
|
|
|
'version 1)) 14))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should throw for unknown version', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -262,6 +324,20 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; version: 5
|
|
|
|
|
; name: 'devon'
|
|
|
|
|
; age: 21
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (send struct size (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 5)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support common header block', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -290,7 +366,28 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; gender: 0
|
|
|
|
|
;
|
|
|
|
|
; size.should.equal 15
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
'header (dictify 'age uint8
|
|
|
|
|
'alive uint8)
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii))
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(check-equal? (send struct size (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'alive 1
|
|
|
|
|
'version 0)) 9)
|
|
|
|
|
|
|
|
|
|
(check-equal? (send struct size (mhasheq 'name "devon 👍"
|
|
|
|
|
'gender 0
|
|
|
|
|
'age 21
|
|
|
|
|
'alive 1
|
|
|
|
|
'version 1)) 15))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; todo: pointers
|
|
|
|
|
; it 'should compute the correct size with pointers', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
|
; 0:
|
|
|
|
@ -308,6 +405,10 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; ptr: 'hello'
|
|
|
|
|
;
|
|
|
|
|
; size.should.equal 15
|
|
|
|
|
|
|
|
|
|
(displayln "warning: pointer test not done in versioned-struct-test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should throw if no value is given', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -322,6 +423,18 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; should.throw ->
|
|
|
|
|
; struct.size()
|
|
|
|
|
; , /not a fixed size/i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))])
|
|
|
|
|
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (send struct size))))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'encode', ->
|
|
|
|
|
; it 'should encode objects to buffers', (done) ->
|
|
|
|
@ -351,6 +464,25 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; gender: 0
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0))
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon 👍"
|
|
|
|
|
'age 21
|
|
|
|
|
'gender 0
|
|
|
|
|
'version 1))
|
|
|
|
|
(check-equal? (send stream dump) (+Buffer "\x00\x05devon\x15\x01\x0adevon 👍\x15\x00" 'utf8)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should throw for unknown version', ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -368,6 +500,19 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; version: 5
|
|
|
|
|
; name: 'devon'
|
|
|
|
|
; age: 21
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(check-exn exn:fail:contract? (λ () (send struct encode stream (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 5)))))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support common header block', (done) ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -399,7 +544,32 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; gender: 0
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
'header (dictify 'age uint8
|
|
|
|
|
'alive uint8)
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii))
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'gender uint8)))]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'alive 1
|
|
|
|
|
'version 0))
|
|
|
|
|
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon 👍"
|
|
|
|
|
'gender 0
|
|
|
|
|
'age 21
|
|
|
|
|
'alive 1
|
|
|
|
|
'version 1))
|
|
|
|
|
|
|
|
|
|
(check-equal? (send stream dump) (+Buffer "\x00\x15\x01\x05devon\x01\x15\x01\x0adevon 👍\x00" 'utf8)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; todo
|
|
|
|
|
; it 'should encode pointer data after structure', (done) ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
|
; 0:
|
|
|
|
@ -409,6 +579,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; name: new StringT uint8, 'utf8'
|
|
|
|
|
; age: uint8
|
|
|
|
|
; ptr: new Pointer uint8, new StringT uint8
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
@ -422,6 +593,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; ptr: 'hello'
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(displayln "warning: pointer test not done in versioned-struct-test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should support preEncode hook', (done) ->
|
|
|
|
|
; struct = new VersionedStruct uint8,
|
|
|
|
@ -450,4 +627,22 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|
|
|
|
|
; age: 21
|
|
|
|
|
; gender: 0
|
|
|
|
|
;
|
|
|
|
|
; stream.end()
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([struct (+VersionedStruct uint8
|
|
|
|
|
(dictify
|
|
|
|
|
0 (dictify 'name (+StringT uint8 'ascii)
|
|
|
|
|
'age uint8)
|
|
|
|
|
1 (dictify 'name (+StringT uint8 'utf8)
|
|
|
|
|
'age uint8
|
|
|
|
|
'gender uint8)))]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(set-field! preEncode struct (λ (val stream) (ref-set! val 'version (if (ref val 'gender) 1 0))))
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon"
|
|
|
|
|
'age 21
|
|
|
|
|
'version 0))
|
|
|
|
|
(send struct encode stream (mhasheq 'name "devon 👍"
|
|
|
|
|
'age 21
|
|
|
|
|
'gender 0))
|
|
|
|
|
(check-equal? (send stream dump) (+Buffer "\x00\x05devon\x15\x01\x0adevon 👍\x15\x00" 'utf8)))
|