File: string-params.js

package info (click to toggle)
node-handlebars 3%3A4.7.9-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,404 kB
  • sloc: javascript: 22,475; yacc: 136; lex: 125; sh: 72; ruby: 10; makefile: 7
file content (207 lines) | stat: -rw-r--r-- 6,369 bytes parent folder | download | duplicates (5)
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
describe('string params mode', function() {
  it('arguments to helpers can be retrieved from options hash in string form', function() {
    expectTemplate('{{wycats is.a slave.driver}}')
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        wycats: function(passiveVoice, noun) {
          return 'HELP ME MY BOSS ' + passiveVoice + ' ' + noun;
        }
      })
      .withMessage('String parameters output')
      .toCompileTo('HELP ME MY BOSS is.a slave.driver');
  });

  it('when using block form, arguments to helpers can be retrieved from options hash in string form', function() {
    expectTemplate('{{#wycats is.a slave.driver}}help :({{/wycats}}')
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        wycats: function(passiveVoice, noun, options) {
          return (
            'HELP ME MY BOSS ' +
            passiveVoice +
            ' ' +
            noun +
            ': ' +
            options.fn(this)
          );
        }
      })
      .withMessage('String parameters output')
      .toCompileTo('HELP ME MY BOSS is.a slave.driver: help :(');
  });

  it('when inside a block in String mode, .. passes the appropriate context in the options hash', function() {
    expectTemplate('{{#with dale}}{{tomdale ../need dad.joke}}{{/with}}')
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        tomdale: function(desire, noun, options) {
          return (
            'STOP ME FROM READING HACKER NEWS I ' +
            options.contexts[0][desire] +
            ' ' +
            noun
          );
        },
        with: function(context, options) {
          return options.fn(options.contexts[0][context]);
        }
      })
      .withInput({
        dale: {},

        need: 'need-a'
      })
      .withMessage('Proper context variable output')
      .toCompileTo('STOP ME FROM READING HACKER NEWS I need-a dad.joke');
  });

  it('information about the types is passed along', function() {
    expectTemplate("{{tomdale 'need' dad.joke true false}}")
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        tomdale: function(desire, noun, trueBool, falseBool, options) {
          equal(options.types[0], 'StringLiteral', 'the string type is passed');
          equal(
            options.types[1],
            'PathExpression',
            'the expression type is passed'
          );
          equal(
            options.types[2],
            'BooleanLiteral',
            'the expression type is passed'
          );
          equal(desire, 'need', 'the string form is passed for strings');
          equal(noun, 'dad.joke', 'the string form is passed for expressions');
          equal(trueBool, true, 'raw booleans are passed through');
          equal(falseBool, false, 'raw booleans are passed through');
          return 'Helper called';
        }
      })
      .toCompileTo('Helper called');
  });

  it('hash parameters get type information', function() {
    expectTemplate("{{tomdale he.says desire='need' noun=dad.joke bool=true}}")
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        tomdale: function(exclamation, options) {
          equal(exclamation, 'he.says');
          equal(options.types[0], 'PathExpression');

          equal(options.hashTypes.desire, 'StringLiteral');
          equal(options.hashTypes.noun, 'PathExpression');
          equal(options.hashTypes.bool, 'BooleanLiteral');
          equal(options.hash.desire, 'need');
          equal(options.hash.noun, 'dad.joke');
          equal(options.hash.bool, true);
          return 'Helper called';
        }
      })
      .toCompileTo('Helper called');
  });

  it('hash parameters get context information', function() {
    var context = { dale: {} };

    var helpers = {
      tomdale: function(exclamation, options) {
        equal(exclamation, 'he.says');
        equal(options.types[0], 'PathExpression');

        equal(options.contexts.length, 1);
        equal(options.hashContexts.noun, context);
        equal(options.hash.desire, 'need');
        equal(options.hash.noun, 'dad.joke');
        equal(options.hash.bool, true);
        return 'Helper called';
      },
      with: function(withContext, options) {
        return options.fn(options.contexts[0][withContext]);
      }
    };

    expectTemplate(
      "{{#with dale}}{{tomdale he.says desire='need' noun=../dad/joke bool=true}}{{/with}}"
    )
      .withCompileOptions({ stringParams: true })
      .withHelpers(helpers)
      .withInput(context)
      .toCompileTo('Helper called');
  });

  it('when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper', function() {
    expectTemplate(
      '{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}'
    )
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        tomdale: function(desire, noun, options) {
          return (
            'STOP ME FROM READING HACKER NEWS I ' +
            options.contexts[0][desire] +
            ' ' +
            noun +
            ' ' +
            options.fn(this)
          );
        },

        with: function(context, options) {
          return options.fn(options.contexts[0][context]);
        }
      })
      .withInput({
        dale: {},

        need: 'need-a'
      })
      .withMessage('Proper context variable output')
      .toCompileTo('STOP ME FROM READING HACKER NEWS I need-a dad.joke wot');
  });

  it('with nested block ambiguous', function() {
    expectTemplate(
      '{{#with content}}{{#view}}{{firstName}} {{lastName}}{{/view}}{{/with}}'
    )
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        with: function() {
          return 'WITH';
        },
        view: function() {
          return 'VIEW';
        }
      })
      .toCompileTo('WITH');
  });

  it('should handle DATA', function() {
    expectTemplate('{{foo @bar}}')
      .withCompileOptions({
        stringParams: true
      })
      .withHelpers({
        foo: function(bar, options) {
          equal(bar, '@bar');
          equal(options.types[0], 'PathExpression');
          return 'Foo!';
        }
      })
      .toCompileTo('Foo!');
  });
});