|
1 | 1 | /* eslint-env mocha */
|
2 | 2 | 'use strict'
|
3 | 3 |
|
4 |
| -const expect = require('chai').expect |
| 4 | +const { expect } = require('aegir/utils/chai') |
5 | 5 | const { Buffer } = require('buffer')
|
| 6 | +const uint8ArrayFromString = require('uint8arrays/from-string') |
6 | 7 | const vd = require('../src')
|
7 | 8 |
|
8 |
| -describe('varint-decoder', () => { |
9 |
| - it('decode 1 varint', () => { |
10 |
| - const buf = Buffer.from('05', 'hex') |
11 |
| - const arr = vd(buf) |
12 |
| - expect(arr[0]).to.equal(5) |
13 |
| - }) |
| 9 | +const types = [{ |
| 10 | + name: 'Buffer', |
| 11 | + fromHexString: (hex) => Buffer.from(hex, 'hex') |
| 12 | +}, { |
| 13 | + name: 'Uint8Array', |
| 14 | + fromHexString: (hex) => uint8ArrayFromString(hex, 'base16') |
| 15 | +}] |
14 | 16 |
|
15 |
| - it('decode 2 varints', () => { |
16 |
| - const buf = Buffer.from('000a', 'hex') |
17 |
| - const arr = vd(buf) |
18 |
| - expect(arr[0]).to.equal(0) |
19 |
| - expect(arr[1]).to.equal(10) |
20 |
| - }) |
| 17 | +types.forEach(({ name, fromHexString }) => { |
| 18 | + describe(`varint-decoder (${name})`, () => { |
| 19 | + it('decode 1 varint', () => { |
| 20 | + const buf = fromHexString('05') |
| 21 | + const arr = vd(buf) |
| 22 | + expect(arr[0]).to.equal(5) |
| 23 | + }) |
21 | 24 |
|
22 |
| - it('decode 3 varints', () => { |
23 |
| - const buf = Buffer.from('0b0c03', 'hex') |
24 |
| - const arr = vd(buf) |
25 |
| - expect(arr[0]).to.equal(11) |
26 |
| - expect(arr[1]).to.equal(12) |
27 |
| - expect(arr[2]).to.equal(3) |
28 |
| - }) |
| 25 | + it('decode 2 varints', () => { |
| 26 | + const buf = fromHexString('000a') |
| 27 | + const arr = vd(buf) |
| 28 | + expect(arr[0]).to.equal(0) |
| 29 | + expect(arr[1]).to.equal(10) |
| 30 | + }) |
29 | 31 |
|
30 |
| - it('decode 1 long varint', () => { |
31 |
| - const buf = Buffer.from('c801', 'hex') |
32 |
| - const arr = vd(buf) |
33 |
| - expect(arr[0]).to.equal(200) |
34 |
| - }) |
| 32 | + it('decode 3 varints', () => { |
| 33 | + const buf = fromHexString('0b0c03') |
| 34 | + const arr = vd(buf) |
| 35 | + expect(arr[0]).to.equal(11) |
| 36 | + expect(arr[1]).to.equal(12) |
| 37 | + expect(arr[2]).to.equal(3) |
| 38 | + }) |
| 39 | + |
| 40 | + it('decode 1 long varint', () => { |
| 41 | + const buf = fromHexString('c801') |
| 42 | + const arr = vd(buf) |
| 43 | + expect(arr[0]).to.equal(200) |
| 44 | + }) |
35 | 45 |
|
36 |
| - it('decode a mix of long and short', () => { |
37 |
| - const buf = Buffer.from('96130208b90a', 'hex') |
38 |
| - const arr = vd(buf) |
39 |
| - expect(arr[0]).to.equal(2454) |
40 |
| - expect(arr[1]).to.equal(2) |
41 |
| - expect(arr[2]).to.equal(8) |
42 |
| - expect(arr[3]).to.equal(1337) |
| 46 | + it('decode a mix of long and short', () => { |
| 47 | + const buf = fromHexString('96130208b90a') |
| 48 | + const arr = vd(buf) |
| 49 | + expect(arr[0]).to.equal(2454) |
| 50 | + expect(arr[1]).to.equal(2) |
| 51 | + expect(arr[2]).to.equal(8) |
| 52 | + expect(arr[3]).to.equal(1337) |
| 53 | + }) |
43 | 54 | })
|
44 | 55 | })
|
0 commit comments