2 // ExpressionParserTest.cs
5 // Atsushi Enomoto (atsushi@xamarin.com)
7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using Microsoft.Build.Construction;
33 using Microsoft.Build.Evaluation;
34 using NUnit.Framework;
35 using Microsoft.Build.Execution;
36 using Microsoft.Build.Exceptions;
37 using System.Collections.Generic;
39 namespace MonoTests.Microsoft.Build.Internal
42 public class ExpressionParserTest
44 string [] invalid_always = {
46 "$([DateTime.Now])", // fullname required
47 "$([System.DateTime.Now])", // member cannot be invoked with '.'
49 string [] invalid_as_boolean = {
61 "A", // must be evaluated as a boolean
62 "1", // ditto (no default conversion to bool)
66 "$(Foo) == And", // reserved keyword 'and'
67 "$(Foo) == Or", // reserved keyword 'or'
68 "$(Foo) == $(Bar) == $(Baz)", // unexpected '=='
69 "$([System.DateTime]::Now)", // it is DateTime
70 "$([System.String]::Format('Tr'))$([System.String]::Format('ue'))", // only one expression is accepted
71 "$([System.String]::Format(null))", // causing ANE, wrapped by InvalidProjectFileException
88 "$([System.String]::Format('True'))",
89 "$([System.String]::Format('True', null))",
90 "False And True == True And True",
91 "True or True or False",
92 "(True or True or False)",
100 string [] depends = {
101 // valid only if evaluated to boolean
107 public void EvaluateAsBoolean ()
109 foreach (var expr in invalid_always.Concat (invalid_as_boolean).Concat (valid)) {
110 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
112 <Foo Condition=""{0}"" Include='x' />
115 var xml = XmlReader.Create (new StringReader (string.Format (project_xml, expr)));
116 var root = ProjectRootElement.Create (xml);
119 if (invalid_as_boolean.Contains (expr) || invalid_always.Contains (expr))
120 Assert.Fail ("Parsing Condition '{0}' should fail", expr);
121 } catch (Exception ex) {
122 if (valid.Contains (expr))
123 throw new Exception (string.Format ("failed to parse '{0}'", expr), ex);
124 else if (ex is InvalidProjectFileException)
126 throw new Exception (string.Format ("unexpected exception to parse '{0}'", expr), ex);
132 public void EvaluateAsString ()
134 foreach (var expr in invalid_always.Concat (invalid_as_boolean).Concat (valid)) {
136 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
138 <Foo Include=""{0}"" />
141 var xml = XmlReader.Create (new StringReader (string.Format (project_xml, expr)));
142 var root = ProjectRootElement.Create (xml);
143 // everything but 'invalid_always' should pass
145 } catch (Exception ex) {
146 if (!invalid_always.Contains (expr))
147 throw new Exception (string.Format ("failed to parse '{0}'", expr), ex);
153 public void EvaluatePropertyReferencesWithProperties ()
155 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
157 <Foo Condition=""$(foo)"" Include='x' />
160 var xml = XmlReader.Create (new StringReader (project_xml));
161 var root = ProjectRootElement.Create (xml);
162 var props = new Dictionary<string,string> ();
163 props ["foo"] = "true";
164 new Project (root, props, null);
168 public void EvaluateItemReferences ()
170 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
172 <Foo Include='false' />
173 <!-- by the time Bar is evaluated, Foo is already evaluated and taken into consideration in expansion -->
174 <Bar Condition=""@(foo)"" Include='x' />
177 var xml = XmlReader.Create (new StringReader (project_xml));
178 var root = ProjectRootElement.Create (xml);
183 public void EvaluateReferencesWithoutProperties ()
185 foreach (var expr in depends) {
186 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
188 <Foo Condition=""{0}"" Include='x' />
191 var xml = XmlReader.Create (new StringReader (string.Format (project_xml, expr)));
192 var root = ProjectRootElement.Create (xml);
195 Assert.Fail ("Parsing Condition '{0}' should fail", expr);
196 } catch (InvalidProjectFileException) {
203 public void SemicolonHandling ()
205 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
207 <Foo Condition=""'A;B'=='A;B'"">'A;B'</Foo>
210 <Bar Include='$(Foo)' />
213 var xml = XmlReader.Create (new StringReader (project_xml));
214 var root = ProjectRootElement.Create (xml);
215 var proj = new Project (root); // at this state property is parsed without error i.e. Condition evaluates fine.
216 var prop = proj.GetProperty ("Foo");
217 Assert.AreEqual ("'A;B'", prop.EvaluatedValue, "#1");
218 var items = proj.GetItems ("Bar");
219 Assert.AreEqual ("'A", items.First ().EvaluatedInclude, "#2");
220 Assert.AreEqual ("$(Foo)", items.First ().UnevaluatedInclude, "#3");
221 Assert.AreEqual (2, items.Count, "#4");
222 Assert.AreEqual ("B'", items.Last ().EvaluatedInclude, "#5");
223 Assert.AreEqual ("$(Foo)", items.Last ().UnevaluatedInclude, "#6");
224 Assert.IsTrue (items.First ().Xml == items.Last ().Xml, "#7");
227 // the same as above except that ItemGroup goes first (and yet evaluated the same).
229 public void EvaluationOrderPropertiesPrecedesItems ()
231 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
233 <Bar Include='$(Foo)' />
236 <Foo Condition=""'A;B'=='A;B'"">'A;B'</Foo>
239 var xml = XmlReader.Create (new StringReader (project_xml));
240 var root = ProjectRootElement.Create (xml);
241 var proj = new Project (root); // at this state property is parsed without error i.e. Condition evaluates fine.
242 var prop = proj.GetProperty ("Foo");
243 Assert.AreEqual ("'A;B'", prop.EvaluatedValue, "#1");
244 var items = proj.GetItems ("Bar");
245 Assert.AreEqual ("'A", items.First ().EvaluatedInclude, "#2");
246 Assert.AreEqual ("$(Foo)", items.First ().UnevaluatedInclude, "#3");
247 Assert.AreEqual (2, items.Count, "#4");
248 Assert.AreEqual ("B'", items.Last ().EvaluatedInclude, "#5");
249 Assert.AreEqual ("$(Foo)", items.Last ().UnevaluatedInclude, "#6");
250 Assert.IsTrue (items.First ().Xml == items.Last ().Xml, "#7");
254 [ExpectedException (typeof (InvalidProjectFileException))]
255 public void PropertyReferencesItem ()
257 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
259 <Bar Include='True' />
262 <Foo Condition='@(Bar)'>X</Foo><!-- not allowed -->
265 var xml = XmlReader.Create (new StringReader (project_xml));
266 var root = ProjectRootElement.Create (xml);
271 [ExpectedException (typeof (InvalidProjectFileException))]
272 public void SequentialPropertyReferenceNotAllowed ()
274 string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
278 <C Condition=""$(A)$(B)==''"">z</C>
281 var reader = XmlReader.Create (new StringReader (xml));
282 var root = ProjectRootElement.Create (reader);