|
1 | | -function errorMsg(lc, n, type, msg) { |
2 | | - const val = JSON.stringify(n) |
3 | | - return `Locale ${lc} ${type} rule self-test failed for ${val} (${msg})` |
4 | | -} |
| 1 | +const errorMsg = (lc, n, type, fn, args, msg) => `\ |
| 2 | +Locale ${lc} ${type} rule self-test failed for ${typeof n} ${n} (${msg}) |
| 3 | +Function: ${fn.toString()} |
| 4 | +Arguments: ${JSON.stringify(args)}` |
5 | 5 |
|
6 | | -function testCond(lc, n, type, expResult, fn) { |
| 6 | +function testCond(lc, fn, ordArg, n, type, expResult) { |
| 7 | + const compact = typeof n === 'string' && n.match(/(.*)c(\d+)$/) |
| 8 | + let args = [n] |
| 9 | + if (ordArg) args.push(type === 'ordinal') |
| 10 | + if (compact) { |
| 11 | + const c = Number(compact[2]) |
| 12 | + args[0] = Number(compact[1]) * Math.pow(10, c) |
| 13 | + args.push(c) |
| 14 | + } |
7 | 15 | try { |
8 | | - var r = fn(n, type === 'ordinal') |
| 16 | + var r = fn(...args) |
9 | 17 | } catch (error) { |
10 | 18 | /* istanbul ignore next: should not happen unless CLDR data is broken */ |
11 | | - throw new Error(errorMsg(lc, n, type, error)) |
| 19 | + throw new Error(errorMsg(lc, n, type, fn, args, error)) |
12 | 20 | } |
13 | 21 | if (r !== expResult) { |
14 | 22 | const res = JSON.stringify(r) |
15 | 23 | const exp = JSON.stringify(expResult) |
16 | | - throw new Error(errorMsg(lc, n, type, `was ${res}, expected ${exp}`)) |
| 24 | + throw new Error( |
| 25 | + errorMsg(lc, n, type, fn, args, `was ${res}, expected ${exp}`) |
| 26 | + ) |
17 | 27 | } |
18 | 28 | } |
19 | 29 |
|
20 | | -export function testCat(lc, type, cat, values, fn) { |
| 30 | +export function testCat(lc, fn, ordArg, type, cat, values) { |
21 | 31 | for (const n of values) { |
22 | | - testCond(lc, n, type, cat, fn) |
23 | | - if (!/\.0+$/.test(n)) testCond(lc, Number(n), type, cat, fn) |
| 32 | + testCond(lc, fn, ordArg, n, type, cat) |
| 33 | + if (!n.includes('c') && !/\.0+$/.test(n)) |
| 34 | + testCond(lc, fn, ordArg, Number(n), type, cat) |
24 | 35 | } |
25 | 36 | } |
0 commit comments