dotnet-dynamic-linq/PoweredSoft.DynamicLinq.Test/StringComparision.cs

128 lines
5.5 KiB
C#
Raw Normal View History

2018-03-06 20:43:49 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq;
2018-03-06 20:43:49 -05:00
using PoweredSoft.DynamicLinq.Test.Helpers;
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class StringComparisionTests
{
2018-03-06 23:40:01 -05:00
internal List<MockPersonObject> Persons => TestData.Persons;
2018-03-06 20:43:49 -05:00
[TestMethod]
public void Equal()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.Equal("FirstName", "David", stringComparision: null));
b = Persons.AsQueryable().Where(t => t.FirstName == "David");
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.Equal("FirstName", "DAVID", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.Equals("DAVID", StringComparison.OrdinalIgnoreCase));
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
[TestMethod]
public void NotEqual()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "David", stringComparision: null));
b = Persons.AsQueryable().Where(t => t.FirstName != "David");
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "DAVID", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => !t.FirstName.Equals("DAVID", StringComparison.OrdinalIgnoreCase));
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
[TestMethod]
public void Contains()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.Contains("FirstName", "vi", stringComparision: null));
b = Persons.AsQueryable().Where(t => t.FirstName.Contains("vi"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.Contains("FirstName", "VI", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.IndexOf("VI", StringComparison.OrdinalIgnoreCase) > -1);
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
2019-03-19 17:54:48 -04:00
[TestMethod]
public void NotContains()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "vi", stringComparision: null));
b = Persons.AsQueryable().Where(t => !t.FirstName.Contains("vi"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "VI", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.IndexOf("VI", StringComparison.OrdinalIgnoreCase) == -1);
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
[TestMethod]
public void NegateNotContains()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "vi", stringComparision: null, negate: true));
b = Persons.AsQueryable().Where(t => !!t.FirstName.Contains("vi"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "VI", stringComparision: StringComparison.OrdinalIgnoreCase, negate: true));
b = Persons.AsQueryable().Where(t => !(t.FirstName.IndexOf("VI", StringComparison.OrdinalIgnoreCase) == -1));
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
2018-03-06 20:43:49 -05:00
[TestMethod]
public void StartsWith()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "Da", stringComparision: null));
b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("Da"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "DA", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("DA", StringComparison.OrdinalIgnoreCase));
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
[TestMethod]
public void EndsWith()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.EndsWith("FirstName", "vid", stringComparision: null));
b = Persons.AsQueryable().Where(t => t.FirstName.EndsWith("vid"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.EndsWith("FirstName", "VID", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.EndsWith("VID", StringComparison.OrdinalIgnoreCase));
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
}
}