partal import implementation.
[mono.git] / mcs / class / Microsoft.Build / Test / Microsoft.Build.Evaluation / ResolvedImportTest.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Xml;
5 using Microsoft.Build.Construction;
6 using Microsoft.Build.Evaluation;
7 using NUnit.Framework;
8 using Microsoft.Build.Exceptions;
9 using Microsoft.Build.Framework;
10
11 namespace MonoTests.Microsoft.Build.Evaluation
12 {
13         [TestFixture]
14         public class ResolvedImportTest
15         {
16                 [Test]
17                 public void SimpleImportAndSemanticValues ()
18                 {
19                         string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
20   <Import Project='test_imported.proj' />
21 </Project>";
22                         string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
23   <PropertyGroup>
24     <A>x</A>
25     <B>y</B>
26   </PropertyGroup>
27   <ItemGroup>
28     <X Include=""included.txt"" />
29   </ItemGroup>
30 </Project>";
31                         using (var ts = File.CreateText ("test_imported.proj"))
32                                 ts.Write (imported);
33                         try {
34                                 var reader = XmlReader.Create (new StringReader (xml));
35                                 var root = ProjectRootElement.Create (reader);
36                                 Assert.AreEqual ("test_imported.proj", root.Imports.First ().Project, "#1");
37                                 var proj = new Project (root);
38                                 var prop = proj.GetProperty ("A");
39                                 Assert.IsNotNull (prop, "#2");
40                                 Assert.IsTrue (prop.IsImported, "#3");
41                                 var item = proj.GetItems ("X").FirstOrDefault ();
42                                 Assert.IsNotNull (item, "#4");
43                                 Assert.AreEqual ("included.txt", item.EvaluatedInclude, "#5");
44                         } finally {
45                                 File.Delete ("imported.proj");
46                         }
47                 }
48
49                         string import_overrides_test_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
50   <PropertyGroup>
51     <A>X</A>
52   </PropertyGroup>
53   <Import Condition=""{0}"" Project='imported.proj' />
54   <PropertyGroup>
55     <B>Y</B>
56   </PropertyGroup>
57 </Project>";
58                         string import_overrides_test_imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
59   <PropertyGroup>
60     <C Condition='$(A)==X'>c</C>
61     <A>a</A>
62     <B>b</B>
63   </PropertyGroup>
64   <ItemGroup>
65     <X Include=""included.txt"" />
66   </ItemGroup>
67 </Project>";
68
69                 void ImportAndPropertyOverrides (string label, string condition, string valueA, string valueB)
70                 {
71                         using (var ts = File.CreateText ("test_imported.proj"))
72                                 ts.Write (import_overrides_test_imported);
73                         try {
74                                 string xml = string.Format (import_overrides_test_xml, condition);
75                                 var reader = XmlReader.Create (new StringReader (xml));
76                                 var root = ProjectRootElement.Create (reader);
77                                 var proj = new Project (root);
78                                 var a = proj.GetProperty ("A");
79                                 Assert.IsNotNull (a, label + "#2");
80                                 Assert.AreEqual (valueA, a.EvaluatedValue, label + "#3");
81                                 var b = proj.GetProperty ("B");
82                                 Assert.IsNotNull (b, label + "#4");
83                                 Assert.AreEqual (valueB, b.EvaluatedValue, label + "#5");
84                                 var c = proj.GetProperty ("C");
85                                 Assert.IsNotNull (b, label + "#6");
86                                 Assert.AreEqual ("c", b.EvaluatedValue, label + "#7");
87                         } finally {
88                                 File.Delete ("test_imported.proj");
89                         }
90                 }
91
92                 [Test]
93                 public void ImportAndPropertyOverrides ()
94                 {
95                         ImportAndPropertyOverrides ("[1]", "'True'", "a", "Y");
96                         ImportAndPropertyOverrides ("[2]", "A=='X'", "a", "Y"); // evaluated as true
97                         ImportAndPropertyOverrides ("[2]", "B=='Y'", "X", "Y"); // evaluated as false
98                         ImportAndPropertyOverrides ("[2]", "B=='b'", "X", "Y"); // of course not evaluated with imported value
99                 }
100         }
101 }
102