Merge pull request #925 from ermshiperete/novell-bug-602934
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / various / Properties.cs
1 //
2 // Properties.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.Xml;
30 using Microsoft.Build.BuildEngine;
31 using NUnit.Framework;
32
33 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
34         [TestFixture]
35         public class Properties {
36
37                 Project proj;
38
39                 [SetUp]
40                 public void Setup ()
41                 {
42                         Engine engine = new Engine (Consts.BinPath);
43                         proj = engine.CreateNewProject ();
44                 }
45
46                 [Test]
47                 public void PropertyReference ()
48                 {
49                         string documentString = @"
50                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
51                                         <PropertyGroup>
52                                                 <Config>debug</Config>
53                                                 <ExpProp>$(Config)-$(Config)</ExpProp>
54                                                 <ExpProp2> $(Config) $(Config) </ExpProp2>
55                                                 <InvProp1>$(Config-$(Config)</InvProp1>
56                                         </PropertyGroup>
57                                 </Project>
58                         ";
59
60                         proj.LoadXml (documentString);
61                         Assert.AreEqual (1, proj.PropertyGroups.Count, "A1");
62                         Assert.AreEqual ("debug", proj.GetEvaluatedProperty ("Config"), "A2");
63                         Assert.AreEqual ("debug-debug", proj.GetEvaluatedProperty ("ExpProp"), "A3");
64                         Assert.AreEqual (" debug debug ", proj.GetEvaluatedProperty ("ExpProp2"), "A4");        
65                         Assert.AreEqual ("$(Config-$(Config)", proj.GetEvaluatedProperty ("InvProp1"), "A5");
66                 }
67
68                 [Test]
69                 [Category ("NotDotNet")]
70                 public void PropertyReference2 ()
71                 {
72                         string documentString = @"
73                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
74                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
75                                         <PropertyGroup>
76                                                 <A>A</A>
77                                                 <B>B</B>
78                                         </PropertyGroup>
79
80                                         <Target Name='Main' >
81                                                 <StringTestTask Array='$(A)$(B)'>
82                                                         <Output TaskParameter='Array' ItemName='Out' />
83                                                 </StringTestTask>
84                                         </Target>
85                                 </Project>
86                         ";
87
88                         proj.LoadXml (documentString);
89                         proj.Build ("Main");
90                         Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("Out").Count, "A1");
91                         Assert.AreEqual ("AB", proj.GetEvaluatedItemsByName ("Out") [0].Include, "A2");
92                 }
93
94                 [Test]
95                 public void StringInstanceProperties ()
96                 {
97                         string documentString = @"
98                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
99                                                 <PropertyGroup>
100                                                         <Config>debug</Config>
101                                                         <NullValue>null</NullValue>
102                                                         <Prop1>$(Config.Substring(0,3)) </Prop1>
103                                                         <Prop2>$(Config.Length )</Prop2>
104                                                         <Prop3>$(Config.StartsWith ('DE', System.StringComparison.OrdinalIgnoreCase))</Prop3>
105                                                         <Prop4>$(NullValue.StartsWith ('Te', StringComparison.OrdinalIgnoreCase))</Prop4>
106                                                 </PropertyGroup>
107                                         </Project>
108                                 ";
109
110                         proj.LoadXml (documentString);
111                         Assert.AreEqual ("deb ", proj.GetEvaluatedProperty ("Prop1"), "#1");
112                         Assert.AreEqual ("5", proj.GetEvaluatedProperty ("Prop2"), "#2");
113                         Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop3"), "#3");
114                         Assert.AreEqual ("False", proj.GetEvaluatedProperty ("Prop4"), "#4");
115                 }
116
117                 [Test]
118                 public void AllowedFrameworkMembers ()
119                 {
120                         string documentString = @"
121                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
122                                                 <PropertyGroup>
123                                                         <Prop1>$([System.Byte]::MaxValue)</Prop1>
124                                                         <Prop2>$([System.math]::Abs (-4.2) )</Prop2>
125                                                         <Prop3>$([System.DateTime]::Today )</Prop3>
126                                                         <Prop4>$([System.Char]::GetNumericValue('3'))</Prop4>
127                                                         <Prop5>$([System.String]::Compare (Null, nUll))</Prop5>
128                                                         <Prop6>$([System.Environment]::GetLogicalDrives ( ))</Prop6>
129                                                         <Prop7>$([System.String]::Concat (`,`, `n`, `,`))</Prop7>
130                                                 </PropertyGroup>
131                                         </Project>
132                                 ";
133
134                         proj.LoadXml (documentString);
135                         Assert.AreEqual ("255", proj.GetEvaluatedProperty ("Prop1"), "#1");
136                         Assert.AreEqual ("4.2", proj.GetEvaluatedProperty ("Prop2"), "#2");
137                         Assert.AreEqual (DateTime.Today.ToString (), proj.GetEvaluatedProperty ("Prop3"), "#3");
138                         Assert.AreEqual ("3", proj.GetEvaluatedProperty ("Prop4"), "#4");
139                         Assert.AreEqual ("0", proj.GetEvaluatedProperty ("Prop5"), "#5");
140                         Assert.AreEqual (string.Join (";", Environment.GetLogicalDrives ()), proj.GetEvaluatedProperty ("Prop6"), "#6");
141                         Assert.AreEqual (",n,", proj.GetEvaluatedProperty ("Prop7"), "#7");
142                 }
143
144                 [Test]
145                 public void InstanceMethodOnStaticProperty ()
146                 {
147                         string documentString = @"
148                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
149                                                 <PropertyGroup>
150                                                         <Prop1>$([System.DateTime]::Now.ToString(""yyyy.MM.dd""))</Prop1>
151                                                 </PropertyGroup>
152                                         </Project>
153                                 ";
154
155                         proj.LoadXml (documentString);
156                         Assert.AreEqual (DateTime.Now.ToString ("yyyy.MM.dd"), proj.GetEvaluatedProperty ("Prop1"), "#1");
157                 }
158
159                 [Test]
160                 public void MSBuildPropertyFunctions ()
161                 {
162                         string documentString = @"
163                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
164                                                 <PropertyGroup>
165                                                         <NumberOne>0.6</NumberOne>
166                                                         <NumberTwo>6</NumberTwo>
167                                                         <Prop1>$([MSBuild]::Add($(NumberOne), $(NumberTwo)))</Prop1>
168                                                 </PropertyGroup>
169                                         </Project>
170                                 ";
171
172                         proj.LoadXml (documentString);
173                         Assert.AreEqual ("6.6", proj.GetEvaluatedProperty ("Prop1"), "#1");
174                 }               
175         }
176 }