xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / dust / test / core.js

2.0 KB
1(function(exports){
2
3exports.coreSetup = function(suite, auto) {
4  auto.forEach(function(test) {
5    suite.test(test.name, function(){
6      testRender(this, test.source, test.context, test.expected);
7    });
8  });
9
10  suite.test("base context", function() {
11    var base = dust.makeBase({
12      sayHello: function() { return "Hello!" }
13    });
14    testRender(this, "{sayHello} {foo}", base.push({foo: "bar"}), "Hello! bar");
15  });
16
17  suite.test("valid keys", function() {
18    testRender(this, "{_foo}{$bar}{baz1}", {_foo: 1, $bar: 2, baz1: 3}, "123");
19  });
20
21  suite.test("onLoad callback", function() {
22    var unit = this;
23    dust.onLoad = function(name, cb) {
24      cb(null, "Loaded: " + name);
25    };
26    dust.render("onLoad", {}, function(err, out) {
27      try {
28        unit.ifError(err);
29        unit.equals(out, "Loaded: onLoad");
30      } catch(err) {
31        unit.fail(err);
32        return;
33      }
34      unit.pass();
35    });
36  });
37
38  suite.test("renderSource (callback)", function() {
39    var unit = this;
40    dust.renderSource('Hello World', {}, function(err, out) {
41      try {
42        unit.ifError(err);
43        unit.equals(out, "Hello World");
44      } catch(err) {
45        unit.fail(err);
46        return;
47      }
48      unit.pass();
49    });
50  });
51
52  suite.test("renderSource (stream)", function() {
53    var unit = this;
54    dust.renderSource('Hello World', {}).on('data', function(data) {
55      try {
56        unit.equals('Hello World', data);
57      } catch(err) {
58        unit.fail(err);
59        return;
60      }
61      unit.pass();
62    }).on('error', function(err) {
63      unit.fail(err);
64    });
65  });
66}
67
68function testRender(unit, source, context, expected) {
69  var name = unit.id;
70  dust.loadSource(dust.compile(source, name));
71  dust.render(name, context, function(err, output) {
72    try {
73      unit.ifError(err);
74      unit.equals(output, expected);
75    } catch(err) {
76      unit.fail(err);
77      return;
78    }
79    unit.pass();
80  });
81}
82
83})(typeof exports !== "undefined" ? exports : window);