main
cs 334 lines 9.61 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 namespace WixToolsetTest.Core
4 {
5 using System;
6 using System.IO;
7 using System.Xml;
8 using WixInternal.TestSupport;
9 using WixToolset.Core;
10 using WixToolset.Extensibility.Data;
11 using WixToolset.Extensibility.Services;
12 using Xunit;
13
14 public class PreprocessorFixture
15 {
16 [Fact]
17 public void CanPreprocessWithSingleEquals()
18 {
19 var input = String.Join(Environment.NewLine,
20 "<Wix>",
21 "<?define A=0?>",
22 "<?if $(A)=\"0\" ?>",
23 " <Package />",
24 "<?endif?>",
25 "</Wix>"
26 );
27 var expected = new[]
28 {
29 "<Wix>",
30 " <Package />",
31 "</Wix>"
32 };
33
34 var result = PreprocessFromString(input);
35
36 var actual = result.Document.ToString().Split("\r\n");
37 WixAssert.CompareLineByLine(expected, actual);
38 }
39
40 [Fact]
41 public void CanPreprocessWithDoubleEquals()
42 {
43 var input = String.Join(Environment.NewLine,
44 "<Wix>",
45 "<?define A=0?>",
46 "<?if $(A)==\"0\" ?>",
47 " <Fragment />",
48 "<?endif?>",
49 "</Wix>"
50 );
51 var expected = new[]
52 {
53 "<Wix>",
54 " <Fragment />",
55 "</Wix>"
56 };
57
58 var result = PreprocessFromString(input);
59
60 var actual = result.Document.ToString().Split("\r\n");
61 WixAssert.CompareLineByLine(expected, actual);
62 }
63
64 [Fact]
65 public void CanPreprocessFalsyCondition()
66 {
67 var input = String.Join(Environment.NewLine,
68 "<Wix>",
69 "<?define A=0?>",
70 "<?if $(A) ?>",
71 " <Fragment />",
72 "<?endif?>",
73 "</Wix>"
74 );
75 var expected = new[]
76 {
77 "<Wix />"
78 };
79
80 var result = PreprocessFromString(input);
81
82 var actual = result.Document.ToString().Split("\r\n");
83 WixAssert.CompareLineByLine(expected, actual);
84 }
85
86 [Fact]
87 public void CanPreprocessTruthyCondition()
88 {
89 var input = String.Join(Environment.NewLine,
90 "<Wix>",
91 "<?define A=1?>",
92 "<?if $(A) ?>",
93 " <Fragment />",
94 "<?endif?>",
95 "</Wix>"
96 );
97 var expected = new[]
98 {
99 "<Wix>",
100 " <Fragment />",
101 "</Wix>"
102 };
103
104 var result = PreprocessFromString(input);
105
106 var actual = result.Document.ToString().Split("\r\n");
107 WixAssert.CompareLineByLine(expected, actual);
108 }
109
110 [Fact]
111 public void CanPreprocessIfdefWithFullVariableSyntaxFalsy()
112 {
113 var input = String.Join(Environment.NewLine,
114 "<Wix>",
115 "<?ifdef $(A) ?>",
116 " <Fragment />",
117 "<?endif?>",
118 "</Wix>"
119 );
120 var expected = new[]
121 {
122 "<Wix />"
123 };
124
125 var result = PreprocessFromString(input);
126
127 var actual = result.Document.ToString().Split("\r\n");
128 WixAssert.CompareLineByLine(expected, actual);
129 }
130
131 [Fact]
132 public void CanPreprocessIfdefWithFullVariableSyntaxTruthy()
133 {
134 var input = String.Join(Environment.NewLine,
135 "<Wix>",
136 "<?define A?>",
137 "<?ifdef $(A) ?>",
138 " <Fragment />",
139 "<?endif?>",
140 "</Wix>"
141 );
142 var expected = new[]
143 {
144 "<Wix>",
145 " <Fragment />",
146 "</Wix>"
147 };
148
149 var result = PreprocessFromString(input);
150
151 var actual = result.Document.ToString().Split("\r\n");
152 WixAssert.CompareLineByLine(expected, actual);
153 }
154
155 [Fact]
156 public void CanPreprocessIfndefWithFullVariableSyntaxTruthy()
157 {
158 var input = String.Join(Environment.NewLine,
159 "<Wix>",
160 "<?ifndef $(A) ?>",
161 " <Fragment />",
162 "<?endif?>",
163 "</Wix>"
164 );
165 var expected = new[]
166 {
167 "<Wix>",
168 " <Fragment />",
169 "</Wix>"
170 };
171
172 var result = PreprocessFromString(input);
173
174 var actual = result.Document.ToString().Split("\r\n");
175 WixAssert.CompareLineByLine(expected, actual);
176 }
177
178 [Fact]
179 public void CanPreprocessIfndefWithFullVariableSyntaxFalsy()
180 {
181 var input = String.Join(Environment.NewLine,
182 "<Wix>",
183 "<?define A?>",
184 "<?ifndef $(A) ?>",
185 " <Fragment />",
186 "<?endif?>",
187 "</Wix>"
188 );
189 var expected = new[]
190 {
191 "<Wix />"
192 };
193
194 var result = PreprocessFromString(input);
195
196 var actual = result.Document.ToString().Split("\r\n");
197 WixAssert.CompareLineByLine(expected, actual);
198 }
199
200 [Fact]
201 public void CanPreprocessIfdefWithFullVariableV3SyntaxTruthy()
202 {
203 var input = String.Join(Environment.NewLine,
204 "<Wix>",
205 "<?define A?>",
206 "<?ifdef $(var.A) ?>",
207 " <Fragment />",
208 "<?endif?>",
209 "</Wix>"
210 );
211 var expected = new[]
212 {
213 "<Wix>",
214 " <Fragment />",
215 "</Wix>"
216 };
217
218 var result = PreprocessFromString(input);
219
220 var actual = result.Document.ToString().Split("\r\n");
221 WixAssert.CompareLineByLine(expected, actual);
222 }
223
224 [Fact]
225 public void CanPreprocessIfndefWithFullVariableV3SyntaxTruthy()
226 {
227 var input = String.Join(Environment.NewLine,
228 "<Wix>",
229 "<?ifndef $(var.A) ?>",
230 " <Fragment />",
231 "<?endif?>",
232 "</Wix>"
233 );
234 var expected = new[]
235 {
236 "<Wix>",
237 " <Fragment />",
238 "</Wix>"
239 };
240
241 var result = PreprocessFromString(input);
242
243 var actual = result.Document.ToString().Split("\r\n");
244 WixAssert.CompareLineByLine(expected, actual);
245 }
246
247 [Fact]
248 public void CanPreprocessIfndefWithFullVariableV3SyntaxFalsy()
249 {
250 var input = String.Join(Environment.NewLine,
251 "<Wix>",
252 "<?define A?>",
253 "<?ifndef $(var.A) ?>",
254 " <Fragment />",
255 "<?endif?>",
256 "</Wix>"
257 );
258 var expected = new[]
259 {
260 "<Wix />"
261 };
262
263 var result = PreprocessFromString(input);
264
265 var actual = result.Document.ToString().Split("\r\n");
266 WixAssert.CompareLineByLine(expected, actual);
267 }
268
269 [Fact]
270 public void CanPreprocessForeach()
271 {
272 var input = String.Join(Environment.NewLine,
273 "<Wix>",
274 "<?foreach value in A ; B ; C ?>",
275 " <Fragment Id='$(value)' />",
276 "<?endforeach?>",
277 "</Wix>"
278 );
279 var expected = new[]
280 {
281 "<Wix>",
282 " <Fragment Id=\"A \" />",
283 " <Fragment Id=\" B \" />",
284 " <Fragment Id=\" C\" />",
285 "</Wix>"
286 };
287
288 var result = PreprocessFromString(input);
289
290 var actual = result.Document.ToString().Split("\r\n");
291 WixAssert.CompareLineByLine(expected, actual);
292 }
293
294 [Fact]
295 public void CanPreprocessForeachWithQuotes()
296 {
297 var input = String.Join(Environment.NewLine,
298 "<Wix>",
299 "<?foreach value in \" A ; B ; C \" ?>",
300 " <Fragment Id='$(value)' />",
301 "<?endforeach?>",
302 "</Wix>"
303 );
304 var expected = new[]
305 {
306 "<Wix>",
307 " <Fragment Id=\" A \" />",
308 " <Fragment Id=\" B \" />",
309 " <Fragment Id=\" C \" />",
310 "</Wix>"
311 };
312
313 var result = PreprocessFromString(input);
314
315 var actual = result.Document.ToString().Split("\r\n");
316 WixAssert.CompareLineByLine(expected, actual);
317 }
318
319 private static IPreprocessResult PreprocessFromString(string xml)
320 {
321 using var stringReader = new StringReader(xml);
322 using var xmlReader = XmlReader.Create(stringReader);
323
324 var sp = WixToolsetServiceProviderFactory.CreateServiceProvider();
325
326 var context = sp.GetService<IPreprocessContext>();
327 context.SourcePath = @"path\provided\for\testing\purposes\only.wxs";
328
329 var preprocessor = context.ServiceProvider.GetService<IPreprocessor>();
330 var result = preprocessor.Preprocess(context, xmlReader);
331 return result;
332 }
333 }
334 }