@@ -5,7 +5,7 @@ import {resolve as resolvePath} from "path";
5
5
import * as sinon from "sinon" ;
6
6
import { TwingEnvironment } from "twing/lib/environment" ;
7
7
8
- let dependencies : string [ ] = [ ] ;
8
+ let dependencies : string [ ] ;
9
9
10
10
const loaderContext = {
11
11
query : {
@@ -18,112 +18,127 @@ const loaderContext = {
18
18
} ;
19
19
20
20
tape ( 'loader' , ( test : Test ) => {
21
- let environment : TwingEnvironment = require ( loaderContext . query . environmentModulePath ) ;
21
+ for ( let separator of [ '/' , '\\' ] ) {
22
+ test . test ( `with "${ separator } " as path separator` , ( test ) => {
23
+ dependencies = [ ] ;
22
24
23
- let spy = sinon . spy ( environment , 'addNodeVisitor' ) ;
24
-
25
- loader . bind ( loaderContext ) ( '{% embed "./bar.twig" %}{% endembed %}' ) ;
26
-
27
- test . same ( dependencies , [ resolvePath ( 'test/unit/fixtures/environment.js' ) ] , 'declares the environment module as a dependency' ) ;
28
-
29
- loader . bind ( loaderContext ) ( '' ) ;
30
-
31
- test . true ( spy . notCalled , 'doesn\'t pollute the environment outside of the loader' ) ;
32
-
33
- test . test ( 'handles "render at compile time" mode' , ( test ) => {
34
- let renderLoaderContext : any = {
35
- query : {
36
- environmentModulePath : loaderContext . query . environmentModulePath ,
37
- renderContext : {
38
- bar : 'BAR'
39
- }
40
- } ,
41
- resourcePath : loaderContext . resourcePath ,
42
- addDependency : loaderContext . addDependency
43
- } ;
44
-
45
- let actual : string = loader . bind ( renderLoaderContext ) ( '{% embed "./bar.twig" %}{% endembed %}{{bar}}' ) ;
46
-
47
- test . same ( actual , 'module.exports = " FOO\\nBAR";' ) ;
48
-
49
- test . end ( ) ;
50
- } ) ;
51
-
52
- test . test ( 'provides options validation' , ( test ) => {
53
- type ValidationError = {
54
- dataPath : string ,
55
- keyword : string ,
56
- message : string
57
- } ;
58
-
59
- type Fixture = {
60
- options : any ,
61
- expectation : ValidationError
62
- } ;
63
-
64
- let fixtures : Fixture [ ] = [
65
- {
66
- options : { } ,
67
- expectation : {
68
- dataPath : '' ,
69
- keyword : 'required' ,
70
- message : 'should have required property \'environmentModulePath\''
71
- }
72
- } ,
73
- {
74
- options : {
75
- environmentModulePath : { }
25
+ let context = Object . assign ( { } , loaderContext , {
26
+ query : {
27
+ environmentModulePath : loaderContext . query . environmentModulePath . replace ( / \/ / g, separator )
76
28
} ,
77
- expectation : {
78
- dataPath : '.environmentModulePath' ,
79
- keyword : 'type' ,
80
- message : 'should be string'
29
+ resourcePath : loaderContext . resourcePath . replace ( / \/ / g, separator )
30
+ } ) ;
31
+
32
+ let environment : TwingEnvironment = require ( loaderContext . query . environmentModulePath ) ;
33
+
34
+ let spy = sinon . spy ( environment , 'addNodeVisitor' ) ;
35
+
36
+ loader . bind ( context ) ( '{% embed "./bar.twig" %}{% endembed %}' ) ;
37
+
38
+ test . same ( dependencies , [ resolvePath ( 'test/unit/fixtures/environment.js' ) ] , 'declares the environment module as a dependency' ) ;
39
+
40
+ loader . bind ( context ) ( '' ) ;
41
+
42
+ test . true ( spy . notCalled , 'doesn\'t pollute the environment outside of the loader' ) ;
43
+
44
+ test . test ( 'handles "render at compile time" mode' , ( test ) => {
45
+ let renderLoaderContext : any = {
46
+ query : {
47
+ environmentModulePath : context . query . environmentModulePath ,
48
+ renderContext : {
49
+ bar : 'BAR'
50
+ }
51
+ } ,
52
+ resourcePath : context . resourcePath ,
53
+ addDependency : context . addDependency
54
+ } ;
55
+
56
+ let actual : string = loader . bind ( renderLoaderContext ) ( '{% embed "./bar.twig" %}{% endembed %}{{bar}}' ) ;
57
+
58
+ test . same ( actual , 'module.exports = " FOO\\nBAR";' ) ;
59
+
60
+ test . end ( ) ;
61
+ } ) ;
62
+
63
+ test . test ( 'provides options validation' , ( test ) => {
64
+ type ValidationError = {
65
+ dataPath : string ,
66
+ keyword : string ,
67
+ message : string
68
+ } ;
69
+
70
+ type Fixture = {
71
+ options : any ,
72
+ expectation : ValidationError
73
+ } ;
74
+
75
+ let fixtures : Fixture [ ] = [
76
+ {
77
+ options : { } ,
78
+ expectation : {
79
+ dataPath : '' ,
80
+ keyword : 'required' ,
81
+ message : 'should have required property \'environmentModulePath\''
82
+ }
83
+ } ,
84
+ {
85
+ options : {
86
+ environmentModulePath : { }
87
+ } ,
88
+ expectation : {
89
+ dataPath : '.environmentModulePath' ,
90
+ keyword : 'type' ,
91
+ message : 'should be string'
92
+ }
93
+ } ,
94
+ {
95
+ options : {
96
+ environmentModulePath : '' ,
97
+ renderContext : ''
98
+ } ,
99
+ expectation : {
100
+ dataPath : '.renderContext' ,
101
+ keyword : 'type' ,
102
+ message : 'should be object'
103
+ }
104
+ } ,
105
+ {
106
+ options : {
107
+ environmentModulePath : '' ,
108
+ foo : ''
109
+ } ,
110
+ expectation : {
111
+ dataPath : '' ,
112
+ keyword : 'additionalProperties' ,
113
+ message : 'should NOT have additional properties'
114
+ }
115
+ }
116
+ ] ;
117
+
118
+ for ( let fixture of fixtures ) {
119
+ let errors : ValidationError [ ] ;
120
+
121
+ try {
122
+ loader . bind ( {
123
+ query : fixture . options
124
+ } ) ( ) ;
125
+
126
+ errors = [ ] ;
127
+ } catch ( e ) {
128
+ errors = e . errors ;
129
+ }
130
+
131
+ test . same ( errors [ 0 ] . dataPath , fixture . expectation . dataPath ) ;
132
+ test . same ( errors [ 0 ] . keyword , fixture . expectation . keyword ) ;
133
+ test . same ( errors [ 0 ] . message , fixture . expectation . message ) ;
81
134
}
82
- } ,
83
- {
84
- options : {
85
- environmentModulePath : '' ,
86
- renderContext : ''
87
- } ,
88
- expectation : {
89
- dataPath : '.renderContext' ,
90
- keyword : 'type' ,
91
- message : 'should be object'
92
- }
93
- } ,
94
- {
95
- options : {
96
- environmentModulePath : '' ,
97
- foo : ''
98
- } ,
99
- expectation : {
100
- dataPath : '' ,
101
- keyword : 'additionalProperties' ,
102
- message : 'should NOT have additional properties'
103
- }
104
- }
105
- ] ;
106
-
107
- for ( let fixture of fixtures ) {
108
- let errors : ValidationError [ ] ;
109
-
110
- try {
111
- loader . bind ( {
112
- query : fixture . options
113
- } ) ( ) ;
114
-
115
- errors = [ ] ;
116
- } catch ( e ) {
117
- errors = e . errors ;
118
- }
119
135
120
- test . same ( errors [ 0 ] . dataPath , fixture . expectation . dataPath ) ;
121
- test . same ( errors [ 0 ] . keyword , fixture . expectation . keyword ) ;
122
- test . same ( errors [ 0 ] . message , fixture . expectation . message ) ;
123
- }
136
+ test . end ( ) ;
137
+ } ) ;
124
138
125
- test . end ( ) ;
126
- } ) ;
139
+ test . end ( ) ;
140
+ } ) ;
141
+ }
127
142
128
143
test . end ( ) ;
129
144
} ) ;
0 commit comments