54f1c8a94cc2c935c49a573869ba9799aa6568df
[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                 public void AllowedFrameworkMembers ()
133                 {
134                         string documentString = @"
135                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
136                                                 <PropertyGroup>
137                                                         <Prop1>$([System.Byte]::MaxValue)</Prop1>
138                                                         <Prop2>$([System.math]::Abs (-4.2) )</Prop2>
139                                                         <Prop3>$([System.DateTime]::Today )</Prop3>
140                                                         <Prop4>$([System.Char]::GetNumericValue('3'))</Prop4>
141                                                         <Prop5>$([System.String]::Compare (Null, nUll))</Prop5>
142                                                         <Prop6>$([System.Environment]::GetLogicalDrives ( ))</Prop6>
143                                                         <Prop7>$([System.String]::Concat (`,`, `n`, `,`))</Prop7>
144                                                 </PropertyGroup>
145                                         </Project>
146                                 ";
147
148                         proj.LoadXml (documentString);
149                         Assert.AreEqual ("255", proj.GetEvaluatedProperty ("Prop1"), "#1");
150                         Assert.AreEqual ("4.2", proj.GetEvaluatedProperty ("Prop2"), "#2");
151                         Assert.AreEqual (DateTime.Today.ToString (), proj.GetEvaluatedProperty ("Prop3"), "#3");
152                         Assert.AreEqual ("3", proj.GetEvaluatedProperty ("Prop4"), "#4");
153                         Assert.AreEqual ("0", proj.GetEvaluatedProperty ("Prop5"), "#5");
154                         Assert.AreEqual (string.Join (";", Environment.GetLogicalDrives ()), proj.GetEvaluatedProperty ("Prop6"), "#6");
155                         Assert.AreEqual (",n,", proj.GetEvaluatedProperty ("Prop7"), "#7");
156                 }
157
158                 [Test]
159                 public void InstanceMethodOnStaticProperty ()
160                 {
161                         string documentString = @"
162                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
163                                                 <PropertyGroup>
164                                                         <Prop1>$([System.DateTime]::Now.ToString(""yyyy.MM.dd""))</Prop1>
165                                                 </PropertyGroup>
166                                         </Project>
167                                 ";
168
169                         proj.LoadXml (documentString);
170                         Assert.AreEqual (DateTime.Now.ToString ("yyyy.MM.dd"), proj.GetEvaluatedProperty ("Prop1"), "#1");
171                 }
172
173                 [Test]
174                 public void InstanceMemberOnStaticProperty ()
175                 {
176                         string documentString = @"
177                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
178                                                 <PropertyGroup>
179                                                         <Prop1>$([System.DateTime]::Now.Year)</Prop1>
180                                                 </PropertyGroup>
181                                         </Project>
182                                 ";
183
184                         proj.LoadXml (documentString);
185                         Assert.AreEqual (DateTime.Now.Year.ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
186                 }
187
188                 [Test]
189                 public void InstanceMembersOnStaticMethod ()
190                 {
191                         string documentString = @"
192                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
193                                                 <PropertyGroup>
194                                                         <Prop1>$([System.String]::Concat('a', 'bb', 'c').Length.GetHashCode ())</Prop1>
195
196                                                 </PropertyGroup>
197                                         </Project>
198                                 ";
199
200                         proj.LoadXml (documentString);
201                         Assert.AreEqual (4.GetHashCode ().ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
202                 }
203
204                 [Test]
205                 public void MSBuildPropertyFunctions ()
206                 {
207                         string documentString = @"
208                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
209                                                 <PropertyGroup>
210                                                         <NumberOne>0.6</NumberOne>
211                                                         <NumberTwo>6</NumberTwo>
212                                                         <Prop1>$([MSBuild]::Add($(NumberOne), $(NumberTwo)))</Prop1>
213                                                 </PropertyGroup>
214                                         </Project>
215                                 ";
216
217                         proj.LoadXml (documentString);
218                         Assert.AreEqual ("6.6", proj.GetEvaluatedProperty ("Prop1"), "#1");
219                 }
220
221                 [Test]
222                 public void Constructor ()
223                 {
224                         string documentString = @"
225                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
226                                                 <PropertyGroup>
227                                                         <NumberOne>0.6</NumberOne>
228                                                         <NumberTwo>6</NumberTwo>
229                                                         <Prop1>$([System.String]::new('value').EndsWith ('ue'))</Prop1>
230                                                 </PropertyGroup>
231                                         </Project>
232                                 ";
233
234                         proj.LoadXml (documentString);
235                         Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop1"), "#1");
236                 }
237         }
238 }