@ -6,9 +6,9 @@
" ../number.rkt "
" ../string.rkt "
" ../pointer.rkt "
" ../ stru ct.rkt"
" ../ di ct.rkt"
" ../base.rkt "
" ../versioned- stru ct.rkt" )
" ../versioned- di ct.rkt" )
#|
approximates
@ -16,12 +16,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
| #
( test-case
" versioned stru ct: decode should get version from number type"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: decode should get version from number type"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x00 \x05 roxyb \x15 " ) ] )
@ -30,41 +30,41 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( check-equal? ( decode vstruct ) ( mhasheq ' name " roxyb 🤘 " ' age 21 x:version-key 1 ' gender 0 ) ) ) ) )
( test-case
" versioned stru ct: decode should get version from number type, nested"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: decode should get version from number type, nested"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8
' nested ( x: stru ct ' foo uint8 ) )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' nested ( x: di ct ' foo uint8 ) )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8
' nested ( x: stru ct ' foo uint8 ) ) ) ) ] )
' nested ( x: di ct ' foo uint8 ) ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x00 \x05 roxyb \x15 \x2a " ) ] )
( check-equal? ( decode vstruct ) ( mhasheq ' name " roxyb " ' age 21 ' nested ( mhasheq ' foo 42 ) x:version-key 0 ) ) )
( parameterize ( [ current-input-port ( open-input-bytes ( string->bytes/utf-8 " \x01 \x0a roxyb 🤘 \x15 \x00 \x2a " ) ) ] )
( check-equal? ( decode vstruct ) ( mhasheq ' name " roxyb 🤘 " ' age 21 x:version-key 1 ' gender 0 ' nested ( mhasheq ' foo 42 ) ) ) ) ) )
( test-case
" versioned stru ct: decode should throw for unknown version"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: decode should throw for unknown version"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x05 \x05 roxyb \x15 " ) ] )
( check-exn exn:fail:contract? ( λ ( ) ( decode vstruct ) ) ) ) ) )
( test-case
" versioned stru ct: decode should support common header block"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: decode should support common header block"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
' header ( dictify ' age uint8
' alive uint8 )
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii ) )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' gender uint8 ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x00 \x15 \x01 \x05 roxyb " ) ] )
( check-equal? ( decode vstruct ) ( mhasheq ' name " roxyb "
@ -79,12 +79,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
' gender 0 ) ) ) ) )
( test-case
" versioned stru ct: decode should support parent version key"
( let ( [ vstruct ( x:versioned- stru ct x:version-key
" versioned di ct: decode should support parent version key"
( let ( [ vstruct ( x:versioned- di ct x:version-key
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x05 roxyb \x15 " ) ] )
@ -95,12 +95,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( mhasheq ' name " roxyb 🤘 " ' age 21 x:version-key 1 ' gender 0 ) ) ) ) )
( test-case
" versioned struct: decode should support sub versioned stru cts"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned dict: decode should support sub versioned di cts"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x:versioned- stru ct uint8
1 ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string uint8 ) )
1 ( dictify ' name ( x:string uint8 )
@ -116,13 +116,13 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( mhasheq ' name " ice cream " ' isDessert 1 x:version-key 1 ) ) ) ) )
( test-case
" versioned stru ct: decode should support process hook"
( let ( [ vstruct ( x:versioned- stru ct #:post-decode ( λ ( val ) ( hash-set! val ' processed " true " ) val )
" versioned di ct: decode should support process hook"
( let ( [ vstruct ( x:versioned- di ct #:post-decode ( λ ( val ) ( hash-set! val ' processed " true " ) val )
uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( parameterize ( [ current-input-port ( open-input-bytes #" \x00 \x05 roxyb \x15 " ) ] )
@ -130,12 +130,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( mhasheq ' name " roxyb " ' processed " true " ' age 21 x:version-key 0 ) ) ) ) )
( test-case
" versioned stru ct: size should compute the correct size"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: size should compute the correct size"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( check-equal? ( size vstruct ( mhasheq ' name " roxyb "
@ -147,35 +147,35 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
x:version-key 1 ) ) 14 ) ) )
( test-case
" versioned stru ct: size should throw for unknown version"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: size should throw for unknown version"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( check-exn exn:fail:contract? ( λ ( ) ( size vstruct ( mhasheq ' name " roxyb " ' age 21 x:version-key 5 ) ) ) ) ) )
( test-case
" versioned stru ct: size should support common header block"
( let ( [ struct ( x:versioned- stru ct uint8
" versioned di ct: size should support common header block"
( let ( [ struct ( x:versioned- di ct uint8
( dictify
' header ( dictify ' age uint8
' alive uint8 )
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii ) )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' gender uint8 ) ) ) ] )
( check-equal? ( size struct ( mhasheq ' name " roxyb " ' age 21 ' alive 1 x:version-key 0 ) ) 9 )
( check-equal? ( size struct ( mhasheq ' name " roxyb 🤘 " ' gender 0 ' age 21 ' alive 1 x:version-key 1 ) ) 15 ) ) )
( test-case
" versioned stru ct: size should compute the correct size with pointers"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: size should compute the correct size with pointers"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' ptr ( x:pointer #:offset-type uint8
#:type ( x:string uint8 ) ) ) ) ) ] )
@ -185,23 +185,23 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
' ptr " hello " ) ) 15 ) ) )
( test-case
" versioned stru ct: size should throw if no value is given"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: size should throw if no value is given"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ] )
( check-exn exn:fail:contract? ( λ ( ) ( size vstruct ) ) ) ) )
( test-case
" versioned stru ct: encode should encode objects to buffers"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: encode should encode objects to buffers"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ]
[ op ( open-output-bytes ) ] )
@ -210,25 +210,25 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( check-equal? ( get-output-bytes op ) ( string->bytes/utf-8 " \x00 \x05 roxyb \x15 \x01 \x0a roxyb 🤘 \x15 \x00 " ) ) ) )
( test-case
" versioned stru ct: encode should throw for unknown version"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: encode should throw for unknown version"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ]
[ op ( open-output-bytes ) ] )
( check-exn exn:fail:contract? ( λ ( ) ( encode vstruct op ( mhasheq ' name " roxyb " ' age 21 x:version-key 5 ) ) ) ) ) )
( test-case
" versioned stru ct: encode should support common header block"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: encode should support common header block"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
' header ( dictify ' age uint8
' alive uint8 )
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii ) )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' gender uint8 ) ) ) ]
[ op ( open-output-bytes ) ] )
( encode vstruct ( mhasheq ' name " roxyb " ' age 21 ' alive 1 x:version-key 0 ) op )
@ -236,12 +236,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( check-equal? ( get-output-bytes op ) ( string->bytes/utf-8 " \x00 \x15 \x01 \x05 roxyb \x01 \x15 \x01 \x0a roxyb 🤘 \x00 " ) ) ) )
( test-case
" versioned stru ct: encode should encode pointer data after structure"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: encode should encode pointer data after structure"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' ptr ( x:pointer #:offset-type uint8
#:type ( x:string uint8 ) ) ) ) ) ]
@ -251,12 +251,12 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
( check-equal? ( get-output-bytes op ) ( string->bytes/utf-8 " \x01 \x05 roxyb \x15 \x09 \x05 hello " ) ) ) )
#; ( test-case
" versioned stru ct: encode should support preEncode hook"
( let ( [ vstruct ( x:versioned- stru ct uint8
" versioned di ct: encode should support preEncode hook"
( let ( [ vstruct ( x:versioned- di ct uint8
( dictify
0 ( dictify ' name ( x:string #:length uint8 #:encoding ' ascii )
' age uint8 )
1 ( x: stru ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
1 ( x: di ct ' name ( x:string #:length uint8 #:encoding ' utf8 )
' age uint8
' gender uint8 ) ) ) ]
[ op ( open-output-bytes ) ] )