From 6008236b1ec6ec93c0ccab58807b8353e85cb76c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 3 Jul 2017 12:20:21 -0700 Subject: [PATCH] reorg tests; add enum, reserved, and optional types --- pitfall/restructure/enum.rkt | 23 ++ pitfall/restructure/lazy-array.rkt | 3 +- pitfall/restructure/main.rkt | 29 +-- pitfall/restructure/optional.rkt | 29 +++ pitfall/restructure/pointer.rkt | 4 +- pitfall/restructure/reserved.rkt | 21 ++ pitfall/restructure/stream.rkt | 43 ++-- pitfall/restructure/struct.rkt | 6 +- pitfall/restructure/test.rkt | 22 -- pitfall/restructure/{ => test}/array-test.rkt | 4 +- .../restructure/{ => test}/bitfield-test.rkt | 4 +- .../restructure/{ => test}/buffer-test.rkt | 4 +- pitfall/restructure/test/enum-test.rkt | 54 +++++ .../{ => test}/lazy-array-test.rkt | 3 +- pitfall/restructure/test/main.rkt | 15 ++ .../restructure/{ => test}/number-test.rkt | 4 +- pitfall/restructure/test/optional-test.rkt | 200 ++++++++++++++++++ .../restructure/{ => test}/pointer-test.rkt | 3 +- pitfall/restructure/test/racket.rkt | 6 + pitfall/restructure/test/reserved-test.rkt | 50 +++++ .../restructure/{ => test}/stream-test.rkt | 4 +- .../restructure/{ => test}/string-test.rkt | 4 +- .../restructure/{ => test}/struct-test.rkt | 13 +- pitfall/restructure/test/test.rkt | 18 ++ .../{ => test}/versioned-struct-test.rkt | 3 +- 25 files changed, 479 insertions(+), 90 deletions(-) create mode 100644 pitfall/restructure/enum.rkt create mode 100644 pitfall/restructure/optional.rkt create mode 100644 pitfall/restructure/reserved.rkt delete mode 100644 pitfall/restructure/test.rkt rename pitfall/restructure/{ => test}/array-test.rkt (98%) rename pitfall/restructure/{ => test}/bitfield-test.rkt (94%) rename pitfall/restructure/{ => test}/buffer-test.rkt (94%) create mode 100644 pitfall/restructure/test/enum-test.rkt rename pitfall/restructure/{ => test}/lazy-array-test.rkt (95%) create mode 100644 pitfall/restructure/test/main.rkt rename pitfall/restructure/{ => test}/number-test.rkt (99%) create mode 100644 pitfall/restructure/test/optional-test.rkt rename pitfall/restructure/{ => test}/pointer-test.rkt (99%) create mode 100644 pitfall/restructure/test/racket.rkt create mode 100644 pitfall/restructure/test/reserved-test.rkt rename pitfall/restructure/{ => test}/stream-test.rkt (98%) rename pitfall/restructure/{ => test}/string-test.rkt (97%) rename pitfall/restructure/{ => test}/struct-test.rkt (92%) create mode 100644 pitfall/restructure/test/test.rkt rename pitfall/restructure/{ => test}/versioned-struct-test.rkt (99%) diff --git a/pitfall/restructure/enum.rkt b/pitfall/restructure/enum.rkt new file mode 100644 index 00000000..48ff1487 --- /dev/null +++ b/pitfall/restructure/enum.rkt @@ -0,0 +1,23 @@ +#lang restructure/racket +(require "stream.rkt") +(provide (all-defined-out)) + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee +|# + +(define-subclass Streamcoder (Enum type [options empty]) + + (define/augment (decode stream . _) + (define index (send type decode stream)) + (or (list-ref options index) index)) + + (define/override (size . _) (send type size)) + + (define/augment (encode stream val [ctx #f]) + (define index (index-of options val)) + (unless index + (raise-argument-error 'Enum:encode "valid option" val)) + (send type encode stream index))) + diff --git a/pitfall/restructure/lazy-array.rkt b/pitfall/restructure/lazy-array.rkt index 5c69ab1f..0b7d93e7 100644 --- a/pitfall/restructure/lazy-array.rkt +++ b/pitfall/restructure/lazy-array.rkt @@ -14,7 +14,8 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee (define/public-final (get index) (unless (<= 0 index (sub1 len)) - (raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index)) + #;(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index) + (void)) (ref! item-cache index (λ () (define orig-pos (· stream pos)) (send stream pos (+ starting-pos (* (send type size #f ctx) index))) diff --git a/pitfall/restructure/main.rkt b/pitfall/restructure/main.rkt index a86d4fc0..0cb6a8b9 100644 --- a/pitfall/restructure/main.rkt +++ b/pitfall/restructure/main.rkt @@ -1,25 +1,16 @@ #lang restructure/racket (r+p "base.rkt" - "number.rkt" - "struct.rkt" - "versioned-struct.rkt" - "string.rkt" "array.rkt" - "lazy-array.rkt" "bitfield.rkt" - "stream.rkt" "buffer.rkt" - "pointer.rkt") - -(test-module - (require "number-test.rkt" - "struct-test.rkt" - "versioned-struct-test.rkt" - "string-test.rkt" - "array-test.rkt" - "lazy-array-test.rkt" - "bitfield-test.rkt" - "stream-test.rkt" - "buffer-test.rkt" - "pointer-test.rkt")) \ No newline at end of file + "enum.rkt" + "lazy-array.rkt" + "number.rkt" + "optional.rkt" + "pointer.rkt" + "reserved.rkt" + "stream.rkt" + "string.rkt" + "struct.rkt" + "versioned-struct.rkt") diff --git a/pitfall/restructure/optional.rkt b/pitfall/restructure/optional.rkt new file mode 100644 index 00000000..c76d7867 --- /dev/null +++ b/pitfall/restructure/optional.rkt @@ -0,0 +1,29 @@ +#lang restructure/racket +(require "stream.rkt") +(provide (all-defined-out)) + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee +|# + +(define-subclass Streamcoder (Optional type [condition #t]) + + (define (resolve-condition parent) + (if (procedure? condition) + (condition parent) + condition)) + + (define/augment (decode stream parent) + (when (resolve-condition parent) + (send type decode stream parent))) + + (define/override (size [val #f] [parent #f]) + (if (resolve-condition parent) + (send type size val parent) + 0)) + + (define/augment (encode stream val parent) + (when (resolve-condition parent) + (send type encode stream val parent)))) + diff --git a/pitfall/restructure/pointer.rkt b/pitfall/restructure/pointer.rkt index 5d068a13..b65e90fe 100644 --- a/pitfall/restructure/pointer.rkt +++ b/pitfall/restructure/pointer.rkt @@ -40,10 +40,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee (relative-getter-or-0 ctx))) (define ptr (+ offset relative)) (cond - [type (define val undefined) + [type (define val (void)) (define (decode-value) (cond - [(not (eq? val undefined)) val] + [(not (void? val)) val] [else (define orig-pos (· stream pos)) (send stream pos ptr) diff --git a/pitfall/restructure/reserved.rkt b/pitfall/restructure/reserved.rkt new file mode 100644 index 00000000..adb8e165 --- /dev/null +++ b/pitfall/restructure/reserved.rkt @@ -0,0 +1,21 @@ +#lang restructure/racket +(require "stream.rkt" "utils.rkt") +(provide (all-defined-out)) + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee +|# + +(define-subclass Streamcoder (Reserved type [count 1]) + + (define/augment (decode stream parent) + (send stream pos (+ (· stream pos) (size #f parent))) + (void)) + + (define/override (size [val #f] [parent #f]) + (* (send type size) (resolve-length count #f parent))) + + (define/augment (encode stream val [parent #f]) + (send stream fill 0 (size val parent)))) + diff --git a/pitfall/restructure/stream.rkt b/pitfall/restructure/stream.rkt index 8a261b59..3be98bec 100644 --- a/pitfall/restructure/stream.rkt +++ b/pitfall/restructure/stream.rkt @@ -34,7 +34,8 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee (define/public-final (write val) (unless (bytes? val) (raise-argument-error 'EncodeStream:write "bytes" val)) - (void (write-bytes val _port))) + (write-bytes val _port) + (void)) (define/public-final (writeBuffer buffer) (write buffer)) @@ -49,25 +50,31 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee [(utf16le ucs2 utf8 ascii) (writeBuffer (string->bytes/utf-8 string)) (when (eq? encoding 'utf16le) (error 'swap-bytes-unimplemented))] - [else (error 'unsupported-string-encoding)]))) + [else (error 'unsupported-string-encoding)])) + + (define/public (fill val len) + (write (make-bytes len val)))) (test-module - (define es (+EncodeStream)) - (check-true (EncodeStream? es)) - (send es write #"AB") - (check-equal? (· es pos) 2) - (send es write #"C") - (check-equal? (· es pos) 3) - (send es write #"D") - (check-equal? (· es pos) 4) - (check-exn exn:fail? (λ () (send es write -42))) - (check-exn exn:fail? (λ () (send es write 1))) - (define op (open-output-bytes)) - (define es2 (+EncodeStream op)) - (send es2 write #"FOOBAR") - (check-equal? (send es2 dump) #"FOOBAR") - (check-equal? (send es2 dump) #"FOOBAR") ; dump can repeat - (check-equal? (get-output-bytes op) #"FOOBAR")) + (define es (+EncodeStream)) + (check-true (EncodeStream? es)) + (send es write #"AB") + (check-equal? (· es pos) 2) + (send es write #"C") + (check-equal? (· es pos) 3) + (send es write #"D") + (check-equal? (· es pos) 4) + (check-exn exn:fail? (λ () (send es write -42))) + (check-exn exn:fail? (λ () (send es write 1))) + (define op (open-output-bytes)) + (define es2 (+EncodeStream op)) + (send es2 write #"FOOBAR") + (check-equal? (send es2 dump) #"FOOBAR") + (check-equal? (send es2 dump) #"FOOBAR") ; dump can repeat + (check-equal? (get-output-bytes op) #"FOOBAR") + (define es3 (+EncodeStream)) + (send es3 fill 0 10) + (check-equal? (send es3 dump) (make-bytes 10 0))) #| approximates diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index 44003700..1f77acca 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -1,6 +1,6 @@ #lang restructure/racket (require racket/dict "stream.rkt" racket/private/generic-methods racket/struct) -(provide (all-defined-out) ref* ref*-set!) +(provide (all-defined-out) ref* ref*-set! (all-from-out racket/dict)) (require (prefix-in d: racket/dict)) #| @@ -82,8 +82,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (define val (if (procedure? type) (type res) (send type decode stream res))) - ;; skip PropertyDescriptor maneuver. Only used for lazy pointer - (ref-set! res key val) + (unless (void? val) + (ref-set! res key val)) (ref-set! res '_currentOffset (- (· stream pos) (· res _startOffset))) res)) diff --git a/pitfall/restructure/test.rkt b/pitfall/restructure/test.rkt deleted file mode 100644 index abd8b6c8..00000000 --- a/pitfall/restructure/test.rkt +++ /dev/null @@ -1,22 +0,0 @@ -#lang restructure/racket -(require "main.rkt") - -(define Person - (make-object RStruct - (list (cons 'name (make-object RString uint8 'utf8)) - (cons 'age uint8)))) - -;; decode a person from a buffer -(define stream-in (make-object RDecodeStream #"\4MikeA")) -(define x (send Person decode stream-in)) - -(test-module - (check-equal? (hash-ref x 'name) "Mike") - (check-equal? (hash-ref x 'age) 65)) - -;; encode a person from a hash -(define stream-out (make-object REncodeStream)) -(send Person encode stream-out (hasheq 'name "Mike" 'age 65)) - -(test-module - (check-equal? (send stream-out dump) #"\4MikeA")) \ No newline at end of file diff --git a/pitfall/restructure/array-test.rkt b/pitfall/restructure/test/array-test.rkt similarity index 98% rename from pitfall/restructure/array-test.rkt rename to pitfall/restructure/test/array-test.rkt index c4a96b4b..87c952d0 100644 --- a/pitfall/restructure/array-test.rkt +++ b/pitfall/restructure/test/array-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "array.rkt" "stream.rkt" "number.rkt" "buffer.rkt" rackunit "pointer.rkt") +#lang restructure/test/racket + #| approximates diff --git a/pitfall/restructure/bitfield-test.rkt b/pitfall/restructure/test/bitfield-test.rkt similarity index 94% rename from pitfall/restructure/bitfield-test.rkt rename to pitfall/restructure/test/bitfield-test.rkt index a10dc04b..31fd5e72 100644 --- a/pitfall/restructure/bitfield-test.rkt +++ b/pitfall/restructure/test/bitfield-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "bitfield.rkt" "number.rkt" "stream.rkt" "buffer.rkt" rackunit racket/match) +#lang restructure/test/racket +(require racket/match) #| approximates diff --git a/pitfall/restructure/buffer-test.rkt b/pitfall/restructure/test/buffer-test.rkt similarity index 94% rename from pitfall/restructure/buffer-test.rkt rename to pitfall/restructure/test/buffer-test.rkt index a90ad4c1..2a420fc2 100644 --- a/pitfall/restructure/buffer-test.rkt +++ b/pitfall/restructure/test/buffer-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "buffer.rkt" "stream.rkt" "number.rkt" rackunit) +#lang restructure/test/racket + #| approximates diff --git a/pitfall/restructure/test/enum-test.rkt b/pitfall/restructure/test/enum-test.rkt new file mode 100644 index 00000000..cbb825b5 --- /dev/null +++ b/pitfall/restructure/test/enum-test.rkt @@ -0,0 +1,54 @@ +#lang restructure/test/racket + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee +|# + +;describe 'Enum', -> +; e = new Enum uint8, ['foo', 'bar', 'baz'] +; it 'should have the right size', -> +; e.size().should.equal 1 + +(define e (+Enum uint8 '("foo" "bar" "baz"))) + +(check-equal? (send e size) 1) + + +; +; it 'should decode', -> +; stream = new DecodeStream new Buffer [1, 2, 0] +; e.decode(stream).should.equal 'bar' +; e.decode(stream).should.equal 'baz' +; e.decode(stream).should.equal 'foo' + +(let ([stream (+DecodeStream (+Buffer '(1 2 0)))]) + (check-equal? (send e decode stream) "bar") + (check-equal? (send e decode stream) "baz") + (check-equal? (send e decode stream) "foo")) + +; +; it 'should encode', (done) -> +; stream = new EncodeStream +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [1, 2, 0] +; done() +; +; e.encode stream, 'bar' +; e.encode stream, 'baz' +; e.encode stream, 'foo' +; stream.end() + +(let ([stream (+EncodeStream)]) + (send e encode stream "bar") + (send e encode stream "baz") + (send e encode stream "foo") + (check-equal? (send stream dump) (+Buffer '(1 2 0)))) + +; +; it 'should throw on unknown option', -> +; stream = new EncodeStream +; should.throw -> +; e.encode stream, 'unknown' + +(check-exn exn:fail:contract? (λ () (send e encode (+EncodeStream) "unknown"))) \ No newline at end of file diff --git a/pitfall/restructure/lazy-array-test.rkt b/pitfall/restructure/test/lazy-array-test.rkt similarity index 95% rename from pitfall/restructure/lazy-array-test.rkt rename to pitfall/restructure/test/lazy-array-test.rkt index f74d68fa..506a6ba2 100644 --- a/pitfall/restructure/lazy-array-test.rkt +++ b/pitfall/restructure/test/lazy-array-test.rkt @@ -1,5 +1,4 @@ -#lang restructure/racket -(require "lazy-array.rkt" "array.rkt" "stream.rkt" "number.rkt" "buffer.rkt" rackunit) +#lang restructure/test/racket #| approximates diff --git a/pitfall/restructure/test/main.rkt b/pitfall/restructure/test/main.rkt new file mode 100644 index 00000000..1ae6ea14 --- /dev/null +++ b/pitfall/restructure/test/main.rkt @@ -0,0 +1,15 @@ +#lang restructure/racket + +(require "array-test.rkt" + "bitfield-test.rkt" + "buffer-test.rkt" + "enum-test.rkt" + "lazy-array-test.rkt" + "number-test.rkt" + "optional-test.rkt" + "pointer-test.rkt" + "reserved-test.rkt" + "stream-test.rkt" + "string-test.rkt" + "struct-test.rkt" + "versioned-struct-test.rkt") diff --git a/pitfall/restructure/number-test.rkt b/pitfall/restructure/test/number-test.rkt similarity index 99% rename from pitfall/restructure/number-test.rkt rename to pitfall/restructure/test/number-test.rkt index 580497d1..2857c86d 100644 --- a/pitfall/restructure/number-test.rkt +++ b/pitfall/restructure/test/number-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "number.rkt" "stream.rkt" rackunit) +#lang restructure/test/racket + #| approximates diff --git a/pitfall/restructure/test/optional-test.rkt b/pitfall/restructure/test/optional-test.rkt new file mode 100644 index 00000000..eb350dba --- /dev/null +++ b/pitfall/restructure/test/optional-test.rkt @@ -0,0 +1,200 @@ +#lang restructure/test/racket + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/test/Optional.coffee +|# + +;describe 'Optional', -> +; describe 'decode', -> +; it 'should not decode when condition is falsy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, false +; should.not.exist optional.decode(stream) +; stream.pos.should.equal 0 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 #f)]) + (check-equal? (send optional decode stream) (void)) + (check-equal? (· stream pos) 0)) + + +; it 'should not decode when condition is a function and falsy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, -> false +; should.not.exist optional.decode(stream) +; stream.pos.should.equal 0 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 (λ _ #f))]) + (check-equal? (send optional decode stream) (void)) + (check-equal? (· stream pos) 0)) + +; +; it 'should decode when condition is omitted', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8 +; should.exist optional.decode(stream) +; stream.pos.should.equal 1 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8)]) + (check-not-equal? (send optional decode stream) (void)) + (check-equal? (· stream pos) 1)) + +; +; it 'should decode when condition is truthy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, true +; should.exist optional.decode(stream) +; stream.pos.should.equal 1 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 #t)]) + (check-not-equal? (send optional decode stream) (void)) + (check-equal? (· stream pos) 1)) + +; +; it 'should decode when condition is a function and truthy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, -> true +; should.exist optional.decode(stream) +; stream.pos.should.equal 1 + + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 (λ _ #t))]) + (check-not-equal? (send optional decode stream) (void)) + (check-equal? (· stream pos) 1)) + +; +; describe 'size', -> +; it 'should return 0 when condition is falsy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, false +; optional.size().should.equal 0 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 #f)]) + (check-equal? (· optional size) 0)) + +; +; it 'should return 0 when condition is a function and falsy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, -> false +; optional.size().should.equal 0 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 (λ _ #f))]) + (check-equal? (· optional size) 0)) + +; +; it 'should return given type size when condition is omitted', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8 +; optional.size().should.equal 1 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8)]) + (check-equal? (· optional size) 1)) +; +; it 'should return given type size when condition is truthy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, true +; optional.size().should.equal 1 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 #t)]) + (check-equal? (· optional size) 1)) + +; +; it 'should return given type size when condition is a function and truthy', -> +; stream = new DecodeStream new Buffer [0] +; optional = new Optional uint8, -> true +; optional.size().should.equal 1 + +(let ([stream (+DecodeStream (+Buffer '(0)))] + [optional (+Optional uint8 (λ _ #t))]) + (check-equal? (· optional size) 1)) + +; +; describe 'encode', -> +; it 'should not encode when condition is falsy', (done) -> +; stream = new EncodeStream +; optional = new Optional uint8, false +; stream.pipe concat (buf) -> +; buf.should.deep.equal [] +; done() +; +; optional.encode stream, 128 +; stream.end() + +(let ([stream (+EncodeStream)] + [optional (+Optional uint8 #f)]) + (send optional encode stream 128) + (check-equal? (send stream dump) (+Buffer empty))) + +; +; it 'should not encode when condition is a function and falsy', (done) -> +; stream = new EncodeStream +; optional = new Optional uint8, -> false +; stream.pipe concat (buf) -> +; buf.should.deep.equal [] +; done() +; +; optional.encode stream, 128 +; stream.end() + +(let ([stream (+EncodeStream)] + [optional (+Optional uint8 (λ _ #f))]) + (send optional encode stream 128) + (check-equal? (send stream dump) (+Buffer empty))) + + +; +; it 'should encode when condition is omitted', (done) -> +; stream = new EncodeStream +; optional = new Optional uint8 +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [128] +; done() +; +; optional.encode stream, 128 +; stream.end() + +(let ([stream (+EncodeStream)] + [optional (+Optional uint8)]) + (send optional encode stream 128) + (check-equal? (send stream dump) (+Buffer '(128)))) + +; +; it 'should encode when condition is truthy', (done) -> +; stream = new EncodeStream +; optional = new Optional uint8, true +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [128] +; done() +; +; optional.encode stream, 128 +; stream.end() + +(let ([stream (+EncodeStream)] + [optional (+Optional uint8 #t)]) + (send optional encode stream 128) + (check-equal? (send stream dump) (+Buffer '(128)))) + +; +; it 'should encode when condition is a function and truthy', (done) -> +; stream = new EncodeStream +; optional = new Optional uint8, -> true +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [128] +; done() +; +; optional.encode stream, 128 +; stream.end() + +(let ([stream (+EncodeStream)] + [optional (+Optional uint8 (λ _ #t))]) + (send optional encode stream 128) + (check-equal? (send stream dump) (+Buffer '(128)))) \ No newline at end of file diff --git a/pitfall/restructure/pointer-test.rkt b/pitfall/restructure/test/pointer-test.rkt similarity index 99% rename from pitfall/restructure/pointer-test.rkt rename to pitfall/restructure/test/pointer-test.rkt index 271db3a0..ab0f1a2c 100644 --- a/pitfall/restructure/pointer-test.rkt +++ b/pitfall/restructure/test/pointer-test.rkt @@ -1,5 +1,4 @@ -#lang restructure/racket -(require "pointer.rkt" "stream.rkt" "buffer.rkt" "base.rkt" "number.rkt" "struct.rkt" rackunit) +#lang restructure/test/racket #| approximates diff --git a/pitfall/restructure/test/racket.rkt b/pitfall/restructure/test/racket.rkt new file mode 100644 index 00000000..ebdb51e8 --- /dev/null +++ b/pitfall/restructure/test/racket.rkt @@ -0,0 +1,6 @@ +#lang racket/base +(require rackunit restructure restructure/racket) +(provide (all-from-out rackunit restructure restructure/racket)) + +(module reader syntax/module-reader + #:language 'restructure/test/racket) \ No newline at end of file diff --git a/pitfall/restructure/test/reserved-test.rkt b/pitfall/restructure/test/reserved-test.rkt new file mode 100644 index 00000000..900a5ace --- /dev/null +++ b/pitfall/restructure/test/reserved-test.rkt @@ -0,0 +1,50 @@ +#lang restructure/test/racket + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/test/Reserved.coffee +|# + +;describe 'Reserved', -> +; it 'should have a default count of 1', -> +; reserved = new Reserved uint8 +; reserved.size().should.equal 1 + +(let ([reserved (+Reserved uint8)]) + (check-equal? (send reserved size) 1)) + +; +; it 'should allow custom counts and types', -> +; reserved = new Reserved uint16, 10 +; reserved.size().should.equal 20 + +(let ([reserved (+Reserved uint16be 10)]) + (check-equal? (send reserved size) 20)) + +; +; it 'should decode', -> +; stream = new DecodeStream new Buffer [0, 0] +; reserved = new Reserved uint16 +; should.not.exist reserved.decode(stream) +; stream.pos.should.equal 2 + +(let ([stream (+DecodeStream (+Buffer '(0 0)))] + [reserved (+Reserved uint16be)]) + (check-equal? (send reserved decode stream) (void)) + (check-equal? (send stream pos) 2)) + +; +; it 'should encode', (done) -> +; stream = new EncodeStream +; reserved = new Reserved uint16 +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [0, 0] +; done() +; +; reserved.encode stream +; stream.end() + +(let ([stream (+EncodeStream)] + [reserved (+Reserved uint16be)]) + (send reserved encode stream) + (check-equal? (send stream dump) (+Buffer '(0 0)))) \ No newline at end of file diff --git a/pitfall/restructure/stream-test.rkt b/pitfall/restructure/test/stream-test.rkt similarity index 98% rename from pitfall/restructure/stream-test.rkt rename to pitfall/restructure/test/stream-test.rkt index f0e0f707..42738047 100644 --- a/pitfall/restructure/stream-test.rkt +++ b/pitfall/restructure/test/stream-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "number.rkt" "buffer.rkt" "stream.rkt" rackunit) +#lang restructure/test/racket + #| approximates diff --git a/pitfall/restructure/string-test.rkt b/pitfall/restructure/test/string-test.rkt similarity index 97% rename from pitfall/restructure/string-test.rkt rename to pitfall/restructure/test/string-test.rkt index 6954737d..013140ca 100644 --- a/pitfall/restructure/string-test.rkt +++ b/pitfall/restructure/test/string-test.rkt @@ -1,5 +1,5 @@ -#lang restructure/racket -(require "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit) +#lang restructure/test/racket + #| approximates diff --git a/pitfall/restructure/struct-test.rkt b/pitfall/restructure/test/struct-test.rkt similarity index 92% rename from pitfall/restructure/struct-test.rkt rename to pitfall/restructure/test/struct-test.rkt index 549a0bc4..070bae16 100644 --- a/pitfall/restructure/struct-test.rkt +++ b/pitfall/restructure/test/struct-test.rkt @@ -1,5 +1,4 @@ -#lang restructure/racket -(require "struct.rkt" "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit "pointer.rkt") +#lang restructure/test/racket #| approximates @@ -207,8 +206,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee (let ([stream (+EncodeStream)] - [struct (+Struct (dictify 'name (+StringT uint8) - 'age uint8 - 'ptr (+Pointer uint8 (+StringT uint8))))]) - (send struct encode stream (mhasheq 'name "devon" 'age 21 'ptr "hello")) - (check-equal? (send stream dump) (+Buffer "\x05devon\x15\x08\x05hello"))) \ No newline at end of file + [struct (+Struct (dictify 'name (+StringT uint8) + 'age uint8 + 'ptr (+Pointer uint8 (+StringT uint8))))]) + (send struct encode stream (mhasheq 'name "devon" 'age 21 'ptr "hello")) + (check-equal? (send stream dump) (+Buffer "\x05devon\x15\x08\x05hello"))) \ No newline at end of file diff --git a/pitfall/restructure/test/test.rkt b/pitfall/restructure/test/test.rkt new file mode 100644 index 00000000..018318fe --- /dev/null +++ b/pitfall/restructure/test/test.rkt @@ -0,0 +1,18 @@ +#lang restructure/test/racket + +(define Person + (make-object Struct + (list (cons 'name (make-object StringT uint8 'utf8)) + (cons 'age uint8)))) + +;; decode a person from a buffer +(define stream-in (make-object DecodeStream #"\4MikeA")) +(define x (send Person decode stream-in)) + +(test-module + (check-equal? (dict-ref x 'name) "Mike") + (check-equal? (dict-ref x 'age) 65)) + +;; encode a person from a hash +(test-module + (check-equal? (send Person encode #f (hasheq 'name "Mike" 'age 65)) #"\4MikeA")) \ No newline at end of file diff --git a/pitfall/restructure/versioned-struct-test.rkt b/pitfall/restructure/test/versioned-struct-test.rkt similarity index 99% rename from pitfall/restructure/versioned-struct-test.rkt rename to pitfall/restructure/test/versioned-struct-test.rkt index eadf0d6b..ba31b4f8 100644 --- a/pitfall/restructure/versioned-struct-test.rkt +++ b/pitfall/restructure/test/versioned-struct-test.rkt @@ -1,5 +1,4 @@ -#lang restructure/racket -(require "versioned-struct.rkt" "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit "pointer.rkt") +#lang restructure/test/racket #| approximates