11import { expect } from 'chai' ;
2- import { tryLatin } from '../../../src/utils/latin' ;
2+ import { tryReadBasicLatin , tryWriteBasicLatin } from '../../../src/utils/latin' ;
33import * as sinon from 'sinon' ;
44
5- describe ( 'tryLatin ()' , ( ) => {
5+ describe ( 'tryReadBasicLatin ()' , ( ) => {
66 context ( 'when given a buffer of length 0' , ( ) => {
77 it ( 'returns an empty string' , ( ) => {
8- expect ( tryLatin ( new Uint8Array ( ) , 0 , 10 ) ) . to . equal ( '' ) ;
8+ expect ( tryReadBasicLatin ( new Uint8Array ( ) , 0 , 10 ) ) . to . equal ( '' ) ;
99 } ) ;
1010 } ) ;
1111
1212 context ( 'when the distance between end and start is 0' , ( ) => {
1313 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 ( '' ) ;
1515 } ) ;
1616 } ) ;
1717
@@ -30,61 +30,61 @@ describe('tryLatin()', () => {
3030 context ( 'when there is 1 byte' , ( ) => {
3131 context ( 'that exceed 127' , ( ) => {
3232 it ( 'returns null' , ( ) => {
33- expect ( tryLatin ( new Uint8Array ( [ 128 ] ) , 0 , 1 ) ) . be . null ;
33+ expect ( tryReadBasicLatin ( new Uint8Array ( [ 128 ] ) , 0 , 1 ) ) . be . null ;
3434 } ) ;
3535 } ) ;
3636
3737 it ( 'calls fromCharCode once' , ( ) => {
38- tryLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
38+ tryReadBasicLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
3939 expect ( fromCharCodeSpy ) . to . have . been . calledOnce ;
4040 } ) ;
4141
4242 it ( 'never calls array.push' , ( ) => {
43- tryLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
43+ tryReadBasicLatin ( new Uint8Array ( [ 95 ] ) , 0 , 1 ) ;
4444 expect ( pushSpy ) . to . have . not . been . called ;
4545 } ) ;
4646 } ) ;
4747
4848 context ( 'when there is 2 bytes' , ( ) => {
4949 context ( 'that exceed 127' , ( ) => {
5050 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 ;
5454 } ) ;
5555 } ) ;
5656
5757 it ( 'calls fromCharCode twice' , ( ) => {
58- tryLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
58+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
5959 expect ( fromCharCodeSpy ) . to . have . been . calledTwice ;
6060 } ) ;
6161
6262 it ( 'never calls array.push' , ( ) => {
63- tryLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
63+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 ] ) , 0 , 2 ) ;
6464 expect ( pushSpy ) . to . have . not . been . called ;
6565 } ) ;
6666 } ) ;
6767
6868 context ( 'when there is 3 bytes' , ( ) => {
6969 context ( 'that exceed 127' , ( ) => {
7070 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 ;
7878 } ) ;
7979 } ) ;
8080
8181 it ( 'calls fromCharCode thrice' , ( ) => {
82- tryLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
82+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
8383 expect ( fromCharCodeSpy ) . to . have . been . calledThrice ;
8484 } ) ;
8585
8686 it ( 'never calls array.push' , ( ) => {
87- tryLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
87+ tryReadBasicLatin ( new Uint8Array ( [ 95 , 105 , 100 ] ) , 0 , 3 ) ;
8888 expect ( pushSpy ) . to . have . not . been . called ;
8989 } ) ;
9090 } ) ;
@@ -93,26 +93,86 @@ describe('tryLatin()', () => {
9393 context ( `when there is ${ stringLength } bytes` , ( ) => {
9494 context ( 'that exceed 127' , ( ) => {
9595 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 ;
9798 } ) ;
9899 } ) ;
99100
100101 it ( 'calls fromCharCode once' , ( ) => {
101- tryLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
102+ tryReadBasicLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
102103 expect ( fromCharCodeSpy ) . to . have . been . calledOnce ;
103104 } ) ;
104105
105106 it ( `calls array.push ${ stringLength } ` , ( ) => {
106- tryLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
107+ tryReadBasicLatin ( new Uint8Array ( stringLength ) . fill ( 95 ) , 0 , stringLength ) ;
107108 expect ( pushSpy ) . to . have . callCount ( stringLength ) ;
108109 } ) ;
109110 } ) ;
110111 }
111112
112113 context ( 'when there is >21 bytes' , ( ) => {
113114 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 ;
116176 } ) ;
117177 } ) ;
118178} ) ;
0 commit comments