1
1
import { expect } from 'chai' ;
2
- import { tryLatin } from '../../../src/utils/latin' ;
2
+ import { tryReadBasicLatin , tryWriteBasicLatin } from '../../../src/utils/latin' ;
3
3
import * as sinon from 'sinon' ;
4
4
5
- describe ( 'tryLatin ()' , ( ) => {
5
+ describe ( 'tryReadBasicLatin ()' , ( ) => {
6
6
context ( 'when given a buffer of length 0' , ( ) => {
7
7
it ( 'returns an empty string' , ( ) => {
8
- expect ( tryLatin ( new Uint8Array ( ) , 0 , 10 ) ) . to . equal ( '' ) ;
8
+ expect ( tryReadBasicLatin ( new Uint8Array ( ) , 0 , 10 ) ) . to . equal ( '' ) ;
9
9
} ) ;
10
10
} ) ;
11
11
12
12
context ( 'when the distance between end and start is 0' , ( ) => {
13
13
it ( 'returns an empty string' , ( ) => {
14
- expect ( tryLatin ( new Uint8Array ( [ 1 , 2 , 3 ] ) , 0 , 0 ) ) . to . equal ( '' ) ;
14
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 1 , 2 , 3 ] ) , 0 , 0 ) ) . to . equal ( '' ) ;
15
15
} ) ;
16
16
} ) ;
17
17
@@ -30,61 +30,61 @@ describe('tryLatin()', () => {
30
30
context ( 'when there is 1 byte' , ( ) => {
31
31
context ( 'that exceed 127' , ( ) => {
32
32
it ( 'returns null' , ( ) => {
33
- expect ( tryLatin ( new Uint8Array ( [ 128 ] ) , 0 , 1 ) ) . be . null ;
33
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 ] ) , 0 , 1 ) ) . be . null ;
34
34
} ) ;
35
35
} ) ;
36
36
37
37
it ( 'calls fromCharCode once' , ( ) => {
38
- tryLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
38
+ tryReadBasicLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
39
39
expect ( fromCharCodeSpy ) . to . have . been . calledOnce ;
40
40
} ) ;
41
41
42
42
it ( 'never calls array.push' , ( ) => {
43
- tryLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
43
+ tryReadBasicLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
44
44
expect ( pushSpy ) . to . have . not . been . called ;
45
45
} ) ;
46
46
} ) ;
47
47
48
48
context ( 'when there is 2 bytes' , ( ) => {
49
49
context ( 'that exceed 127' , ( ) => {
50
50
it ( 'returns null' , ( ) => {
51
- expect ( tryLatin ( new Uint8Array ( [ 0 , 128 ] ) , 0 , 2 ) ) . be . null ;
52
- expect ( tryLatin ( new Uint8Array ( [ 128 , 0 ] ) , 0 , 2 ) ) . be . null ;
53
- expect ( tryLatin ( new Uint8Array ( [ 128 , 128 ] ) , 0 , 2 ) ) . be . null ;
51
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 0 , 128 ] ) , 0 , 2 ) ) . be . null ;
52
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 0 ] ) , 0 , 2 ) ) . be . null ;
53
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 128 ] ) , 0 , 2 ) ) . be . null ;
54
54
} ) ;
55
55
} ) ;
56
56
57
57
it ( 'calls fromCharCode twice' , ( ) => {
58
- tryLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
58
+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
59
59
expect ( fromCharCodeSpy ) . to . have . been . calledTwice ;
60
60
} ) ;
61
61
62
62
it ( 'never calls array.push' , ( ) => {
63
- tryLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
63
+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
64
64
expect ( pushSpy ) . to . have . not . been . called ;
65
65
} ) ;
66
66
} ) ;
67
67
68
68
context ( 'when there is 3 bytes' , ( ) => {
69
69
context ( 'that exceed 127' , ( ) => {
70
70
it ( 'returns null' , ( ) => {
71
- expect ( tryLatin ( new Uint8Array ( [ 0 , 0 , 128 ] ) , 0 , 3 ) ) . be . null ;
72
- expect ( tryLatin ( new Uint8Array ( [ 0 , 128 , 0 ] ) , 0 , 3 ) ) . be . null ;
73
- expect ( tryLatin ( new Uint8Array ( [ 128 , 0 , 0 ] ) , 0 , 3 ) ) . be . null ;
74
- expect ( tryLatin ( new Uint8Array ( [ 128 , 128 , 128 ] ) , 0 , 3 ) ) . be . null ;
75
- expect ( tryLatin ( new Uint8Array ( [ 128 , 128 , 0 ] ) , 0 , 3 ) ) . be . null ;
76
- expect ( tryLatin ( new Uint8Array ( [ 128 , 0 , 128 ] ) , 0 , 3 ) ) . be . null ;
77
- expect ( tryLatin ( new Uint8Array ( [ 0 , 128 , 128 ] ) , 0 , 3 ) ) . be . null ;
71
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 0 , 0 , 128 ] ) , 0 , 3 ) ) . be . null ;
72
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 0 , 128 , 0 ] ) , 0 , 3 ) ) . be . null ;
73
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 0 , 0 ] ) , 0 , 3 ) ) . be . null ;
74
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 128 , 128 ] ) , 0 , 3 ) ) . be . null ;
75
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 128 , 0 ] ) , 0 , 3 ) ) . be . null ;
76
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 , 0 , 128 ] ) , 0 , 3 ) ) . be . null ;
77
+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 0 , 128 , 128 ] ) , 0 , 3 ) ) . be . null ;
78
78
} ) ;
79
79
} ) ;
80
80
81
81
it ( 'calls fromCharCode thrice' , ( ) => {
82
- tryLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
82
+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
83
83
expect ( fromCharCodeSpy ) . to . have . been . calledThrice ;
84
84
} ) ;
85
85
86
86
it ( 'never calls array.push' , ( ) => {
87
- tryLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
87
+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
88
88
expect ( pushSpy ) . to . have . not . been . called ;
89
89
} ) ;
90
90
} ) ;
@@ -93,26 +93,86 @@ describe('tryLatin()', () => {
93
93
context ( `when there is ${ stringLength } bytes` , ( ) => {
94
94
context ( 'that exceed 127' , ( ) => {
95
95
it ( 'returns null' , ( ) => {
96
- expect ( tryLatin ( new Uint8Array ( stringLength ) . fill ( 128 ) , 0 , stringLength ) ) . be . null ;
96
+ expect ( tryReadBasicLatin ( new Uint8Array ( stringLength ) . fill ( 128 ) , 0 , stringLength ) ) . be
97
+ . null ;
97
98
} ) ;
98
99
} ) ;
99
100
100
101
it ( 'calls fromCharCode once' , ( ) => {
101
- tryLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
102
+ tryReadBasicLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
102
103
expect ( fromCharCodeSpy ) . to . have . been . calledOnce ;
103
104
} ) ;
104
105
105
106
it ( `calls array.push ${ stringLength } ` , ( ) => {
106
- tryLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
107
+ tryReadBasicLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
107
108
expect ( pushSpy ) . to . have . callCount ( stringLength ) ;
108
109
} ) ;
109
110
} ) ;
110
111
}
111
112
112
113
context ( 'when there is >21 bytes' , ( ) => {
113
114
it ( 'returns null' , ( ) => {
114
- expect ( tryLatin ( new Uint8Array ( 21 ) . fill ( 95 ) , 0 , 21 ) ) . be . null ;
115
- expect ( tryLatin ( new Uint8Array ( 201 ) . fill ( 95 ) , 0 , 201 ) ) . be . null ;
115
+ expect ( tryReadBasicLatin ( new Uint8Array ( 21 ) . fill ( 95 ) , 0 , 21 ) ) . be . null ;
116
+ expect ( tryReadBasicLatin ( new Uint8Array ( 201 ) . fill ( 95 ) , 0 , 201 ) ) . be . null ;
117
+ } ) ;
118
+ } ) ;
119
+ } ) ;
120
+
121
+ describe ( 'tryWriteBasicLatin()' , ( ) => {
122
+ context ( 'when given a string of length 0' , ( ) => {
123
+ it ( 'returns 0 and does not modify the destination' , ( ) => {
124
+ const input = Uint8Array . from ( { length : 10 } , ( ) => 1 ) ;
125
+ expect ( tryWriteBasicLatin ( input , '' , 2 ) ) . to . equal ( 0 ) ;
126
+ expect ( input ) . to . deep . equal ( Uint8Array . from ( { length : 10 } , ( ) => 1 ) ) ;
127
+ } ) ;
128
+ } ) ;
129
+
130
+ context ( 'when given a string with a length larger than the buffer' , ( ) => {
131
+ it ( 'returns null' , ( ) => {
132
+ const input = Uint8Array . from ( { length : 10 } , ( ) => 1 ) ;
133
+ expect ( tryWriteBasicLatin ( input , 'a' . repeat ( 11 ) , 0 ) ) . to . be . null ;
134
+ expect ( tryWriteBasicLatin ( input , 'a' . repeat ( 13 ) , 2 ) ) . to . be . null ;
135
+ } ) ;
136
+ } ) ;
137
+
138
+ let charCodeAtSpy ;
139
+
140
+ beforeEach ( ( ) => {
141
+ charCodeAtSpy = sinon . spy ( String . prototype , 'charCodeAt' ) ;
142
+ } ) ;
143
+
144
+ afterEach ( ( ) => {
145
+ sinon . restore ( ) ;
146
+ } ) ;
147
+
148
+ for ( let stringLength = 1 ; stringLength <= 25 ; stringLength ++ ) {
149
+ context ( `when there is ${ stringLength } bytes` , ( ) => {
150
+ context ( 'that exceed 127' , ( ) => {
151
+ it ( 'returns null' , ( ) => {
152
+ expect (
153
+ tryWriteBasicLatin (
154
+ new Uint8Array ( stringLength * 3 ) ,
155
+ 'a' . repeat ( stringLength - 1 ) + '\x80' ,
156
+ 0
157
+ )
158
+ ) . be . null ;
159
+ } ) ;
160
+ } ) ;
161
+
162
+ it ( `calls charCodeAt ${ stringLength } ` , ( ) => {
163
+ tryWriteBasicLatin (
164
+ new Uint8Array ( stringLength * 3 ) ,
165
+ String . fromCharCode ( 127 ) . repeat ( stringLength ) ,
166
+ stringLength
167
+ ) ;
168
+ expect ( charCodeAtSpy ) . to . have . callCount ( stringLength ) ;
169
+ } ) ;
170
+ } ) ;
171
+ }
172
+
173
+ context ( 'when there is >25 characters' , ( ) => {
174
+ it ( 'returns null' , ( ) => {
175
+ expect ( tryWriteBasicLatin ( new Uint8Array ( 75 ) , 'a' . repeat ( 26 ) , 0 ) ) . be . null ;
116
176
} ) ;
117
177
} ) ;
118
178
} ) ;
0 commit comments