11// Copyright (c) Microsoft. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ using System ;
45using System . Linq ;
56using Xunit ;
67
@@ -10,108 +11,239 @@ namespace Mono.TextTemplating.Tests
1011 [ CollectionDefinition ( nameof ( MSBuildExecutionTests ) , DisableParallelization = true ) ]
1112 public class MSBuildExecutionTests : IClassFixture < MSBuildFixture >
1213 {
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 )
1519 {
1620 using var ctx = new MSBuildTestContext ( ) ;
17- var project = ctx . LoadTestProject ( "TransformTemplates" ) ;
21+ var project = ctx . LoadTestProject ( projectName ) ;
1822
1923 var instance = project . Build ( "TransformTemplates" ) ;
2024
21- var generated = project . DirectoryPath [ "foo.txt" ] . AssertTextStartsWith ( "Hello 2019!" ) ;
25+ var generated = project . DirectoryPath [ expectedFilePath ] . AssertTextStartsWith ( expectedText ) ;
2226
2327 instance . AssertSingleItem ( "GeneratedTemplates" , withFullPath : generated ) ;
2428 instance . AssertNoItems ( "PreprocessedTemplates" ) ;
2529 }
2630
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 )
2936 {
3037 using var ctx = new MSBuildTestContext ( ) ;
31- var project = ctx . LoadTestProject ( "TransformTemplates" )
38+ var project = ctx . LoadTestProject ( projectName )
3239 . WithProperty ( "TransformOnBuild" , "true" ) ;
3340
3441 project . Restore ( ) ;
3542
3643 var instance = project . Build ( "Build" ) ;
3744
38- var generatedFilePath = project . DirectoryPath [ "foo.txt" ] . AssertTextStartsWith ( "Hello 2019!" ) ;
45+ var generatedFilePath = project . DirectoryPath [ expectedFilePath ] . AssertTextStartsWith ( expectedText ) ;
3946
4047 instance . AssertSingleItem ( "GeneratedTemplates" , withFullPath : generatedFilePath ) ;
4148 instance . AssertNoItems ( "PreprocessedTemplates" ) ;
4249 }
4350
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 )
4656 {
4757 using var ctx = new MSBuildTestContext ( ) ;
48- var project = ctx . LoadTestProject ( "TransformTemplates" ) ;
58+ var project = ctx . LoadTestProject ( projectName ) ;
4959
5060 project . Restore ( ) ;
5161
5262 var instance = project . Build ( "Build" ) ;
5363
54- project . DirectoryPath [ "foo.txt" ] . AssertFileExists ( false ) ;
64+ project . DirectoryPath [ expectedFilePath ] . AssertFileExists ( false ) ;
5565
5666 instance . AssertNoItems ( "GeneratedTemplates" , "PreprocessedTemplates" ) ;
5767 }
5868
5969 [ 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 )
61119 {
62120 using var ctx = new MSBuildTestContext ( ) ;
63- var project = ctx . LoadTestProject ( "PreprocessTemplate" )
121+ var project = ctx . LoadTestProject ( projectName )
64122 . WithProperty ( "UseLegacyT4Preprocessing" , "true" ) ;
65123
66124 var instance = project . Build ( "TransformTemplates" ) ;
67125
68- var generatedFilePath = project . DirectoryPath [ "foo.cs" ] . AssertTextStartsWith ( "//--------" ) ;
126+ var generatedFilePath = project . DirectoryPath [ expectedFilePath ]
127+ . AssertContainsText
128+ (
129+ StringComparison . Ordinal ,
130+ expectedContents
131+ ) ;
69132
70133 instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
71134 instance . AssertNoItems ( "GeneratedTemplates" ) ;
72135 }
73136
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 )
76154 {
77155 using var ctx = new MSBuildTestContext ( ) ;
78- var project = ctx . LoadTestProject ( "PreprocessTemplate" ) ;
156+ var project = ctx . LoadTestProject ( projectName ) ;
79157
80158 project . Restore ( ) ;
81159
82160 var instance = project . Build ( "Build" ) ;
83161 var objDir = project . DirectoryPath [ "obj" , "Debug" , "netstandard2.0" ] ;
84162
85- var generatedFilePath = instance . GetIntermediateDirFile ( "TextTransform" , "foo.cs" )
163+ var generatedFilePath = instance . GetIntermediateDirFile ( expectedFilePath )
86164 . AssertTextStartsWith ( "//--------" ) ;
87165
88166 instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
89167 instance . AssertNoItems ( "GeneratedTemplates" ) ;
90168
91169 instance . GetTargetPath ( )
92- . AssertFileName ( "PreprocessTemplate .dll")
93- . AssertAssemblyContainsType ( "PreprocessTemplate.foo" ) ;
170+ . AssertFileName ( $ " { projectName } .dll")
171+ . AssertAssemblyContainsType ( expectedType ) ;
94172 }
95173
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 )
98188 {
99189 using var ctx = new MSBuildTestContext ( ) ;
100- var project = ctx . LoadTestProject ( "PreprocessTemplate" )
190+ var project = ctx . LoadTestProject ( projectName )
101191 . WithProperty ( "DesignTimeBuild" , "true" )
102192 . WithProperty ( "SkipCompilerExecution" , "true" ) ;
103193
104194 project . Restore ( ) ;
105195
106196 var instance = project . Build ( "CoreCompile" ) ;
107197
108- var generatedFilePath = instance . GetIntermediateDirFile ( "TextTransform" , "foo.cs" )
198+ var generatedFilePath = instance . GetIntermediateDirFile ( expectedFilePath )
109199 . AssertTextStartsWith ( "//--------" ) ;
110200
111201 instance . AssertSingleItem ( "PreprocessedTemplates" , generatedFilePath ) ;
112202 instance . AssertNoItems ( "GeneratedTemplates" ) ;
113203 }
114204
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+
115247 [ Fact ]
116248 public void IncrementalTransform ( )
117249 {
@@ -120,13 +252,13 @@ public void IncrementalTransform ()
120252
121253 project . Restore ( ) ;
122254
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" ] ;
128260
129- void ExecuteAndValidate ( )
261+ void ExecuteAndValidate ( )
130262 {
131263 var instance = project . Build ( "TransformTemplates" ) ;
132264
0 commit comments