pass test14

main
Matthew Butterick 7 years ago
parent 3590abd539
commit 3aba76f338

@ -1,8 +1,13 @@
#lang fontkit/racket
(require fontkit "subset.rkt" rackunit xenomorph racket/serialize)
(require fontkit fontkit/gpos-processor "subset.rkt" rackunit xenomorph racket/serialize)
(define fira-path "../pitfall/test/assets/fira.ttf")
(define f (openSync fira-path))
(define gpos (· f GPOS))
(get (· gpos lookupList) 11)
#;(get (· gpos lookupList) 11)
(define gp (+GPOSProcessor f gpos))
(· gpos scriptList)
(send gp selectScript 'cyrl)

@ -50,7 +50,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/OS2.js
'sFamilyClass int16be ;; classification of font-family design
'panose (+Array uint8 10) ;; describe the visual characteristics of a given typeface
'ulCharRange (+Array uint32be 4)
'vendorID (+String 4) ;; four character identifier for the font vendor
'vendorID (+Symbol 4) ;; four character identifier for the font vendor
;; bit field containing information about the font
'fsSelection (+Bitfield uint16 '(italic underscore negative outlined strikeout bold regular useTypoMetrics wws oblique))
'usFirstCharIndex uint16be ;; The minimum Unicode index in this font

