-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtabstop.js
100 lines (85 loc) · 2.8 KB
/
tabstop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
require('mocha');
const assert = require('assert').strict;
const { Parser, compile, parse } = require('../lib/Parser');
const { normalize } = require('../lib/utils');
const inner = input => {
let ast = parse(input);
let node = ast.nodes.find(n => n.type !== 'text');
if (node) {
return node.inner();
}
};
describe('tabstops', () => {
describe('compile - with no tabstops entered', () => {
const fixtures = [
['foo{${1}}bar', 'foo{}bar'],
['foo{$1}bar', 'foo{}bar'],
['foo ${1} bar', 'foo bar'],
['foo $1 bar', 'foo bar'],
['foo $1 bar}', 'foo bar}'],
['foo $1 bar\\}', 'foo bar\\}'],
['foo $1\\}\nbar\n$BAZ', 'foo \\}\nbar\nBAZ'],
// should match "\\" when it's the last character in the string
['foo $1\\}\nbar\n$BAZ\\', 'foo \\}\nbar\nBAZ\\']
];
for (let fixture of fixtures) {
it(`should compile: "${normalize(fixture[0])}"`, () => {
assert.equal(compile(fixture[0])(), fixture[1]);
});
}
});
describe('compile - with tabstop 1 entered', () => {
const fixtures = [
['foo{${1}}bar', 'foo{ENTERED}bar'],
['foo{$1}bar', 'foo{ENTERED}bar'],
['foo ${1} bar', 'foo ENTERED bar'],
['foo $1 bar', 'foo ENTERED bar'],
['foo $1 bar}', 'foo ENTERED bar}'],
['foo $1 bar\\}', 'foo ENTERED bar\\}'],
['foo $1\\}\nbar\n$BAZ', 'foo ENTERED\\}\nbar\nBAZ'],
// should match "\\" when it's the last character in the string
['foo $1\\}\nbar\n$BAZ\\', 'foo ENTERED\\}\nbar\nBAZ\\']
];
for (let fixture of fixtures) {
it(`should compile: "${normalize(fixture[0])}" with tabstop 1`, () => {
let parser = new Parser(fixture[0]);
let ast = parser.parse();
parser.values.tabstop.set(1, 'ENTERED');
let fn = ast.compile();
assert.equal(fn(), fixture[1]);
});
}
});
describe('inner', () => {
const fixtures = [
['foo{${1}}bar', '1'],
['foo{$1}bar', '1'],
['foo ${1} bar', '1'],
['foo $1 bar', '1'],
['foo $1 bar}', '1'],
['foo $1 bar\\}', '1'],
['foo $1\\}\nbar\n$BAZ', '1'],
// should match "\\" when it's the last character in the string
['foo $1\\}\nbar\n$BAZ\\', '1']
];
for (let fixture of fixtures) {
it(`should return inner value for: "${normalize(fixture[0])}"`, () => {
assert.equal(inner(fixture[0]), fixture[1]);
});
}
});
describe('escaped', () => {
const fixtures = [
['foo \\${1} bar', undefined],
['foo ${1\\} bar', undefined],
['foo ${1.} bar', undefined],
['foo \\$1 bar', undefined]
];
for (let fixture of fixtures) {
it(`should not match escaped characters: "${normalize(fixture[0])}"`, () => {
assert.equal(inner(fixture[0]), fixture[1]);
});
}
});
});