string tests pass

main
Matthew Butterick 8 years ago
parent 18305b8d2f
commit 9fa5664087

@ -34,10 +34,22 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee
(define/public-final (write val)
(unless (bytes? val)
(raise-argument-error 'EncodeStream:write "bytes" val))
(void (write-bytes val (· this _port))))
(void (write-bytes val _port)))
(define/public-final (writeBuffer buffer)
(write buffer)))
(write buffer))
(define/public-final (writeUInt8 int)
(write (bytes int)))
(define/public (writeString string [encoding 'ascii])
;; todo: handle encodings correctly.
;; right now just utf8 and ascii are correct
(caseq encoding
[(utf16le ucs2 utf8 ascii) (writeBuffer (string->bytes/utf-8 string))
(when (eq? encoding 'utf16le)
(error 'swap-bytes-unimplemented))]
[else (error 'unsupported-string-encoding)])))
#;(test-module
(define es (+EncodeStream))

@ -150,7 +150,7 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
(let ([string (+StringT 7)]
[stream (+EncodeStream)])
(send string encode stream "testing")
(check-equal? (send stream dump) "testing"))
(check-equal? (send stream dump) #"testing"))
;
@ -163,6 +163,12 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
; string = new StringT uint8
; string.encode(stream, 'testing')
; stream.end()
(let ([string (+StringT uint8)]
[stream (+EncodeStream)])
(send string encode stream "testing")
(check-equal? (send stream dump) #"\7testing"))
;
; it 'should encode length as number before string utf8', (done) ->
; stream = new EncodeStream
@ -173,6 +179,12 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
; string = new StringT uint8, 'utf8'
; string.encode(stream, 'testing 😜')
; stream.end()
(let ([string (+StringT uint8 'utf8)]
[stream (+EncodeStream)])
(send string encode stream "testing 😜")
(check-equal? (send stream dump) (+Buffer "\14testing 😜" 'utf8)))
;
; it 'should encode utf8', (done) ->
; stream = new EncodeStream
@ -183,6 +195,12 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
; string = new StringT 4, 'utf8'
; string.encode(stream, '🍻')
; stream.end()
(let ([string (+StringT 4 'utf8)]
[stream (+EncodeStream)])
(send string encode stream "🍻")
(check-equal? (send stream dump) (+Buffer "🍻")))
;
; it 'should encode encoding computed from function', (done) ->
; stream = new EncodeStream
@ -193,6 +211,12 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
; string = new StringT 4, -> 'utf8'
; string.encode(stream, '🍻')
; stream.end()
(let ([string (+StringT 4 (λ _ 'utf8))]
[stream (+EncodeStream)])
(send string encode stream "🍻")
(check-equal? (send stream dump) (+Buffer "🍻")))
;
; it 'should encode null-terminated string', (done) ->
; stream = new EncodeStream
@ -202,4 +226,10 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
;
; string = new StringT null, 'utf8'
; string.encode(stream, '🍻')
; stream.end()
; stream.end()
(let ([string (+StringT #f 'utf8)]
[stream (+EncodeStream)])
(send string encode stream "🍻")
(check-equal? (send stream dump) (+Buffer "🍻\0")))
Loading…
Cancel
Save