UNPKG

2.7 kBJavaScriptView Raw
1(function() {
2 var expect, util, lib;
3
4 if(typeof require != 'undefined') {
5 expect = require('expect.js');
6 util = require('./util');
7 lib = require('../src/lib');
8 Environment = require('../src/environment').Environment;
9 Loader = require('../src/node-loaders').FileSystemLoader;
10 templatesPath = 'tests/templates';
11 }
12 else {
13 expect = window.expect;
14 util = window.util;
15 lib = nunjucks.require('lib');
16 Environment = nunjucks.Environment;
17 Loader = nunjucks.WebLoader;
18 templatesPath = '../templates';
19 }
20
21 var equal = util.equal;
22 var finish = util.finish;
23
24 describe('global', function() {
25 it('should have range', function(done) {
26 equal('{% for i in range(0, 10) %}{{ i }}{% endfor %}', '0123456789');
27 equal('{% for i in range(10) %}{{ i }}{% endfor %}', '0123456789');
28 equal('{% for i in range(5, 10) %}{{ i }}{% endfor %}', '56789');
29 equal('{% for i in range(5, 10, 2) %}{{ i }}{% endfor %}', '579');
30 equal('{% for i in range(5, 10, 2.5) %}{{ i }}{% endfor %}', '57.5');
31 equal('{% for i in range(5, 10, 2.5) %}{{ i }}{% endfor %}', '57.5');
32
33 //equal('{% for i in range(5, 10, -1) %}{{ i }}{% endfor %}', '56789');
34 //equal('{% for i in range(5, 10, -1 | abs) %}{{ i }}{% endfor %}','56789');
35
36 finish(done);
37 });
38
39 it('should have cycler', function(done) {
40 equal('{% set cls = cycler("odd", "even") %}' +
41 '{{ cls.next() }}' +
42 '{{ cls.next() }}' +
43 '{{ cls.next() }}',
44 'oddevenodd');
45
46 equal('{% set cls = cycler("odd", "even") %}' +
47 '{{ cls.next() }}' +
48 '{{ cls.reset() }}' +
49 '{{ cls.next() }}',
50 'oddodd');
51
52 finish(done);
53 });
54
55 it('should have joiner', function(done) {
56 equal('{% set comma = joiner() %}' +
57 'foo{{ comma() }}bar{{ comma() }}baz{{ comma() }}',
58 'foobar,baz,');
59
60 equal('{% set pipe = joiner("|") %}' +
61 'foo{{ pipe() }}bar{{ pipe() }}baz{{ pipe() }}',
62 'foobar|baz|');
63
64 finish(done);
65 });
66
67 it('should allow addition of globals', function(done) {
68 var env = new Environment(new Loader(templatesPath));
69
70 env.addGlobal('hello', function(arg1) {
71 return 'Hello ' + arg1;
72 });
73
74 equal('{{ hello("World!") }}', 'Hello World!');
75
76 finish(done);
77 });
78 });
79})();