From 9fa56640870e20a451200791ac98cceeec806918 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 28 Jun 2017 10:13:26 -0700 Subject: [PATCH] string tests pass --- pitfall/restructure/stream.rkt | 16 ++++++++++++-- pitfall/restructure/string-test.rkt | 34 +++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/pitfall/restructure/stream.rkt b/pitfall/restructure/stream.rkt index 5087579c..fa7051c6 100644 --- a/pitfall/restructure/stream.rkt +++ b/pitfall/restructure/stream.rkt @@ -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)) diff --git a/pitfall/restructure/string-test.rkt b/pitfall/restructure/string-test.rkt index db0bc96f..6954737d 100644 --- a/pitfall/restructure/string-test.rkt +++ b/pitfall/restructure/string-test.rkt @@ -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() \ No newline at end of file +; stream.end() + + +(let ([string (+StringT #f 'utf8)] + [stream (+EncodeStream)]) + (send string encode stream "๐Ÿป") + (check-equal? (send stream dump) (+Buffer "๐Ÿป\0"))) \ No newline at end of file