1- var global = ( function ( ) { return this ; } ( ) ) ;
1+ var global = ( function ( ) {
2+ return this ;
3+ } ( ) ) ;
24
35var AssertError ;
46if ( Error . captureStackTrace ) {
@@ -16,17 +18,26 @@ if (Error.captureStackTrace) {
1618 AssertError = Error ;
1719}
1820
21+ /**
22+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
23+ */
1924global . shouldCompileTo = function ( string , hashOrArray , expected , message ) {
2025 shouldCompileToWithPartials ( string , hashOrArray , false , expected , message ) ;
2126} ;
2227
28+ /**
29+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
30+ */
2331global . shouldCompileToWithPartials = function shouldCompileToWithPartials ( string , hashOrArray , partials , expected , message ) {
2432 var result = compileWithPartials ( string , hashOrArray , partials ) ;
2533 if ( result !== expected ) {
2634 throw new AssertError ( "'" + result + "' should === '" + expected + "': " + message , shouldCompileToWithPartials ) ;
2735 }
2836} ;
2937
38+ /**
39+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
40+ */
3041global . compileWithPartials = function ( string , hashOrArray , partials ) {
3142 var template ,
3243 ary ,
@@ -36,8 +47,8 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
3647 delete hashOrArray . hash ;
3748 } else if ( Object . prototype . toString . call ( hashOrArray ) === '[object Array]' ) {
3849 ary = [ ] ;
39- ary . push ( hashOrArray [ 0 ] ) ;
40- ary . push ( { helpers : hashOrArray [ 1 ] , partials : hashOrArray [ 2 ] } ) ;
50+ ary . push ( hashOrArray [ 0 ] ) ; // input
51+ ary . push ( { helpers : hashOrArray [ 1 ] , partials : hashOrArray [ 2 ] } ) ;
4152 options = typeof hashOrArray [ 3 ] === 'object' ? hashOrArray [ 3 ] : { compat : hashOrArray [ 3 ] } ;
4253 if ( hashOrArray [ 4 ] != null ) {
4354 options . data = ! ! hashOrArray [ 4 ] ;
@@ -75,3 +86,72 @@ global.shouldThrow = function(callback, type, msg) {
7586 throw new AssertError ( 'It failed to throw' , shouldThrow ) ;
7687 }
7788} ;
89+
90+
91+ global . expectTemplate = function ( templateAsString ) {
92+ return new HandlebarsTestBench ( templateAsString ) ;
93+ } ;
94+
95+
96+ function HandlebarsTestBench ( templateAsString ) {
97+ this . templateAsString = templateAsString ;
98+ this . helpers = { } ;
99+ this . partials = { } ;
100+ this . input = { } ;
101+ this . message = 'Template' + templateAsString + ' does not evaluate to expected output' ;
102+ this . compileOptions = { } ;
103+ this . runtimeOptions = { } ;
104+ }
105+
106+ HandlebarsTestBench . prototype . withInput = function ( input ) {
107+ this . input = input ;
108+ return this ;
109+ } ;
110+
111+ HandlebarsTestBench . prototype . withHelper = function ( name , helperFunction ) {
112+ this . helpers [ name ] = helperFunction ;
113+ return this ;
114+ } ;
115+
116+ HandlebarsTestBench . prototype . withPartial = function ( name , partialAsString ) {
117+ this . partials [ name ] = partialAsString ;
118+ return this ;
119+ } ;
120+
121+ HandlebarsTestBench . prototype . withCompileOptions = function ( compileOptions ) {
122+ this . compileOptions = compileOptions ;
123+ return this ;
124+ } ;
125+
126+ HandlebarsTestBench . prototype . withRuntimeOptions = function ( runtimeOptions ) {
127+ this . runtimeOptions = runtimeOptions ;
128+ return this ;
129+ } ;
130+
131+ HandlebarsTestBench . prototype . withMessage = function ( message ) {
132+ this . message = message ;
133+ return this ;
134+ } ;
135+
136+ HandlebarsTestBench . prototype . toCompileTo = function ( expectedOutputAsString ) {
137+ var self = this ;
138+ var compile = Object . keys ( this . partials ) . length > 0
139+ ? CompilerContext . compileWithPartial
140+ : CompilerContext . compile ;
141+
142+ var combinedRuntimeOptions = { } ;
143+ Object . keys ( this . runtimeOptions ) . forEach ( function ( key ) {
144+ combinedRuntimeOptions [ key ] = self . runtimeOptions [ key ] ;
145+ } ) ;
146+ combinedRuntimeOptions . helpers = this . helpers ;
147+ combinedRuntimeOptions . partials = this . partials ;
148+
149+ var template = compile ( this . templateAsString , this . compileOptions ) ;
150+ var output = template ( this . input , combinedRuntimeOptions ) ;
151+
152+ if ( output !== expectedOutputAsString ) {
153+ // Error message formatted so that IntelliJ-Idea shows "diff"-button
154+ // https://stackoverflow.com/a/10945655/4251384
155+ throw new AssertError ( this . message + '\nexpected:' + expectedOutputAsString + 'but was:' + output ) ;
156+ }
157+ } ;
0 commit comments