1
- var global = ( function ( ) { return this ; } ( ) ) ;
1
+ var global = ( function ( ) {
2
+ return this ;
3
+ } ( ) ) ;
2
4
3
5
var AssertError ;
4
6
if ( Error . captureStackTrace ) {
@@ -16,17 +18,26 @@ if (Error.captureStackTrace) {
16
18
AssertError = Error ;
17
19
}
18
20
21
+ /**
22
+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
23
+ */
19
24
global . shouldCompileTo = function ( string , hashOrArray , expected , message ) {
20
25
shouldCompileToWithPartials ( string , hashOrArray , false , expected , message ) ;
21
26
} ;
22
27
28
+ /**
29
+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
30
+ */
23
31
global . shouldCompileToWithPartials = function shouldCompileToWithPartials ( string , hashOrArray , partials , expected , message ) {
24
32
var result = compileWithPartials ( string , hashOrArray , partials ) ;
25
33
if ( result !== expected ) {
26
34
throw new AssertError ( "'" + result + "' should === '" + expected + "': " + message , shouldCompileToWithPartials ) ;
27
35
}
28
36
} ;
29
37
38
+ /**
39
+ * @deprecated Use "expectTemplate(template)...toCompileTo(output)" instead
40
+ */
30
41
global . compileWithPartials = function ( string , hashOrArray , partials ) {
31
42
var template ,
32
43
ary ,
@@ -36,8 +47,8 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
36
47
delete hashOrArray . hash ;
37
48
} else if ( Object . prototype . toString . call ( hashOrArray ) === '[object Array]' ) {
38
49
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 ] } ) ;
41
52
options = typeof hashOrArray [ 3 ] === 'object' ? hashOrArray [ 3 ] : { compat : hashOrArray [ 3 ] } ;
42
53
if ( hashOrArray [ 4 ] != null ) {
43
54
options . data = ! ! hashOrArray [ 4 ] ;
@@ -75,3 +86,72 @@ global.shouldThrow = function(callback, type, msg) {
75
86
throw new AssertError ( 'It failed to throw' , shouldThrow ) ;
76
87
}
77
88
} ;
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