1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
+ using System ;
4
5
using System . Linq ;
5
6
using Xunit ;
6
7
@@ -10,108 +11,239 @@ namespace Mono.TextTemplating.Tests
10
11
[ CollectionDefinition ( nameof ( MSBuildExecutionTests ) , DisableParallelization = true ) ]
11
12
public class MSBuildExecutionTests : IClassFixture < MSBuildFixture >
12
13
{
13
- [ Fact ]
14
- public void TransformExplicitWithArguments ( )
14
+ [ Theory ]
15
+ [ InlineData ( "TransformTemplates" , "foo.txt" , "Hello 2019!" ) ]
16
+ [ InlineData ( "TransformTemplateFromRelativePath" , "Nested/Template/Folder/foo.txt" , "Hello 2024!" ) ]
17
+ [ InlineData ( "TransformTemplateWithExtension" , "foo.html" , "<h1>Hello 2024!</h1>" ) ]
18
+ public void TransformExplicitWithArguments ( string projectName , string expectedFilePath , string expectedText )
15
19
{
16
20
using var ctx = new MSBuildTestContext ( ) ;
17
- var project = ctx . LoadTestProject ( "TransformTemplates" ) ;
21
+ var project = ctx . LoadTestProject ( projectName ) ;
18
22
19
23
var instance = project . Build ( "TransformTemplates" ) ;
20
24
21
- var generated = project . DirectoryPath [ "foo.txt" ] . AssertTextStartsWith ( "Hello 2019!" ) ;
25
+ var generated = project . DirectoryPath [ expectedFilePath ] . AssertTextStartsWith ( expectedText ) ;
22
26
23
27
instance . AssertSingleItem ( "GeneratedTemplates" , withFullPath : generated ) ;
24
28
instance . AssertNoItems ( "PreprocessedTemplates" ) ;
25
29
}
26
30
27
- [ Fact ]
28
- public void TransformOnBuild ( )
31
+ [ Theory ]
32
+ [ InlineData ( "TransformTemplates" , "foo.txt" , "Hello 2019!" ) ]
33
+ [ InlineData ( "TransformTemplateFromRelativePath" , "Nested/Template/Folder/foo.txt" , "Hello 2024!" ) ]
34
+ [ InlineData ( "TransformTemplateWithExtension" , "foo.html" , "<h1>Hello 2024!</h1>" ) ]
35
+ public void TransformOnBuild ( string projectName , string expectedFilePath , string expectedText )
29
36
{
30
37
using var ctx = new MSBuildTestContext ( ) ;
31
- var project = ctx . LoadTestProject ( "TransformTemplates" )
38
+ var project = ctx . LoadTestProject ( projectName )
32
39
. WithProperty ( "TransformOnBuild" , "true" ) ;
33
40
34
41
project . Restore ( ) ;
35
42
36
43
var instance = project . Build ( "Build" ) ;
37
44
38
- var generatedFilePath = project . DirectoryPath [ "foo.txt" ] . AssertTextStartsWith ( "Hello 2019!" ) ;
45
+ var generatedFilePath = project . DirectoryPath [ expectedFilePath ] . AssertTextStartsWith ( expectedText ) ;
39
46
40
47
instance . AssertSingleItem ( "GeneratedTemplates" , withFullPath : generatedFilePath ) ;
41
48
instance . AssertNoItems ( "PreprocessedTemplates" ) ;
42
49
}
43
50
44
- [ Fact ]
45
- public void TransformOnBuildDisabled ( )
51
+ [ Theory ]
52
+ [ InlineData ( "TransformTemplates" , "foo.txt" ) ]
53
+ [ InlineData ( "TransformTemplateFromRelativePath" , "Nested/Template/Folder/foo.txt" ) ]
54
+ [ InlineData ( "TransformTemplateWithExtension" , "foo.html" ) ]
55
+ public void TransformOnBuildDisabled ( string projectName , string expectedFilePath )
46
56
{
47
57
using var ctx = new MSBuildTestContext ( ) ;
48
- var project = ctx . LoadTestProject ( "TransformTemplates" ) ;
58
+ var project = ctx . LoadTestProject ( projectName ) ;
49
59
50
60
project . Restore ( ) ;
51
61
52
62
var instance = project . Build ( "Build" ) ;
53
63
54
- project . DirectoryPath [ "foo.txt" ] . AssertFileExists ( false ) ;
64
+ project . DirectoryPath [ expectedFilePath ] . AssertFileExists ( false ) ;
55
65
56
66
instance . AssertNoItems ( "GeneratedTemplates" , "PreprocessedTemplates" ) ;
57
67
}
58
68
59
69
[ Fact ]
60
- public void PreprocessLegacy ( )
70
+ public void TransformMetadata ( )
71
+ {
72
+ // Arrange
73
+ using var ctx = new MSBuildTestContext ( ) ;
74
+ var project = ctx . LoadTestProject ( "TransformTemplateMetadata" ) ;
75
+
76
+ var outputDirectory = project . DirectoryPath [ "Demo/Output/OutputDirectory.txt" ] ;
77
+ var outputFileName = project . DirectoryPath [ "Demo/OutputFileNameTest" ] ;
78
+ var outputDirectoryAndOutputFileName = project . DirectoryPath [ "Demo/Output/OutputDirectoryAndFileNameTest.log" ] ;
79
+
80
+ // Act
81
+ var instance = project . Build ( "TransformTemplates" ) ;
82
+
83
+ // Assert
84
+ Assert . Multiple ( ( ) => {
85
+ outputDirectory . AssertTextStartsWith ( "Hello Metadata OutputDirectory 2024!" ) ;
86
+ outputFileName . AssertTextStartsWith ( "Hello Metadata OutputFileName 2024!" ) ;
87
+ outputDirectoryAndOutputFileName . AssertTextStartsWith ( "Hello Metadata OutputDirectory and OutputFileName 2024!" ) ;
88
+ } ) ;
89
+
90
+ instance . AssertNoItems ( "PreprocessedTemplates" ) ;
91
+ }
92
+
93
+ [ Theory ]
94
+ [ InlineData (
95
+ "PreprocessTemplate" ,
96
+ "foo.cs" ,
97
+ new string [ ] {
98
+ "namespace PreprocessTemplate {" ,
99
+ "public partial class foo : fooBase {"
100
+ }
101
+ ) ]
102
+ [ InlineData (
103
+ "PreprocessTemplateFromRelativePath" ,
104
+ "Nested/Template/Folder/foo.cs" ,
105
+ new string [ ] {
106
+ "namespace PreprocessTemplateFromRelativePath.Nested.Template.Folder {" ,
107
+ "public partial class foo : fooBase {"
108
+ }
109
+ ) ]
110
+ [ InlineData (
111
+ "PreprocessTemplateWithExtension" ,
112
+ "foo.g.cs" ,
113
+ new string [ ] {
114
+ "namespace PreprocessTemplateWithExtension {" ,
115
+ "public partial class foo : fooBase {"
116
+ }
117
+ ) ]
118
+ public void PreprocessLegacy ( string projectName , string expectedFilePath , string [ ] expectedContents )
61
119
{
62
120
using var ctx = new MSBuildTestContext ( ) ;
63
- var project = ctx . LoadTestProject ( "PreprocessTemplate" )
121
+ var project = ctx . LoadTestProject ( projectName )
64
122
. WithProperty ( "UseLegacyT4Preprocessing" , "true" ) ;
65
123
66
124
var instance = project . Build ( "TransformTemplates" ) ;
67
125
68
- var generatedFilePath = project . DirectoryPath [ "foo.cs" ] . AssertTextStartsWith ( "//--------" ) ;
126
+ var generatedFilePath = project . DirectoryPath [ expectedFilePath ]
127
+ . AssertContainsText
128
+ (
129
+ StringComparison . Ordinal ,
130
+ expectedContents
131
+ ) ;
69
132
70
133
instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
71
134
instance . AssertNoItems ( "GeneratedTemplates" ) ;
72
135
}
73
136
74
- [ Fact ]
75
- public void PreprocessOnBuild ( )
137
+ [ Theory ]
138
+ [ InlineData (
139
+ "PreprocessTemplate" ,
140
+ "TextTransform/foo.cs" ,
141
+ "PreprocessTemplate.foo"
142
+ ) ]
143
+ [ InlineData (
144
+ "PreprocessTemplateFromRelativePath" ,
145
+ "TextTransform/Nested/Template/Folder/foo.cs" ,
146
+ "PreprocessTemplateFromRelativePath.Nested.Template.Folder.foo"
147
+ ) ]
148
+ [ InlineData (
149
+ "PreprocessTemplateWithExtension" ,
150
+ "TextTransform/foo.g.cs" ,
151
+ "PreprocessTemplateWithExtension.foo"
152
+ ) ]
153
+ public void PreprocessOnBuild ( string projectName , string expectedFilePath , string expectedType )
76
154
{
77
155
using var ctx = new MSBuildTestContext ( ) ;
78
- var project = ctx . LoadTestProject ( "PreprocessTemplate" ) ;
156
+ var project = ctx . LoadTestProject ( projectName ) ;
79
157
80
158
project . Restore ( ) ;
81
159
82
160
var instance = project . Build ( "Build" ) ;
83
161
var objDir = project . DirectoryPath [ "obj" , "Debug" , "netstandard2.0" ] ;
84
162
85
- var generatedFilePath = instance . GetIntermediateDirFile ( "TextTransform" , "foo.cs" )
163
+ var generatedFilePath = instance . GetIntermediateDirFile ( expectedFilePath )
86
164
. AssertTextStartsWith ( "//--------" ) ;
87
165
88
166
instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
89
167
instance . AssertNoItems ( "GeneratedTemplates" ) ;
90
168
91
169
instance . GetTargetPath ( )
92
- . AssertFileName ( "PreprocessTemplate .dll")
93
- . AssertAssemblyContainsType ( "PreprocessTemplate.foo" ) ;
170
+ . AssertFileName ( $ " { projectName } .dll")
171
+ . AssertAssemblyContainsType ( expectedType ) ;
94
172
}
95
173
96
- [ Fact ]
97
- public void PreprocessOnDesignTimeBuild ( )
174
+ [ Theory ]
175
+ [ InlineData (
176
+ "PreprocessTemplate" ,
177
+ "TextTransform/foo.cs"
178
+ ) ]
179
+ [ InlineData (
180
+ "PreprocessTemplateFromRelativePath" ,
181
+ "TextTransform/Nested/Template/Folder/foo.cs"
182
+ ) ]
183
+ [ InlineData (
184
+ "PreprocessTemplateWithExtension" ,
185
+ "TextTransform/foo.g.cs"
186
+ ) ]
187
+ public void PreprocessOnDesignTimeBuild ( string projectName , string expectedFilePath )
98
188
{
99
189
using var ctx = new MSBuildTestContext ( ) ;
100
- var project = ctx . LoadTestProject ( "PreprocessTemplate" )
190
+ var project = ctx . LoadTestProject ( projectName )
101
191
. WithProperty ( "DesignTimeBuild" , "true" )
102
192
. WithProperty ( "SkipCompilerExecution" , "true" ) ;
103
193
104
194
project . Restore ( ) ;
105
195
106
196
var instance = project . Build ( "CoreCompile" ) ;
107
197
108
- var generatedFilePath = instance . GetIntermediateDirFile ( "TextTransform" , "foo.cs" )
198
+ var generatedFilePath = instance . GetIntermediateDirFile ( expectedFilePath )
109
199
. AssertTextStartsWith ( "//--------" ) ;
110
200
111
201
instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
112
202
instance . AssertNoItems ( "GeneratedTemplates" ) ;
113
203
}
114
204
205
+ [ Fact ]
206
+ public void PreprocessLegacyMetadata ( )
207
+ {
208
+ // Arrange
209
+ using var ctx = new MSBuildTestContext ( ) ;
210
+ var project = ctx . LoadTestProject ( "PreprocessTemplateMetadata" )
211
+ . WithProperty ( "UseLegacyT4Preprocessing" , "true" ) ;
212
+
213
+ var outputDirectory = project . DirectoryPath [ "Demo/Output/OutputDirectory.cs" ] ;
214
+ var outputFileName = project . DirectoryPath [ "Demo/OutputFileNameTest.cs" ] ;
215
+ var outputFileNameAndOutputDirectory = project . DirectoryPath [ "Demo/Output/OutputDirectoryAndFileNameTest.g.cs" ] ;
216
+
217
+ // Act
218
+ var instance = project . Build ( "TransformTemplates" ) ;
219
+
220
+ // Assert
221
+ Assert . Multiple ( ( ) => {
222
+ outputDirectory . AssertContainsText
223
+ (
224
+ StringComparison . Ordinal ,
225
+ "namespace PreprocessTemplateMetadata.Demo.Output {" ,
226
+ "partial class OutputDirectory"
227
+ ) ;
228
+
229
+ outputFileName . AssertContainsText
230
+ (
231
+ StringComparison . Ordinal ,
232
+ "namespace PreprocessTemplateMetadata.Demo {" ,
233
+ "partial class OutputFileNameTest"
234
+ ) ;
235
+
236
+ outputFileNameAndOutputDirectory . AssertContainsText
237
+ (
238
+ StringComparison . Ordinal ,
239
+ "namespace PreprocessTemplateMetadata.Demo.Output {" ,
240
+ "partial class OutputDirectoryAndFileNameTest"
241
+ ) ;
242
+ } ) ;
243
+
244
+ instance . AssertNoItems ( "GeneratedTemplates" ) ;
245
+ }
246
+
115
247
[ Fact ]
116
248
public void IncrementalTransform ( )
117
249
{
@@ -120,13 +252,13 @@ public void IncrementalTransform ()
120
252
121
253
project . Restore ( ) ;
122
254
123
- var fooGenerated = project . DirectoryPath [ "foo.txt" ] ;
124
- var fooTemplate = project . DirectoryPath [ "foo.tt" ] ;
125
- var barGenerated = project . DirectoryPath [ "bar.txt" ] ;
126
- var barTemplate = project . DirectoryPath [ "bar.tt" ] ;
127
- var includeFile = project . DirectoryPath [ "helper.ttinclude" ] ;
255
+ var fooGenerated = project . DirectoryPath [ "foo.txt" ] ;
256
+ var fooTemplate = project . DirectoryPath [ "foo.tt" ] ;
257
+ var barGenerated = project . DirectoryPath [ "bar.txt" ] ;
258
+ var barTemplate = project . DirectoryPath [ "bar.tt" ] ;
259
+ var includeFile = project . DirectoryPath [ "helper.ttinclude" ] ;
128
260
129
- void ExecuteAndValidate ( )
261
+ void ExecuteAndValidate ( )
130
262
{
131
263
var instance = project . Build ( "TransformTemplates" ) ;
132
264
0 commit comments