1 | const {compile, and, or, root, arg0, arg1, setter, splice, bind, chain, template} = require('../../index');
|
2 | const {
|
3 | describeCompilers,
|
4 | evalOrLoad,
|
5 | currentValues,
|
6 | funcLibrary,
|
7 | expectTapFunctionToHaveBeenCalled,
|
8 | rand
|
9 | } = require('../test-utils');
|
10 | const _ = require('lodash');
|
11 |
|
12 | describe('testing string functions', () => {
|
13 | describeCompilers(['simple', 'optimizing', 'bytecode'], compiler => {
|
14 | function testStringFunction(str, func, args, expected) {
|
15 | it(`string function: ${func}`, () => {
|
16 | const model = {transform: root.map(val => val[func](...args).call('tap'))};
|
17 | const optCode = evalOrLoad(compile(model, {compiler}));
|
18 | const inst = optCode([str], funcLibrary);
|
19 | expect(inst.transform[0]).toEqual(expected);
|
20 | expectTapFunctionToHaveBeenCalled(inst.$model.length, compiler);
|
21 | });
|
22 | }
|
23 |
|
24 | testStringFunction('abc', 'endsWith', ['c'], true);
|
25 | testStringFunction('abcde', 'substring', [1, 3], 'bc');
|
26 | testStringFunction('abcde', 'toUpperCase', [], 'ABCDE');
|
27 | testStringFunction('abcDE', 'toLowerCase', [], 'abcde');
|
28 | testStringFunction('0xff', 'parseInt', [16], 255);
|
29 | testStringFunction('0.3asd', 'parseFloat', [], 0.3);
|
30 | testStringFunction('0.8', 'parseFloat', [], 0.8);
|
31 | testStringFunction('abcde', 'stringLength', [], 5)
|
32 |
|
33 | describe('String.split', () => {
|
34 | testStringFunction('ab/cd/e', 'split', ['/'], ['ab', 'cd', 'e']);
|
35 | testStringFunction('ab', 'split', ['/'], ['ab']);
|
36 |
|
37 |
|
38 | });
|
39 | });
|
40 |
|
41 | describe('template', () => {
|
42 | it('should concatenate hardcoded strings and expressions', () => {
|
43 | const model = {
|
44 | output: template`Hello ${root}`
|
45 | }
|
46 | const input = 'World'
|
47 |
|
48 | expect(evalOrLoad(compile(model))(input).output).toEqual('Hello World')
|
49 | })
|
50 |
|
51 | it('should concatenate multiple hardcoded strings and expressions', () => {
|
52 | const model = {
|
53 | output: template`Hello ${root.get('firstName')} ${root.get('lastName')}! Your template is ${root.get('template')}`
|
54 | }
|
55 | const input = {
|
56 | firstName: 'Netanel',
|
57 | lastName: 'Gilad',
|
58 | template: 'cool'
|
59 | }
|
60 |
|
61 | expect(evalOrLoad(compile(model))(input).output).toEqual('Hello Netanel Gilad! Your template is cool')
|
62 | })
|
63 | })
|
64 | });
|