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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
20 // Federico Di Gregorio <fog@initd.org>
21 // Jb Evain <jbevain@novell.com>
24 using System.Reflection;
26 using System.Linq.Expressions;
27 using NUnit.Framework;
29 namespace MonoTests.System.Linq.Expressions
32 public class ExpressionTest_Or
35 [ExpectedException (typeof (ArgumentNullException))]
36 public void Arg1Null ()
38 Expression.Or (null, Expression.Constant (1));
42 [ExpectedException (typeof (ArgumentNullException))]
43 public void Arg2Null ()
45 Expression.Or (Expression.Constant (1), null);
49 [ExpectedException (typeof (InvalidOperationException))]
50 public void NoOperatorClass ()
52 Expression.Or (Expression.Constant (new NoOpClass ()), Expression.Constant (new NoOpClass ()));
56 [ExpectedException (typeof (InvalidOperationException))]
57 public void ArgTypesDifferent ()
59 Expression.Or (Expression.Constant (1), Expression.Constant (true));
63 [ExpectedException (typeof (InvalidOperationException))]
66 Expression.Or (Expression.Constant (1.0), Expression.Constant (2.0));
70 public void Integer ()
72 BinaryExpression expr = Expression.Or (Expression.Constant (1), Expression.Constant (2));
73 Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#01");
74 Assert.AreEqual (typeof (int), expr.Type, "Or#02");
75 Assert.IsNull (expr.Method, "Or#03");
76 Assert.AreEqual ("(1 | 2)", expr.ToString(), "Or#04");
80 public void Boolean ()
82 BinaryExpression expr = Expression.Or (Expression.Constant (true), Expression.Constant (false));
83 Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#05");
84 Assert.AreEqual (typeof (bool), expr.Type, "Or#06");
85 Assert.IsNull (expr.Method, "Or#07");
86 Assert.AreEqual ("(True Or False)", expr.ToString(), "Or#08");
90 public void UserDefinedClass ()
92 // We can use the simplest version of GetMethod because we already know only one
93 // exists in the very simple class we're using for the tests.
94 MethodInfo mi = typeof (OpClass).GetMethod ("op_BitwiseOr");
96 BinaryExpression expr = Expression.Or (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
97 Assert.AreEqual (ExpressionType.Or, expr.NodeType, "Or#09");
98 Assert.AreEqual (typeof (OpClass), expr.Type, "Or#10");
99 Assert.AreEqual (mi, expr.Method, "Or#11");
100 Assert.AreEqual ("op_BitwiseOr", expr.Method.Name, "Or#12");
101 Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) | value(MonoTests.System.Linq.Expressions.OpClass))",
102 expr.ToString(), "Or#13");
106 public void OrBoolTest ()
108 var a = Expression.Parameter (typeof (bool), "a");
109 var b = Expression.Parameter (typeof (bool), "b");
110 var l = Expression.Lambda<Func<bool, bool, bool>> (
111 Expression.Or (a, b), a, b);
113 var be = l.Body as BinaryExpression;
114 Assert.IsNotNull (be);
115 Assert.AreEqual (typeof (bool), be.Type);
116 Assert.IsFalse (be.IsLifted);
117 Assert.IsFalse (be.IsLiftedToNull);
119 var c = l.Compile ();
121 Assert.AreEqual (true, c (true, true), "o1");
122 Assert.AreEqual (true, c (true, false), "o2");
123 Assert.AreEqual (true, c (false, true), "o3");
124 Assert.AreEqual (false, c (false, false), "o4");
128 public void OrBoolNullableTest ()
130 var a = Expression.Parameter (typeof (bool?), "a");
131 var b = Expression.Parameter (typeof (bool?), "b");
132 var l = Expression.Lambda<Func<bool?, bool?, bool?>> (
133 Expression.Or (a, b), a, b);
135 var be = l.Body as BinaryExpression;
136 Assert.IsNotNull (be);
137 Assert.AreEqual (typeof (bool?), be.Type);
138 Assert.IsTrue (be.IsLifted);
139 Assert.IsTrue (be.IsLiftedToNull);
141 var c = l.Compile ();
143 Assert.AreEqual (true, c (true, true), "o1");
144 Assert.AreEqual (true, c (true, false), "o2");
145 Assert.AreEqual (true, c (false, true), "o3");
146 Assert.AreEqual (false, c (false, false), "o4");
148 Assert.AreEqual (true, c (true, null), "o5");
149 Assert.AreEqual (null, c (false, null), "o6");
150 Assert.AreEqual (null, c (null, false), "o7");
151 Assert.AreEqual (true, c (true, null), "o8");
152 Assert.AreEqual (null, c (null, null), "o9");
156 public void OrBoolItem ()
158 var i = Expression.Parameter (typeof (Item<bool>), "i");
159 var and = Expression.Lambda<Func<Item<bool>, bool>> (
161 Expression.Property (i, "Left"),
162 Expression.Property (i, "Right")), i).Compile ();
164 var item = new Item<bool> (true, false);
165 Assert.AreEqual (true, and (item));
166 Assert.IsTrue (item.LeftCalled);
167 Assert.IsTrue (item.RightCalled);
171 public void OrNullableBoolItem ()
173 var i = Expression.Parameter (typeof (Item<bool?>), "i");
174 var and = Expression.Lambda<Func<Item<bool?>, bool?>> (
176 Expression.Property (i, "Left"),
177 Expression.Property (i, "Right")), i).Compile ();
179 var item = new Item<bool?> (true, false);
180 Assert.AreEqual ((bool?) true, and (item));
181 Assert.IsTrue (item.LeftCalled);
182 Assert.IsTrue (item.RightCalled);
186 public void OrIntTest ()
188 var a = Expression.Parameter (typeof (int), "a");
189 var b = Expression.Parameter (typeof (int), "b");
190 var or = Expression.Lambda<Func<int, int, int>> (
191 Expression.Or (a, b), a, b).Compile ();
193 Assert.AreEqual ((int?) 1, or (1, 1), "o1");
194 Assert.AreEqual ((int?) 1, or (1, 0), "o2");
195 Assert.AreEqual ((int?) 1, or (0, 1), "o3");
196 Assert.AreEqual ((int?) 0, or (0, 0), "o4");
200 public void OrIntNullableTest ()
202 var a = Expression.Parameter (typeof (int?), "a");
203 var b = Expression.Parameter (typeof (int?), "b");
204 var c = Expression.Lambda<Func<int?, int?, int?>> (
205 Expression.Or (a, b), a, b).Compile ();
207 Assert.AreEqual ((int?) 1, c (1, 1), "o1");
208 Assert.AreEqual ((int?) 1, c (1, 0), "o2");
209 Assert.AreEqual ((int?) 1, c (0, 1), "o3");
210 Assert.AreEqual ((int?) 0, c (0, 0), "o4");
212 Assert.AreEqual ((int?) null, c (1, null), "o5");
213 Assert.AreEqual ((int?) null, c (0, null), "o6");
214 Assert.AreEqual ((int?) null, c (null, 0), "o7");
215 Assert.AreEqual ((int?) null, c (1, null), "o8");
216 Assert.AreEqual ((int?) null, c (null, null), "o9");