diff --git a/test/utils/TestAsync.ts b/test/utils/TestAsync.ts new file mode 100644 index 0000000..9737282 --- /dev/null +++ b/test/utils/TestAsync.ts @@ -0,0 +1,15 @@ +import { expect } from 'chai'; + +import { defer, timeout } from '../../src/utils/Async'; +import { describeLeaks, itLeaks } from '../helpers/async'; +import { TimeoutError } from '../../src'; + +describeLeaks('async utils', async () => { + describeLeaks('defer', async () => { + itLeaks('should resolve', async () => expect(defer(10, true)).to.eventually.equal(true)); + }); + + describeLeaks('timeout', async () => { + itLeaks('should reject slow promises', async () => expect(timeout(10, defer(20))).to.eventually.be.rejectedWith(TimeoutError)); + }); +}); diff --git a/test/utils/TestBuffer.ts b/test/utils/TestBuffer.ts new file mode 100644 index 0000000..8658681 --- /dev/null +++ b/test/utils/TestBuffer.ts @@ -0,0 +1,33 @@ +import { expect } from 'chai'; + +import { concat, encode } from '../../src/utils/Buffer'; +import { describeLeaks, itLeaks } from '../helpers/async'; + +describeLeaks('buffer utils', async () => { + describeLeaks('concat', async () => { + itLeaks('should append chunk buffers', async () => { + expect(concat([ + Buffer.from('hello'), + Buffer.from('world'), + ])).to.deep.equal(Buffer.from('helloworld')); + }); + }); + + describeLeaks('encode', async () => { + itLeaks('should encode chunk buffers', async () => { + expect(encode([ + Buffer.from('hello world'), + ], 'utf-8')).to.equal('hello world'); + }); + + itLeaks('should encode no buffers', async () => { + expect(encode([], 'utf-8')).to.equal(''); + }); + + itLeaks('should encode empty buffers', async () => { + expect(encode([ + new Buffer(0), + ], 'utf-8')).to.equal(''); + }); + }); +}); diff --git a/test/utils/TestMap.ts b/test/utils/TestMap.ts index 0876230..b7caa23 100644 --- a/test/utils/TestMap.ts +++ b/test/utils/TestMap.ts @@ -10,6 +10,9 @@ import { makeMap, mustGet, pairsToMap, + setOrPush, + mergeMap, + pushMergeMap, } from '../../src/utils/Map'; import { describeLeaks, itLeaks } from '../helpers/async'; @@ -102,7 +105,10 @@ describeLeaks('map utils', async () => { expect(getOrDefault(singleItem, 'missing', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE); }); - xit('should return the default for nil values'); + it('should return the default for nil values', () => { + expect(getOrDefault(multiItem, 'nilKey', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE); + }); + xit('should return falsy values for existing keys'); }); @@ -140,4 +146,64 @@ describeLeaks('map utils', async () => { expect(entriesOf(undefined)).to.deep.equal([]); }); }); + + describe('set or push helper', () => { + xit('should insert new lists'); + + it('should wrap new items', () => { + const values = new Map>([]); + setOrPush(values, mapKey, 'fin'); + + expect(Array.from(values.keys()).length).to.equal(1); + expect(mustGet(values, mapKey).length).to.equal(1); + }); + + it('should append to existing items', () => { + const values = new Map(multiItem); + setOrPush(values, mapKey, 'fin'); + + expect(Array.from(values.keys()).length).to.equal(3); + expect(mustGet(values, mapKey).length).to.equal(2); + }); + }); + + describe('merge map helper', () => { + it('should combine two maps', () => { + const firstValue = new Map([[mapKey, mapValue]]); + const otherValue = new Map([['foo', 'bar']]); + + const merged = mergeMap(firstValue, otherValue); + + expect(Array.from(merged.keys()).length).to.equal(2); + }); + }); + + describe('push merge map helper', () => { + it('should combine two overlapping maps', () => { + const firstValue = new Map([[mapKey, [1]]]); + const otherValue = new Map([[mapKey, [2, 3]]]); + + const merged = pushMergeMap(firstValue, otherValue); + expect(merged).to.have.keys(mapKey); + expect(merged.get(mapKey)).to.deep.equal([1, 2, 3]); + }); + }); + + describe('make map helper', () => { + it('should make an empty map', () => { + expect(makeMap(undefined).size).to.equal(0); + }); + + it('should copy an existing map', () => { + const copy = makeMap(singleItem); + expect(copy).to.deep.equal(singleItem); + expect(copy).not.to.equal(singleItem); + }); + + it('should copy a dict', () => { + expect(makeMap({ + [mapKey]: mapValue, + })).to.deep.equal(singleItem); + }); + }); }); diff --git a/test/utils/TestReflect.ts b/test/utils/TestReflect.ts new file mode 100644 index 0000000..7897df4 --- /dev/null +++ b/test/utils/TestReflect.ts @@ -0,0 +1,18 @@ +import { expect } from 'chai'; +import { getMethods } from '../../src/utils/Reflect'; + +describe('reflect utils', () => { + describe('get methods helper', () => { + it('should collect method functions', () => { + class Test { + public foo() { /* noop */ } + public bar() { /* noop */ } + } + + const methods = getMethods(new Test()).values(); + /* eslint-disable @typescript-eslint/unbound-method */ + expect(methods).to.include(Test.prototype.foo); + expect(methods).to.include(Test.prototype.bar); + }); + }); +});