You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.0 KiB
Racket
200 lines
6.0 KiB
Racket
7 years ago
|
#lang reader (submod "racket.rkt" reader)
|
||
7 years ago
|
|
||
|
#|
|
||
|
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)])
|
||
7 years ago
|
(check-equal? (decode optional stream) (void))
|
||
7 years ago
|
(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))])
|
||
7 years ago
|
(check-equal? (decode optional stream) (void))
|
||
7 years ago
|
(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)])
|
||
7 years ago
|
(check-not-equal? (decode optional stream) (void))
|
||
7 years ago
|
(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)])
|
||
7 years ago
|
(check-not-equal? (decode optional stream) (void))
|
||
7 years ago
|
(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))])
|
||
7 years ago
|
(check-not-equal? (decode optional stream) (void))
|
||
7 years ago
|
(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)])
|
||
7 years ago
|
(encode optional stream 128)
|
||
|
(check-equal? (dump stream) (+Buffer empty)))
|
||
7 years ago
|
|
||
|
;
|
||
|
; 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))])
|
||
7 years ago
|
(encode optional stream 128)
|
||
|
(check-equal? (dump stream) (+Buffer empty)))
|
||
7 years ago
|
|
||
|
|
||
|
;
|
||
|
; 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)])
|
||
7 years ago
|
(encode optional stream 128)
|
||
|
(check-equal? (dump stream) (+Buffer '(128))))
|
||
7 years ago
|
|
||
|
;
|
||
|
; 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)])
|
||
7 years ago
|
(encode optional stream 128)
|
||
|
(check-equal? (dump stream) (+Buffer '(128))))
|
||
7 years ago
|
|
||
|
;
|
||
|
; 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))])
|
||
7 years ago
|
(encode optional stream 128)
|
||
|
(check-equal? (dump stream) (+Buffer '(128))))
|