11import { expect } from 'chai' ;
2- import { tryLatin , tryWriteLatin } 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,44 +93,45 @@ 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 ;
116117 } ) ;
117118 } ) ;
118119} ) ;
119120
120- describe ( 'tryWriteLatin ()' , ( ) => {
121+ describe ( 'tryWriteBasicLatin ()' , ( ) => {
121122 context ( 'when given a string of length 0' , ( ) => {
122123 it ( 'returns 0 and does not modify the destination' , ( ) => {
123124 const input = Uint8Array . from ( { length : 10 } , ( ) => 1 ) ;
124- expect ( tryWriteLatin ( input , '' , 2 ) ) . to . equal ( 0 ) ;
125+ expect ( tryWriteBasicLatin ( input , '' , 2 ) ) . to . equal ( 0 ) ;
125126 expect ( input ) . to . deep . equal ( Uint8Array . from ( { length : 10 } , ( ) => 1 ) ) ;
126127 } ) ;
127128 } ) ;
128129
129130 context ( 'when given a string with a length larger than the buffer' , ( ) => {
130131 it ( 'returns null' , ( ) => {
131132 const input = Uint8Array . from ( { length : 10 } , ( ) => 1 ) ;
132- expect ( tryWriteLatin ( input , 'a' . repeat ( 11 ) , 0 ) ) . to . be . null ;
133- expect ( tryWriteLatin ( input , 'a' . repeat ( 13 ) , 2 ) ) . to . be . null ;
133+ expect ( tryWriteBasicLatin ( input , 'a' . repeat ( 11 ) , 0 ) ) . to . be . null ;
134+ expect ( tryWriteBasicLatin ( input , 'a' . repeat ( 13 ) , 2 ) ) . to . be . null ;
134135 } ) ;
135136 } ) ;
136137
@@ -149,7 +150,7 @@ describe('tryWriteLatin()', () => {
149150 context ( 'that exceed 127' , ( ) => {
150151 it ( 'returns null' , ( ) => {
151152 expect (
152- tryWriteLatin (
153+ tryWriteBasicLatin (
153154 new Uint8Array ( stringLength * 3 ) ,
154155 'a' . repeat ( stringLength - 1 ) + '\x80' ,
155156 0
@@ -159,7 +160,7 @@ describe('tryWriteLatin()', () => {
159160 } ) ;
160161
161162 it ( `calls charCodeAt ${ stringLength } ` , ( ) => {
162- tryWriteLatin (
163+ tryWriteBasicLatin (
163164 new Uint8Array ( stringLength * 3 ) ,
164165 String . fromCharCode ( 127 ) . repeat ( stringLength ) ,
165166 stringLength
@@ -171,7 +172,7 @@ describe('tryWriteLatin()', () => {
171172
172173 context ( 'when there is >25 characters' , ( ) => {
173174 it ( 'returns null' , ( ) => {
174- expect ( tryWriteLatin ( new Uint8Array ( 75 ) , 'a' . repeat ( 26 ) , 0 ) ) . be . null ;
175+ expect ( tryWriteBasicLatin ( new Uint8Array ( 75 ) , 'a' . repeat ( 26 ) , 0 ) ) . be . null ;
175176 } ) ;
176177 } ) ;
177178} ) ;
0 commit comments