reorg tests; add enum, reserved, and optional types
parent
dd82c98ef5
commit
6008236b1e
@ -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)))
|
||||
|
@ -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"))
|
||||
"enum.rkt"
|
||||
"lazy-array.rkt"
|
||||
"number.rkt"
|
||||
"optional.rkt"
|
||||
"pointer.rkt"
|
||||
"reserved.rkt"
|
||||
"stream.rkt"
|
||||
"string.rkt"
|
||||
"struct.rkt"
|
||||
"versioned-struct.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))))
|
||||
|
@ -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"))
|
@ -1,5 +1,5 @@
|
||||
#lang restructure/racket
|
||||
(require "array.rkt" "stream.rkt" "number.rkt" "buffer.rkt" rackunit "pointer.rkt")
|
||||
#lang restructure/test/racket
|
||||
|
||||
|
||||
#|
|
||||
approximates
|
@ -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
|
@ -1,5 +1,5 @@
|
||||
#lang restructure/racket
|
||||
(require "buffer.rkt" "stream.rkt" "number.rkt" rackunit)
|
||||
#lang restructure/test/racket
|
||||
|
||||
|
||||
#|
|
||||
approximates
|
@ -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")))
|
@ -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
|
@ -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")
|
@ -1,5 +1,5 @@
|
||||
#lang restructure/racket
|
||||
(require "number.rkt" "stream.rkt" rackunit)
|
||||
#lang restructure/test/racket
|
||||
|
||||
|
||||
#|
|
||||
approximates
|
@ -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))))
|
@ -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
|
@ -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)
|
@ -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))))
|
@ -1,5 +1,5 @@
|
||||
#lang restructure/racket
|
||||
(require "number.rkt" "buffer.rkt" "stream.rkt" rackunit)
|
||||
#lang restructure/test/racket
|
||||
|
||||
|
||||
#|
|
||||
approximates
|
@ -1,5 +1,5 @@
|
||||
#lang restructure/racket
|
||||
(require "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit)
|
||||
#lang restructure/test/racket
|
||||
|
||||
|
||||
#|
|
||||
approximates
|
@ -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"))
|
@ -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
|
Loading…
Reference in New Issue