|
|
|
@ -9,227 +9,120 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
|
|
|
|
|
;describe 'String', ->
|
|
|
|
|
; describe 'decode', ->
|
|
|
|
|
; it 'should decode fixed length', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer 'testing'
|
|
|
|
|
; string = new StringT 7
|
|
|
|
|
; string.decode(stream).should.equal 'testing'
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "testing"))]
|
|
|
|
|
[string (+StringT 7)])
|
|
|
|
|
(check-equal? (decode string stream) "testing"))
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes #"testing")])
|
|
|
|
|
(check-equal? (decode (+StringT 7)) "testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should decode length from parent key', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer 'testing'
|
|
|
|
|
; string = new StringT 'len'
|
|
|
|
|
; string.decode(stream, len: 7).should.equal 'testing'
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "testing"))]
|
|
|
|
|
[string (+StringT 'len)])
|
|
|
|
|
(check-equal? (decode string stream (mhash 'len 7)) "testing"))
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes #"testing")])
|
|
|
|
|
(check-equal? (decode (+StringT 'len) #:parent (mhash 'len 7)) "testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should decode length as number before string', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '\x07testing'
|
|
|
|
|
; string = new StringT uint8
|
|
|
|
|
; string.decode(stream).should.equal 'testing'
|
|
|
|
|
|
|
|
|
|
; octal \7 will print as \a
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "\x07testing"))]
|
|
|
|
|
[string (+StringT uint8)])
|
|
|
|
|
(check-equal? (decode string stream (mhash 'len 7)) "testing"))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should decode utf8', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '🍻'
|
|
|
|
|
; string = new StringT 4, 'utf8'
|
|
|
|
|
; string.decode(stream).should.equal '🍻'
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "🍻"))]
|
|
|
|
|
[string (+StringT 4 'utf8)])
|
|
|
|
|
(check-equal? (decode string stream) "🍻"))
|
|
|
|
|
;
|
|
|
|
|
; it 'should decode encoding computed from function', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '🍻'
|
|
|
|
|
; string = new StringT 4, -> 'utf8'
|
|
|
|
|
; string.decode(stream).should.equal '🍻'
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "🍻"))]
|
|
|
|
|
[string (+StringT 4 (λ _ 'utf8))])
|
|
|
|
|
(check-equal? (decode string stream) "🍻"))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes #"\x07testing")])
|
|
|
|
|
(check-equal? (decode (+StringT uint8) #:parent (mhash 'len 7)) "testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; it 'should decode utf8', ->
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
|
|
|
|
|
(check-equal? (decode (+StringT 4 'utf8)) "🍻"))
|
|
|
|
|
|
|
|
|
|
;; it 'should decode encoding computed from function', ->
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
|
|
|
|
|
(check-equal? (decode (+StringT 4 (λ _ 'utf8))) "🍻"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should decode null-terminated string and read past terminator', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '🍻\x00'
|
|
|
|
|
; string = new StringT null, 'utf8'
|
|
|
|
|
; string.decode(stream).should.equal '🍻'
|
|
|
|
|
; stream.pos.should.equal 5
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "🍻\x00"))]
|
|
|
|
|
[string (+StringT #f 'utf8)])
|
|
|
|
|
(check-equal? (decode string stream) "🍻")
|
|
|
|
|
(check-equal? (pos stream) 5))
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻\x00"))])
|
|
|
|
|
(check-equal? (decode (+StringT #f 'utf8)) "🍻")
|
|
|
|
|
(check-equal? (pos (current-input-port)) 5))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should decode remainder of buffer when null-byte missing', ->
|
|
|
|
|
; stream = new DecodeStream new Buffer '🍻'
|
|
|
|
|
; string = new StringT null, 'utf8'
|
|
|
|
|
; string.decode(stream).should.equal '🍻'
|
|
|
|
|
|
|
|
|
|
(let ([stream (+DecodeStream (+Buffer "🍻"))]
|
|
|
|
|
[string (+StringT #f 'utf8)])
|
|
|
|
|
(check-equal? (decode string stream) "🍻"))
|
|
|
|
|
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
|
|
|
|
|
(check-equal? (decode (+StringT #f 'utf8)) "🍻"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'size', ->
|
|
|
|
|
; it 'should use string length', ->
|
|
|
|
|
; string = new StringT 7
|
|
|
|
|
; string.size('testing').should.equal 7
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 7)])
|
|
|
|
|
(check-equal? (size string "testing") 7))
|
|
|
|
|
(check-equal? (size (+StringT 7) "testing") 7)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should use correct encoding', ->
|
|
|
|
|
; string = new StringT 10, 'utf8'
|
|
|
|
|
; string.size('🍻').should.equal 4
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 10 'utf8)])
|
|
|
|
|
(check-equal? (size string "🍻") 4))
|
|
|
|
|
(check-equal? (size (+StringT 10 'utf8) "🍻") 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should use encoding from function', ->
|
|
|
|
|
; string = new StringT 10, -> 'utf8'
|
|
|
|
|
; string.size('🍻').should.equal 4
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 10 (λ _ 'utf8))])
|
|
|
|
|
(check-equal? (size string "🍻") 4))
|
|
|
|
|
(check-equal? (size (+StringT 10 (λ _ 'utf8)) "🍻") 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should add size of length field before string', ->
|
|
|
|
|
; string = new StringT uint8, 'utf8'
|
|
|
|
|
; string.size('🍻').should.equal 5
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT uint8 'utf8)])
|
|
|
|
|
(check-equal? (size string "🍻") 5))
|
|
|
|
|
(check-equal? (size (+StringT uint8 'utf8) "🍻") 5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; todo
|
|
|
|
|
; it 'should work with utf16be encoding', ->
|
|
|
|
|
; string = new StringT 10, 'utf16be'
|
|
|
|
|
; string.size('🍻').should.equal 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; it 'should take null-byte into account', ->
|
|
|
|
|
; string = new StringT null, 'utf8'
|
|
|
|
|
; string.size('🍻').should.equal 5
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT #f 'utf8)])
|
|
|
|
|
(check-equal? (size string "🍻") 5))
|
|
|
|
|
(check-equal? (size (+StringT #f 'utf8) "🍻") 5)
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
; it 'should use defined length if no value given', ->
|
|
|
|
|
; array = new StringT 10
|
|
|
|
|
; array.size().should.equal 10
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 10)])
|
|
|
|
|
(check-equal? (size string) 10))
|
|
|
|
|
(check-equal? (size (+StringT 10)) 10)
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
; describe 'encode', ->
|
|
|
|
|
; it 'should encode using string length', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer 'testing'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT 7
|
|
|
|
|
; string.encode(stream, 'testing')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 7)]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "testing")
|
|
|
|
|
(check-equal? (dump stream) #"testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT 7) "testing")
|
|
|
|
|
(check-equal? (dump (current-output-port)) #"testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should encode length as number before string', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer '\x07testing'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT uint8
|
|
|
|
|
; string.encode(stream, 'testing')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT uint8)]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "testing")
|
|
|
|
|
(check-equal? (dump stream) #"\x07testing"))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT uint8) "testing")
|
|
|
|
|
(check-equal? (dump (current-output-port)) #"\x07testing"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should encode length as number before string utf8', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer '\x0ctesting 😜', 'utf8'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT uint8, 'utf8'
|
|
|
|
|
; string.encode(stream, 'testing 😜')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT uint8 'utf8)]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "testing 😜")
|
|
|
|
|
(check-equal? (dump stream) (+Buffer "\x0ctesting 😜" 'utf8)))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT uint8 'utf8) "testing 😜")
|
|
|
|
|
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "\x0ctesting 😜")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should encode utf8', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer '🍻'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT 4, 'utf8'
|
|
|
|
|
; string.encode(stream, '🍻')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 4 'utf8)]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "🍻")
|
|
|
|
|
(check-equal? (dump stream) (+Buffer "🍻")))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT 4 'utf8) "🍻" )
|
|
|
|
|
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should encode encoding computed from function', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer '🍻'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT 4, -> 'utf8'
|
|
|
|
|
; string.encode(stream, '🍻')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT 4 (λ _ 'utf8))]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "🍻")
|
|
|
|
|
(check-equal? (dump stream) (+Buffer "🍻")))
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT 4 (λ _ 'utf8)) "🍻")
|
|
|
|
|
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; it 'should encode null-terminated string', (done) ->
|
|
|
|
|
; stream = new EncodeStream
|
|
|
|
|
; stream.pipe concat (buf) ->
|
|
|
|
|
; buf.should.deep.equal new Buffer '🍻\x00'
|
|
|
|
|
; done()
|
|
|
|
|
;
|
|
|
|
|
; string = new StringT null, 'utf8'
|
|
|
|
|
; string.encode(stream, '🍻')
|
|
|
|
|
; stream.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let ([string (+StringT #f 'utf8)]
|
|
|
|
|
[stream (+EncodeStream)])
|
|
|
|
|
(encode string stream "🍻")
|
|
|
|
|
(check-equal? (dump stream) (+Buffer "🍻\x00")))
|
|
|
|
|
|
|
|
|
|
(parameterize ([current-output-port (open-output-bytes)])
|
|
|
|
|
(encode (+StringT #f 'utf8) "🍻" )
|
|
|
|
|
(check-equal? (dump (current-output-port)) (string->bytes/utf-8 "🍻\x00")))
|