Home Reference Source Test

spec/predicates/isInteger.spec.ts

  1. import 'mocha';
  2.  
  3. import { given } from 'mocha-testdata';
  4.  
  5. import { ensure, isInteger, TinyType } from '../../src';
  6. import { expect } from '../expect';
  7.  
  8. describe('predicates', () => {
  9.  
  10. /** @test {isInteger} */
  11. describe('::isInteger', () => {
  12. class AgeInYears extends TinyType {
  13. constructor(public readonly value: number) {
  14. super();
  15.  
  16. ensure('AgeInYears', value, isInteger());
  17. }
  18. }
  19.  
  20. it('ensures that the argument in an integer', () => {
  21. expect(() => new AgeInYears(42)).to.not.throw();
  22. });
  23.  
  24. given([
  25. 1 / 3,
  26. 0.42,
  27. undefined,
  28. null,
  29. Number.NaN,
  30. Number.POSITIVE_INFINITY,
  31. {},
  32. 'string',
  33. ]).
  34. it('complains if the value is not an integer', (value: any) => {
  35. expect(() => new AgeInYears(value)).to.throw(`AgeInYears should be an integer`);
  36. });
  37. });
  38. });