From 7df1ae2e21e2d5a47fd1e697e44efc663d73f399 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 23 Jun 2017 17:10:10 -0700 Subject: [PATCH] resume in buffer-test: encoding --- pitfall/restructure/buffer-test.rkt | 31 ++++++++++++++++++++++++----- pitfall/restructure/buffer.rkt | 8 +++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/pitfall/restructure/buffer-test.rkt b/pitfall/restructure/buffer-test.rkt index de28316a..0db2df7c 100644 --- a/pitfall/restructure/buffer-test.rkt +++ b/pitfall/restructure/buffer-test.rkt @@ -6,8 +6,6 @@ approximates https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee |# - - ;describe 'Buffer', -> ; describe 'decode', -> ; it 'should decode', -> @@ -16,10 +14,10 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee ; buf.decode(stream).should.deep.equal new Buffer [0xab, 0xff] ; buf.decode(stream).should.deep.equal new Buffer [0x1f, 0xb6] -(let ([stream (+DecodeStream (+Buffer (list #xab #xff #x1f #xb6)))] +(let ([stream (+DecodeStream (+Buffer '(#xab #xff #x1f #xb6)))] [buf (+BufferT 2)]) - (check-equal? (send buf decode stream) (list #xab #xff)) - (check-equal? (send buf decode stream) (list #x1f #xb6))) + (check-equal? (send buf decode stream) (+Buffer '(#xab #xff))) + (check-equal? (send buf decode stream) (+Buffer '(#x1f #xb6)))) ; @@ -28,15 +26,31 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee ; buf = new BufferT('len') ; buf.decode(stream, len: 3).should.deep.equal new Buffer [0xab, 0xff, 0x1f] ; buf.decode(stream, len: 1).should.deep.equal new Buffer [0xb6] + +(let ([stream (+DecodeStream (+Buffer '(#xab #xff #x1f #xb6)))] + [buf (+BufferT 'len)]) + (check-equal? (send buf decode stream (hash 'len 3)) (+Buffer '(#xab #xff #x1f))) + (check-equal? (send buf decode stream (hash 'len 1)) (+Buffer '(#xb6)))) + + ; ; describe 'size', -> ; it 'should return size', -> ; buf = new BufferT(2) ; buf.size(new Buffer [0xab, 0xff]).should.equal 2 + +(let ([buf (+BufferT 2)]) + (check-equal? (send buf size (+Buffer '(#xab #xff))) 2)) + ; ; it 'should use defined length if no value given', -> ; array = new BufferT 10 ; array.size().should.equal 10 + +(let ([array (+BufferT 10)]) + (check-equal? (send array size) 10)) + + ; ; describe 'encode', -> ; it 'should encode', (done) -> @@ -50,6 +64,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee ; buf.encode stream, new Buffer [0x1f, 0xb6] ; stream.end() ; + +(let ([buf (+BufferT 2)] + [stream (+EncodeStream)]) + (send buf encode stream (+Buffer '(#xab #xff))) + (send buf encode stream (+Buffer '(#x1f #xb6))) + (check-equal? (send stream dump) (+Buffer '(#xab #xff #x1f #xb6)))) + ; it 'should encode length before buffer', (done) -> ; stream = new EncodeStream ; stream.pipe concat (buf) -> diff --git a/pitfall/restructure/buffer.rkt b/pitfall/restructure/buffer.rkt index 6686962e..38113089 100644 --- a/pitfall/restructure/buffer.rkt +++ b/pitfall/restructure/buffer.rkt @@ -33,14 +33,16 @@ A Restructure RBuffer object is separate. (bytes-length val) (utils-resolveLength length_ val parent))) - (define/override (encode stream buf [parent #f]) + (define/override (encode stream buf-in [parent #f]) + (define buf (if (bytes? buf-in) (bytes->list buf-in) buf-in)) (unless (and (list? buf) (andmap byte? buf)) (raise-argument-error 'Buffer:encode "list of bytes" buf)) + (report* buf length_) (when (NumberT? length_) - (send length_ encode stream (length buf)) + (send length_ encode stream (*length buf)) (send stream writeBuffer buf)))) - +(define-subclass RBuffer (BufferT)) #;(test-module