1 |
|
2 |
|
3 | var expect = require('expect.js')
|
4 | var et = require('..')
|
5 | var path = require('path')
|
6 |
|
7 | describe('compile', function() {
|
8 | it('should works with empty template', function () {
|
9 | var fn = et.compile('')
|
10 | expect(fn()).to.be('')
|
11 | })
|
12 |
|
13 | it('should works with simple string', function () {
|
14 | var fn = et.compile("a='abc'\nb")
|
15 | expect(fn()).to.be("a='abc'\nb")
|
16 | })
|
17 |
|
18 | it('should works with if tag', function () {
|
19 | var fn = et.compile("{{if _.x}}\n <abc>\n{{/}}")
|
20 | expect(fn({x:1})).to.be(' <abc>\n')
|
21 | expect(fn({x:0})).to.be('')
|
22 | })
|
23 |
|
24 | it('should works with unescape syntax', function () {
|
25 | var fn = et.compile("{{! _.code}}")
|
26 | expect(fn({code: '<code>hello</code>'})).to.be('<code>hello</code>')
|
27 | })
|
28 |
|
29 | it('should works with if elif else tag', function () {
|
30 | var str =`{{if _.x === 0}}
|
31 | empty
|
32 | {{elif _.x === 1}}
|
33 | has one
|
34 | {{else}}
|
35 | has many
|
36 | {{/}}`
|
37 | var fn = et.compile(str)
|
38 | expect(fn({x:0})).to.be(' empty\n')
|
39 | expect(fn({x:1})).to.be(' has one\n')
|
40 | expect(fn({x:2})).to.be(' has many\n')
|
41 | })
|
42 |
|
43 | it('should works with mixed line', function () {
|
44 | var fn = et.compile('start{{if _.x}} {{= _.name}} {{/}}e\nend')
|
45 | expect(fn({x: true, name: 'foo'})).to.be('start foo e\nend')
|
46 | })
|
47 |
|
48 | it('should works with inline each statement', function () {
|
49 | var fn = et.compile('{{each _.children as child}}{{= child.name}}{{/}}')
|
50 | expect(fn({children: [{name: 'foo'}, {name: 'bar'}]})).to.be('foobar')
|
51 | expect(fn({children: [{name: 'foo'}]})).to.be('foo')
|
52 | })
|
53 |
|
54 | it('should works with each statement with index', function () {
|
55 | var fn = et.compile('{{each _.children as child,i}}\n{{= i+1}} of {{= _.children.length}} {{= child.name}}\n{{/}}')
|
56 | var res = fn({children: [{name: 'foo'}, {name: 'bar'}]})
|
57 | expect(res).to.be('1 of 2 foo\n2 of 2 bar\n')
|
58 | })
|
59 |
|
60 | it('should works with inline if else statement', function () {
|
61 | var fn = et.compile('')
|
62 | })
|
63 |
|
64 | it('should throw when no matched begin tag', function () {
|
65 | expect(function () {
|
66 | var fn = et.compile('a\n{{/}}')
|
67 | fn()
|
68 | }).to.throwError()
|
69 | })
|
70 |
|
71 | it('should throw when tag not recognized', function () {
|
72 | expect(function () {
|
73 | var fn = et.compile('a\n{{abd}}')
|
74 | fn()
|
75 | }).to.throwError()
|
76 | })
|
77 | })
|
78 |
|
79 | describe('compile file', function () {
|
80 | it('should compile file', function () {
|
81 | var fn = et.compileFile(path.join(__dirname, './templates/body.html'))
|
82 | var str = fn({data: 'Hello javascript'})
|
83 | expect(str).to.be('<html>\nHello javascript\n</html>\n')
|
84 | })
|
85 | })
|