//
// Properties.cs
//
// Authors:
// Marek Sieradzki (marek.sieradzki@gmail.com)
// Marek Safar (marek.safar@gmail.com)
//
// (C) 2006 Marek Sieradzki
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Xml;
using Microsoft.Build.BuildEngine;
using NUnit.Framework;
namespace MonoTests.Microsoft.Build.BuildEngine.Various {
[TestFixture]
public class Properties {
Project proj;
[SetUp]
public void Setup ()
{
Engine engine = new Engine (Consts.BinPath);
proj = engine.CreateNewProject ();
}
[Test]
public void PropertyReference ()
{
string documentString = @"
debug
$(Config)-$(Config)
$(Config) $(Config)
$(Config-$(Config)
";
proj.LoadXml (documentString);
Assert.AreEqual (1, proj.PropertyGroups.Count, "A1");
Assert.AreEqual ("debug", proj.GetEvaluatedProperty ("Config"), "A2");
Assert.AreEqual ("debug-debug", proj.GetEvaluatedProperty ("ExpProp"), "A3");
Assert.AreEqual (" debug debug ", proj.GetEvaluatedProperty ("ExpProp2"), "A4");
Assert.AreEqual ("$(Config-$(Config)", proj.GetEvaluatedProperty ("InvProp1"), "A5");
}
[Test]
[Category ("NotDotNet")]
public void PropertyReference2 ()
{
string documentString = @"
A
B
";
proj.LoadXml (documentString);
proj.Build ("Main");
Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("Out").Count, "A1");
Assert.AreEqual ("AB", proj.GetEvaluatedItemsByName ("Out") [0].Include, "A2");
}
[Test]
public void StringInstanceProperties ()
{
string documentString = @"
debug
null
$(Config.Substring(0,3))
$(Config.Length )
$(Config.StartsWith ('DE', System.StringComparison.OrdinalIgnoreCase))
$(NullValue.StartsWith ('Te', StringComparison.OrdinalIgnoreCase))
$(TargetValue.Trim('\\'))
";
proj.LoadXml (documentString);
Assert.AreEqual ("deb ", proj.GetEvaluatedProperty ("Prop1"), "#1");
Assert.AreEqual ("5", proj.GetEvaluatedProperty ("Prop2"), "#2");
Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop3"), "#3");
Assert.AreEqual ("False", proj.GetEvaluatedProperty ("Prop4"), "#4");
Assert.AreEqual ("", proj.GetEvaluatedProperty ("Prop5"), "#5");
}
[Test]
public void AllowedFrameworkMembers ()
{
string documentString = @"
$([System.Byte]::MaxValue)
$([System.math]::Abs (-4.2) )
$([System.DateTime]::Today )
$([System.Char]::GetNumericValue('3'))
$([System.String]::Compare (Null, nUll))
$([System.Environment]::GetLogicalDrives ( ))
$([System.String]::Concat (`,`, `n`, `,`))
";
proj.LoadXml (documentString);
Assert.AreEqual ("255", proj.GetEvaluatedProperty ("Prop1"), "#1");
Assert.AreEqual ("4.2", proj.GetEvaluatedProperty ("Prop2"), "#2");
Assert.AreEqual (DateTime.Today.ToString (), proj.GetEvaluatedProperty ("Prop3"), "#3");
Assert.AreEqual ("3", proj.GetEvaluatedProperty ("Prop4"), "#4");
Assert.AreEqual ("0", proj.GetEvaluatedProperty ("Prop5"), "#5");
Assert.AreEqual (string.Join (";", Environment.GetLogicalDrives ()), proj.GetEvaluatedProperty ("Prop6"), "#6");
Assert.AreEqual (",n,", proj.GetEvaluatedProperty ("Prop7"), "#7");
}
[Test]
public void InstanceMethodOnStaticProperty ()
{
string documentString = @"
$([System.DateTime]::Now.ToString(""yyyy.MM.dd""))
";
proj.LoadXml (documentString);
Assert.AreEqual (DateTime.Now.ToString ("yyyy.MM.dd"), proj.GetEvaluatedProperty ("Prop1"), "#1");
}
[Test]
public void InstanceMemberOnStaticProperty ()
{
string documentString = @"
$([System.DateTime]::Now.Year)
";
proj.LoadXml (documentString);
Assert.AreEqual (DateTime.Now.Year.ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
}
[Test]
public void InstanceMembersOnStaticMethod ()
{
string documentString = @"
$([System.String]::Concat('a', 'bb', 'c').Length.GetHashCode ())
";
proj.LoadXml (documentString);
Assert.AreEqual (4.GetHashCode ().ToString (), proj.GetEvaluatedProperty ("Prop1"), "#1");
}
[Test]
public void MSBuildPropertyFunctions ()
{
string documentString = @"
0.6
6
$([MSBuild]::Add($(NumberOne), $(NumberTwo)))
";
proj.LoadXml (documentString);
Assert.AreEqual ("6.6", proj.GetEvaluatedProperty ("Prop1"), "#1");
}
[Test]
public void Constructor ()
{
string documentString = @"
0.6
6
$([System.String]::new('value').EndsWith ('ue'))
";
proj.LoadXml (documentString);
Assert.AreEqual ("True", proj.GetEvaluatedProperty ("Prop1"), "#1");
}
}
}