-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest-conditionals.js
93 lines (74 loc) · 2.38 KB
/
test-conditionals.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
const test = require('ava')
const join = require('path').join
const readSync = require('fs').readFileSync
const posthtml = require('posthtml')
const expressions = require('../lib')
const fixture = (file) => {
return readSync(join(__dirname, 'fixtures', `${file}.html`), 'utf8')
}
const expect = (file) => {
return readSync(join(__dirname, 'expect', `${file}.html`), 'utf8')
}
function process (t, name, options, log = false) {
return posthtml([expressions(options)])
.process(fixture(name))
.then((result) => {
log && console.log(result.html)
return clean(result.html)
})
.then((html) => {
t.is(html, expect(name).trim())
})
}
function error (name, cb, options) {
return posthtml([expressions(options)])
.process(fixture(name))
.catch(cb)
}
function clean (html) {
return html.replace(/[^\S\r\n]+$/gm, '').trim()
}
test('Conditionals', (t) => {
return process(t, 'conditional', { locals: { foo: 'bar' } })
})
test('Conditionals - only "if" condition', (t) => {
return process(t, 'conditional_if', { locals: { foo: 'bar', bool: '' } })
})
test('Conditionals - no render', (t) => {
return process(t, 'conditional_norender', {})
})
test('Conditionals - "if" tag missing condition', (t) => {
return error('conditional_if_error', (err) => {
t.is(err.toString(), 'Error: the "if" tag must have a "condition" attribute')
})
})
test('Conditionals - "elseif" tag missing condition', (t) => {
return error('conditional_elseif_error', (err) => {
t.is(err.toString(), 'Error: the "elseif" tag must have a "condition" attribute')
})
})
test('Conditionals - other tag in middle of statement', (t) => {
return process(t, 'conditional_tag_break', {})
})
test('Conditionals - nested conditionals', (t) => {
return process(t, 'conditional_nested', {})
})
test('conditional - expression error', (t) => {
return error('conditional_expression_error', (err) => {
t.is(err.name, 'SyntaxError')
})
})
test('Conditionals - if key exists in locals', (t) => {
return process(t, 'conditional_if_key_not_exists', { strictMode: false })
})
test('Conditionals - custom tags', (t) => {
return process(t, 'conditional_customtags', {
conditionalTags: ['zif', 'zelseif', 'zelse'],
locals: { foo: 'bar' }
})
})
test('Conditionals - expression in else/elseif', (t) => {
return process(t, 'conditional_expression', {
locals: { foo: 'bar' }
})
})