Merge pull request #3715 from kumpera/fix-44707
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / various / Properties.cs
1 //
2 // Properties.cs
3 //
4 // Authors:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // (C) 2006 Marek Sieradzki
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 using System;
30 using System.Xml;
31 using Microsoft.Build.BuildEngine;
32 using NUnit.Framework;
33
34 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
35         [TestFixture]
36         public class Properties {
37
38                 Project proj;
39
40                 [SetUp]
41                 public void Setup ()
42                 {
43                         Engine engine = new Engine (Consts.BinPath);
44                         proj = engine.CreateNewProject ();
45                 }
46
47                 [Test]
48                 public void PropertyReference ()
49                 {
50                         string documentString = @"
51                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
52                                         <PropertyGroup>
53                                                 <Config>debug</Config>
54                                                 <ExpProp>$(Config)-$(Config)</ExpProp>
55                                                 <ExpProp2> $(Config) $(Config) </ExpProp2>
56                                                 <InvProp1>$(Config-$(Config)</InvProp1>
57                                         </PropertyGroup>
58                                 </Project>
59                         ";
60
61                         proj.LoadXml (documentString);
62                         Assert.AreEqual (1, proj.PropertyGroups.Count, "A1");
63                         Assert.AreEqual ("debug", proj.GetEvaluatedProperty ("Config"), "A2");
64                         Assert.AreEqual ("debug-debug", proj.GetEvaluatedProperty ("ExpProp"), "A3");
65                         Assert.AreEqual (" debug debug ", proj.GetEvaluatedProperty ("ExpProp2"), "A4");        
66                         Assert.AreEqual ("$(Config-$(Config)", proj.GetEvaluatedProperty ("InvProp1"), "A5");
67                 }
68
69                 [Test]
70                 [Category ("NotDotNet")]
71                 public void PropertyReference2 ()
72                 {
73                         string documentString = @"
74                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
75                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
76                                         <PropertyGroup>
77                                                 <A>A</A>
78                                                 <B>B</B>
79                                         </PropertyGroup>
80
81                                         <Target Name='Main' >
82                                                 <StringTestTask Array='$(A)$(B)'>
83                                                         <Output TaskParameter='Array' ItemName='Out' />
84                                                 </StringTestTask>
85                                         </Target>
86                                 </Project>
87                         ";
88
89                         proj.LoadXml (documentString);
90                         proj.Build ("Main");
91                         Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("Out").Count, "A1");
92                         Assert.AreEqual ("AB", proj.GetEvaluatedItemsByName ("Out") [0].Include, "A2");
93                 }
94
95                 [Test]
96                 public void StringInstanceProperties ()
97                 {
98                         string documentString = @"
99                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
100                                                 <PropertyGroup>
101                                                         <Config>debug</Config>
102                                                         <NullValue>null</NullValue>
103                                                         <TargetValue>  </TargetValue>
104                                                         <StringWithQuotes>abc""def</StringWithQuotes>
105                                                         <Prop1>$(Config.Substring(0,3)) </Prop1>
106                                                         <Prop2>$(Config.Length )</Prop2>
107                                                         <Prop3>$(Config.StartsWith ('DE', System.StringComparison.OrdinalIgnoreCase))</Prop3>
108                                                         <Prop4>$(NullValue.StartsWith ('Te', StringComparison.OrdinalIgnoreCase))</Prop4>
109                                                         <Prop5>$(TargetValue.Trim('\\'))</Prop5>
110                                                         <Prop6>$(StringWithQuotes.Replace('""', ""'""))</Prop6>
111                                                         <Prop7>$(StringWithQuotes.Replace('""', ''))</Prop7>
112                                                         <Prop8>$(StringWithQuotes.Replace('""', """"))</Prop8>
113                                                         <Prop9>$(StringWithQuotes.Replace('""', ``))</Prop9>
114                                                         <Prop9>$(StringWithQuotes.Replace(`c""d`, `2""'3`))</Prop9>
115                                                 </PropertyGroup>
116                                         </Project>
117                                 ";
118
119                         proj.LoadXml (documentString);
120                         Assert.AreEqual ("deb ", proj.GetEvaluatedProperty ("Prop1"), "#1");
121                         Assert.AreEqual ("5", proj.GetEvaluatedProperty ("Prop2"), "#2");
122                         Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop3"), "#3");
123                         Assert.AreEqual ("False", proj.GetEvaluatedProperty ("Prop4"), "#4");
124                         Assert.AreEqual ("", proj.GetEvaluatedProperty ("Prop5"), "#5");
125                         Assert.AreEqual ("abc'def", proj.GetEvaluatedProperty ("Prop6"), "#6");
126                         Assert.AreEqual ("abcdef", proj.GetEvaluatedProperty ("Prop7"), "#7");
127                         Assert.AreEqual ("abcdef", proj.GetEvaluatedProperty ("Prop8"), "#8");
128                         Assert.AreEqual ("ab2\"'3ef", proj.GetEvaluatedProperty ("Prop9"), "#9");
129                 }
130
131                 [Test]
132                 [SetCulture ("en-us")]
133                 public void AllowedFrameworkMembers ()
134                 {
135                         string documentString = @"
136                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
137                                                 <PropertyGroup>
138                                                         <Prop1>$([System.Byte]::MaxValue)</Prop1>
139                                                         <Prop2>$([System.math]::Abs (-4.2) )</Prop2>
140                                                         <Prop3>$([System.DateTime]::Today )</Prop3>
141                                                         <Prop4>$([System.Char]::GetNumericValue('3'))</Prop4>
142                                                         <Prop5>$([System.String]::Compare (Null, nUll))</Prop5>
143                                                         <Prop6>$([System.Environment]::GetLogicalDrives ( ))</Prop6>
144                                                         <Prop7>$([System.String]::Concat (`,`, `n`, `,`))</Prop7>
145                                                 </PropertyGroup>
146                                         </Project>
147                                 ";
148
149                         proj.LoadXml (documentString);
150                         Assert.AreEqual ("255", proj.GetEvaluatedProperty ("Prop1"), "#1");
151                         Assert.AreEqual ("4.2", proj.GetEvaluatedProperty ("Prop2"), "#2");
152                         Assert.AreEqual (DateTime.Today.ToString (), proj.GetEvaluatedProperty ("Prop3"), "#3");
153                         Assert.AreEqual ("3", proj.GetEvaluatedProperty ("Prop4"), "#4");
154                         Assert.AreEqual ("0", proj.GetEvaluatedProperty ("Prop5"), "#5");
155                         Assert.AreEqual (string.Join (";", Environment.GetLogicalDrives ()), proj.GetEvaluatedProperty ("Prop6"), "#6");
156                         Assert.AreEqual (",n,", proj.GetEvaluatedProperty ("Prop7"), "#7");
157                 }
158
159                 [Test]
160                 public void InstanceMethodOnStaticProperty ()
161                 {
162                         string documentString = @"
163                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
164                                                 <PropertyGroup>
165                                                         <Prop1>$([System.DateTime]::Now.ToString(""yyyy.MM.dd""))</Prop1>
166                                                 </PropertyGroup>
167                                         </Project>
168                                 ";
169
170                         proj.LoadXml (documentString);
171                         Assert.AreEqual (DateTime.Now.ToString ("yyyy.MM.dd"), proj.GetEvaluatedProperty ("Prop1"), "#1");
172                 }
173
174                 [Test]
175                 public void InstanceMemberOnStaticProperty ()
176                 {
177                         string documentString = @"
178                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
179                                                 <PropertyGroup>
180                                                         <Prop1>$([System.DateTime]::Now.Year)</Prop1>
181                                                 </PropertyGroup>
182                                         </Project>
183                                 ";
184
185                         proj.LoadXml (documentString);
186                         Assert.AreEqual (DateTime.Now.Year.ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
187                 }
188
189                 [Test]
190                 public void InstanceMembersOnStaticMethod ()
191                 {
192                         string documentString = @"
193                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
194                                                 <PropertyGroup>
195                                                         <Prop1>$([System.String]::Concat('a', 'bb', 'c').Length.GetHashCode ())</Prop1>
196
197                                                 </PropertyGroup>
198                                         </Project>
199                                 ";
200
201                         proj.LoadXml (documentString);
202                         Assert.AreEqual (4.GetHashCode ().ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
203                 }
204
205                 [Test]
206                 [SetCulture ("en-us")]
207                 public void MSBuildPropertyFunctions ()
208                 {
209                         string documentString = @"
210                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
211                                                 <PropertyGroup>
212                                                         <NumberOne>0.6</NumberOne>
213                                                         <NumberTwo>6</NumberTwo>
214                                                         <Prop1>$([MSBuild]::Add($(NumberOne), $(NumberTwo)))</Prop1>
215                                                 </PropertyGroup>
216                                         </Project>
217                                 ";
218
219                         proj.LoadXml (documentString);
220                         Assert.AreEqual ("6.6", proj.GetEvaluatedProperty ("Prop1"), "#1");
221                 }
222
223                 [Test]
224                 public void Constructor ()
225                 {
226                         string documentString = @"
227                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
228                                                 <PropertyGroup>
229                                                         <NumberOne>0.6</NumberOne>
230                                                         <NumberTwo>6</NumberTwo>
231                                                         <Prop1>$([System.String]::new('value').EndsWith ('ue'))</Prop1>
232                                                 </PropertyGroup>
233                                         </Project>
234                                 ";
235
236                         proj.LoadXml (documentString);
237                         Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop1"), "#1");
238                 }
239         }
240 }