Skip to content

Commit 4240379

Browse files
ngbrownhazzik
authored andcommitted
Handle all overloads of String.Trim*()
Prevents mismatch between netstandard2.0 and netcoreapp2.0. Fixes #1640
1 parent 4d26936 commit 4240379

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ 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
327+
// (the single character parameter is a new overload in .netcoreapp2.0, but not net461 or .netstandard2.0).
328+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.Trim(new [] { 'h' }) == "e")));
329+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimStart(new[] { 'h' }) == "eh")));
330+
Assert.AreEqual(1, await (session.Query<AnotherEntity>().CountAsync(e => e.Input.TrimEnd(new[] { 'h' }) == "he")));
331+
326332
// Let it rollback to get rid of temporary changes.
327333
}
328334
}

src/NHibernate.Test/Linq/FunctionTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ 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
309+
// (the single character parameter is a new overload in .netcoreapp2.0, but not net461 or .netstandard2.0).
310+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.Trim(new [] { 'h' }) == "e"));
311+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimStart(new[] { 'h' }) == "eh"));
312+
Assert.AreEqual(1, session.Query<AnotherEntity>().Count(e => e.Input.TrimEnd(new[] { 'h' }) == "he"));
313+
308314
// Let it rollback to get rid of temporary changes.
309315
}
310316
}

src/NHibernate/Linq/Functions/StringGenerator.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Reflection;
67
using NHibernate.Hql.Ast;
@@ -241,18 +242,7 @@ public class TrimGenerator : BaseHqlGeneratorForMethod
241242
{
242243
public TrimGenerator()
243244
{
244-
SupportedMethods = new HashSet<MethodInfo>
245-
{
246-
ReflectHelper.GetMethodDefinition<string>(s => s.Trim()),
247-
ReflectHelper.GetMethodDefinition<string>(s => s.Trim('a')),
248-
ReflectHelper.GetMethodDefinition<string>(s => s.Trim('a', 'a')),
249-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimStart()),
250-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimStart('a')),
251-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimStart('a', 'a')),
252-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimEnd()),
253-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimEnd('a')),
254-
ReflectHelper.GetMethodDefinition<string>(s => s.TrimEnd('a', 'a'))
255-
};
245+
SupportedMethods = typeof(string).GetMethods().Where(x => new[] { "Trim", "TrimStart", "TrimEnd" }.Contains(x.Name)).ToArray();
256246
}
257247

258248
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)

0 commit comments

Comments
 (0)