|
| 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