Skip to content

Commit e2482a6

Browse files
committed
Merge pull request #160 from tgmayfield/NH-3293
NH-3293 - Expand what gets ToString() called on it in SimpleExpression.ToString()
2 parents 796acf5 + 41568cb commit e2482a6

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/NHibernate.Test/NHSpecificTest/NH2982/Fixture.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
23
using NHibernate.Criterion;
34

45
namespace NHibernate.Test.NHSpecificTest.NH2982
@@ -65,5 +66,29 @@ public void SimpleExpressionWithPrimitive()
6566
var restriction = Restrictions.Eq("A", 5);
6667
Assert.AreEqual("A = 5", restriction.ToString());
6768
}
69+
70+
[Test]
71+
public void SimpleExpressionWithNullablePrimitive()
72+
{
73+
int? value = null;
74+
value = 5;
75+
var restriction = Restrictions.Eq("A", value);
76+
Assert.AreEqual("A = 5", restriction.ToString());
77+
}
78+
79+
[Test]
80+
public void SimpleExpressionWithString()
81+
{
82+
var restriction = Restrictions.Like("A", "Test");
83+
Assert.AreEqual("A like Test", restriction.ToString());
84+
}
85+
86+
[Test]
87+
public void SimpleExpressionWithNullableDate()
88+
{
89+
DateTime? date = new DateTime(2012, 1, 1);
90+
var restriction = Restrictions.Eq("A", date);
91+
Assert.AreEqual("A = " + date, restriction.ToString());
92+
}
6893
}
6994
}

src/NHibernate/Criterion/SimpleExpression.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,24 @@ protected virtual string Op
173173
get { return op; }
174174
}
175175

176-
string ValueToStrings()
176+
private static readonly System.Type[] CallToStringTypes = new[]
177177
{
178-
if(value!=null && value.GetType().IsPrimitive)
178+
typeof(DateTime),
179+
typeof(string),
180+
};
181+
182+
private string ValueToStrings()
183+
{
184+
if (value == null)
185+
{
186+
return "null";
187+
}
188+
var type = value.GetType();
189+
if (type.IsPrimitive || CallToStringTypes.Any(t => t.IsAssignableFrom(type)))
179190
{
180191
return value.ToString();
181192
}
193+
182194
return ObjectHelpers.IdentityToString(value);
183195
}
184196
}

0 commit comments

Comments
 (0)