index.test.js
6.74 KB
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
'use strict';
var assert = require('assert');
var utils = require('util');
var attrs = require('../');
var options;
function test(input, expected, locals) {
var opts = options;
locals = locals || {};
locals.pug = locals.pug || require('pug-runtime');
it(utils.inspect(input).replace(/\n/g, '') + ' => ' + utils.inspect(expected), function () {
var src = attrs(input, opts);
var localKeys = Object.keys(locals).sort();
var output = Function(localKeys.join(', '), 'return (' + src + ');').apply(null, localKeys.map(function (key) { return locals[key]; }));
if (opts.format === 'html') {
expect(output).toBe(expected);
} else {
expect(output).toEqual(expected);
}
});
}
function withOptions(opts, fn) {
describe('options: ' + utils.inspect(opts), function () {
options = opts;
fn();
});
}
withOptions({terse: true, format: 'html', runtime: function (name) { return 'pug.' + name; }}, function () {
test([], '');
test([{name: 'foo', val: 'false', mustEscape: true}], '');
test([{name: 'foo', val: 'true', mustEscape: true}], ' foo');
test([{name: 'foo', val: false, mustEscape: true}], '');
test([{name: 'foo', val: true, mustEscape: true}], ' foo');
test([{name: 'foo', val: 'foo', mustEscape: true}], '', {foo: false});
test([{name: 'foo', val: 'foo', mustEscape: true}], ' foo', {foo: true});
test([{name: 'foo', val: '"foo"', mustEscape: true}], ' foo="foo"');
test([{name: 'foo', val: '"foo"', mustEscape: true}, {name: 'bar', val: '"bar"', mustEscape: true}], ' foo="foo" bar="bar"');
test([{name: 'foo', val: 'foo', mustEscape: true}], ' foo="fooo"', {foo: 'fooo'});
test([{name: 'foo', val: 'foo', mustEscape: true}, {name: 'bar', val: 'bar', mustEscape: true}], ' foo="fooo" bar="baro"', {foo: 'fooo', bar: 'baro'});
test([{name: 'style', val: '{color: "red"}', mustEscape: true}], ' style="color:red;"');
test([{name: 'style', val: '{color: color}', mustEscape: true}], ' style="color:red;"', {color: 'red'});
test([{name: 'class', val: '"foo"', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], ' class="foo bar baz"');
test([{name: 'class', val: '{foo: foo}', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], ' class="foo bar baz"', {foo: true});
test([{name: 'class', val: '{foo: foo}', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], ' class="bar baz"', {foo: false});
test([{name: 'class', val: 'foo', mustEscape: true}, {name: 'class', val: '"<str>"', mustEscape: true}], ' class="<foo> <str>"', {foo: '<foo>'});
test([{name: 'foo', val: '"foo"', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], ' class="bar baz" foo="foo"');
test([{name: 'class', val: '["bar", "baz"]', mustEscape: true}, {name: 'foo', val: '"foo"', mustEscape: true}], ' class="bar baz" foo="foo"');
test([{name: 'foo', val: '"<foo>"', mustEscape: false}], ' foo="<foo>"');
test([{name: 'foo', val: '"<foo>"', mustEscape: true}], ' foo="<foo>"');
test([{name: 'foo', val: 'foo', mustEscape: false}], ' foo="<foo>"', {foo: '<foo>'});
test([{name: 'foo', val: 'foo', mustEscape: true}], ' foo="<foo>"', {foo: '<foo>'});
});
withOptions({terse: false, format: 'html', runtime: function (name) { return 'pug.' + name; }}, function () {
test([{name: 'foo', val: 'false', mustEscape: true}], '');
test([{name: 'foo', val: 'true', mustEscape: true}], ' foo="foo"');
test([{name: 'foo', val: false, mustEscape: true}], '');
test([{name: 'foo', val: true, mustEscape: true}], ' foo="foo"');
test([{name: 'foo', val: 'foo', mustEscape: true}], '', {foo: false});
test([{name: 'foo', val: 'foo', mustEscape: true}], ' foo="foo"', {foo: true});
});
withOptions({terse: true, format: 'object', runtime: function (name) { return 'pug.' + name; }}, function () {
test([], {});
test([{name: 'foo', val: 'false', mustEscape: true}], {foo: false});
test([{name: 'foo', val: 'true', mustEscape: true}], {foo: true});
test([{name: 'foo', val: false, mustEscape: true}], {foo: false});
test([{name: 'foo', val: true, mustEscape: true}], {foo: true});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: false}, {foo: false});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: true}, {foo: true});
test([{name: 'foo', val: '"foo"', mustEscape: true}], {foo: 'foo'});
test([{name: 'foo', val: '"foo"', mustEscape: true}, {name: 'bar', val: '"bar"', mustEscape: true}], {foo: 'foo', bar: 'bar'});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: 'fooo'}, {foo: 'fooo'});
test([{name: 'foo', val: 'foo', mustEscape: true}, {name: 'bar', val: 'bar', mustEscape: true}], {foo: 'fooo', bar: 'baro'}, {foo: 'fooo', bar: 'baro'});
test([{name: 'style', val: '{color: "red"}', mustEscape: true}], {style: 'color:red;'});
test([{name: 'style', val: '{color: color}', mustEscape: true}], {style: 'color:red;'}, {color: 'red'});
test([{name: 'class', val: '"foo"', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], {'class': 'foo bar baz'});
test([{name: 'class', val: '{foo: foo}', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], {'class': 'foo bar baz'}, {foo: true});
test([{name: 'class', val: '{foo: foo}', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], {'class': 'bar baz'}, {foo: false});
test([{name: 'class', val: 'foo', mustEscape: true}, {name: 'class', val: '"<str>"', mustEscape: true}], {'class': '<foo> <str>'}, {foo: '<foo>'});
test([{name: 'foo', val: '"foo"', mustEscape: true}, {name: 'class', val: '["bar", "baz"]', mustEscape: true}], {'class': 'bar baz', foo: 'foo'});
test([{name: 'class', val: '["bar", "baz"]', mustEscape: true}, {name: 'foo', val: '"foo"', mustEscape: true}], {'class': 'bar baz', foo: 'foo'});
test([{name: 'foo', val: '"<foo>"', mustEscape: false}], {foo: "<foo>"});
test([{name: 'foo', val: '"<foo>"', mustEscape: true}], {foo: "<foo>"});
test([{name: 'foo', val: 'foo', mustEscape: false}], {foo: "<foo>"}, {foo: '<foo>'});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: "<foo>"}, {foo: '<foo>'});
});
withOptions({terse: false, format: 'object', runtime: function (name) { return 'pug.' + name; }}, function () {
test([{name: 'foo', val: 'false', mustEscape: true}], {foo: false});
test([{name: 'foo', val: 'true', mustEscape: true}], {foo: true});
test([{name: 'foo', val: false, mustEscape: true}], {foo: false});
test([{name: 'foo', val: true, mustEscape: true}], {foo: true});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: false}, {foo: false});
test([{name: 'foo', val: 'foo', mustEscape: true}], {foo: true}, {foo: true});
});