main
Matthew Butterick 7 years ago
parent a1f098918a
commit c53fd0b0e6

@ -10,33 +10,33 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
; describe 'decode', ->
; it 'should decode into an object', ->
(parameterize ([current-input-port (open-input-bytes #"\x05devon\x15")])
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal?
(dump (decode (+Struct (dictify 'name (+StringT uint8)
'age uint8))))
(mhasheq 'name "devon" 'age 21)))
(mhasheq 'name "roxyb" 'age 21)))
; it 'should support process hook', ->
(parameterize ([current-input-port (open-input-bytes #"\x05devon\x20")])
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x20")])
(define struct (+Struct (dictify 'name (+StringT uint8)
'age uint8)))
(set-field! post-decode struct (λ (o . _) (ref-set! o 'canDrink (>= (· o age) 21)) o))
(check-equal? (dump (decode struct))
(mhasheq 'name "devon" 'age 32 'canDrink #t)))
(mhasheq 'name "roxyb" 'age 32 'canDrink #t)))
; it 'should support function keys', ->
(parameterize ([current-input-port (open-input-bytes #"\x05devon\x20")])
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x20")])
(define struct (+Struct (dictify 'name (+StringT uint8)
'age uint8
'canDrink (λ (o) (>= (ref o 'age) 21)))))
(check-equal? (dump (decode struct))
(mhasheq 'name "devon" 'age 32 'canDrink #t)))
(mhasheq 'name "roxyb" 'age 32 'canDrink #t)))
@ -48,7 +48,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(check-equal? (size (+Struct (dictify
'name (+StringT uint8)
'age uint8))
(hasheq 'name "devon" 'age 32)) 7)
(hasheq 'name "roxyb" 'age 32)) 7)
@ -58,7 +58,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
'name (+StringT uint8)
'age uint8
'ptr (+Pointer uint8 (+StringT uint8))))
(mhash 'name "devon" 'age 21 'ptr "hello")) 14)
(mhash 'name "roxyb" 'age 21 'ptr "hello")) 14)
; it 'should get the correct size when no value is given', ->
@ -79,7 +79,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
; it 'should encode objects to buffers', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer '\x05devon\x15'
; buf.should.deep.equal new Buffer '\x05roxyb\x15'
; done()
;
; struct = new Struct
@ -87,15 +87,15 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
; age: uint8
;
; struct.encode stream,
; name: 'devon'
; name: 'roxyb'
; age: 21
;
; stream.end()
(parameterize ([current-input-port (open-input-bytes #"\x05devon\x15")])
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal? (dump (decode (+Struct (dictify 'name (+StringT uint8)
'age uint8))))
(mhasheq 'name "devon" 'age 21)))
(mhasheq 'name "roxyb" 'age 21)))
; it 'should support preEncode hook', (done) ->
@ -105,8 +105,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
'name (+StringT 'nameLength)
'age uint8)))
(set-field! pre-encode struct (λ (val port) (ref-set! val 'nameLength (length (ref val 'name))) val))
(encode struct (mhasheq 'name "devon" 'age 21))
(check-equal? (dump (current-output-port)) #"\x05devon\x15"))
(encode struct (mhasheq 'name "roxyb" 'age 21))
(check-equal? (dump (current-output-port)) #"\x05roxyb\x15"))
; it 'should encode pointer data after structure', (done) ->
@ -115,6 +115,6 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(define struct (+Struct (dictify 'name (+StringT uint8)
'age uint8
'ptr (+Pointer uint8 (+StringT uint8)))))
(encode struct (mhasheq 'name "devon" 'age 21 'ptr "hello"))
(check-equal? (dump (current-output-port)) #"\x05devon\x15\x08\x05hello"))
(encode struct (mhasheq 'name "roxyb" 'age 21 'ptr "hello"))
(check-equal? (dump (current-output-port)) #"\x05roxyb\x15\x08\x05hello"))

@ -17,13 +17,13 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(parameterize ([current-input-port (open-input-bytes #"\x00\x05devon\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "devon"
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb"
'age 21
'version 0)))
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x01\x0adevon 👍\x15\x00"))])
(check-equal? (dump (decode struct)) (mhasheq 'name "devon 👍"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x01\x0aroxyb 🤘\x15\x00"))])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb 🤘"
'age 21
'version 1
'gender 0))))
@ -39,7 +39,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(parameterize ([current-input-port (open-input-bytes #"\x05\x05devon\x15")])
(parameterize ([current-input-port (open-input-bytes #"\x05\x05roxyb\x15")])
(check-exn exn:fail:contract? (λ () (decode struct)))))
@ -54,14 +54,14 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
1 (dictify 'name (+StringT uint8 'utf8)
'gender uint8)))])
(parameterize ([current-input-port (open-input-bytes #"\x00\x15\x01\x05devon")])
(check-equal? (dump (decode struct)) (mhasheq 'name "devon"
(parameterize ([current-input-port (open-input-bytes #"\x00\x15\x01\x05roxyb")])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb"
'age 21
'alive 1
'version 0)))
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x01\x15\x01\x0adevon 👍\x00"))])
(check-equal? (dump (decode struct)) (mhasheq 'name "devon 👍"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x01\x15\x01\x0aroxyb 🤘\x00"))])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb 🤘"
'age 21
'version 1
'alive 1
@ -78,13 +78,13 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(parameterize ([current-input-port (open-input-bytes #"\x05devon\x15")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "devon"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "roxyb"
'age 21
'version 0)))
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x0adevon 👍\x15\x00"))])
(check-equal? (dump (decode struct #:parent (mhash 'version 1))) (mhasheq 'name "devon 👍"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "\x0aroxyb 🤘\x15\x00"))])
(check-equal? (dump (decode struct #:parent (mhash 'version 1))) (mhasheq 'name "roxyb 🤘"
'age 21
'version 1
'gender 0))))
@ -104,8 +104,8 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
1 (dictify 'name (+StringT uint8)
'isDessert uint8)))))])
(parameterize ([current-input-port (open-input-bytes #"\x00\x05devon\x15")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "devon"
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "roxyb"
'age 21
'version 0)))
@ -130,8 +130,8 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(set-field! post-decode struct (λ (o stream ctx) (ref-set! o 'processed "true") o))
(parameterize ([current-input-port (open-input-bytes #"\x00\x05devon\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "devon"
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb"
'processed "true"
'age 21
'version 0))))
@ -149,11 +149,11 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(check-equal? (size struct (mhasheq 'name "devon"
(check-equal? (size struct (mhasheq 'name "roxyb"
'age 21
'version 0)) 8)
(check-equal? (size struct (mhasheq 'name "devon 👍"
(check-equal? (size struct (mhasheq 'name "roxyb 🤘"
'gender 0
'age 21
'version 1)) 14))
@ -172,7 +172,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))])
(check-exn exn:fail:contract? (λ () (size struct (mhasheq 'name "devon"
(check-exn exn:fail:contract? (λ () (size struct (mhasheq 'name "roxyb"
'age 21
'version 5)))))
@ -188,12 +188,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
1 (dictify 'name (+StringT uint8 'utf8)
'gender uint8)))])
(check-equal? (size struct (mhasheq 'name "devon"
(check-equal? (size struct (mhasheq 'name "roxyb"
'age 21
'alive 1
'version 0)) 9)
(check-equal? (size struct (mhasheq 'name "devon 👍"
(check-equal? (size struct (mhasheq 'name "roxyb 🤘"
'gender 0
'age 21
'alive 1
@ -212,7 +212,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'ptr (+Pointer uint8 (+StringT uint8)))))])
(check-equal? (size struct (mhasheq 'name "devon"
(check-equal? (size struct (mhasheq 'name "roxyb"
'age 21
'version 1
'ptr "hello")) 15))
@ -246,14 +246,14 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))]
[port (open-output-bytes)])
(encode struct (mhasheq 'name "devon"
(encode struct (mhasheq 'name "roxyb"
'age 21
'version 0) port)
(encode struct (mhasheq 'name "devon 👍"
(encode struct (mhasheq 'name "roxyb 🤘"
'age 21
'gender 0
'version 1) port)
(check-equal? (dump port) (string->bytes/utf-8 "\x00\x05devon\x15\x01\x0adevon 👍\x15\x00")))
(check-equal? (dump port) (string->bytes/utf-8 "\x00\x05roxyb\x15\x01\x0aroxyb 🤘\x15\x00")))
;
@ -267,7 +267,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'age uint8
'gender uint8)))]
[port (open-output-bytes)])
(check-exn exn:fail:contract? (λ () (encode struct port (mhasheq 'name "devon"
(check-exn exn:fail:contract? (λ () (encode struct port (mhasheq 'name "roxyb"
'age 21
'version 5)))))
@ -284,18 +284,18 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))]
[stream (open-output-bytes)])
(encode struct (mhasheq 'name "devon"
(encode struct (mhasheq 'name "roxyb"
'age 21
'alive 1
'version 0) stream)
(encode struct (mhasheq 'name "devon 👍"
(encode struct (mhasheq 'name "roxyb 🤘"
'gender 0
'age 21
'alive 1
'version 1) stream)
(check-equal? (dump stream) (string->bytes/utf-8 "\x00\x15\x01\x05devon\x01\x15\x01\x0adevon 👍\x00")))
(check-equal? (dump stream) (string->bytes/utf-8 "\x00\x15\x01\x05roxyb\x01\x15\x01\x0aroxyb 🤘\x00")))
@ -310,11 +310,11 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'ptr (+Pointer uint8 (+StringT uint8)))))]
[stream (open-output-bytes)])
(encode struct (mhasheq 'version 1
'name "devon"
'name "roxyb"
'age 21
'ptr "hello") stream)
(check-equal? (dump stream) (string->bytes/utf-8 "\x01\x05devon\x15\x09\x05hello")))
(check-equal? (dump stream) (string->bytes/utf-8 "\x01\x05roxyb\x15\x09\x05hello")))
@ -330,10 +330,10 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))]
[stream (open-output-bytes)])
(set-field! pre-encode struct (λ (val port) (ref-set! val 'version (if (ref val 'gender) 1 0)) val))
(encode struct (mhasheq 'name "devon"
(encode struct (mhasheq 'name "roxyb"
'age 21
'version 0) stream)
(encode struct (mhasheq 'name "devon 👍"
(encode struct (mhasheq 'name "roxyb 🤘"
'age 21
'gender 0) stream)
(check-equal? (dump stream) (string->bytes/utf-8 "\x00\x05devon\x15\x01\x0adevon 👍\x15\x00")))
(check-equal? (dump stream) (string->bytes/utf-8 "\x00\x05roxyb\x15\x01\x0aroxyb 🤘\x15\x00")))

Loading…
Cancel
Save