[xbuild] Remove property if Output TaskParameter is null.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / CreatePropertyTest.cs
1 //
2 // CreatePropertyTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using System.IO;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
36
37 namespace MonoTests.Microsoft.Build.Tasks {
38
39         [TestFixture]
40         public class CreatePropertyTest {
41                 [Test]
42                 public void TestAssignment ()
43                 {
44                         CreateProperty cp = new CreateProperty ();
45
46                         cp.Value = new string [1] { "1" };
47
48                         Assert.AreEqual ("1", cp.Value [0], "A1");
49                         Assert.AreEqual ("1", cp.ValueSetByTask [0], "A2");
50                 }
51
52                 [Test]
53                 public void TestExecution1 ()
54                 {
55                         Engine engine;
56                         Project project;
57
58                         string documentString = @"
59                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
60                                         <PropertyGroup>
61                                                 <A>1</A>
62                                                 <B>2</B>
63                                         </PropertyGroup>
64                                         <Target Name='1'>
65                                                 <CreateProperty Value='$(A)$(B)' >
66                                                         <Output
67                                                                 TaskParameter='Value'
68                                                                 PropertyName='Value'
69                                                         />
70                                                         <Output
71                                                                 TaskParameter='ValueSetByTask'
72                                                                 PropertyName='ValueSetByTask'
73                                                         />
74                                                 </CreateProperty>
75                                         </Target>
76                                 </Project>
77                         ";
78
79                         engine = new Engine (Consts.BinPath);
80                         project = engine.CreateNewProject ();
81                         project.LoadXml (documentString);
82                         Assert.IsTrue (project.Build ("1"), "A1");
83
84                         Assert.AreEqual ("12", project.EvaluatedProperties ["Value"].Value, "A2");
85                         Assert.AreEqual ("12", project.EvaluatedProperties ["Value"].FinalValue, "A3");
86                         Assert.AreEqual ("12", project.EvaluatedProperties ["ValueSetByTask"].Value, "A4");
87                         Assert.AreEqual ("12", project.EvaluatedProperties ["ValueSetByTask"].FinalValue, "A5");
88                 }
89
90                 [Test]
91                 public void TestExecution2 () {
92                         Engine engine;
93                         Project project;
94
95                         string documentString = @"
96                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
97                                 <ItemGroup>
98                                         <Second Include=""Abc""/>
99                                         <IG Include=""@(Second)""/></ItemGroup>
100                                         <PropertyGroup>
101                                                 <C>@(IG)</C>
102                                         </PropertyGroup>
103                                         <Target Name='1'>
104                                                 <CreateProperty Value='$(C)' >
105                                                         <Output
106                                                                 TaskParameter='Value'
107                                                                 PropertyName='Value'
108                                                         />
109                                                         <Output
110                                                                 TaskParameter='ValueSetByTask'
111                                                                 PropertyName='ValueSetByTask'
112                                                         />
113                                                 </CreateProperty>
114                                         </Target>
115                                 </Project>
116                         ";
117
118                         engine = new Engine (Consts.BinPath);
119                         project = engine.CreateNewProject ();
120                         project.LoadXml (documentString);
121                         Assert.IsTrue (project.Build ("1"), "A1");
122
123                         Assert.AreEqual ("Abc", project.EvaluatedProperties["Value"].Value, "A2");
124                         Assert.AreEqual ("Abc", project.EvaluatedProperties["Value"].FinalValue, "A3");
125                         Assert.AreEqual ("Abc", project.EvaluatedProperties["ValueSetByTask"].Value, "A4");
126                         Assert.AreEqual ("Abc", project.EvaluatedProperties["ValueSetByTask"].FinalValue, "A5");
127                         Assert.AreEqual ("@(IG)", project.EvaluatedProperties["C"].FinalValue, "A6");
128                 }
129
130                 [Test]
131                 public void TestEmptyPropertyValue ()
132                 {
133                         Engine engine;
134                         Project project;
135
136                         string documentString = @"
137                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
138                                         <PropertyGroup>
139                                                 <A>1</A>
140                                         </PropertyGroup>
141                                         <Target Name='1'>
142                                                 <Message Text='Before: $(A)'/>
143                                                 <CreateProperty Value=''>
144                                                         <Output
145                                                                 TaskParameter='Value'
146                                                                 PropertyName='A'
147                                                         />
148                                                 </CreateProperty>
149                                                 <Message Text='After: $(A)'/>
150                                         </Target>
151                                 </Project>
152                         ";
153
154                         engine = new Engine (Consts.BinPath);
155
156                         TestMessageLogger testLogger = new TestMessageLogger ();
157                         engine.RegisterLogger (testLogger);
158
159                         project = engine.CreateNewProject ();
160                         project.LoadXml (documentString);
161                         if (!project.Build ("1")) {
162                                 testLogger.DumpMessages ();
163                                 Assert.Fail ("Build failed");
164                         }
165
166                         testLogger.CheckLoggedMessageHead ("Before: 1", "A1");
167                         testLogger.CheckLoggedMessageHead ("After: ", "A2");
168                         Assert.AreEqual (0, testLogger.NormalMessageCount, "Unexpected messages found");
169                 }
170         }
171 }