|
| 1 | +import { assert } from 'chai'; |
| 2 | +import { cls, fun, number, string, boolean, typ } from '../src'; |
| 3 | + |
| 4 | +describe('type safety tests', () => { |
| 5 | + |
| 6 | + const string1 = typ(string); |
| 7 | + const string2 = typ(String); |
| 8 | + const string3 = typ('hallo'); |
| 9 | + |
| 10 | + const number1 = typ(number); |
| 11 | + const number2 = typ(Number); |
| 12 | + const number3 = typ(55); |
| 13 | + |
| 14 | + const bool1 = typ(boolean); |
| 15 | + const bool2 = typ(Boolean); |
| 16 | + const bool3 = typ(true); |
| 17 | + |
| 18 | + const C = cls({ |
| 19 | + // h: typ(string), |
| 20 | + constructor: fun((hello = typ(string)) => { |
| 21 | + // this.h = hello; |
| 22 | + }), |
| 23 | + hallo: fun(string, (hello = typ(string)) => { |
| 24 | + |
| 25 | + return hello; |
| 26 | + }) |
| 27 | + }); |
| 28 | + |
| 29 | + const fun1 = fun(string, (str = typ(string), num = typ(number)) => { |
| 30 | + |
| 31 | + return str; |
| 32 | + }); |
| 33 | + |
| 34 | + const c = new C('blub'); |
| 35 | + |
| 36 | + it('tests valid typings', () => { |
| 37 | + |
| 38 | + const hello = fun1('hello', 2); |
| 39 | + |
| 40 | + assert.equal(hello, 'hello'); |
| 41 | + |
| 42 | + const world = c.hallo('world'); |
| 43 | + |
| 44 | + assert.equal(world, 'world'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('throw errors on invalid typings', () => { |
| 48 | + const funBroken = fun(number, (str = typ(string), blub = typ(number)) => { |
| 49 | + |
| 50 | + return 'bar'; |
| 51 | + }); |
| 52 | + |
| 53 | + assert.throws(() => { |
| 54 | + funBroken('1', 2); |
| 55 | + }); |
| 56 | + |
| 57 | + assert.throws(() => { |
| 58 | + fun1('', 2, 3); |
| 59 | + }); |
| 60 | + |
| 61 | + assert.throws(() => { |
| 62 | + c.foo(''); |
| 63 | + }); |
| 64 | + |
| 65 | + assert.throws(() => { |
| 66 | + const c2 = new C(1); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments