Add tutorial for adding more completion types and added new completion
[mono.git] / mcs / tests / eval-test.cs
1 using System;
2 using Mono.CSharp;
3
4 public class MyTest {
5         static void Run (string id, string stmt)
6         {
7                 if (!Evaluator.Run (stmt))
8                         Console.WriteLine ("Failed on test {0}", id);
9         }
10
11         static void Evaluate (string id, string expr, object expected)
12         {
13                 try {
14                         object res = Evaluator.Evaluate (expr);
15                         if (res == null && expected == null)
16                                 return;
17
18                         if (!expected.Equals (res)){
19                                 Console.WriteLine ("Failed on test {2} Expecting {0}, got {1}", expected, res, id);
20                                 throw new Exception ();
21                         }
22                 } catch {
23                         Console.WriteLine ("Failed on test {0}", id);
24                         throw;
25                 }
26         }
27         
28         static void Main ()
29         {
30                 Evaluator.Init (new string [] { "-v", "-v" });
31                 Evaluate ("multiply", "1*2;", 2);
32                 Run ("1",      "System.Console.WriteLine (100);");
33                 Run ("Length", "var a = new int [] {1,2,3}; var b = a.Length;");
34                 
35                 Evaluate ("CompareString", "\"foo\" == \"bar\";", false);
36                 Evaluate ("CompareInt", "var a = 1; a+2;", 3);
37
38                 Evaluator.Run ("using System; using System.Linq;");
39                 Run ("LINQ-1", "var a = new int[]{1,2,3};\nfrom x in a select x;");
40                 Run ("LINQ-2", "var a = from f in System.IO.Directory.GetFiles (\"/tmp\") where f == \"passwd\" select f;");
41
42                 Evaluator.ReferenceAssembly (typeof (MyTest).Assembly);
43                 Evaluate ("assembly reference test", "typeof (MyTest) != null;", true);
44
45                 Run ("LINQ-3", "var first_scope = new int [] {1,2,3};");
46                 Run ("LINQ-4", "var second_scope = from x in first_scope select x;");
47
48                 string prefix = "";
49                 string [] res = Evaluator.GetCompletions ("ConsoleK", out prefix);
50                 if (res [0] != "ey" || res [1] != "eyInfo"){
51                         Console.WriteLine (res [0]);
52                         Console.WriteLine (res [1]);
53                         throw new Exception ("Expected two completions ConsoleKey and ConsoleKeyInfo");
54                 }
55
56                 res = Evaluator.GetCompletions ("Converte", out prefix);
57                 if (res [0] != "r<"){
58                         throw new Exception ("Expected one completion for Conveter<");
59                 }
60
61                 res = Evaluator.GetCompletions ("Sys", out prefix);
62                 if (res [0] != "tem"){
63                         throw new Exception ("Expected at least a conversion for System");
64                 }
65
66                 res = Evaluator.GetCompletions ("System.Int3", out prefix);
67                 if (res [0] != "2"){
68                         throw new Exception ("Expected completion to System.Int32");
69                 }
70
71                 res = Evaluator.GetCompletions ("new System.Text.StringBuilder () { Ca", out prefix);
72                 if (res [0] != "pacity"){
73                         throw new Expected ("Expected completion to Capacity");
74                 }
75
76                 res = Evaluator.GetCompletions ("new System.Text.StringBuilder () { ", out prefix);
77                 if (res.Lenght != 4){
78                         throw new Expected ("Expected 4 completions (Capacity Chars Length MaxCapacity)");
79                 }
80         }
81         
82 }