@ -8,7 +8,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
|#
(define TableEntry (+Struct
(dictify 'tag (+String 4)
(dictify 'tag (+Symbol 4)
'checkSum uint32be
'offset (+Pointer uint32be 'void (mhash 'type 'global))
'length uint32be)))
@ -39,7 +39,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
(define searchRange (* (floor (log numTables 2)) 16))
(hash-set*! this-val
'tag "true"
'tag 'true
'numTables numTables
'tables tables
'searchRange searchRange
@ -48,7 +48,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js
this-val))
(define Directory (+RDirectory (dictify 'tag (+String 4)
(define Directory (+RDirectory (dictify 'tag (+Symbol 4)
'numTables uint16be
'searchRange uint16be
'entrySelector uint16be

@ -235,8 +235,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/TTFFont.js
(define/contract (layout this string [userFeatures #f] [script #f] [language #f])
((string?) ((option/c (listof symbol?)) (option/c symbol?) (option/c symbol?)) . ->*m . GlyphRun?)
(unless (· this _layoutEngine)
(set-field! _layoutEngine this (make-object LayoutEngine this)))
(report 'in-layout)
(set-field! _layoutEngine this (+LayoutEngine this)))
(report* 'in-layout (· this _layoutEngine))
(send (· this _layoutEngine) layout string userFeatures script language))

@ -0,0 +1,31 @@
#lang fontkit/racket
(require fontkit "gpos-processor.rkt" rackunit xenomorph racket/serialize describe)
(define fira-path "../pitfall/test/assets/fira.ttf")
(define f (openSync fira-path))
(define gpos (· f GPOS))
(define proc (+GPOSProcessor f gpos))
(check-equal? (dump (· proc features))
'((cpsp (lookupCount . 1) (lookupListIndexes 0) (featureParams . 0))
(mkmk (lookupCount . 5) (lookupListIndexes 8 9 10 11 12) (featureParams . 0))
(mark (lookupCount . 3) (lookupListIndexes 5 6 7) (featureParams . 0))
(kern (lookupCount . 4) (lookupListIndexes 1 2 3 4) (featureParams . 0))))
(check-equal? (dump (· proc script))
'((count . 0)
(defaultLangSys (featureIndexes 0 14 28 42)
(reserved . 0)
(reqFeatureIndex . 65535)
(featureCount . 4))
(langSysRecords)))
(check-equal? (dump (· proc scriptTag)) 'DFLT)
(check-equal? (dump (· proc language))
'((featureIndexes 0 14 28 42)
(reserved . 0)
(reqFeatureIndex . 65535)
(featureCount . 4)))
(check-equal? (dump (· proc languageTag)) #f)
(check-equal? (dump (· proc lookups)) empty)
(check-equal? (dump (· proc direction)) 'ltr)

@ -35,8 +35,5 @@
(define table-data (decode hhea table-bytes))
(check-equal? (· table-data ascent) 980)
(check-equal? (· table-data descent) -238)
(check-equal? (· table-data numberOfMetrics) 229)
)
(check-equal? (· table-data numberOfMetrics) 229))

@ -37,8 +37,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/hmtx.js
(define hmtx-data (decode hmtx-test hmtx-bytes))
(check-equal? (send hmtx-test size) (* 229 (send HmtxEntry size)))
(define H-gid 41) (define OE-gid 142)
(check-equal? (dump (send (· hmtx-data metrics) get H-gid)) (mhasheq 'advance 738 'bearing 33))
(check-equal? (dump (send (· hmtx-data metrics) get OE-gid)) (mhasheq 'advance 993 'bearing 43))
(check-equal? (dump (send (· hmtx-data metrics) get H-gid)) '#hasheq((advance . 738) (bearing . 33)))
(check-equal? (dump (send (· hmtx-data metrics) get OE-gid)) '#hasheq((advance . 993) (bearing . 43)))
)

@ -18,6 +18,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/LayoutEngine.js
(cond
[(· this font has-morx-table?) (error 'morx-layout-unimplemented)]
[(or (· this font has-gsub-table?) (· this font has-gpos-table?))
(report/file 'starting-layout-engine)
(+OTLayoutEngine (· this font))]
[else #f])])

@ -21,7 +21,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/opentype.js
(define-subclass Pointer (LSR-Pointer))
(define LangSysRecord (+Struct
(dictify 'tag (+String 4)
(dictify 'tag (+Symbol 4)
'langSys (+LSR-Pointer uint16be LangSysTable (mhash 'type 'parent)))))
(define-subclass Pointer (DLS-Pointer))
@ -34,7 +34,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/opentype.js
(define-subclass Struct (ScriptRecord-Struct))
(define-subclass Pointer (ScriptRecord-Pointer))
(define ScriptRecord (+ScriptRecord-Struct
(dictify 'tag (+String 4)
(dictify 'tag (+Symbol 4)
'script (+ScriptRecord-Pointer uint16be Script (mhash 'type 'parent)))))
(define ScriptList (+Array ScriptRecord uint16be))
@ -52,7 +52,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/opentype.js
(define-subclass Struct (FeatureRec))
(define-subclass Pointer (FeatureRec-Pointer))
(define FeatureRecord (+FeatureRec (dictify
'tag (+String 4)
'tag (+Symbol 4)
'feature (+FeatureRec-Pointer uint16be Feature (mhash 'type 'parent)))))
(define FeatureList (+Array FeatureRecord uint16be))

@ -12,10 +12,13 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTLayoutEngine.js
[GSUBProcessor #f]
[GPOSProcessor #f])
(report 'dingdong)
(when (· font has-gsub-table?)
(report/file 'starting-ot-layout-engine)
;; todo: gsub
#;(when (· font has-gsub-table?)
(set-field! GSUBProcessor this (+GSUBProcessor font (· font GSUB))))
(report* 'starting-gpos-dingdong)
(when (· font has-gpos-table?)
(set-field! GPOSProcessor this (+GPOSProcessor font (· font GPOS))))

@ -15,7 +15,8 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js
[language #f]
[languageTag #f]
[features (mhash)]
[lookups (mhash)])
[lookups (mhash)]
[direction #f]) ; appears below
;; initialize to default script + language
(selectScript)
@ -25,22 +26,20 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js
[positions empty] ; only used by GPOS
[ligatureID 1])
(define/public (findScript script)
(unless (script? script)
(raise-argument-error 'findScript "script" script))
(define/public (findScript script-or-scripts)
(and (· this table scriptList)
(let ([script (if (not (list? script)) (list script) script)])
(let ([scripts (if (pair? script-or-scripts) script-or-scripts (list script-or-scripts))])
(for*/first ([entry (in-list (· this table scriptList))]
[s (in-list script)]
#:when (equal? (· entry tag) s))
[s (in-list scripts)]
#:when (eq? (· entry tag) s))
entry))))
(define/public (selectScript script language)
(define/public (selectScript [script #f] [language #f])
(let/ec return!
(define changed #f)
(define entry #f)
(when (or (not (· this script)) (not (equal? script (· this scriptTag))))
(when (or (not (· this script)) (not (eq? script (· this scriptTag))))
(set! entry (findScript script))
(when script
(set! entry (findScript script))) ; ? why double dip
@ -69,11 +68,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/opentype/OTProcessor.js
(set-field! features this (mhash))
(when (· this language)
(for ([featureIndex (in-list (· this language featureIndexes))])
(define record (hash-ref (· this table featureList) featureIndex))
(hash-set! (· this features) (· record tag)) (· record feature))))))
)
(define record (list-ref (· this table featureList) featureIndex))
(dict-set! (· this features) (· record tag) (· record feature))))))))

@ -23,9 +23,9 @@
sugar/dict
sugar/stub
sugar/port
sugar/contract)
sugar/contract
describe)
(define script? symbol?)
(module reader syntax/module-reader
#:language 'fontkit/racket

@ -141,7 +141,7 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/Script.js
(define/contract (fromUnicode script)
((option/c script?) . -> . script?)
((option/c symbol?) . -> . symbol?)
(hash-ref UNICODE_SCRIPTS script #f))
(define-stub-stop forString)
@ -178,5 +178,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/layout/Script.js
))
(define/contract (direction script)
((option/c script?) . -> . (or/c 'rtl 'ltr))
((option/c symbol?) . -> . (or/c 'rtl 'ltr))
(if (memq script RTL) 'rtl 'ltr))

@ -113,17 +113,19 @@ https://github.com/mbutterick/fontkit/blob/master/src/subset/TTFSubset.js
(define gid (list-ref (· this glyphs) idx))
(send this _addGlyph gid))
(define maxp (cloneDeep (dump (· this font maxp))))
(define maxp (cloneDeep (· this font maxp to-hash)))
(dict-set! maxp 'numGlyphs (length (· this glyf)))
;; populate the new loca table
(dict-update! (· this loca) 'offsets (λ (vals) (append vals (list (· this offset)))))
(loca-pre-encode (· this loca))
(define head (cloneDeep (dump (· this font head))))
(define head (cloneDeep (· this font head to-hash)))
(dict-set! head 'indexToLocFormat (· this loca version))
(define hhea (cloneDeep (dump (· this font hhea))))
(define hhea (cloneDeep (· this font hhea to-hash)))
(dict-set! hhea 'numberOfMetrics (length (· this hmtx metrics)))
(send Directory encode port
(mhash 'tables

@ -14,6 +14,6 @@
pitfall/test/test11
pitfall/test/test12 ; ttf subset
pitfall/test/test13 ; subset with composites
;pitfall/test/test14 ; Fira ttf
pitfall/test/test14 ; Fira ttf with GPOS
pitfall/page-test
(submod pitfall/zlib test)))

@ -1,5 +1,6 @@
#lang pitfall/pdftest
;; subset font with GPOS table
(define-runtime-path ttf-path "assets/fira.ttf")
(define (proc doc)
@ -14,7 +15,7 @@
;; test against non-subsetted font version
(define-runtime-path this "test14rkt.pdf")
(make-doc this #f proc #:pdfkit #t)
(make-doc this #f proc )
(define-runtime-path that "test14crkt.pdf")
(make-doc that #t proc #:pdfkit #f)

@ -33,9 +33,6 @@
#'(define ID (let ([ID-CLASS (class BASE-CLASS (super-new))])
(MAKER ID-CLASS . ARGS)))))
(define-macro (define-subclass SUPERCLASS (ID . INIT-ARGS) . BODY)
#'(define-subclass* SUPERCLASS (ID . INIT-ARGS) (super-new) . BODY))
(define-macro (define-class-predicates ID)
(with-pattern ([+ID (prefix-id "+" #'ID)]
@ -43,11 +40,20 @@
#'(begin (define (ID? x) (is-a? x ID))
(define (+ID . args) (apply make-object ID args)))))
(define-macro (define-subclass* SUPERCLASS (ID . INIT-ARGS) . BODY)
(define-macro (define-subclass*/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) . BODY)
#'(begin
(define ID (class SUPERCLASS (init-field . INIT-ARGS) . BODY))
(define ID (class* SUPERCLASS INTERFACES (init-field . INIT-ARGS) . BODY))
(define-class-predicates ID)))
(define-macro (define-subclass/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) . BODY)
#'(define-subclass*/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) (super-new) . BODY))
(define-macro (define-subclass* SUPERCLASS (ID . INIT-ARGS) . BODY)
#'(define-subclass*/interfaces SUPERCLASS () (ID . INIT-ARGS) . BODY))
(define-macro (define-subclass SUPERCLASS (ID . INIT-ARGS) . BODY)
#'(define-subclass* SUPERCLASS (ID . INIT-ARGS) (super-new) . BODY))
(define-macro (push-field! FIELD O EXPR)
#'(set-field! FIELD O (cons EXPR (get-field FIELD O))))

@ -46,11 +46,12 @@
(for/fold ([x x])
([ref (in-list refs)])
(cond
;; give `send` precedence (presence of method => wants runtime resolution of value)
[(and (object? x)
(memq ref (interface->method-names (object-interface x)))) (dynamic-send x ref)]
;; dict first, to catch objects that implement gen:dict
[(dict? x) (dict-ref x ref #f)]
;; give `send` precedence (presence of method => wants runtime resolution of value)
[(object? x) (cond
[(memq ref (interface->method-names (object-interface x))) (dynamic-send x ref)]
[(memq ref (field-names x)) (dynamic-get-field ref x)]
[else #f])]
[else (raise-argument-error '· "object or dict" x)]))) '·))

@ -1,5 +1,5 @@
#lang racket/base
(require racket/class sugar/class racket/generic racket/private/generic-methods "generic.rkt" racket/port)
(require racket/class sugar/class racket/generic racket/private/generic-methods "generic.rkt" racket/port racket/dict racket/function)
(require sugar/debug)
(provide (all-defined-out))
(define-generics posable
@ -41,24 +41,28 @@
(generic-method-table gen:sizable
(define (size o [val #f] [parent #f]) (send o size val parent)))])))
(define-generics dumpable
(dump dumpable)
#:defaults
([input-port? (define (dump p) (port->bytes p))]
[output-port? (define (dump p) (get-output-bytes p))]))
(define dumpable<%>
(interface* ()
([(generic-property gen:dumpable)
(generic-method-table gen:dumpable
(define (dump o) (send o dump)))])))
(define (dump x)
(let loop ([x x])
(cond
[(input-port? x) (port->bytes x)]
[(output-port? x) (get-output-bytes x)]
[(dict? x) (for/list ([(k v) (in-dict x)])
(cons (loop k) (loop v)))]
[(list? x) (map loop x)]
[(and (object? x) (memq 'dump (interface->method-names (object-interface x)))) (send x dump)]
[else x])))
#;(define dumpable<%>
(interface* ()
([(generic-property gen:dumpable)
(generic-method-table gen:dumpable
(define (dump o) (send o dump)))])))
(define (symbol-append . syms)
(string->symbol (apply string-append (map symbol->string syms))))
(define xenomorph-base%
(class* object% (codable<%> sizable<%> dumpable<%>)
(class* object% (codable<%> sizable<%>)
(super-new)
(field [_hash (make-hash)]
[_list null])

@ -8,27 +8,27 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
|#
(define (read-encoded-string port len [encoding 'ascii])
(define proc (caseq encoding
[(utf16le) (error 'bah)]
[(ucs2) (error 'bleh)]
[(utf8) bytes->string/utf-8]
[(ascii) bytes->string/latin-1]
[else identity]))
(proc (read-bytes len port)))
(define proc (caseq encoding
[(utf16le) (error 'bah)]
[(ucs2) (error 'bleh)]
[(utf8) bytes->string/utf-8]
[(ascii) bytes->string/latin-1]
[else identity]))
(proc (read-bytes len port)))
(define (write-encoded-string port string [encoding 'ascii])
;; todo: handle encodings correctly.
;; right now just utf8 and ascii are correct
(caseq encoding
[(utf16le ucs2 utf8 ascii) (write-bytes (string->bytes/utf-8 string) port)
(when (eq? encoding 'utf16le)
(error 'swap-bytes-unimplemented))]
[else (error 'unsupported-string-encoding)]))
;; todo: handle encodings correctly.
;; right now just utf8 and ascii are correct
(define proc (caseq encoding
[(ucs2 utf8 ascii) string->bytes/utf-8]
[(utf16le) (error 'swap-bytes-unimplemented)]
[else (error 'unsupported-string-encoding)]))
(write-bytes (proc string) port))
(define (count-nonzero-chars port)
;; helper function for String
;; counts nonzero chars from current position
(length (car (regexp-match-peek "[^\u0]*" port))))
;; helper function for String
;; counts nonzero chars from current position
(length (car (regexp-match-peek "[^\u0]*" port))))
(define (byte-length val encoding)
(define encoder
@ -78,8 +78,22 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
(define-values (String? +String) (values StringT? +StringT))
(define-subclass StringT (Symbol)
(define/override (post-decode string-val . _)
(string->symbol string-val))
(define/override (pre-encode sym-val . _)
(unless (or (string? sym-val) (symbol? sym-val))
(raise-argument-error 'Symbol "symbol or string" sym-val))
(if (symbol? sym-val) sym-val (string->symbol sym-val))))
(test-module
(define S (+String uint8 'utf8))
(check-equal? (send S decode #"\2BCDEF") "BC")
(check-equal? (send S encode #f "Mike") #"\4Mike")
(check-equal? (send (+String) size "foobar") 7)) ; null terminated when no len
(define S (+String uint8 'utf8))
(check-equal? (decode S #"\2BCDEF") "BC")
(check-equal? (encode S "Mike" #f) #"\4Mike")
(check-equal? (size (+String) "foobar") 7) ; null terminated when no len
(check-equal? (decode (+Symbol 4) #"Mike") 'Mike)
(check-equal? (encode (+Symbol 4) 'Mike #f) #"Mike")
(check-equal? (encode (+Symbol 4) "Mike" #f) #"Mike")
(check-exn exn:fail:contract? (λ () (encode (+Symbol 4) 42 #f))))

@ -26,7 +26,11 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(if (LazyThunk? res) ((LazyThunk-proc res)) res))
(define (dict-remove! d k) (d:dict-remove! (choose-dict d k) k))
;; public keys only
(define (dict-keys d) (d:dict-keys (get-field _kv d))))]
(define (dict-keys d) (d:dict-keys (get-field _kv d)))
(define (dict-iterate-first d) (and (pair? (dict-keys d)) 0))
(define (dict-iterate-next d i) (and (< (add1 i) (length (dict-keys d))) (add1 i)))
(define (dict-iterate-key d i) (list-ref (dict-keys d) i))
(define (dict-iterate-value d i) (dict-ref d (dict-iterate-key d i))))]
[(generic-property gen:custom-write)
(generic-method-table gen:custom-write
(define (write-proc o port mode)
@ -34,23 +38,29 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
[(#t) write]
[(#f) display]
[else (λ (p port) (print p port mode))]))
(proc (get-field _kv o) port)))])))
(proc (dump o) port)))])))
(define StructDictRes (class* RestructureBase (dictable<%>)
(super-make-object)
(field [_kv (mhasheq)]
[_pvt (mhasheq)])
(define-subclass*/interfaces xenomorph-base% (dictable<%>)
(StructDictRes)
(super-make-object)
(field [_kv (mhasheq)]
[_pvt (mhasheq)])
(define/override (dump) _kv)))
(define/override (dump)
;; convert to immutable for display & debug
(for/hasheq ([(k v) (in-hash _kv)])
(values k v)))
(define/public (to-hash) _kv))
(define-subclass xenomorph-base% (Struct [fields (dictify)])
(field [[_post-decode post-decode] (λ (val port ctx) val)]
[[_pre-encode pre-encode] (λ (val port) val)]) ; store as field so it can be mutated from outside
(define/overment (post-decode res stream [ctx #f])
(let* ([res (_post-decode res stream ctx)]
[res (inner res post-decode res stream ctx)])
(define/overment (post-decode res . args)
(let* ([res (apply _post-decode res args)]
[res (inner res post-decode res . args)])
(unless (dict? res) (raise-result-error 'Struct:post-decode "dict" res))
res))
@ -65,30 +75,30 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define/augride (decode stream [parent #f] [len 0])
;; _setup and _parse-fields are separate to cooperate with VersionedStruct
(let* ([res (_setup stream parent len)]
[res (_parse-fields stream res fields)])
res))
(let* ([sdr (_setup stream parent len)] ; returns StructDictRes
[sdr (_parse-fields stream sdr fields)])
sdr))
(define/public-final (_setup port parent len)
(define res (make-object StructDictRes)) ; not mere hash
(dict-set*! res 'parent parent
(define sdr (make-object StructDictRes)) ; not mere hash
(dict-set*! sdr 'parent parent
'_startOffset (pos port)
'_currentOffset 0
'_length len)
res)
sdr)
(define/public-final (_parse-fields port res fields)
(define/public-final (_parse-fields port sdr fields)
(unless (assocs? fields)
(raise-argument-error '_parse-fields "assocs" fields))
(for/fold ([res res])
(for/fold ([sdr sdr])
([(key type) (in-dict fields)])
(define val (if (procedure? type)
(type res)
(send type decode port res)))
(type sdr)
(send type decode port sdr)))
(unless (void? val)
(ref-set! res key val))
(ref-set! res '_currentOffset (- (pos port) (· res _startOffset)))
res))
(dict-set! sdr key val))
(dict-set! sdr '_currentOffset (- (pos port) (· sdr _startOffset)))
sdr))
(define/augride (size [val #f] [parent #f] [include-pointers #t])

@ -14,7 +14,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(check-equal?
(dump (decode (+Struct (dictify 'name (+StringT uint8)
'age uint8))))
(mhasheq 'name "roxyb" 'age 21)))
(hasheq 'name "roxyb" 'age 21)))
@ -25,7 +25,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
'age uint8)))
(set-field! post-decode struct (λ (o . _) (ref-set! o 'canDrink (>= (· o age) 21)) o))
(check-equal? (dump (decode struct))
(mhasheq 'name "roxyb" 'age 32 'canDrink #t)))
(hasheq 'name "roxyb" 'age 32 'canDrink #t)))
@ -36,7 +36,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
'age uint8
'canDrink (λ (o) (>= (ref o 'age) 21)))))
(check-equal? (dump (decode struct))
(mhasheq 'name "roxyb" 'age 32 'canDrink #t)))
(hasheq 'name "roxyb" 'age 32 'canDrink #t)))
@ -95,7 +95,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal? (dump (decode (+Struct (dictify 'name (+StringT uint8)
'age uint8))))
(mhasheq 'name "roxyb" 'age 21)))
(hasheq 'name "roxyb" 'age 21)))
; it 'should support preEncode hook', (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 "roxyb" 'age 21 'ptr "hello"))
(encode struct (hasheq 'name "roxyb" 'age 21 'ptr "hello"))
(check-equal? (dump (current-output-port)) #"\x05roxyb\x15\x08\x05hello"))

@ -18,15 +18,15 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))])
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb"
'age 21
'version 0)))
(check-equal? (dump (decode struct)) (hasheq 'name "roxyb"
'age 21
'version 0)))
(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))))
(check-equal? (dump (decode struct)) (hasheq 'name "roxyb 🤘"
'age 21
'version 1
'gender 0))))
; it 'should throw for unknown version', ->
@ -55,17 +55,17 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))])
(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)))
(check-equal? (dump (decode struct)) (hasheq 'name "roxyb"
'age 21
'alive 1
'version 0)))
(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
'gender 0))))
(check-equal? (dump (decode struct)) (hasheq 'name "roxyb 🤘"
'age 21
'version 1
'alive 1
'gender 0))))
; it 'should support parent version key', ->
@ -79,15 +79,15 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))])
(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)))
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (hasheq 'name "roxyb"
'age 21
'version 0)))
(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))))
(check-equal? (dump (decode struct #:parent (mhash 'version 1))) (hasheq 'name "roxyb 🤘"
'age 21
'version 1
'gender 0))))
@ -105,18 +105,18 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'isDessert uint8)))))])
(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)))
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (hasheq 'name "roxyb"
'age 21
'version 0)))
(parameterize ([current-input-port (open-input-bytes #"\x01\x00\x05pasta")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "pasta"
'version 0)))
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (hasheq 'name "pasta"
'version 0)))
(parameterize ([current-input-port (open-input-bytes #"\x01\x01\x09ice cream\x01")])
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (mhasheq 'name "ice cream"
'isDessert 1
'version 1))))
(check-equal? (dump (decode struct #:parent (mhash 'version 0))) (hasheq 'name "ice cream"
'isDessert 1
'version 1))))
;
@ -131,10 +131,10 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender uint8)))])
(set-field! post-decode struct (λ (o stream ctx) (ref-set! o 'processed "true") o))
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (dump (decode struct)) (mhasheq 'name "roxyb"
'processed "true"
'age 21
'version 0))))
(check-equal? (dump (decode struct)) (hasheq 'name "roxyb"
'processed "true"
'age 21
'version 0))))
;
@ -331,9 +331,9 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
[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 "roxyb"
'age 21
'version 0) stream)
'age 21
'version 0) stream)
(encode struct (mhasheq 'name "roxyb 🤘"
'age 21
'gender 0) stream)
'age 21
'gender 0) stream)
(check-equal? (dump stream) (string->bytes/utf-8 "\x00\x05roxyb\x15\x01\x0aroxyb 🤘\x15\x00")))

Loading…
Cancel
Save