PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/mcs/class/Mono.CSharp/Test/Evaluator/ExpressionsTest.cs

https://github.com/ztfuqingvip/mono
C# | 154 lines | 108 code | 19 blank | 27 comment | 0 complexity | 3616eaaf13698aca8fd1c5c5a256e9fa MD5 | raw file
  1//
  2// ExpressionsTest.cs
  3//
  4// Authors:
  5//	Marek Safar  <marek.safar@gmail.com>
  6//
  7// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
  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
 29using System;
 30using NUnit.Framework;
 31using Mono.CSharp;
 32using System.Collections;
 33using System.Linq;
 34using System.Collections.Generic;
 35
 36namespace MonoTests.EvaluatorTest
 37{
 38	[TestFixture]
 39	public class ExpressionsTest : EvaluatorFixture
 40	{
 41		[Test]
 42		public void StatementExpression_1 ()
 43		{
 44			Evaluator.Run ("System.Console.WriteLine (100)");
 45		}
 46
 47		[Test]
 48		public void StatementExpression_2 ()
 49		{
 50			Evaluator.Run ("var a = new int [] {1,2,3}; var b = a.Length;");
 51		}
 52
 53		[Test]
 54		public void AnonymousType ()
 55		{
 56			Evaluator.Run ("var foo = new { Bar = \"baz\" };");
 57		}
 58
 59		[Test]
 60		public void Simple ()
 61		{
 62			object res;
 63			res = Evaluator.Evaluate ("\"foo\" == \"bar\";");
 64			Assert.AreEqual (false, res, "CompareString");
 65
 66			res = Evaluator.Evaluate ("var a = 1; a+2;");
 67			Assert.AreEqual (3, res, "CompareInt");
 68
 69			res = Evaluator.Evaluate ("2 * 4;");
 70			Assert.AreEqual (8, res, "Multiply");
 71		}
 72
 73		[Test]
 74		public void UsingAfterError ()
 75		{
 76			try {
 77				Evaluator.Evaluate ("File.OpenRead (\"/etc/passwd\");");
 78				Assert.Fail ("#1");
 79			} catch {
 80			}
 81
 82			Evaluator.Run ("using System.IO;");
 83			Evaluator.Evaluate ("File.Exists (\"/etc/passwd\");");
 84		}
 85
 86		[Test]
 87		public void WithTypeBuilders ()
 88		{
 89			object res;
 90			Evaluator.Run ("var a = new { A = 1 };");
 91			res = Evaluator.Evaluate ("a.ToString ();");
 92			Assert.AreEqual ("{ A = 1 }", res, "#1");
 93		}
 94
 95		[Test]
 96		public void LinqExpressions ()
 97		{
 98			Evaluator.Run ("using System; using System.Linq;");
 99
100			Evaluator.Run ("var a = new int[]{1,2,3};");
101
102			object res = Evaluator.Evaluate ("from x in a select x + 1;");
103			CollectionAssert.AreEqual (new int[] { 2, 3, 4 }, ((IEnumerable<int>) res).ToArray ());
104		}
105
106		[Test]
107		public void LinqExpressionStatements ()
108		{
109			Evaluator.Run ("using System; using System.Linq;");
110
111			Evaluator.Run ("var first_scope = new int [] {1,2,3};");
112			Evaluator.Run ("var second_scope = from x in first_scope select x;");
113		}
114
115		[Test]
116		public void ReferenceLoading ()
117		{
118			Evaluator.ReferenceAssembly (typeof (ExpressionsTest).Assembly);
119			object res = Evaluator.Evaluate ("typeof (MonoTests.EvaluatorTest.ExpressionsTest) != null;");
120			Assert.AreEqual (true, res, "#1");
121		}
122
123		[Test]
124		public void PartialExpression ()
125		{
126			object eval_result;
127			bool result_set;
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");
131		}
132
133#if NET_4_0
134		[Test]
135		public void DynamicStatement ()
136		{
137			Evaluator.Run ("dynamic d = 1;");
138			Evaluator.Run ("d = 'a';");
139			Evaluator.Run ("d.GetType ();");
140		}
141#endif
142
143#if NET_4_5
144		[Test]
145		public void AwaitExpression ()
146		{
147			Evaluator.WaitOnTask = true;
148			var res = Evaluator.Evaluate("var res = await System.Threading.Tasks.Task.FromResult (1) + await System.Threading.Tasks.Task.FromResult (2);");
149			res = Evaluator.Evaluate ("res;");
150			Assert.AreEqual (3, res, "#1");
151		}
152#endif
153	}
154}