47aee14e4c57d9f357f359f3ce70ca1d1b1a5ec1
[mono.git] / mcs / class / Mono.CSharp / Test / Evaluator / TypesTest.cs
1 //
2 // TypesTest.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
34 namespace MonoTests.EvaluatorTest
35 {
36         [TestFixture]
37         public class TypesTest : EvaluatorFixture
38         {
39                 [Test]
40                 public void SimpleTypeStaticMethod ()
41                 {
42                         object res;
43                         Evaluator.Run ("class Y { public static int Test () { return 5; }}");
44                         res = Evaluator.Evaluate ("Y.Test ();");
45                         Assert.AreEqual (5, res, "#1");
46                 }
47
48                 [Test]
49                 public void SameTypeNameRedefinition ()
50                 {
51                         Evaluator.Run ("class X { }");
52                         Evaluator.Run ("class X { public static void Foo () { throw new System.ApplicationException (); } }");
53                         Evaluator.Run ("class X {}");
54                         Evaluator.Run ("class X { public static string Foo () { return \"Test\"; } }");
55                         object res = Evaluator.Evaluate ("X.Foo ();");
56                         Assert.AreEqual ("Test", res, "#1");
57                         Evaluator.Run ("class X { public static int Foo () { return 5; } }");
58                         res = Evaluator.Evaluate ("X.Foo ();");
59                         Assert.AreEqual (5, res, "#2");
60                 }
61
62                 [Test]
63                 public void SimpleConstructor ()
64                 {
65                         Evaluator.Run ("class Y2 { public int Value; public Y2 (){ Value = 99; } }");
66                         Evaluator.Run ("var a = new Y2 ();");
67                         object res = Evaluator.Evaluate ("a.Value;");
68                         Assert.AreEqual (99, res);
69                 }
70
71                 [Test]
72                 public void TypeOfType ()
73                 {
74                         Evaluator.Run ("struct Z { }");
75                         object res = Evaluator.Evaluate ("typeof (Z);");
76                         Assert.AreEqual ("Z", res.ToString ());
77                 }
78
79                 [Test]
80                 public void UsingAfterType ()
81                 {
82                         Evaluator.Run ("struct Z { }");
83                         Evaluator.Run ("using System;");
84                         Evaluator.Run ("struct Z { }");
85                 }
86
87                 [Test]
88                 public void MoreThanOneType ()
89                 {
90                         Evaluator.Run ("public class D { int x; public int X { get { return x; } set { x = value;} } };");
91                         Evaluator.Run ("public class C { public int Speed{get;set;}};");
92                 }
93
94                 [Test]
95                 public void StructType ()
96                 {
97                         Evaluator.Run ("class C { }");
98                         Evaluator.Run ("struct B { public string foo; public int bar; }");
99                         Evaluator.Run ("B aStruct = new B { foo = \"foo\", bar = 1 };");
100                 }
101
102                 [Test]
103                 public void NestedType ()
104                 {
105                         Evaluator.Run ("class A { class B { } }");
106                         Evaluator.Run ("var x = new A ();");
107                 }
108
109                 [Test]
110                 public void DelegateType ()
111                 {
112                         Evaluator.Run ("public delegate int D();");
113                         Evaluator.Run ("D d = delegate () { return 7; };");
114                         object res = Evaluator.Evaluate ("d();");
115                         Assert.AreEqual (7, res);
116                 }
117
118                 [Test]
119                 public void ClassWithAttribute ()
120                 {
121                         Evaluator.Run ("public class A : System.Attribute { }");
122                         Evaluator.Run ("[A] public class B {}");
123                         Evaluator.Run ("var attr = new B().GetType().GetCustomAttributes(false)[0];");
124
125                         object res = Evaluator.Evaluate ("attr.GetType().Name;");
126                         Assert.AreEqual ("A", res);
127                 }
128         }
129 }