PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ekovalenko-softheme/mono
C# | 137 lines | 93 code | 17 blank | 27 comment | 0 complexity | 8908811db5c1212c9824dad4bc66b85d 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 Simple ()
 55		{
 56			object res;
 57			res = Evaluator.Evaluate ("\"foo\" == \"bar\";");
 58			Assert.AreEqual (false, res, "CompareString");
 59
 60			res = Evaluator.Evaluate ("var a = 1; a+2;");
 61			Assert.AreEqual (3, res, "CompareInt");
 62
 63			res = Evaluator.Evaluate ("2 * 4;");
 64			Assert.AreEqual (8, res, "Multiply");
 65		}
 66
 67		[Test]
 68		public void UsingAfterError ()
 69		{
 70			try {
 71				Evaluator.Evaluate ("File.OpenRead (\"/etc/passwd\");");
 72				Assert.Fail ("#1");
 73			} catch {
 74			}
 75
 76			Evaluator.Run ("using System.IO;");
 77			Evaluator.Evaluate ("File.Exists (\"/etc/passwd\");");
 78		}
 79
 80		[Test]
 81		public void WithTypeBuilders ()
 82		{
 83			object res;
 84			Evaluator.Run ("var a = new { A = 1 };");
 85			res = Evaluator.Evaluate ("a.ToString ();");
 86			Assert.AreEqual ("{ A = 1 }", res, "#1");
 87		}
 88
 89		[Test]
 90		public void LinqExpressions ()
 91		{
 92			Evaluator.Run ("using System; using System.Linq;");
 93
 94			Evaluator.Run ("var a = new int[]{1,2,3};");
 95
 96			object res = Evaluator.Evaluate ("from x in a select x + 1;");
 97			CollectionAssert.AreEqual (new int[] { 2, 3, 4 }, ((IEnumerable<int>) res).ToArray ());
 98		}
 99
100		[Test]
101		public void LinqExpressionStatements ()
102		{
103			Evaluator.Run ("using System; using System.Linq;");
104
105			Evaluator.Run ("var first_scope = new int [] {1,2,3};");
106			Evaluator.Run ("var second_scope = from x in first_scope select x;");
107		}
108
109		[Test]
110		public void ReferenceLoading ()
111		{
112			Evaluator.ReferenceAssembly (typeof (ExpressionsTest).Assembly);
113			object res = Evaluator.Evaluate ("typeof (MonoTests.EvaluatorTest.ExpressionsTest) != null;");
114			Assert.AreEqual (true, res, "#1");
115		}
116
117		[Test]
118		public void PartialExpression ()
119		{
120			object eval_result;
121			bool result_set;
122			string sres = Evaluator.Evaluate ("1+", out eval_result, out result_set);
123			Assert.IsFalse (result_set, "No result should have been set");
124			Assert.AreEqual ("1+", sres, "The result should have been the input string, since we have a partial input");
125		}
126
127#if NET_4_0
128		[Test]
129		public void DynamicStatement ()
130		{
131			Evaluator.Run ("dynamic d = 1;");
132			Evaluator.Run ("d = 'a';");
133			Evaluator.Run ("d.GetType ();");
134		}
135#endif
136	}
137}