PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/MatthiasKainer/mono
C# | 170 lines | 121 code | 22 blank | 27 comment | 0 complexity | a0b00f3fd65e21acd12b408913ab0f04 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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. using System;
  29. using NUnit.Framework;
  30. using Mono.CSharp;
  31. using System.Collections;
  32. using System.Linq;
  33. using System.Collections.Generic;
  34. namespace MonoTests.EvaluatorTest
  35. {
  36. [TestFixture]
  37. public class ExpressionsTest : EvaluatorFixture
  38. {
  39. [Test]
  40. public void StatementExpression_1 ()
  41. {
  42. Evaluator.Run ("System.Console.WriteLine (100)");
  43. }
  44. [Test]
  45. public void StatementExpression_2 ()
  46. {
  47. Evaluator.Run ("var a = new int [] {1,2,3}; var b = a.Length;");
  48. }
  49. [Test]
  50. public void AnonymousType ()
  51. {
  52. Evaluator.Run ("var foo = new { Bar = \"baz\" };");
  53. }
  54. [Test]
  55. public void Simple ()
  56. {
  57. object res;
  58. res = Evaluator.Evaluate ("\"foo\" == \"bar\";");
  59. Assert.AreEqual (false, res, "CompareString");
  60. res = Evaluator.Evaluate ("var a = 1; a+2;");
  61. Assert.AreEqual (3, res, "CompareInt");
  62. res = Evaluator.Evaluate ("2 * 4;");
  63. Assert.AreEqual (8, res, "Multiply");
  64. }
  65. [Test]
  66. public void UsingAfterError ()
  67. {
  68. try {
  69. Evaluator.Evaluate ("File.OpenRead (\"/etc/passwd\");");
  70. Assert.Fail ("#1");
  71. } catch {
  72. }
  73. Evaluator.Run ("using System.IO;");
  74. Evaluator.Evaluate ("File.Exists (\"/etc/passwd\");");
  75. }
  76. [Test]
  77. public void WithTypeBuilders ()
  78. {
  79. object res;
  80. Evaluator.Run ("var a = new { A = 1 };");
  81. res = Evaluator.Evaluate ("a.ToString ();");
  82. Assert.AreEqual ("{ A = 1 }", res, "#1");
  83. }
  84. [Test]
  85. public void LinqExpressions ()
  86. {
  87. Evaluator.Run ("using System; using System.Linq;");
  88. Evaluator.Run ("var a = new int[]{1,2,3};");
  89. object res = Evaluator.Evaluate ("from x in a select x + 1;");
  90. CollectionAssert.AreEqual (new int[] { 2, 3, 4 }, ((IEnumerable<int>) res).ToArray ());
  91. }
  92. [Test]
  93. public void LinqExpressionStatements ()
  94. {
  95. Evaluator.Run ("using System; using System.Linq;");
  96. Evaluator.Run ("var first_scope = new int [] {1,2,3};");
  97. Evaluator.Run ("var second_scope = from x in first_scope select x;");
  98. }
  99. [Test]
  100. public void ReferenceLoading ()
  101. {
  102. Evaluator.ReferenceAssembly (typeof (ExpressionsTest).Assembly);
  103. object res = Evaluator.Evaluate ("typeof (MonoTests.EvaluatorTest.ExpressionsTest) != null;");
  104. Assert.AreEqual (true, res, "#1");
  105. }
  106. [Test]
  107. public void PartialExpression ()
  108. {
  109. object eval_result;
  110. bool result_set;
  111. string sres = Evaluator.Evaluate ("1+", out eval_result, out result_set);
  112. Assert.IsFalse (result_set, "No result should have been set");
  113. Assert.AreEqual ("1+", sres, "The result should have been the input string, since we have a partial input");
  114. }
  115. [Test]
  116. public void GotoWithUnreachableStatement ()
  117. {
  118. Evaluator.Run ("using System;");
  119. string code = "var x = new Action(() => {" +
  120. "Console.WriteLine(\"beforeGoto\");" +
  121. "goto L;" +
  122. "L:" +
  123. "Console.WriteLine(\"afterGoto\");" +
  124. "});";
  125. Assert.IsTrue (Evaluator.Run (code), "#1");
  126. Assert.IsTrue (Evaluator.Run ("x();"), "#2");
  127. }
  128. #if NET_4_0
  129. [Test]
  130. public void DynamicStatement ()
  131. {
  132. Evaluator.Run ("dynamic d = 1;");
  133. Evaluator.Run ("d = 'a';");
  134. Evaluator.Run ("d.GetType ();");
  135. }
  136. #endif
  137. #if NET_4_5
  138. [Test]
  139. public void AwaitExpression ()
  140. {
  141. Evaluator.WaitOnTask = true;
  142. var res = Evaluator.Evaluate("var res = await System.Threading.Tasks.Task.FromResult (1) + await System.Threading.Tasks.Task.FromResult (2);");
  143. res = Evaluator.Evaluate ("res;");
  144. Assert.AreEqual (3, res, "#1");
  145. }
  146. #endif
  147. }
  148. }