Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('IdentityMatcher', () => {
given(
[true, 'received "true"'],
[false, 'received "false"'],
).it('matches a boolean', (input: boolean, expected_result: string) => {
const result = new IdentityMatcher(input)
.when(true, _ => `received "true"`)
.else(_ => `received "false"`);
expect(result).to.equal(expected_result);
});
given(
[-1, 'received "-1"'],
[0.1, 'received "0.1"'],
[5, 'else, received "5"'],
// [NaN, 'received "NaN"'],
describe('MatchesAnything', () => {
given(
1, false, {},
).it('is always executed', (input: any) => {
const rule = new MatchesAnything(_ => _);
expect(rule.matches(input)).to.be.true; // tslint:disable-line:no-unused-expression
});
});
});
describe('::isGreaterThanOrEqualTo', () => {
class InvestmentLength extends TinyType {
constructor(public readonly value: number) {
super();
ensure('InvestmentLength', value, isGreaterThanOrEqualTo(0));
}
}
given(0, 1).
it('ensures that the argument is greater than or equal to a specified number', (value: number) => {
expect(() => new InvestmentLength(value)).to.not.throw; // tslint:disable-line:no-unused-expression
});
it('complains if the argument is less than the lower bound', () => {
expect(() => new InvestmentLength(-1))
.to.throw(`InvestmentLength should either be equal to 0 or be greater than 0`);
});
given(
-1,
undefined,
null,
{},
'string',
).it('complains if the value does not meet the predicate', (value: any) => {
given(
null,
undefined,
Infinity,
1,
false,
'string',
{},
[],
).
it('ensures they are equal', (value: any) => {
// tslint:disable-next-line:no-unused-expression
expect(() => ensure('Val', value, isEqualTo(value))).to.not.throw;
});
given(
1,
false,
'string',
{},
[],
).
it('complains if they are not equal', (value: any) => {
expect(() => ensure('Value', value, isEqualTo('expected value')))
.to.throw('Value should be equal to expected value');
});
});
});
describe('instantiation', () => {
/** @test {Timestamp} */
it('can be instantiated with an arbitrary Date', () => {
expect(() => new Timestamp(new Date())).to.not.throw; // tslint:disable-line:no-unused-expression
});
/** @test {Timestamp} */
it('defaults to current time if no argument is provided', () => {
expect(() => new Timestamp()).to.not.throw; // tslint:disable-line:no-unused-expression
});
given(
{},
'01 May 2018 10:00 UTC-2',
0,
).
it('complains if given an incorrect value as a constructor argument', (value: any) => {
expect(() => new Timestamp()).to.not.throw('Timestamp should be an instance of Date');
});
});
describe('::or', () => {
class Percentage extends TinyType {
constructor(public readonly value: number) {
super();
ensure('Percentage', value,
isDefined(),
isInteger(),
or(isEqualTo(0), isGreaterThan(0)),
or(isLessThan(100), isEqualTo(100)),
);
}
}
given(0, 1, 99, 100).
it('ensures that at least one of the `or` predicates is met', (value: number) => {
expect(() => new Percentage(value)).to.not.throw; // tslint:disable-line:no-unused-expression
});
given(
[-1, 'Percentage should either be equal to 0 or be greater than 0'],
[101, 'Percentage should either be less than 100 or be equal to 100'],
).
it('complains if at least one of the `or` predicates is not met', (value: number, errorMessage: string) => {
expect(() => new Percentage(value))
.to.throw(errorMessage);
});
it('complains if there are no predicates specified', () => {
expect(() => or()).to.throw(`Looks like you haven't specified any predicates to check the value against?`);
});
describe('::isBoolean', () => {
class MarketingOptIn extends TinyType {
constructor(public readonly value: boolean) {
super();
ensure('MarketingOptIn', value, isBoolean());
}
}
it('ensures that the argument is a boolean value', () => {
expect(() => new MarketingOptIn(false)).to.not.throw; // tslint:disable-line:no-unused-expression
});
given(
undefined,
null,
{},
'string',
5,
).it('complains if the value is not a boolean', (value: any) => {
expect(() => new MarketingOptIn(value)).to.throw(`MarketingOptIn should be a boolean value`);
});
});
});
ensure('InvestmentLength', value, isGreaterThanOrEqualTo(0));
}
}
given(0, 1).
it('ensures that the argument is greater than or equal to a specified number', (value: number) => {
expect(() => new InvestmentLength(value)).to.not.throw; // tslint:disable-line:no-unused-expression
});
it('complains if the argument is less than the lower bound', () => {
expect(() => new InvestmentLength(-1))
.to.throw(`InvestmentLength should either be equal to 0 or be greater than 0`);
});
given(
-1,
undefined,
null,
{},
'string',
).it('complains if the value does not meet the predicate', (value: any) => {
expect(() => new InvestmentLength(value))
.to.throw(`InvestmentLength should either be equal to 0 or be greater than 0`);
});
});
});
ensure('InvestmentLength', value, isLessThanOrEqualTo(50));
}
}
given(49, 50).
it('ensures that the argument is less than or equal to the upper bound', (value: number) => {
expect(() => new InvestmentLength(value)).to.not.throw; // tslint:disable-line:no-unused-expression
});
it('complains if the argument is greater than the upper bound', () => {
expect(() => new InvestmentLength(55))
.to.throw(`InvestmentLength should either be less than 50 or be equal to 50`);
});
given(
undefined,
null,
{},
'string',
).it('complains if the value is not an integer', (value: any) => {
expect(() => new InvestmentLength(value))
.to.throw(`InvestmentLength should either be less than 50 or be equal to 50`);
});
});
});
constructor(public readonly value: number) {
super();
ensure('InvestmentLength', value, isLessThan(50));
}
}
it('ensures that the argument is less than a specified number', () => {
expect(() => new InvestmentLength(5)).to.not.throw; // tslint:disable-line:no-unused-expression
});
it('complains if the argument is more than a specified number', () => {
expect(() => new InvestmentLength(55)).to.throw(`InvestmentLength should be less than 50`);
});
given(
undefined,
null,
{},
'string',
).it('complains if the value is not an integer', (value: any) => {
expect(() => new InvestmentLength(value)).to.throw(`InvestmentLength should be less than 50`);
});
});
});