Initial commit

This commit is contained in:
undergroundwires
2019-12-31 16:09:39 +01:00
commit 4e7f244190
108 changed files with 17296 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
import { AsyncLazy } from '@/infrastructure/Threading/AsyncLazy';
import 'mocha';
import { expect } from 'chai';
describe('AsyncLazy', () => {
it('returns value from lambda', async () => {
// arrange
const expected = 'test';
const lambda = () => Promise.resolve(expected);
const sut = new AsyncLazy(lambda);
// act
const actual = await sut.getValueAsync();
// assert
expect(actual).to.equal(expected);
});
describe('when running multiple times', () => {
let totalExecuted: number = 0;
beforeEach(() => totalExecuted = 0);
it('when running sync', async () => {
const sut = new AsyncLazy(() => {
totalExecuted++;
return Promise.resolve(totalExecuted);
});
const results = new Array<number>();
for (let i = 0; i < 5; i++) {
results.push(await sut.getValueAsync());
}
expect(totalExecuted).to.equal(1);
expect(results).to.deep.equal([1, 1, 1, 1, 1]);
});
it('when running long-running task paralelly', async () => {
const sleep = (time: number) => new Promise(((resolve) => setTimeout(resolve, time)));
const sut = new AsyncLazy(async () => {
await sleep(100);
totalExecuted++;
return Promise.resolve(totalExecuted);
});
const results = await Promise.all([
sut.getValueAsync(),
sut.getValueAsync(),
sut.getValueAsync(),
sut.getValueAsync(),
sut.getValueAsync()]);
expect(totalExecuted).to.equal(1);
expect(results).to.deep.equal([1, 1, 1, 1, 1]);
});
});
});

View File

@@ -0,0 +1,72 @@
import { NumericEntityStub } from './../stubs/NumericEntityStub';
import { InMemoryRepository } from '@/infrastructure/Repository/InMemoryRepository';
import { expect } from 'chai';
describe('InMemoryRepository', () => {
describe('exists', () => {
const sut = new InMemoryRepository<number, NumericEntityStub>(
[new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3)]);
describe('item exists', () => {
const actual = sut.exists(new NumericEntityStub(1));
it('returns true', () => expect(actual).to.be.true);
});
describe('item does not exist', () => {
const actual = sut.exists(new NumericEntityStub(99));
it('returns false', () => expect(actual).to.be.false);
});
});
it('can get', () => {
// arrange
const expected = [
new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3), new NumericEntityStub(4)];
// act
const sut = new InMemoryRepository<number, NumericEntityStub>(expected);
const actual = sut.getItems();
// assert
expect(actual).to.deep.equal(expected);
});
it('can add', () => {
// arrange
const sut = new InMemoryRepository<number, NumericEntityStub>();
const expected = {
length: 1,
item: new NumericEntityStub(1),
};
// act
sut.addItem(expected.item);
const actual = {
length: sut.length,
item: sut.getItems()[0],
};
// assert
expect(actual.length).to.equal(expected.length);
expect(actual.item).to.deep.equal(expected.item);
});
it('can remove', () => {
// arrange
const initialItems = [
new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(3), new NumericEntityStub(4)];
const idToDelete = 3;
const expected = {
length: 3,
items: [new NumericEntityStub(1), new NumericEntityStub(2), new NumericEntityStub(4)],
};
const sut = new InMemoryRepository<number, NumericEntityStub>(initialItems);
// act
sut.removeItem(idToDelete);
const actual = {
length: sut.length,
items: sut.getItems(),
};
// assert
expect(actual.length).to.equal(expected.length);
expect(actual.items).to.deep.equal(expected.items);
});
});

View File

@@ -0,0 +1,77 @@
import { Signal } from '@/infrastructure/Events/Signal';
import { expect } from 'chai';
describe('Signal Tests', () => {
class ReceiverMock {
public onRecieveCalls = new Array<number>();
public onReceive(arg: number): void { this.onRecieveCalls.push(arg); }
}
let signal: Signal<number>;
beforeEach(() => signal = new Signal());
describe('single reciever', () => {
let receiver: ReceiverMock;
beforeEach(() => {
receiver = new ReceiverMock();
signal.on((arg) => receiver.onReceive(arg));
});
it('notify() executes the callback', () => {
signal.notify(5);
expect(receiver.onRecieveCalls).to.have.length(1);
});
it('notify() executes the callback with the payload', () => {
const expected = 5;
signal.notify(expected);
expect(receiver.onRecieveCalls).to.deep.equal([expected]);
});
});
describe('multiple recievers', () => {
let receivers: ReceiverMock[];
beforeEach(() => {
receivers = [
new ReceiverMock(), new ReceiverMock(),
new ReceiverMock(), new ReceiverMock()];
for (const receiver of receivers) {
signal.on((arg) => receiver.onReceive(arg));
}});
it('notify() should execute all callbacks', () => {
signal.notify(5);
receivers.every((receiver) => {
expect(receiver.onRecieveCalls).to.have.length(1);
});
});
it('notify() should execute all callbacks with payload', () => {
const expected = 5;
signal.notify(expected);
receivers.every((receiver) => {
expect(receiver.onRecieveCalls).to.deep.equal([expected]);
});
});
it('notify() executes in FIFO order', () => {
// arrange
const expectedSequence = [0, 1, 2, 3];
const actualSequence = new Array<number>();
for (let i = 0; i < receivers.length; i++) {
receivers[i].onReceive = ((arg) => {
actualSequence.push(i);
});
}
// act
signal.notify(5);
// assert
expect(actualSequence).to.deep.equal(expectedSequence);
});
});
});