forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-test.ts
133 lines (106 loc) · 3.49 KB
/
print-test.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { preprocess as parse, print } from '@glimmer/syntax';
const { test } = QUnit;
let templates = [
'<h1></h1>',
'<h1 class="foo" id="title"></h1>',
'<h1>Test</h1>',
'<h1>{{model.title}}</h1>',
'<h1>{{link-to "Foo" class="bar"}}</h1>',
'<h1 class={{if foo "foo" "bar"}}>Test</h1>',
'<h1 class={{color}}>Test</h1>',
'<h1 class="{{if active "active" "inactive"}} foo">Test</h1>',
'<p {{action "activate"}} {{someting foo="bar"}}>Test</p>',
'<p>{{my-component submit=(action (mut model.name) (full-name model.firstName "Smith"))}}</p>',
'<ul>{{#each foos as |foo index|}}\n <li>{{foo}}: {{index}}</li>\n{{/each}}</ul>',
'{{#if foo}}<p>{{foo}}</p>{{/if}}',
'{{#if foo}}Foo{{else if bar}}Bar{{else}}Baz{{/if}}',
'<Foo>{{bar}}</Foo>',
'<Foo></Foo>',
'<Foo />',
'<Foo as |bar|>{{bar}}</Foo>',
'<input disabled />',
// void elements
'<br>',
'<br />',
// comments
'<!-- foo -->',
'<div {{!-- foo --}}></div>',
'<div>{{!-- foo bar --}}<b></b></div>',
'{{!-- {{foo-bar}} --}}',
// literals
'<Panel @arg={{"Foo"}}></Panel>',
'<Panel @arg={{true}}></Panel>',
'<Panel @arg={{5}}></Panel>',
'{{panel arg="Foo"}}',
'{{panel arg=true}}',
'{{panel arg=5}}',
// nested tags with indent
'<div>\n <p>Test</p>\n</div>',
// attributes escaping
'<h1 class="foo" id="title" data-a=""Foo & Bar""></h1>',
'<h1 class="< {{if x "&" "<"}} & "">Test</h1>',
// slash in path
'{{namespace/foo "bar" baz="qux"}}',
// unescaped
'{{{unescaped}}}',
];
QUnit.module('[glimmer-syntax] Code generation', function() {
function printTransform(template: string) {
return print(parse(template));
}
templates.forEach(template => {
test(`${template} is stable when printed`, function(assert) {
assert.equal(printTransform(template), template);
});
});
test('TextNode: chars escape - but do not match', assert => {
assert.equal(
printTransform('< & > ©2018'),
'< & > ©2018'
);
});
test('Handlebars comment', assert => {
assert.equal(printTransform('{{! foo }}'), '{{!-- foo --}}');
});
});
QUnit.module('[glimmer-syntax] Code generation - source -> source', function() {
function printTransform(template: string) {
let ast = parse(template, {
mode: 'codemod',
parseOptions: { ignoreStandalone: true },
});
return print(ast, { entityEncoding: 'raw' });
}
function buildTest(template: string) {
test(`${template} is stable when printed`, function(assert) {
assert.equal(printTransform(template), template);
});
}
templates.forEach(buildTest);
[
// custom HTML Entities
'< & > ©2018',
// whitespace control
'\n{{~var~}} ',
'\n{{~#foo-bar~}} {{~else if x~}} {{~else~}} {{~/foo-bar~}} ',
// newlines after opening block
'{{#each}}\n <li> foo </li>\n{{/each}}',
// "stand alone"
' {{#foo}}\n {{bar}}\n {{/foo}}',
].forEach(buildTest);
});
QUnit.module('[glimmer-syntax] Code generation - override', function() {
test('can provide a custom options.override to be used', function(assert) {
let ast = parse(`<FooBar @baz="qux" @derp="qux" />`);
let actual = print(ast, {
entityEncoding: 'transformed',
override(ast) {
if (ast.type === 'AttrNode' && ast.name === '@baz') {
return '@baz="ZOMG!!!!"';
}
return;
},
});
assert.equal(actual, `<FooBar @baz="ZOMG!!!!" @derp="qux" />`);
});
});