@@ -66,9 +66,29 @@ describe('builtin helpers', function() {
66
66
"each with array argument ignores the contents when empty" ) ;
67
67
} ) ;
68
68
69
+ it ( 'each without data' , function ( ) {
70
+ var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!' ;
71
+ var hash = { goodbyes : [ { text : 'goodbye' } , { text : 'Goodbye' } , { text : 'GOODBYE' } ] , world : 'world' } ;
72
+ shouldCompileTo ( string , [ hash , , , , false ] , 'goodbye! Goodbye! GOODBYE! cruel world!' ) ;
73
+
74
+ hash = { goodbyes : 'cruel' , world : 'world' } ;
75
+ shouldCompileTo ( '{{#each .}}{{.}}{{/each}}' , [ hash , , , , false ] , 'cruelworld' ) ;
76
+ } ) ;
77
+
78
+ it ( 'each without context' , function ( ) {
79
+ var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!' ;
80
+ shouldCompileTo ( string , [ , , , , ] , 'cruel !' ) ;
81
+ } ) ;
82
+
69
83
it ( "each with an object and @key" , function ( ) {
70
84
var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!" ;
71
- var hash = { goodbyes : { "<b>#1</b>" : { text : "goodbye" } , 2 : { text : "GOODBYE" } } , world : "world" } ;
85
+
86
+ function Clazz ( ) {
87
+ this [ '<b>#1</b>' ] = { text : 'goodbye' } ;
88
+ this [ 2 ] = { text : 'GOODBYE' } ;
89
+ }
90
+ Clazz . prototype . foo = 'fail' ;
91
+ var hash = { goodbyes : new Clazz ( ) , world : 'world' } ;
72
92
73
93
// Object property iteration order is undefined according to ECMA spec,
74
94
// so we need to check both possible orders
@@ -193,19 +213,77 @@ describe('builtin helpers', function() {
193
213
} ) ;
194
214
} ) ;
195
215
196
- it ( "#log" , function ( ) {
197
- var string = "{{log blah}}" ;
198
- var hash = { blah : "whee" } ;
216
+ describe ( "#log" , function ( ) {
217
+ var info ,
218
+ error ;
219
+ beforeEach ( function ( ) {
220
+ info = console . info ;
221
+ error = console . error ;
222
+ } ) ;
223
+ afterEach ( function ( ) {
224
+ console . info = info ;
225
+ console . error = error ;
226
+ } ) ;
227
+
228
+ it ( 'should call logger at default level' , function ( ) {
229
+ var string = "{{log blah}}" ;
230
+ var hash = { blah : "whee" } ;
199
231
200
- var levelArg , logArg ;
201
- handlebarsEnv . log = function ( level , arg ) {
202
- levelArg = level ;
203
- logArg = arg ;
204
- } ;
232
+ var levelArg , logArg ;
233
+ handlebarsEnv . log = function ( level , arg ) {
234
+ levelArg = level ;
235
+ logArg = arg ;
236
+ } ;
205
237
206
- shouldCompileTo ( string , hash , "" , "log should not display" ) ;
207
- equals ( 1 , levelArg , "should call log with 1" ) ;
208
- equals ( "whee" , logArg , "should call log with 'whee'" ) ;
238
+ shouldCompileTo ( string , hash , "" , "log should not display" ) ;
239
+ equals ( 1 , levelArg , "should call log with 1" ) ;
240
+ equals ( "whee" , logArg , "should call log with 'whee'" ) ;
241
+ } ) ;
242
+ it ( 'should call logger at data level' , function ( ) {
243
+ var string = "{{log blah}}" ;
244
+ var hash = { blah : "whee" } ;
245
+
246
+ var levelArg , logArg ;
247
+ handlebarsEnv . log = function ( level , arg ) {
248
+ levelArg = level ;
249
+ logArg = arg ;
250
+ } ;
251
+
252
+ shouldCompileTo ( string , [ hash , , , , { level : '03' } ] , "" ) ;
253
+ equals ( 3 , levelArg ) ;
254
+ equals ( "whee" , logArg ) ;
255
+ } ) ;
256
+ it ( 'should not output to console' , function ( ) {
257
+ var string = "{{log blah}}" ;
258
+ var hash = { blah : "whee" } ;
259
+
260
+ console . info = function ( ) {
261
+ throw new Error ( ) ;
262
+ } ;
263
+
264
+ shouldCompileTo ( string , hash , "" , "log should not display" ) ;
265
+ } ) ;
266
+ it ( 'should log at data level' , function ( ) {
267
+ var string = "{{log blah}}" ;
268
+ var hash = { blah : "whee" } ;
269
+ var called ;
270
+
271
+ console . error = function ( log ) {
272
+ equals ( "whee" , log ) ;
273
+ called = true ;
274
+ } ;
275
+
276
+ shouldCompileTo ( string , [ hash , , , , { level : '03' } ] , "" ) ;
277
+ equals ( true , called ) ;
278
+ } ) ;
279
+ it ( 'should handle missing logger' , function ( ) {
280
+ var string = "{{log blah}}" ;
281
+ var hash = { blah : "whee" } ;
282
+
283
+ console . error = undefined ;
284
+
285
+ shouldCompileTo ( string , [ hash , , , , { level : '03' } ] , "" ) ;
286
+ } ) ;
209
287
} ) ;
210
288
211
289
0 commit comments