forked from project-chip/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.test.js
151 lines (138 loc) · 4.83 KB
/
templates.test.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
*
* Copyright (c) 2020 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @jest-environment node
*/
const handlebars = require('handlebars')
const helperC = require('../src-electron/generator/helper-c.js')
const templateEngine = require('../src-electron/generator/template-engine.js')
test('handlebars: simple test', () => {
let template = handlebars.compile('{{a}} {{b}} {{c}}!')
let output = template({ a: 'Very', b: 'simple', c: 'test' })
expect(output).toEqual('Very simple test!')
})
test('handlebars: comment test', () => {
let template = handlebars.compile(
'{{!-- some random comment --}}{{a}} {{b}} {{c}}!'
)
let output = template({ a: 'Very', b: 'simple', c: 'test' })
expect(output).toEqual('Very simple test!')
})
test('handlebars: object test', () => {
let template = handlebars.compile('{{in.a}} {{in.b}} {{in.c}}!')
let output = template({ in: { a: 'Very', b: 'simple', c: 'test' } })
expect(output).toEqual('Very simple test!')
})
test('handlebars: with test', () => {
let template = handlebars.compile('{{#with in}}{{a}} {{b}} {{c}}!{{/with}}')
let output = template({ in: { a: 'Very', b: 'simple', c: 'test' } })
expect(output).toEqual('Very simple test!')
})
test('handlebars: each test', () => {
let template = handlebars.compile('{{#each in}}{{this}} {{/each}}!')
let output = template({ in: ['Very', 'simple', 'test'] })
expect(output).toEqual('Very simple test !')
})
test('handlebars: partials', () => {
handlebars.registerPartial('very_simple_test', 'Very simple test!')
let template = handlebars.compile('{{> very_simple_test}}')
let output = template()
expect(output).toEqual('Very simple test!')
})
test('handlebars: helper', () => {
handlebars.registerHelper(
'supreme_leader',
(name) => `His most evil excelency, Mr. ${name}`
)
let template = handlebars.compile(
'{{#each list_of_lunatics}}{{supreme_leader this}} {{/each}}'
)
let output = template({
list_of_lunatics: ['Stalin', 'Trotsky', 'Genghis Khan'],
})
expect(output).toEqual(
'His most evil excelency, Mr. Stalin His most evil excelency, Mr. Trotsky His most evil excelency, Mr. Genghis Khan '
)
})
test('handlebars: if helper', () => {
let template = handlebars.compile(
'{{#if flag}}Yes flag!{{else}}No flag!{{/if}}'
)
let output = template({ flag: true })
expect(output).toEqual('Yes flag!')
output = template({ flag: false })
expect(output).toEqual('No flag!')
})
test('handlebars: using functions inside the passed input', () => {
let template = handlebars.compile('{{fn}}')
let output = template({
fn: () => {
let text = 'example text'
let uc = text.toUpperCase()
return `Got ${text}, returned ${uc}`
},
})
expect(output).toEqual('Got example text, returned EXAMPLE TEXT')
})
test('handlebars: using helper to populate the context', () => {
let template = handlebars.compile('{{#each custom_list}}{{value}}{{/each}}')
let output = template({
custom_list: () => {
let list = []
for (let i = 0; i < 10; i++) {
list.push({ value: i })
}
return list
},
})
expect(output).toEqual('0123456789')
})
test('handlebars: helper this processing', () => {
handlebars.registerHelper('inc', function () {
this.data++
return this.data
})
let template = handlebars.compile('{{inc}}{{inc}}{{inc}}{{inc}}{{inc}}')
let output = template({ data: 0 })
expect(output).toEqual('12345')
})
test('handlebars: iterator', () => {
handlebars.registerHelper('it', function (options) {
let ret = this.prefix
let context = this
for (let x = 0; x < 10; x++) {
context.thing = x
ret = ret + options.fn(context)
}
ret = ret + this.postfix
return ret
})
let template = handlebars.compile('{{#it}}{{thing}}{{/it}}')
let output = template({ prefix: 'PRE:', postfix: ':ERP' })
expect(output).toEqual('PRE:0123456789:ERP')
})
test('delimeter macros', () => {
expect(helperC.as_delimited_macro('VerySimple')).toEqual('VERY_SIMPLE')
expect(helperC.as_delimited_macro('Very_simple')).toEqual('VERY_SIMPLE')
expect(helperC.as_delimited_macro('Very_Simple')).toEqual('VERY_SIMPLE')
expect(helperC.as_delimited_macro('Very_123_Simple')).toEqual(
'VERY_123_SIMPLE'
)
expect(helperC.as_delimited_macro('MfrDefGpdCmd0')).toEqual(
'MFR_DEF_GPD_CMD0'
)
})