1
0
Fork 0

fix(test): cover async, buffer, map, and reflect utils

This commit is contained in:
ssube 2020-03-30 18:09:36 -05:00
parent 89c13b6611
commit 852045f7b1
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
4 changed files with 133 additions and 1 deletions

15
test/utils/TestAsync.ts Normal file
View File

@ -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));
});
});

33
test/utils/TestBuffer.ts Normal file
View File

@ -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('');
});
});
});

View File

@ -10,6 +10,9 @@ import {
makeMap, makeMap,
mustGet, mustGet,
pairsToMap, pairsToMap,
setOrPush,
mergeMap,
pushMergeMap,
} from '../../src/utils/Map'; } from '../../src/utils/Map';
import { describeLeaks, itLeaks } from '../helpers/async'; import { describeLeaks, itLeaks } from '../helpers/async';
@ -102,7 +105,10 @@ describeLeaks('map utils', async () => {
expect(getOrDefault(singleItem, 'missing', DEFAULT_VALUE)).to.equal(DEFAULT_VALUE); 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'); xit('should return falsy values for existing keys');
}); });
@ -140,4 +146,64 @@ describeLeaks('map utils', async () => {
expect(entriesOf(undefined)).to.deep.equal([]); 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<string, Array<string>>([]);
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);
});
});
}); });

18
test/utils/TestReflect.ts Normal file
View File

@ -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);
});
});
});