PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/tests/eval-test.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 85 lines | 66 code | 15 blank | 4 comment | 20 complexity | f2ba80179487a092de58f6fad8855ea8 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. using System;
  2. using Mono.CSharp;
  3. public class MyTest {
  4. static void Run (string id, string stmt)
  5. {
  6. if (!Evaluator.Run (stmt))
  7. Console.WriteLine ("Failed on test {0}", id);
  8. }
  9. static void Evaluate (string id, string expr, object expected)
  10. {
  11. try {
  12. object res = Evaluator.Evaluate (expr);
  13. if (res == null && expected == null)
  14. return;
  15. if (!expected.Equals (res)){
  16. Console.WriteLine ("Failed on test {2} Expecting {0}, got {1}", expected, res, id);
  17. throw new Exception ();
  18. }
  19. } catch {
  20. Console.WriteLine ("Failed on test {0}", id);
  21. throw;
  22. }
  23. }
  24. static void Main ()
  25. {
  26. Evaluator.Init (new string [0]); //new string [] { "-v", "-v" });
  27. //
  28. // This fails because of the grammar issue with the pointer type
  29. // Evaluate ("multiply", "1*2;", 2);
  30. //
  31. Run ("1", "System.Console.WriteLine (100);");
  32. Run ("Length", "var a = new int [] {1,2,3}; var b = a.Length;");
  33. Evaluate ("CompareString", "\"foo\" == \"bar\";", false);
  34. Evaluate ("CompareInt", "var a = 1; a+2;", 3);
  35. Evaluator.Run ("using System; using System.Linq;");
  36. Run ("LINQ-1", "var a = new int[]{1,2,3};\nfrom x in a select x;");
  37. Run ("LINQ-2", "var a = from f in System.IO.Directory.GetFiles (\"/tmp\") where f == \"passwd\" select f;");
  38. Evaluator.ReferenceAssembly (typeof (MyTest).Assembly);
  39. Evaluate ("assembly reference test", "typeof (MyTest) != null;", true);
  40. Run ("LINQ-3", "var first_scope = new int [] {1,2,3};");
  41. Run ("LINQ-4", "var second_scope = from x in first_scope select x;");
  42. string prefix = "";
  43. string [] res = Evaluator.GetCompletions ("ConsoleK", out prefix);
  44. if (res [0] != "ey" || res [1] != "eyInfo"){
  45. Console.WriteLine (res [0]);
  46. Console.WriteLine (res [1]);
  47. throw new Exception ("Expected two completions ConsoleKey and ConsoleKeyInfo");
  48. }
  49. res = Evaluator.GetCompletions ("Converte", out prefix);
  50. if (res [0] != "r<"){
  51. throw new Exception ("Expected one completion for Conveter<");
  52. }
  53. res = Evaluator.GetCompletions ("Sys", out prefix);
  54. if (res [0] != "tem"){
  55. throw new Exception ("Expected at least a conversion for System");
  56. }
  57. res = Evaluator.GetCompletions ("System.Int3", out prefix);
  58. if (res [0] != "2"){
  59. throw new Exception ("Expected completion to System.Int32");
  60. }
  61. res = Evaluator.GetCompletions ("new System.Text.StringBuilder () { Ca", out prefix);
  62. if (res [0] != "pacity"){
  63. throw new Exception ("Expected completion to Capacity");
  64. }
  65. res = Evaluator.GetCompletions ("new System.Text.StringBuilder () { ", out prefix);
  66. if (res.Length != 4){
  67. throw new Exception ("Epxected 4 completions (Capacity Chars Length MaxCapacity)");
  68. }
  69. }
  70. }