|
1 | 1 | import assert from 'assert'; |
2 | | -import {String as StringT, uint8, DecodeStream, EncodeStream} from 'restructure'; |
| 2 | +import {String as StringT, uint16le, uint8, DecodeStream, Struct} from 'restructure'; |
3 | 3 |
|
4 | 4 | describe('String', function() { |
5 | 5 | describe('decode', function() { |
@@ -40,6 +40,18 @@ describe('String', function() { |
40 | 40 | const string = new StringT(null, 'utf8'); |
41 | 41 | assert.equal(string.fromBuffer(Buffer.from('π»')), 'π»'); |
42 | 42 | }); |
| 43 | + |
| 44 | + it('should decode two-byte null-terminated string for utf16le', function() { |
| 45 | + const stream = new DecodeStream(Buffer.from('π»\x00', 'utf16le')); |
| 46 | + const string = new StringT(null, 'utf16le'); |
| 47 | + assert.equal(string.decode(stream), 'π»'); |
| 48 | + assert.equal(stream.pos, 6); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should decode remainder of buffer when null-byte missing, utf16le', function() { |
| 52 | + const string = new StringT(null, 'utf16le'); |
| 53 | + assert.equal(string.fromBuffer(Buffer.from('π»', 'utf16le')), 'π»'); |
| 54 | + }); |
43 | 55 | }); |
44 | 56 |
|
45 | 57 | describe('size', function() { |
@@ -73,6 +85,11 @@ describe('String', function() { |
73 | 85 | assert.equal(string.size('π»'), 5); |
74 | 86 | }); |
75 | 87 |
|
| 88 | + it('should take null-byte into account, utf16le', function() { |
| 89 | + const string = new StringT(null, 'utf16le'); |
| 90 | + assert.equal(string.size('π»'), 6); |
| 91 | + }); |
| 92 | + |
76 | 93 | it('should use defined length if no value given', function() { |
77 | 94 | const array = new StringT(10); |
78 | 95 | assert.equal(array.size(), 10); |
@@ -109,5 +126,20 @@ describe('String', function() { |
109 | 126 | const string = new StringT(null, 'utf8'); |
110 | 127 | assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»\x00')); |
111 | 128 | }); |
| 129 | + |
| 130 | + it('should encode using string length, utf16le', function() { |
| 131 | + const string = new StringT(16, 'utf16le'); |
| 132 | + assert.deepEqual(string.toBuffer('testing'), Buffer.from('testing', 'utf16le')); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should encode length as number before string utf16le', function() { |
| 136 | + const string = new StringT(uint16le, 'utf16le'); |
| 137 | + assert.deepEqual(string.toBuffer('testing π'), Buffer.from('\u0014testing π', 'utf16le')); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should encode two-byte null-terminated string for UTF-16', function() { |
| 141 | + const string = new StringT(null, 'utf16le'); |
| 142 | + assert.deepEqual(string.toBuffer('π»'), Buffer.from('π»\x00', 'utf16le')); |
| 143 | + }); |
112 | 144 | }); |
113 | 145 | }); |
0 commit comments