|
| 1 | +import { LiteralSource } from '@app-config/core'; |
| 2 | +import { parseDirective } from './parse-directive'; |
| 3 | + |
| 4 | +describe('$parseBool', () => { |
| 5 | + it('should parse an existing boolean value', async () => { |
| 6 | + await expect( |
| 7 | + new LiteralSource({ $parseBool: true }).readToJSON([parseDirective()]), |
| 8 | + ).resolves.toBe(true); |
| 9 | + |
| 10 | + await expect( |
| 11 | + new LiteralSource({ $parseBool: false }).readToJSON([parseDirective()]), |
| 12 | + ).resolves.toBe(false); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should parse string values', async () => { |
| 16 | + await expect( |
| 17 | + new LiteralSource({ $parseBool: 'true' }).readToJSON([parseDirective()]), |
| 18 | + ).resolves.toBe(true); |
| 19 | + |
| 20 | + await expect( |
| 21 | + new LiteralSource({ $parseBool: 'false' }).readToJSON([parseDirective()]), |
| 22 | + ).resolves.toBe(false); |
| 23 | + |
| 24 | + await expect( |
| 25 | + new LiteralSource({ $parseBool: '1' }).readToJSON([parseDirective()]), |
| 26 | + ).resolves.toBe(true); |
| 27 | + |
| 28 | + await expect( |
| 29 | + new LiteralSource({ $parseBool: '0' }).readToJSON([parseDirective()]), |
| 30 | + ).resolves.toBe(false); |
| 31 | + |
| 32 | + await expect( |
| 33 | + new LiteralSource({ $parseBool: 'null' }).readToJSON([parseDirective()]), |
| 34 | + ).resolves.toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should parse null as false', async () => { |
| 38 | + await expect( |
| 39 | + new LiteralSource({ $parseBool: null }).readToJSON([parseDirective()]), |
| 40 | + ).resolves.toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should parse numbers', async () => { |
| 44 | + await expect( |
| 45 | + new LiteralSource({ $parseBool: 1 }).readToJSON([parseDirective()]), |
| 46 | + ).resolves.toBe(true); |
| 47 | + |
| 48 | + await expect( |
| 49 | + new LiteralSource({ $parseBool: 0 }).readToJSON([parseDirective()]), |
| 50 | + ).resolves.toBe(false); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('$parseFloat', () => { |
| 55 | + it('should parse an existing number value', async () => { |
| 56 | + await expect( |
| 57 | + new LiteralSource({ $parseFloat: 12.12 }).readToJSON([parseDirective()]), |
| 58 | + ).resolves.toBe(12.12); |
| 59 | + |
| 60 | + await expect( |
| 61 | + new LiteralSource({ $parseFloat: 0 }).readToJSON([parseDirective()]), |
| 62 | + ).resolves.toBe(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should parse string values', async () => { |
| 66 | + await expect( |
| 67 | + new LiteralSource({ $parseFloat: '12.12' }).readToJSON([parseDirective()]), |
| 68 | + ).resolves.toBe(12.12); |
| 69 | + |
| 70 | + await expect( |
| 71 | + new LiteralSource({ $parseFloat: '0' }).readToJSON([parseDirective()]), |
| 72 | + ).resolves.toBe(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should reject invalid values', async () => { |
| 76 | + await expect( |
| 77 | + new LiteralSource({ $parseFloat: 'not a number' }).readToJSON([parseDirective()]), |
| 78 | + ).rejects.toThrow('Failed to $parseFloat'); |
| 79 | + |
| 80 | + await expect( |
| 81 | + new LiteralSource({ $parseFloat: null }).readToJSON([parseDirective()]), |
| 82 | + ).rejects.toThrow('Failed to $parseFloat'); |
| 83 | + |
| 84 | + await expect( |
| 85 | + new LiteralSource({ $parseFloat: [] }).readToJSON([parseDirective()]), |
| 86 | + ).rejects.toThrow('Failed to $parseFloat'); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('$parseInt', () => { |
| 91 | + it('should parse an existing number value', async () => { |
| 92 | + await expect( |
| 93 | + new LiteralSource({ $parseInt: 12.12 }).readToJSON([parseDirective()]), |
| 94 | + ).resolves.toBe(12); |
| 95 | + |
| 96 | + await expect( |
| 97 | + new LiteralSource({ $parseInt: 0 }).readToJSON([parseDirective()]), |
| 98 | + ).resolves.toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should parse string values', async () => { |
| 102 | + await expect( |
| 103 | + new LiteralSource({ $parseInt: '12.12' }).readToJSON([parseDirective()]), |
| 104 | + ).resolves.toBe(12); |
| 105 | + |
| 106 | + await expect( |
| 107 | + new LiteralSource({ $parseInt: '0' }).readToJSON([parseDirective()]), |
| 108 | + ).resolves.toBe(0); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should reject invalid values', async () => { |
| 112 | + await expect( |
| 113 | + new LiteralSource({ $parseInt: 'not a number' }).readToJSON([parseDirective()]), |
| 114 | + ).rejects.toThrow('Failed to $parseInt'); |
| 115 | + |
| 116 | + await expect( |
| 117 | + new LiteralSource({ $parseInt: null }).readToJSON([parseDirective()]), |
| 118 | + ).rejects.toThrow('Failed to $parseInt'); |
| 119 | + |
| 120 | + await expect( |
| 121 | + new LiteralSource({ $parseInt: [] }).readToJSON([parseDirective()]), |
| 122 | + ).rejects.toThrow('Failed to $parseInt'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments