Permalink
Browse files

fixed issue with user-defined type conversion operator

  • Loading branch information...
1 parent b70d49a commit 8f22cd8d44bda87c9f204b9ea1db229b8a69a9f8 @tgrassau tgrassau committed Nov 16, 2012
@@ -178,6 +178,56 @@ public void Can_select_unary_cast_variable_expression()
CollectionAssert.Contains(actual, expected);
}
+ [Test]
+ public void Can_select_unary_cast_variable_with_explicit_userdefined_type_conversion_expression()
+ {
+ // ReSharper disable ConvertToConstant.Local
+ var intVal = 12;
+ // ReSharper restore ConvertToConstant.Local
+
+ var expected = new TestType()
+ {
+ IntColumn = 12,
+ BoolColumn = true,
+ StringColumn = "test"
+ };
+
+ EstablishContext(10, expected);
+
+ var value = new IntWrapper(intVal);
+
+ var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == (int)value);
+
+ Assert.IsNotNull(actual);
+ Assert.AreEqual(1, actual.Count);
+ CollectionAssert.Contains(actual, expected);
+ }
+
+ [Test]
+ public void Can_select_unary_cast_variable_with_implicit_userdefined_type_conversion_expression()
+ {
+ // ReSharper disable ConvertToConstant.Local
+ var intVal = 12;
+ // ReSharper restore ConvertToConstant.Local
+
+ var expected = new TestType()
+ {
+ IntColumn = 12,
+ BoolColumn = true,
+ StringColumn = "test"
+ };
+
+ EstablishContext(10, expected);
+
+ var value = new IntWrapper(intVal);
+
+ var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == value);
+
+ Assert.IsNotNull(actual);
+ Assert.AreEqual(1, actual.Count);
+ CollectionAssert.Contains(actual, expected);
+ }
+
#endregion
#region method
@@ -222,4 +272,24 @@ public void Can_select_unary_cast_method_expression()
#endregion
}
+
+ public struct IntWrapper
+ {
+ private readonly int _id;
+
+ public IntWrapper(int projectId)
+ {
+ _id = projectId;
+ }
+
+ public static explicit operator IntWrapper(int value)
+ {
+ return new IntWrapper(value);
+ }
+
+ public static implicit operator int(IntWrapper value)
+ {
+ return value._id;
+ }
+ }
}
@@ -773,9 +773,13 @@ protected virtual object VisitUnary(UnaryExpression u)
o = o + "=" + GetQuotedTrueValue();
return new PartialSqlString("NOT (" + o + ")");
- default:
- return Visit(u.Operand);
- }
+ case ExpressionType.Convert:
+ if (u.Method != null)
+ return Expression.Lambda(u).Compile().DynamicInvoke();
+ break;
+ }
+
+ return Visit(u.Operand);
}
@@ -178,6 +178,56 @@ public void Can_select_unary_cast_variable_expression()
CollectionAssert.Contains(actual, expected);
}
+ [Test]
+ public void Can_select_unary_cast_variable_with_explicit_userdefined_type_conversion_expression()
+ {
+ // ReSharper disable ConvertToConstant.Local
+ var intVal = 12;
+ // ReSharper restore ConvertToConstant.Local
+
+ var expected = new TestType()
+ {
+ IntColumn = 12,
+ BoolColumn = true,
+ StringColumn = "test"
+ };
+
+ EstablishContext(10, expected);
+
+ var value = new IntWrapper(intVal);
+
+ var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == (int)value);
+
+ Assert.IsNotNull(actual);
+ Assert.AreEqual(1, actual.Count);
+ CollectionAssert.Contains(actual, expected);
+ }
+
+ [Test]
+ public void Can_select_unary_cast_variable_with_implicit_userdefined_type_conversion_expression()
+ {
+ // ReSharper disable ConvertToConstant.Local
+ var intVal = 12;
+ // ReSharper restore ConvertToConstant.Local
+
+ var expected = new TestType()
+ {
+ IntColumn = 12,
+ BoolColumn = true,
+ StringColumn = "test"
+ };
+
+ EstablishContext(10, expected);
+
+ var value = new IntWrapper(intVal);
+
+ var actual = ConnectionString.OpenDbConnection().Select<TestType>(q => q.IntColumn == value);
+
+ Assert.IsNotNull(actual);
+ Assert.AreEqual(1, actual.Count);
+ CollectionAssert.Contains(actual, expected);
+ }
+
#endregion
#region method
@@ -222,4 +272,25 @@ public void Can_select_unary_cast_method_expression()
#endregion
}
+
+ public struct IntWrapper
+ {
+ private readonly int _id;
+
+ public IntWrapper(int projectId)
+ {
+ _id = projectId;
+ }
+
+ public static explicit operator IntWrapper(int value)
+ {
+ return new IntWrapper(value);
+ }
+
+ public static implicit operator int(IntWrapper value)
+ {
+ return value._id;
+ }
+ }
+
}

0 comments on commit 8f22cd8

Please sign in to comment.