PageRenderTime 45ms CodeModel.GetById 19ms 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
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense
  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 Simple ()
  51. {
  52. object res;
  53. res = Evaluator.Evaluate ("\"foo\" == \"bar\";");
  54. Assert.AreEqual (false, res, "CompareString");
  55. res = Evaluator.Evaluate ("var a = 1; a+2;");
  56. Assert.AreEqual (3, res, "CompareInt");
  57. res = Evaluator.Evaluate ("2 * 4;");
  58. Assert.AreEqual (8, res, "Multiply");
  59. }
  60. [Test]
  61. public void UsingAfterError ()
  62. {
  63. try {
  64. Evaluator.Evaluate ("File.OpenRead (\"/etc/passwd\");");
  65. Assert.Fail ("#1");
  66. } catch {
  67. }
  68. Evaluator.Run ("using System.IO;");
  69. Evaluator.Evaluate ("File.Exists (\"/etc/passwd\");");
  70. }
  71. [Test]
  72. public void WithTypeBuilders ()
  73. {
  74. object res;
  75. Evaluator.Run ("var a = new { A = 1 };");
  76. res = Evaluator.Evaluate ("a.ToString ();");
  77. Assert.AreEqual ("{ A = 1 }", res, "#1");
  78. }
  79. [Test]
  80. public void LinqExpressions ()
  81. {
  82. Evaluator.Run ("using System; using System.Linq;");
  83. Evaluator.Run ("var a = new int[]{1,2,3};");
  84. object res = Evaluator.Evaluate ("from x in a select x + 1;");
  85. CollectionAssert.AreEqual (new int[] { 2, 3, 4 }, ((IEnumerable<int>) res).ToArray ());
  86. }
  87. [Test]
  88. public void LinqExpressionStatements ()
  89. {
  90. Evaluator.Run ("using System; using System.Linq;");
  91. Evaluator.Run ("var first_scope = new int [] {1,2,3};");
  92. Evaluator.Run ("var second_scope = from x in first_scope select x;");
  93. }
  94. [Test]
  95. public void ReferenceLoading ()
  96. {
  97. Evaluator.ReferenceAssembly (typeof (ExpressionsTest).Assembly);
  98. object res = Evaluator.Evaluate ("typeof (MonoTests.EvaluatorTest.ExpressionsTest) != null;");
  99. Assert.AreEqual (true, res, "#1");
  100. }
  101. [Test]
  102. public void PartialExpression ()
  103. {
  104. object eval_result;
  105. bool result_set;
  106. string sres = Evaluator.Evaluate ("1+", out eval_result, out result_set);
  107. Assert.IsFalse (result_set, "No result should have been set");
  108. Assert.AreEqual ("1+", sres, "The result should have been the input string, since we have a partial input");
  109. }
  110. #if NET_4_0
  111. [Test]
  112. public void DynamicStatement ()
  113. {
  114. Evaluator.Run ("dynamic d = 1;");
  115. Evaluator.Run ("d = 'a';");
  116. Evaluator.Run ("d.GetType ();");
  117. }
  118. #endif
  119. }
  120. }