5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2012 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.
30 using NUnit.Framework;
32 using System.Collections;
34 using System.Collections.Generic;
36 namespace MonoTests.EvaluatorTest
39 public class ExpressionsTest : EvaluatorFixture
42 public void StatementExpression_1 ()
44 Evaluator.Run ("System.Console.WriteLine (100)");
48 public void StatementExpression_2 ()
50 Evaluator.Run ("var a = new int [] {1,2,3}; var b = a.Length;");
54 public void AnonymousType ()
56 Evaluator.Run ("var foo = new { Bar = \"baz\" };");
63 res = Evaluator.Evaluate ("\"foo\" == \"bar\";");
64 Assert.AreEqual (false, res, "CompareString");
66 res = Evaluator.Evaluate ("var a = 1; a+2;");
67 Assert.AreEqual (3, res, "CompareInt");
69 res = Evaluator.Evaluate ("2 * 4;");
70 Assert.AreEqual (8, res, "Multiply");
74 public void UsingAfterError ()
77 Evaluator.Evaluate ("File.OpenRead (\"/etc/passwd\");");
82 Evaluator.Run ("using System.IO;");
83 Evaluator.Evaluate ("File.Exists (\"/etc/passwd\");");
87 public void WithTypeBuilders ()
90 Evaluator.Run ("var a = new { A = 1 };");
91 res = Evaluator.Evaluate ("a.ToString ();");
92 Assert.AreEqual ("{ A = 1 }", res, "#1");
96 public void LinqExpressions ()
98 Evaluator.Run ("using System; using System.Linq;");
100 Evaluator.Run ("var a = new int[]{1,2,3};");
102 object res = Evaluator.Evaluate ("from x in a select x + 1;");
103 CollectionAssert.AreEqual (new int[] { 2, 3, 4 }, ((IEnumerable<int>) res).ToArray ());
107 public void LinqExpressionStatements ()
109 Evaluator.Run ("using System; using System.Linq;");
111 Evaluator.Run ("var first_scope = new int [] {1,2,3};");
112 Evaluator.Run ("var second_scope = from x in first_scope select x;");
116 public void ReferenceLoading ()
118 Evaluator.ReferenceAssembly (typeof (ExpressionsTest).Assembly);
119 object res = Evaluator.Evaluate ("typeof (MonoTests.EvaluatorTest.ExpressionsTest) != null;");
120 Assert.AreEqual (true, res, "#1");
124 public void PartialExpression ()
128 string sres = Evaluator.Evaluate ("1+", out eval_result, out result_set);
129 Assert.IsFalse (result_set, "No result should have been set");
130 Assert.AreEqual ("1+", sres, "The result should have been the input string, since we have a partial input");
134 public void GotoWithUnreachableStatement ()
136 Evaluator.Run ("using System;");
138 string code = "var x = new Action(() => {" +
139 "Console.WriteLine(\"beforeGoto\");" +
142 "Console.WriteLine(\"afterGoto\");" +
145 Assert.IsTrue (Evaluator.Run (code), "#1");
146 Assert.IsTrue (Evaluator.Run ("x();"), "#2");
151 public void DynamicStatement ()
153 Evaluator.Run ("dynamic d = 1;");
154 Evaluator.Run ("d = 'a';");
155 Evaluator.Run ("d.GetType ();");
161 public void AwaitExpression ()
163 Evaluator.WaitOnTask = true;
164 var res = Evaluator.Evaluate("var res = await System.Threading.Tasks.Task.FromResult (1) + await System.Threading.Tasks.Task.FromResult (2);");
165 res = Evaluator.Evaluate ("res;");
166 Assert.AreEqual (3, res, "#1");