5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) 2006 Marek Sieradzki
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using Microsoft.Build.BuildEngine;
32 using NUnit.Framework;
34 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
36 public class Properties {
43 Engine engine = new Engine (Consts.BinPath);
44 proj = engine.CreateNewProject ();
48 public void PropertyReference ()
50 string documentString = @"
51 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
53 <Config>debug</Config>
54 <ExpProp>$(Config)-$(Config)</ExpProp>
55 <ExpProp2> $(Config) $(Config) </ExpProp2>
56 <InvProp1>$(Config-$(Config)</InvProp1>
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");
70 [Category ("NotDotNet")]
71 public void PropertyReference2 ()
73 string documentString = @"
74 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
75 <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
82 <StringTestTask Array='$(A)$(B)'>
83 <Output TaskParameter='Array' ItemName='Out' />
89 proj.LoadXml (documentString);
91 Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("Out").Count, "A1");
92 Assert.AreEqual ("AB", proj.GetEvaluatedItemsByName ("Out") [0].Include, "A2");
96 public void StringInstanceProperties ()
98 string documentString = @"
99 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
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>
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");
132 [SetCulture ("en-us")]
133 public void AllowedFrameworkMembers ()
135 string documentString = @"
136 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
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>
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");
160 public void InstanceMethodOnStaticProperty ()
162 string documentString = @"
163 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
165 <Prop1>$([System.DateTime]::Now.ToString(""yyyy.MM.dd""))</Prop1>
170 proj.LoadXml (documentString);
171 Assert.AreEqual (DateTime.Now.ToString ("yyyy.MM.dd"), proj.GetEvaluatedProperty ("Prop1"), "#1");
175 public void InstanceMemberOnStaticProperty ()
177 string documentString = @"
178 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
180 <Prop1>$([System.DateTime]::Now.Year)</Prop1>
185 proj.LoadXml (documentString);
186 Assert.AreEqual (DateTime.Now.Year.ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
190 public void InstanceMembersOnStaticMethod ()
192 string documentString = @"
193 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
195 <Prop1>$([System.String]::Concat('a', 'bb', 'c').Length.GetHashCode ())</Prop1>
201 proj.LoadXml (documentString);
202 Assert.AreEqual (4.GetHashCode ().ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
206 [SetCulture ("en-us")]
207 public void MSBuildPropertyFunctions ()
209 string documentString = @"
210 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
212 <NumberOne>0.6</NumberOne>
213 <NumberTwo>6</NumberTwo>
214 <Prop1>$([MSBuild]::Add($(NumberOne), $(NumberTwo)))</Prop1>
219 proj.LoadXml (documentString);
220 Assert.AreEqual ("6.6", proj.GetEvaluatedProperty ("Prop1"), "#1");
224 public void Constructor ()
226 string documentString = @"
227 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
229 <NumberOne>0.6</NumberOne>
230 <NumberTwo>6</NumberTwo>
231 <Prop1>$([System.String]::new('value').EndsWith ('ue'))</Prop1>
236 proj.LoadXml (documentString);
237 Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop1"), "#1");