Merge pull request #832 from xplicit/webperf1
[mono.git] / mcs / class / Mono.CSharp / Test / Evaluator / ExpressionsTest.cs
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
29 using System;
30 using NUnit.Framework;
31 using Mono.CSharp;
32 using System.Collections;
33 using System.Linq;
34 using System.Collections.Generic;
35
36 namespace 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                 [Test]
134                 public void GotoWithUnreachableStatement ()
135                 {
136                         Evaluator.Run ("using System;");
137
138                         string code = "var x = new Action(() => {" +
139                         "Console.WriteLine(\"beforeGoto\");" +
140                         "goto L;" +
141                 "L:" +
142                         "Console.WriteLine(\"afterGoto\");" +
143                         "});";
144
145                         Assert.IsTrue (Evaluator.Run (code), "#1");
146                         Assert.IsTrue (Evaluator.Run ("x();"), "#2");
147                 }
148
149 #if NET_4_0
150                 [Test]
151                 public void DynamicStatement ()
152                 {
153                         Evaluator.Run ("dynamic d = 1;");
154                         Evaluator.Run ("d = 'a';");
155                         Evaluator.Run ("d.GetType ();");
156                 }
157 #endif
158
159 #if NET_4_5
160                 [Test]
161                 public void AwaitExpression ()
162                 {
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");
167                 }
168 #endif
169         }
170 }