|
| 1 | +/* global describe, it */ |
| 2 | + |
| 3 | +import { Parser } from '../../index'; |
| 4 | +import { expect } from 'vitest'; |
| 5 | + |
| 6 | +describe('Array Function Error Messages', function () { |
| 7 | + describe('filter()', function () { |
| 8 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 9 | + const parser = new Parser(); |
| 10 | + expect(() => parser.evaluate('filter(42, [1, 2, 3])')).toThrow( |
| 11 | + /filter\(predicate, array\) expects a function as first argument, got number/ |
| 12 | + ); |
| 13 | + expect(() => parser.evaluate('filter(42, [1, 2, 3])')).toThrow(/Example:/); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 17 | + const parser = new Parser(); |
| 18 | + expect(() => parser.evaluate('f(x) = x > 0; filter(f, "not an array")')).toThrow( |
| 19 | + /filter\(predicate, array\) expects an array as second argument, got string/ |
| 20 | + ); |
| 21 | + expect(() => parser.evaluate('f(x) = x > 0; filter(f, "not an array")')).toThrow(/Example:/); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('map()', function () { |
| 26 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 27 | + const parser = new Parser(); |
| 28 | + expect(() => parser.evaluate('map("not a function", [1, 2, 3])')).toThrow( |
| 29 | + /map\(mapper, array\) expects a function as first argument, got string/ |
| 30 | + ); |
| 31 | + expect(() => parser.evaluate('map("not a function", [1, 2, 3])')).toThrow(/Example:/); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 35 | + const parser = new Parser(); |
| 36 | + expect(() => parser.evaluate('f(x) = x * 2; map(f, 123)')).toThrow( |
| 37 | + /map\(mapper, array\) expects an array as second argument, got number/ |
| 38 | + ); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('fold()', function () { |
| 43 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 44 | + const parser = new Parser(); |
| 45 | + expect(() => parser.evaluate('fold(null, 0, [1, 2, 3])', { null: null })).toThrow( |
| 46 | + /fold\(reducer, initial, array\) expects a function as first argument, got null/ |
| 47 | + ); |
| 48 | + expect(() => parser.evaluate('fold(null, 0, [1, 2, 3])', { null: null })).toThrow(/Example:/); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should provide user-friendly error when third argument is not an array', function () { |
| 52 | + const parser = new Parser(); |
| 53 | + expect(() => parser.evaluate('f(a, b) = a + b; fold(f, 0, {a: 1})')).toThrow( |
| 54 | + /fold\(reducer, initial, array\) expects an array as third argument, got object/ |
| 55 | + ); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('reduce()', function () { |
| 60 | + it('should provide user-friendly error from fold when first argument is not a function', function () { |
| 61 | + const parser = new Parser(); |
| 62 | + expect(() => parser.evaluate('reduce(true, 0, [1, 2, 3])')).toThrow( |
| 63 | + /fold\(reducer, initial, array\) expects a function as first argument, got boolean/ |
| 64 | + ); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('find()', function () { |
| 69 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 70 | + const parser = new Parser(); |
| 71 | + expect(() => parser.evaluate('find([1, 2], [1, 2, 3])')).toThrow( |
| 72 | + /find\(predicate, array\) expects a function as first argument, got array/ |
| 73 | + ); |
| 74 | + expect(() => parser.evaluate('find([1, 2], [1, 2, 3])')).toThrow(/Example:/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 78 | + const parser = new Parser(); |
| 79 | + expect(() => parser.evaluate('f(x) = x > 0; find(f, 99)')).toThrow( |
| 80 | + /find\(predicate, array\) expects an array as second argument, got number/ |
| 81 | + ); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('some()', function () { |
| 86 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 87 | + const parser = new Parser(); |
| 88 | + expect(() => parser.evaluate('some(5, [1, 2, 3])')).toThrow( |
| 89 | + /some\(predicate, array\) expects a function as first argument, got number/ |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 94 | + const parser = new Parser(); |
| 95 | + expect(() => parser.evaluate('f(x) = x > 0; some(f, "string")')).toThrow( |
| 96 | + /some\(predicate, array\) expects an array as second argument, got string/ |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('every()', function () { |
| 102 | + it('should provide user-friendly error when first argument is not a function', function () { |
| 103 | + const parser = new Parser(); |
| 104 | + expect(() => parser.evaluate('every({a: 1}, [1, 2, 3])')).toThrow( |
| 105 | + /every\(predicate, array\) expects a function as first argument, got object/ |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 110 | + const parser = new Parser(); |
| 111 | + expect(() => parser.evaluate('f(x) = x > 0; every(f, false)')).toThrow( |
| 112 | + /every\(predicate, array\) expects an array as second argument, got boolean/ |
| 113 | + ); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('indexOf()', function () { |
| 118 | + it('should provide user-friendly error when second argument is not an array or string', function () { |
| 119 | + const parser = new Parser(); |
| 120 | + expect(() => parser.evaluate('indexOf(1, 123)')).toThrow( |
| 121 | + /indexOf\(target, arrayOrString\) expects a string or array as second argument, got number/ |
| 122 | + ); |
| 123 | + expect(() => parser.evaluate('indexOf(1, 123)')).toThrow(/Example:/); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('join()', function () { |
| 128 | + it('should provide user-friendly error when second argument is not an array', function () { |
| 129 | + const parser = new Parser(); |
| 130 | + expect(() => parser.evaluate('join(", ", "not array")')).toThrow( |
| 131 | + /join\(separator, array\) expects an array as second argument, got string/ |
| 132 | + ); |
| 133 | + expect(() => parser.evaluate('join(", ", "not array")')).toThrow(/Example:/); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('sum()', function () { |
| 138 | + it('should provide user-friendly error when argument is not an array', function () { |
| 139 | + const parser = new Parser(); |
| 140 | + expect(() => parser.evaluate('sum(42)')).toThrow( |
| 141 | + /sum\(array\) expects an array as argument, got number/ |
| 142 | + ); |
| 143 | + expect(() => parser.evaluate('sum(42)')).toThrow(/Example:/); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('count()', function () { |
| 148 | + it('should provide user-friendly error when argument is not an array', function () { |
| 149 | + const parser = new Parser(); |
| 150 | + expect(() => parser.evaluate('count("string")')).toThrow( |
| 151 | + /count\(array\) expects an array as argument, got string/ |
| 152 | + ); |
| 153 | + expect(() => parser.evaluate('count("string")')).toThrow(/Example:/); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('unique()', function () { |
| 158 | + it('should provide user-friendly error when argument is not an array', function () { |
| 159 | + const parser = new Parser(); |
| 160 | + expect(() => parser.evaluate('unique(123)')).toThrow( |
| 161 | + /unique\(array\) expects an array as argument, got number/ |
| 162 | + ); |
| 163 | + expect(() => parser.evaluate('unique(123)')).toThrow(/Example:/); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('distinct()', function () { |
| 168 | + it('should provide user-friendly error from unique when argument is not an array', function () { |
| 169 | + const parser = new Parser(); |
| 170 | + expect(() => parser.evaluate('distinct({a: 1})')).toThrow( |
| 171 | + /unique\(array\) expects an array as argument, got object/ |
| 172 | + ); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments