resume in buffer-test: encoding

main
Matthew Butterick 8 years ago
parent 6ae7b64775
commit 7df1ae2e21

@ -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) ->

@ -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

Loading…
Cancel
Save