-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement strict and assumeObject modes
Causes templates to throw when lookup fields are not defined within the context. Strict mode will throw when any field is omitted. assumeObjects mode maintains the existing behavior of outputting an empty response when fields are not defined but allows for performance/size optimizations by not doing safety checks for intermediate objects in the lookup chain. Strict mode effectively disables the helperMissing and inverse handling for blockHelperMissing as templates will throw rather than call those features for missing data fields. Fixes #651 Fixes #805
- Loading branch information
Showing
3 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/*global CompilerContext, Handlebars, shouldThrow */ | ||
var Exception = Handlebars.Exception; | ||
|
||
describe('strict', function() { | ||
describe('strict mode', function() { | ||
it('should error on missing property lookup', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello}}', {strict: true}); | ||
|
||
template({}); | ||
}, Exception, /"hello" not defined in/); | ||
}); | ||
it('should error on missing child', function() { | ||
var template = CompilerContext.compile('{{hello.bar}}', {strict: true}); | ||
equals(template({hello: {bar: 'foo'}}), 'foo'); | ||
|
||
shouldThrow(function() { | ||
template({hello: {}}); | ||
}, Exception, /"bar" not defined in/); | ||
}); | ||
it('should handle explicit undefined', function() { | ||
var template = CompilerContext.compile('{{hello.bar}}', {strict: true}); | ||
|
||
equals(template({hello: {bar: undefined}}), ''); | ||
}); | ||
it('should error on missing property lookup in known helpers mode', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello}}', {strict: true, knownHelpersOnly: true}); | ||
|
||
template({}); | ||
}, Exception, /"hello" not defined in/); | ||
}); | ||
it('should error on missing context', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello}}', {strict: true}); | ||
|
||
template(); | ||
}, Error); | ||
}); | ||
|
||
it('should error on missing data lookup', function() { | ||
var template = CompilerContext.compile('{{@hello}}', {strict: true}); | ||
equals(template(undefined, {data: {hello: 'foo'}}), 'foo'); | ||
|
||
shouldThrow(function() { | ||
template(); | ||
}, Error); | ||
}); | ||
|
||
it('should not run helperMissing for helper calls', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello foo}}', {strict: true}); | ||
|
||
template({foo: true}); | ||
}, Exception, /"hello" not defined in/); | ||
|
||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{#hello foo}}{{/hello}}', {strict: true}); | ||
|
||
template({foo: true}); | ||
}, Exception, /"hello" not defined in/); | ||
}); | ||
it('should throw on ambiguous blocks', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{#hello}}{{/hello}}', {strict: true}); | ||
|
||
template({}); | ||
}, Exception, /"hello" not defined in/); | ||
|
||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{^hello}}{{/hello}}', {strict: true}); | ||
|
||
template({}); | ||
}, Exception, /"hello" not defined in/); | ||
|
||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{#hello.bar}}{{/hello.bar}}', {strict: true}); | ||
|
||
template({hello: {}}); | ||
}, Exception, /"bar" not defined in/); | ||
}); | ||
}); | ||
|
||
describe('assume objects', function() { | ||
it('should ignore missing property', function() { | ||
var template = CompilerContext.compile('{{hello}}', {assumeObjects: true}); | ||
|
||
equal(template({}), ''); | ||
}); | ||
it('should ignore missing child', function() { | ||
var template = CompilerContext.compile('{{hello.bar}}', {assumeObjects: true}); | ||
|
||
equal(template({hello: {}}), ''); | ||
}); | ||
it('should error on missing object', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello.bar}}', {assumeObjects: true}); | ||
|
||
template({}); | ||
}, Error); | ||
}); | ||
it('should error on missing context', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{hello}}', {assumeObjects: true}); | ||
|
||
template(); | ||
}, Error); | ||
}); | ||
|
||
it('should error on missing data lookup', function() { | ||
shouldThrow(function() { | ||
var template = CompilerContext.compile('{{@hello.bar}}', {assumeObjects: true}); | ||
|
||
template(); | ||
}, Error); | ||
}); | ||
|
||
it('should execute blockHelperMissing', function() { | ||
var template = CompilerContext.compile('{{^hello}}foo{{/hello}}', {assumeObjects: true}); | ||
|
||
equals(template({}), 'foo'); | ||
}); | ||
}); | ||
}); |