Skip to content

Commit 139febe

Browse files
committed
NH-3807 - Handle new overloads of String.Trim*() in .NET Core 2.0.
1 parent b64a4c0 commit 139febe

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/NHibernate.Test/Async/Linq/FunctionTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ public async Task TrimAsync()
323323
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimStart('h') == "eh")));
324324
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimEnd('h') == "he")));
325325

326+
// Check when passed as array (new overloads in .NET Core App 2.0).
327+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.Trim(new [] { 'h' }) == "e")));
328+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimStart(new[] { 'h' }) == "eh")));
329+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimEnd(new[] { 'h' }) == "he")));
330+
326331
// Let it rollback to get rid of temporary changes.
327332
}
328333
}

src/NHibernate.Test/Linq/FunctionTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ public void Trim()
305305
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimStart('h') == "eh"));
306306
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimEnd('h') == "he"));
307307

308+
// Check when passed as array (new overloads in .NET Core App 2.0).
309+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.Trim(new [] { 'h' }) == "e"));
310+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimStart(new[] { 'h' }) == "eh"));
311+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimEnd(new[] { 'h' }) == "he"));
312+
308313
// Let it rollback to get rid of temporary changes.
309314
}
310315
}

src/NHibernate/Linq/Functions/StringGenerator.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NHibernate.Hql.Ast;
77
using NHibernate.Linq.Visitors;
88
using NHibernate.Util;
9+
using System.Linq;
910

1011
namespace NHibernate.Linq.Functions
1112
{
@@ -241,13 +242,7 @@ public class TrimGenerator : BaseHqlGeneratorForMethod
241242
{
242243
public TrimGenerator()
243244
{
244-
SupportedMethods = new[]
245-
{
246-
ReflectHelper.GetMethodDefinition<string>(s => s.Trim()),
247-
ReflectHelper.GetMethodDefinition<string>(s => s.Trim('a')),
248-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimStart('a')),
249-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimEnd('a'))
250-
};
245+
SupportedMethods = typeof(string).GetMethods().Where(x => new[] { "Trim", "TrimStart", "TrimEnd" }.Contains(x.Name)).ToArray();
251246
}
252247

253248
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
@@ -262,8 +257,20 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
262257

263258
string trimChars = "";
264259
if (method.GetParameters().Length > 0)
265-
foreach (char c in (char[])((ConstantExpression)arguments[0]).Value)
266-
trimChars += c;
260+
{
261+
object argumentValue = ((ConstantExpression)arguments[0]).Value;
262+
if (argumentValue is char)
263+
{
264+
trimChars += (char)argumentValue;
265+
}
266+
else
267+
{
268+
foreach (char c in (char[])argumentValue)
269+
{
270+
trimChars += c;
271+
}
272+
}
273+
}
267274

268275

269276
if (trimChars == "")

0 commit comments

Comments
 (0)