Skip to content

Commit 49d96af

Browse files
committed
test: add unit tests
1 parent 5837166 commit 49d96af

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

src/utils/latin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable prefer-spread */
2+
13
/**
24
* This function is an optimization for small basic latin strings.
35
* @internal
@@ -57,7 +59,6 @@ export function tryLatin(uint8array: Uint8Array, start: number, end: number): st
5759
}
5860

5961
if (basicLatin) {
60-
// eslint-disable-next-line prefer-spread
6162
return String.fromCharCode.apply(String, latinBytes);
6263
}
6364
}

test/node/utils/latin.test.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { expect } from 'chai';
2+
import { tryLatin } from '../../../src/utils/latin';
3+
import * as sinon from 'sinon';
4+
5+
describe('tryLatin()', () => {
6+
context('when given a buffer of length 0', () => {
7+
it('returns an empty string', () => {
8+
expect(tryLatin(new Uint8Array(), 0, 10)).to.equal('');
9+
});
10+
});
11+
12+
context('when the distance between end and start is 0', () => {
13+
it('returns an empty string', () => {
14+
expect(tryLatin(new Uint8Array([1, 2, 3]), 0, 0)).to.equal('');
15+
});
16+
});
17+
18+
let pushSpy;
19+
let fromCharCodeSpy;
20+
21+
beforeEach(() => {
22+
pushSpy = sinon.spy(Array.prototype, 'push');
23+
fromCharCodeSpy = sinon.spy(String, 'fromCharCode');
24+
});
25+
26+
afterEach(() => {
27+
sinon.restore();
28+
});
29+
30+
context('when there is 1 byte', () => {
31+
context('that exceed 127', () => {
32+
it('returns null', () => {
33+
expect(tryLatin(new Uint8Array([128]), 0, 1)).be.null;
34+
});
35+
});
36+
37+
it('calls fromCharCode once', () => {
38+
tryLatin(new Uint8Array([95]), 0, 1);
39+
expect(fromCharCodeSpy).to.have.been.calledOnce;
40+
});
41+
42+
it('never calls array.push', () => {
43+
tryLatin(new Uint8Array([95]), 0, 1);
44+
expect(pushSpy).to.have.not.been.called;
45+
});
46+
});
47+
48+
context('when there is 2 bytes', () => {
49+
context('that exceed 127', () => {
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;
54+
});
55+
});
56+
57+
it('calls fromCharCode twice', () => {
58+
tryLatin(new Uint8Array([95, 105]), 0, 2);
59+
expect(fromCharCodeSpy).to.have.been.calledTwice;
60+
});
61+
62+
it('never calls array.push', () => {
63+
tryLatin(new Uint8Array([95, 105]), 0, 2);
64+
expect(pushSpy).to.have.not.been.called;
65+
});
66+
});
67+
68+
context('when there is 3 bytes', () => {
69+
context('that exceed 127', () => {
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;
78+
});
79+
});
80+
81+
it('calls fromCharCode thrice', () => {
82+
tryLatin(new Uint8Array([95, 105, 100]), 0, 3);
83+
expect(fromCharCodeSpy).to.have.been.calledThrice;
84+
});
85+
86+
it('never calls array.push', () => {
87+
tryLatin(new Uint8Array([95, 105, 100]), 0, 3);
88+
expect(pushSpy).to.have.not.been.called;
89+
});
90+
});
91+
92+
for (let stringLength = 4; stringLength <= 20; stringLength++) {
93+
context(`when there is ${stringLength} bytes`, () => {
94+
context('that exceed 127', () => {
95+
it('returns null', () => {
96+
expect(tryLatin(new Uint8Array(stringLength).fill(128), 0, stringLength)).be.null;
97+
});
98+
});
99+
100+
it('calls fromCharCode once', () => {
101+
tryLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
102+
expect(fromCharCodeSpy).to.have.been.calledOnce;
103+
});
104+
105+
it(`calls array.push ${stringLength}`, () => {
106+
tryLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
107+
expect(pushSpy).to.have.callCount(stringLength);
108+
});
109+
});
110+
}
111+
112+
context('when there is >21 bytes', () => {
113+
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;
116+
});
117+
});
118+
});

0 commit comments

Comments
 (0)