1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
const ByteArray = imports.byteArray;
const {GIMarshallingTests, GLib} = imports.gi;
describe('Byte array', function () {
it('can be created from a string', function () {
let a = ByteArray.fromString('abcd');
expect(a.length).toEqual(4);
[97, 98, 99, 100].forEach((val, ix) => expect(a[ix]).toEqual(val));
});
it('can be encoded from a string', function () {
// Pick a string likely to be stored internally as Latin1
let a = ByteArray.fromString('äbcd', 'LATIN1');
expect(a.length).toEqual(4);
[228, 98, 99, 100].forEach((val, ix) => expect(a[ix]).toEqual(val));
// Try again with a string not likely to be Latin1 internally
a = ByteArray.fromString('⅜', 'UTF-8');
expect(a.length).toEqual(3);
[0xe2, 0x85, 0x9c].forEach((val, ix) => expect(a[ix]).toEqual(val));
});
it('encodes as UTF-8 by default', function () {
let a = ByteArray.fromString('⅜');
expect(a.length).toEqual(3);
[0xe2, 0x85, 0x9c].forEach((val, ix) => expect(a[ix]).toEqual(val));
});
it('can be converted to a string of ASCII characters', function () {
let a = new Uint8Array(4);
a[0] = 97;
a[1] = 98;
a[2] = 99;
a[3] = 100;
let s = ByteArray.toString(a);
expect(s.length).toEqual(4);
expect(s).toEqual('abcd');
});
it('can be converted to a string of UTF-8 characters even if it ends with a 0', function () {
const a = Uint8Array.of(97, 98, 99, 100, 0);
const s = ByteArray.toString(a);
expect(s.length).toEqual(4);
expect(s).toEqual('abcd');
});
it('can be converted to a string of encoded characters even with a 0 byte', function () {
const a = Uint8Array.of(97, 98, 99, 100, 0);
const s = ByteArray.toString(a, 'LATIN1');
expect(s.length).toEqual(4);
expect(s).toEqual('abcd');
});
it('stops converting to a string at an embedded 0 byte', function () {
const a = Uint8Array.of(97, 98, 0, 99, 100);
const s = ByteArray.toString(a);
expect(s.length).toEqual(2);
expect(s).toEqual('ab');
});
it('deals gracefully with a 0-length array', function () {
const a = new Uint8Array(0);
expect(ByteArray.toString(a)).toEqual('');
expect(ByteArray.toGBytes(a).get_size()).toEqual(0);
});
it('deals gracefully with a non Uint8Array', function () {
const a = [97, 98, 99, 100, 0];
expect(() => ByteArray.toString(a)).toThrow();
expect(() => ByteArray.toGBytes(a)).toThrow();
});
describe('legacy toString() behavior', function () {
beforeEach(function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
'Some code called array.toString()*');
});
it('is preserved when created from a string', function () {
let a = ByteArray.fromString('⅜');
expect(a.toString()).toEqual('⅜');
});
it('is preserved when marshalled from GI', function () {
let a = GIMarshallingTests.bytearray_full_return();
expect(a.toString()).toEqual('');
});
afterEach(function () {
GLib.test_assert_expected_messages_internal('Gjs',
'testByteArray.js', 0, 'testToStringCompatibility');
});
});
});
|