summaryrefslogtreecommitdiffstats
path: root/spec/javascript-compiler.js
blob: fb78658546ea681b1c8aac7ac1bf4786c79bd422 (plain)
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
/*global Handlebars, beforeEach, handlebarsEnv, shouldCompileTo */
describe('javascript-compiler api', function() {
  if (!Handlebars.JavaScriptCompiler) {
    return;
  }

  describe('#nameLookup', function() {
    var $superName;
    beforeEach(function() {
      $superName = handlebarsEnv.JavaScriptCompiler.prototype.nameLookup;
    });
    afterEach(function() {
      handlebarsEnv.JavaScriptCompiler.prototype.nameLookup = $superName;
    });

    it('should allow override', function() {
      handlebarsEnv.JavaScriptCompiler.prototype.nameLookup = function(parent, name) {
        return parent + '.bar_' + name;
      };
      shouldCompileTo("{{foo}}", { bar_foo: "food" }, "food");
    });

    // Tests nameLookup dot vs. bracket behavior.  Bracket is required in certain cases
    // to avoid errors in older browsers.
    it('should handle reserved words', function() {
      shouldCompileTo("{{foo}} {{~null~}}", { foo: "food" }, "food");
    });
  });
  describe('#compilerInfo', function() {
    var $superCheck, $superInfo;
    beforeEach(function() {
      $superCheck = handlebarsEnv.VM.checkRevision;
      $superInfo = handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo;
    });
    afterEach(function() {
      handlebarsEnv.VM.checkRevision = $superCheck;
      handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo = $superInfo;
    });
    it('should allow compilerInfo override', function() {
      handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo = function() {
        return 'crazy';
      };
      handlebarsEnv.VM.checkRevision = function(compilerInfo) {
        if (compilerInfo !== 'crazy') {
          throw new Error('It didn\'t work');
        }
      };
      shouldCompileTo("{{foo}} ", { foo: "food" }, "food ");
    });
  });
  describe('buffer', function() {
    var $superAppend, $superCreate;
    beforeEach(function() {
      handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = true;
      $superAppend = handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer;
      $superCreate = handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer;
    });
    afterEach(function() {
      handlebarsEnv.JavaScriptCompiler.prototype.forceBuffer = false;
      handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = $superAppend;
      handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = $superCreate;
    });

    it('should allow init buffer override', function() {
      handlebarsEnv.JavaScriptCompiler.prototype.initializeBuffer = function() {
        return this.quotedString('foo_');
      };
      shouldCompileTo("{{foo}} ", { foo: "food" }, "foo_food ");
    });
    it('should allow append buffer override', function() {
      handlebarsEnv.JavaScriptCompiler.prototype.appendToBuffer = function(string) {
        return $superAppend.call(this, [string, ' + "_foo"']);
      };
      shouldCompileTo("{{foo}}", { foo: "food" }, "food_foo");
    });
  });
});