next: make unit tests for struct type

main
Matthew Butterick 8 years ago
parent 2bb9a0afef
commit 567bf6a8f9

@ -19,7 +19,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/fpgm.js
(test-module
(require racket/serialize)
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define offset (· dir tables fpgm offset))

@ -11,5 +11,5 @@
(define-macro (test-module . EXPRS)
#`(module+ test
(require #,(datum->syntax caller-stx 'rackunit))
(require #,(datum->syntax caller-stx 'rackunit) #,(datum->syntax caller-stx 'racket/serialize))
. EXPRS))

@ -23,7 +23,6 @@
)))
(test-module
(require racket/serialize)
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define offset (· dir tables hhea offset))

@ -19,4 +19,5 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/hmtx.js
'bearing uint16be)))
(test-module
)

@ -43,7 +43,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/loca.js
(test-module
(require racket/serialize)
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define offset (· dir tables loca offset))

@ -24,7 +24,6 @@
(test-module
(require racket/serialize)
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define maxp-offset (· dir tables maxp offset))

@ -14,7 +14,6 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/prep.js
(test-module
(require racket/serialize)
(define ip (open-input-file charter-path))
(define dir (deserialize (read (open-input-file charter-directory-path))))
(define offset (· dir tables prep offset))
@ -25,81 +24,4 @@ https://github.com/mbutterick/fontkit/blob/master/src/tables/prep.js
(define table-bytes #"\270\0\0+\0\272\0\1\0\1\0\2+\1\272\0\2\0\1\0\2+\1\277\0\2\0C\0007\0+\0\37\0\23\0\0\0\b+\0\277\0\1\0\200\0i\0R\0;\0#\0\0\0\b+\0\272\0\3\0\5\0\a+\270\0\0 E}i\30D")
(check-equal? table-bytes (peek-bytes len offset ip))
(define ds (+DecodeStream (peek-bytes len offset ip)))
(check-equal? (hash-ref (send prep decode ds) 'controlValueProgram) '(184
0
0
43
0
186
0
1
0
1
0
2
43
1
186
0
2
0
1
0
2
43
1
191
0
2
0
67
0
55
0
43
0
31
0
19
0
0
0
8
43
0
191
0
1
0
128
0
105
0
82
0
59
0
35
0
0
0
8
43
0
186
0
3
0
5
0
7
43
184
0
0
32
69
125
105
24
68)))
(check-equal? (hash-ref (send prep decode ds) 'controlValueProgram) '(184 0 0 43 0 186 0 1 0 1 0 2 43 1 186 0 2 0 1 0 2 43 1 191 0 2 0 67 0 55 0 43 0 31 0 19 0 0 0 8 43 0 191 0 1 0 128 0 105 0 82 0 59 0 35 0 0 0 8 43 0 186 0 3 0 5 0 7 43 184 0 0 32 69 125 105 24 68)))

@ -2,10 +2,11 @@
(provide (all-defined-out))
(define-macro (define-table-decoders ID TABLE-ID ...)
(with-pattern ([TABLE-ID-STRINGS (pattern-case-filter #'(TABLE-ID ...)
(with-pattern ([(TABLE-ID-STRING ...) (pattern-case-filter #'(TABLE-ID ...)
[STX (datum->syntax caller-stx (format "~a.rkt" (syntax->datum #'STX)))])])
#'(begin
(r+p . TABLE-ID-STRINGS)
(r+p TABLE-ID-STRING ...)
(test-module (require (submod TABLE-ID-STRING test) ...))
(define ID (make-hasheq (map cons (list 'TABLE-ID ...) (list TABLE-ID ...)))))))
(define-table-decoders table-decoders maxp hhea head loca prep fpgm hmtx)

@ -16,7 +16,7 @@
(define-macro (test-module . EXPRS)
#`(module+ test
(require #,(datum->syntax caller-stx 'rackunit))
(require #,(datum->syntax caller-stx 'rackunit) #,(datum->syntax caller-stx 'racket/serialize))
. EXPRS))
(define index? (λ (x) (and (number? x) (integer? x) (not (negative? x)))))

@ -23,12 +23,14 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define/augride (encode stream input-hash [parent #f])
(unless (hash? input-hash)
(raise-argument-error 'Struct:encode "hash" input-hash))
(send this preEncode input-hash stream) ; might bring input hash into compliance
(define sorted-input-keys (sort (hash-keys input-hash) #:key symbol->string string<?))
(define sorted-struct-keys (sort key-index #:key symbol->string string<?))
(unless (equal? sorted-input-keys sorted-struct-keys)
(raise-argument-error 'Struct:encode (format "hash with same keys as Struct: ~a" sorted-struct-keys) sorted-input-keys))
(send this preEncode input-hash stream)
(for* ([key (in-list key-index)] ; iterate over original keys in order
[struct-type (in-value (hash-ref fields key))]
[value-to-encode (in-value (hash-ref input-hash key))])

Loading…
Cancel
Save