|
| 1 | +const module = require('./interpolation.js') |
| 2 | + |
| 3 | +const string_tests = [ |
| 4 | + ['foo', 'foo', {}], |
| 5 | + ['Hello World!', 'Hello World!', {}], |
| 6 | + ['Hello ${name}', 'Hello World!', {'name': 'World!'}], |
| 7 | + ['Hello ${name}!', 'Hello Person!', {'name': 'Person'}], |
| 8 | + ['${greeting} ${name}!', 'Hola Person!', {'name': 'Person', 'greeting': 'Hola'}], |
| 9 | + ['${foo} ${bar}!', 'Hola Person!', {'bar': 'Person', 'foo': 'Hola'}], |
| 10 | +] |
| 11 | + |
| 12 | +const array_tests = [ |
| 13 | + [[], [], {}], |
| 14 | + [['Hello ${name}!'], ['Hello World!'], {'name': 'World'}], |
| 15 | + [['Hello ${foo}!'], ['Hello World!'], {'foo': 'World'}], |
| 16 | + [['Hello ${foo}!', 'Hello ${foo}!'], ['Hello World!', 'Hello World!'], {'foo': 'World'}], |
| 17 | + [['Hello ${foo}!', ['Hello ${foo}!']], ['Hello World!', ['Hello World!']], {'foo': 'World'}], |
| 18 | + [['Hello ${foo}!', [{'${foo}': 42}]], ['Hello World!', [{'World': 42}]], {'foo': 'World'}], |
| 19 | +] |
| 20 | + |
| 21 | +const static_tests = [ |
| 22 | + [42, 42, {}], |
| 23 | + [false, false, {}], |
| 24 | + [889593.234234, 889593.234234, {}], |
| 25 | +] |
| 26 | + |
| 27 | +const object_tests = [ |
| 28 | + [{}, {}, {}], |
| 29 | + [{'foo': '${bar}'}, {'foo': 'hello'}, {'bar': 'hello'}], |
| 30 | + [{'foo': '${bar}', 'baz': '${asd}'}, {'foo': 'hello', 'baz': '33'}, {'bar': 'hello', 'asd': '33'}], |
| 31 | + [{'${bar}': '${bar}', 'baz': '${asd}'}, {'hello': 'hello', 'baz': '33'}, {'bar': 'hello', 'asd': '33'}], |
| 32 | + [{'${bar}': ['${bar}'], 'baz': '${asd}'}, {'hello': ['hello'], 'baz': '33'}, {'bar': 'hello', 'asd': '33'}], |
| 33 | +] |
| 34 | + |
| 35 | +const tests = [ |
| 36 | + ['String interpolation', 'format_string', [string_tests]], |
| 37 | + ['Array interpolation', 'format_array', [array_tests]], |
| 38 | + ['Value interpolation', 'format_value', [string_tests, array_tests, static_tests, object_tests]], |
| 39 | + ['Object interpolation', 'format_object', [object_tests]], |
| 40 | +] |
| 41 | + |
| 42 | +tests.forEach(([test_suite_name, function_name, test_sets]) => |
| 43 | + describe(test_suite_name, () => { |
| 44 | + test_sets.forEach(test_set => |
| 45 | + test_set.forEach(([input, output, state]) => |
| 46 | + it(`${[JSON.stringify(input), JSON.stringify(output), JSON.stringify(state)]}`, () => { |
| 47 | + module[function_name](input, state).should.deepEqual(output) |
| 48 | + }) |
| 49 | + ) |
| 50 | + ) |
| 51 | + }) |
| 52 | +) |
0 commit comments