# Code coverage report for spec/basic.js

## Statements: 29.31% (34 / 116) Branches: 100% (0 / 0) Functions: 2.13% (1 / 47) Lines: 30.09% (34 / 113) Ignored: none

All files » spec/ » basic.js

    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    3

    3

    3
    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

    3

     /*global CompilerContext, Handlebars, beforeEach, shouldCompileTo */
    global.handlebarsEnv = null;

    beforeEach(function() {
      global.handlebarsEnv = Handlebars.create();
    });

    describe("basic context", function() {
      it("most basic", function() {
        shouldCompileTo("{{foo}}", { foo: "foo" }, "foo");
      });

      it("escaping", function() {
        shouldCompileTo("\\{{foo}}", { foo: "food" }, "{{foo}}");
        shouldCompileTo("content \\{{foo}}", { foo: "food" }, "content {{foo}}");
        shouldCompileTo("\\\\{{foo}}", { foo: "food" }, "\\food");
        shouldCompileTo("content \\\\{{foo}}", { foo: "food" }, "content \\food");
        shouldCompileTo("\\\\ {{foo}}", { foo: "food" }, "\\\\ food");
      });

      it("compiling with a basic context", function() {
        shouldCompileTo("Goodbye\n{{cruel}}\n{{world}}!", {cruel: "cruel", world: "world"}, "Goodbye\ncruel\nworld!",
                        "It works if all the required keys are provided");
      });

      it("compiling with an undefined context", function() {
        shouldCompileTo("Goodbye\n{{cruel}}\n{{world.bar}}!", undefined, "Goodbye\n\n!");

        shouldCompileTo("{{#unless foo}}Goodbye{{../test}}{{test2}}{{/unless}}", undefined, "Goodbye");
      });

      it("comments", function() {
        shouldCompileTo("{{! Goodbye}}Goodbye\n{{cruel}}\n{{world}}!",
          {cruel: "cruel", world: "world"}, "Goodbye\ncruel\nworld!",
          "comments are ignored");
      });

      it("boolean", function() {
        var string   = "{{#goodbye}}GOODBYE {{/goodbye}}cruel {{world}}!";
        shouldCompileTo(string, {goodbye: true, world: "world"}, "GOODBYE cruel world!",
                        "booleans show the contents when true");

        shouldCompileTo(string, {goodbye: false, world: "world"}, "cruel world!",
                        "booleans do not show the contents when false");
      });

      it("zeros", function() {
        shouldCompileTo("num1: {{num1}}, num2: {{num2}}", {num1: 42, num2: 0},
            "num1: 42, num2: 0");
        shouldCompileTo("num: {{.}}", 0, "num: 0");
        shouldCompileTo("num: {{num1/num2}}", {num1: {num2: 0}}, "num: 0");
      });

      it("newlines", function() {
          shouldCompileTo("Alan's\nTest", {}, "Alan's\nTest");
          shouldCompileTo("Alan's\rTest", {}, "Alan's\rTest");
      });

      it("escaping text", function() {
        shouldCompileTo("Awesome's", {}, "Awesome's", "text is escaped so that it doesn't get caught on single quotes");
        shouldCompileTo("Awesome\\", {}, "Awesome\\", "text is escaped so that the closing quote can't be ignored");
        shouldCompileTo("Awesome\\\\ foo", {}, "Awesome\\\\ foo", "text is escaped so that it doesn't mess up backslashes");
        shouldCompileTo("Awesome {{foo}}", {foo: '\\'}, "Awesome \\", "text is escaped so that it doesn't mess up backslashes");
        shouldCompileTo(' " " ', {}, ' " " ', "double quotes never produce invalid javascript");
      });

      it("escaping expressions", function() {
       shouldCompileTo("{{{awesome}}}", {awesome: "&\"\\<>"}, '&\"\\<>',
              "expressions with 3 handlebars aren't escaped");

       shouldCompileTo("{{&awesome}}", {awesome: "&\"\\<>"}, '&\"\\<>',
              "expressions with {{& handlebars aren't escaped");

       shouldCompileTo("{{awesome}}", {awesome: "&\"'`\\<>"}, '&amp;&quot;&#x27;&#x60;\\&lt;&gt;',
              "by default expressions should be escaped");

       shouldCompileTo("{{awesome}}", {awesome: "Escaped, <b> looks like: &lt;b&gt;"}, 'Escaped, &lt;b&gt; looks like: &amp;lt;b&amp;gt;',
              "escaping should properly handle amperstands");
      });

      it("functions returning safestrings shouldn't be escaped", function() {
        var hash = {awesome: function() { return new Handlebars.SafeString("&\"\\<>"); }};
        shouldCompileTo("{{awesome}}", hash, '&\"\\<>',
            "functions returning safestrings aren't escaped");
      });

      it("functions", function() {
        shouldCompileTo("{{awesome}}", {awesome: function() { return "Awesome"; }}, "Awesome",
                        "functions are called and render their output");
        shouldCompileTo("{{awesome}}", {awesome: function() { return this.more; }, more:  "More awesome"}, "More awesome",
                        "functions are bound to the context");
      });

      it("functions with context argument", function() {
        shouldCompileTo("{{awesome frank}}",
            {awesome: function(context) { return context; },
              frank: "Frank"},
            "Frank", "functions are called with context arguments");
      });
      it("pathed functions with context argument", function() {
        shouldCompileTo("{{bar.awesome frank}}",
            {bar: {awesome: function(context) { return context; }},
              frank: "Frank"},
            "Frank", "functions are called with context arguments");
      });
      it("depthed functions with context argument", function() {
        shouldCompileTo("{{#with frank}}{{../awesome .}}{{/with}}",
            {awesome: function(context) { return context; },
              frank: "Frank"},
            "Frank", "functions are called with context arguments");
      });

      it("block functions with context argument", function() {
        shouldCompileTo("{{#awesome 1}}inner {{.}}{{/awesome}}",
            {awesome: function(context, options) { return options.fn(context); }},
            "inner 1", "block functions are called with context and options");
      });

      it("depthed block functions with context argument", function() {
        shouldCompileTo("{{#with value}}{{#../awesome 1}}inner {{.}}{{/../awesome}}{{/with}}",
            {value: true, awesome: function(context, options) { return options.fn(context); }},
            "inner 1", "block functions are called with context and options");
      });

      it("block functions without context argument", function() {
        shouldCompileTo("{{#awesome}}inner{{/awesome}}",
            {awesome: function(options) { return options.fn(this); }},
            "inner", "block functions are called with options");
      });
      it("pathed block functions without context argument", function() {
        shouldCompileTo("{{#foo.awesome}}inner{{/foo.awesome}}",
            {foo: {awesome: function(options) { return this; }}},
            "inner", "block functions are called with options");
      });
      it("depthed block functions without context argument", function() {
        shouldCompileTo("{{#with value}}{{#../awesome}}inner{{/../awesome}}{{/with}}",
            {value: true, awesome: function(options) { return this; }},
            "inner", "block functions are called with options");
      });

      it("paths with hyphens", function() {
        shouldCompileTo("{{foo-bar}}", {"foo-bar": "baz"}, "baz", "Paths can contain hyphens (-)");
        shouldCompileTo("{{foo.foo-bar}}", {foo: {"foo-bar": "baz"}}, "baz", "Paths can contain hyphens (-)");
        shouldCompileTo("{{foo/foo-bar}}", {foo: {"foo-bar": "baz"}}, "baz", "Paths can contain hyphens (-)");
      });

      it("nested paths", function() {
        shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: "beautiful"}},
                        "Goodbye beautiful world!", "Nested paths access nested objects");
      });

      it("nested paths with empty string value", function() {
        shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: ""}},
                        "Goodbye  world!", "Nested paths access nested objects with empty string");
      });

      it("literal paths", function() {
        shouldCompileTo("Goodbye {{[@alan]/expression}} world!", {"@alan": {expression: "beautiful"}},
            "Goodbye beautiful world!", "Literal paths can be used");
        shouldCompileTo("Goodbye {{[foo bar]/expression}} world!", {"foo bar": {expression: "beautiful"}},
            "Goodbye beautiful world!", "Literal paths can be used");
      });

      it('literal references', function() {
        shouldCompileTo("Goodbye {{[foo bar]}} world!", {"foo bar": "beautiful"},
            "Goodbye beautiful world!", "Literal paths can be used");
      });

      it("that current context path ({{.}}) doesn't hit helpers", function() {
        shouldCompileTo("test: {{.}}", [null, {helper: "awesome"}], "test: ");
      });

      it("complex but empty paths", function() {
        shouldCompileTo("{{person/name}}", {person: {name: null}}, "");
        shouldCompileTo("{{person/name}}", {person: {}}, "");
      });

      it("this keyword in paths", function() {
        var string = "{{#goodbyes}}{{this}}{{/goodbyes}}";
        var hash = {goodbyes: ["goodbye", "Goodbye", "GOODBYE"]};
        shouldCompileTo(string, hash, "goodbyeGoodbyeGOODBYE",
          "This keyword in paths evaluates to current context");

        string = "{{#hellos}}{{this/text}}{{/hellos}}";
        hash = {hellos: [{text: "hello"}, {text: "Hello"}, {text: "HELLO"}]};
        shouldCompileTo(string, hash, "helloHelloHELLO", "This keyword evaluates in more complex paths");
      });

      it("this keyword nested inside path", function() {
        var string = "{{#hellos}}{{text/this/foo}}{{/hellos}}";
        shouldThrow(function() {
          CompilerContext.compile(string);
        }, Error);
      });

      it("this keyword in helpers", function() {
        var helpers = {foo: function(value) {
            return 'bar ' + value;
        }};
        var string = "{{#goodbyes}}{{foo this}}{{/goodbyes}}";
        var hash = {goodbyes: ["goodbye", "Goodbye", "GOODBYE"]};
        shouldCompileTo(string, [hash, helpers], "bar goodbyebar Goodbyebar GOODBYE",
          "This keyword in paths evaluates to current context");

        string = "{{#hellos}}{{foo this/text}}{{/hellos}}";
        hash = {hellos: [{text: "hello"}, {text: "Hello"}, {text: "HELLO"}]};
        shouldCompileTo(string, [hash, helpers], "bar hellobar Hellobar HELLO", "This keyword evaluates in more complex paths");
      });

      it("this keyword nested inside helpers param", function() {
        var string = "{{#hellos}}{{foo text/this/foo}}{{/hellos}}";
        shouldThrow(function() {
          CompilerContext.compile(string);
        }, Error);
      });
    });