Merge pull request #642 from Ventero/CleanCopyLocal
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Field.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //
19 // Authors:
20 //              Federico Di Gregorio <fog@initd.org>
21
22 using System;
23 using System.Reflection;
24 using System.Linq;
25 using System.Linq.Expressions;
26 using NUnit.Framework;
27
28 namespace MonoTests.System.Linq.Expressions
29 {
30         [TestFixture]
31         public class ExpressionTest_Field
32         {
33                 [Test]
34                 [ExpectedException (typeof (ArgumentNullException))]
35                 public void Arg1Null ()
36                 {
37                         Expression.Field (null, "NoField");
38                 }
39
40                 [Test]
41                 [ExpectedException (typeof (ArgumentNullException))]
42                 public void Arg2Null1 ()
43                 {
44                         Expression.Field (Expression.Constant (new MemberClass()), (string)null);
45                 }
46
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void Arg2Null2 ()
50                 {
51                         Expression.Field (Expression.Constant (new MemberClass()), (FieldInfo)null);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (ArgumentException))]
56                 public void NoField ()
57                 {
58                         Expression.Field (Expression.Constant (new MemberClass()), "NoField");
59                 }
60
61                 [Test]
62                 public void InstanceField ()
63                 {
64                         MemberExpression expr = Expression.Field (Expression.Constant (new MemberClass()), "TestField1");
65                         Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#01");
66                         Assert.AreEqual (typeof (int), expr.Type, "Field#02");
67                         Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestField1", expr.ToString(), "Field#03");
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentException))]
72                 public void StaticField1 ()
73                 {
74                         // This will fail because access to a static field should be created using a FieldInfo and
75                         // not an instance plus the field name.
76                         Expression.Field (Expression.Constant (new MemberClass()), "StaticField");
77                 }
78
79                 [Test]
80                 public void StaticField2 ()
81                 {
82                         MemberExpression expr = Expression.Field (null, MemberClass.GetStaticFieldInfo());
83                         Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#07");
84                         Assert.AreEqual (typeof (int), expr.Type, "Field#08");
85                         Assert.AreEqual ("MemberClass.StaticField", expr.ToString(), "Field#09");
86                 }
87
88                 [Test]
89                 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void StaticFieldWithInstanceArgument ()
92                 {
93                         Expression.Field (
94                                 Expression.Parameter (GetType (), "t"),
95                                 GetType ().GetField ("foo"));
96                 }
97
98                 public static string foo = "foo";
99
100                 [Test]
101                 public void CompileStaticField ()
102                 {
103                         var foo = Expression.Lambda<Func<string>> (
104                                 Expression.Field (null, GetType ().GetField (
105                                         "foo", BindingFlags.Static | BindingFlags.Public))).Compile ();
106
107                         Assert.AreEqual ("foo", foo ());
108                 }
109
110                 public class Bar {
111                         public string baz;
112
113                         public Bar ()
114                         {
115                                 baz = "baz";
116                         }
117                 }
118
119                 [Test]
120                 public void CompileInstanceField ()
121                 {
122                         var p = Expression.Parameter (typeof (Bar), "bar");
123                         var baz = Expression.Lambda<Func<Bar, string>> (
124                                 Expression.Field (p, typeof (Bar).GetField (
125                                         "baz", BindingFlags.Public | BindingFlags.Instance)), p).Compile ();
126
127                         Assert.AreEqual ("baz", baz (new Bar ()));
128                 }
129
130                 public struct Gazonk {
131                         public string Tzap;
132
133                         public Gazonk (string tzap)
134                         {
135                                 Tzap = tzap;
136                         }
137                 }
138
139                 [Test]
140                 public void CompileStructInstanceField ()
141                 {
142                         var p = Expression.Parameter (typeof (Gazonk), "gazonk");
143                         var gazonker = Expression.Lambda<Func<Gazonk, string>> (
144                                 Expression.Field (p, typeof (Gazonk).GetField ("Tzap")), p).Compile ();
145
146                         Assert.AreEqual ("bang", gazonker (new Gazonk ("bang")));
147                 }
148         }
149 }