public void DoIt() { var filter = new ThingFilter<Subject>() .MatchOn(s => s.Prop1) .MatchOn(s => s.Value) .MatchOn(s => s.Deep) .MatchOn(s => s.Deep?.Value); var collection = new List<Subject> { new Subject { Prop1 = "Prop1", Value = 5, Exclude = "Prop2" }, new Subject { Prop1 = "Prop1a", Value = 6, Exclude = "Prop2" }, new Subject { Prop1 = "Prop3", Exclude = "Prop1", Deep = new Subject {Value = 6} }, new Subject { Prop1 = "Prop4", Value = 8, Exclude = "Prop1" } }; var filtered = filter.Apply(collection, "prop1 6"); Assert.AreEqual(3, filtered.Count()); collection.Add(new Subject { Prop1 = "Prop5", Value = 6 }); Assert.AreEqual(4, filtered.Count()); var sorted = filter.Apply(collection, "prop1 6").OrderByDescending(r => r.Score); Assert.AreSame(collection[1], sorted.First().Item); var typeFilter = filter.Apply(collection, "ubj"); Assert.AreEqual(1, typeFilter.Count()); }
public void EqualToUntagged() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, "prop") .MatchOn(s => s.Exclude); var collection = new List <Subject> { new Subject { Prop1 = "prop1", Exclude = "temp" }, new Subject { Prop1 = "prop1a", Exclude = "temp" }, new Subject { Prop1 = "temp", Exclude = "prop1a" }, new Subject { Prop1 = "temp", Exclude = "prop1" } }; var filtered = filter.Apply(collection, "=prop1"); Assert.AreEqual(2, filtered.Count()); }
public void NumericSearch() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Value) .MatchOn(s => s.Deep?.Value); var collection = new List <Subject> { new Subject { Value = 5, }, new Subject { Value = 6, }, new Subject { Deep = new Subject { Value = 6 } }, new Subject { Value = 8, } }; var filtered = filter.Apply(collection, "6.0"); Assert.AreEqual(2, filtered.Count()); }
public void QuotedNullQuery() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, "prop"); var collection = new List <Subject> { new Subject { Prop1 = "value" }, new Subject { Prop1 = null }, new Subject { Prop1 = null }, new Subject { Prop1 = "null" }, }; var filtered = filter.Apply(collection, "prop=\"null\""); Assert.AreEqual(1, filtered.Count()); Assert.AreEqual(1, filtered.Last().Score); }
public void AndQuery() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, weight: 2) .MatchOn(s => s.Exclude); var collection = new List <Subject> { new Subject { Prop1 = "prop1", Exclude = "prop2" }, new Subject { Prop1 = "prop1", Exclude = "no" }, new Subject { Prop1 = "no", Exclude = "prop2" }, new Subject { Exclude = "and" }, }; var filtered = filter.Apply(collection, "prop1 AND prop2"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[0], filtered.First().Item); Assert.AreEqual(3, filtered.First().Score); }
public void EqualToString() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, "prop"); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "prop1a" } }; var filtered = filter.Apply(collection, "prop=prop1"); Assert.AreEqual(1, filtered.Count()); }
public void EqualToBooleanFalse() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Bool, "prop"); var collection = new List <Subject> { new Subject { Bool = true }, new Subject { Bool = false }, }; var filtered = filter.Apply(collection, "prop=false"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[1], filtered.First().Item); }
public void GreaterThanNumber() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Value, "prop"); var collection = new List <Subject> { new Subject { Value = 5 }, new Subject { Value = 15 } }; var filtered = filter.Apply(collection, "prop>10"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[1], filtered.First().Item); }
public void GreaterThanString() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, "prop"); var collection = new List <Subject> { new Subject { Prop1 = "abc" }, new Subject { Prop1 = "xyz" } }; var filtered = filter.Apply(collection, "prop>prop1"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[1], filtered.First().Item); }
public void NotEqualToNumber() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Value, "prop"); var collection = new List <Subject> { new Subject { Value = 5 }, new Subject { Value = 6 }, }; var filtered = filter.Apply(collection, "prop<>6"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[0], filtered.First().Item); }
public void StringSearch() { var filter = new ThingFilter <Subject>() .MatchOnAll(); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "don't match" } }; var filtered = filter.Apply(collection, "prop1"); Assert.AreEqual(1, filtered.Count()); Assert.AreEqual(1, filtered.Last().Score); }
public void RequiredTag() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, "prop", true) .MatchOn(s => s.Exclude); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Exclude = "Prop1a" } }; var filtered = filter.Apply(collection, "prop:prop1"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[0], filtered.First().Item); }
public void CaseSensitive() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .CaseSensitive(); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "Prop1a" } }; var filtered = filter.Apply(collection, "prop1"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[0], filtered.First().Item); }
public void Unmatched() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .IncludeUnmatched(); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "don't match" } }; var filtered = filter.Apply(collection, "prop1"); Assert.AreEqual(2, filtered.Count()); Assert.AreEqual(0, filtered.Last().Score); }
public void TaggedSearch() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .MatchOn(s => s.Deep?.Prop1, "nest"); var collection = new List <Subject> { new Subject { Prop1 = "Prop1", Value = 5, Exclude = "Prop2" }, new Subject { Prop1 = "Prop1a", Value = 6, Exclude = "Prop2" }, new Subject { Prop1 = "Prop3", Exclude = "Prop1", Deep = new Subject { Prop1 = "test" } }, new Subject { Prop1 = "test", Value = 8, Exclude = "Prop1" } }; var results = filter.Apply(collection, "prop1 nest:te"); Assert.AreEqual(3, results.Count()); Assert.IsTrue(results.Select(r => r.Item).Contains(collection[2])); Assert.IsFalse(results.Select(r => r.Item).Contains(collection[3])); }
public void WeightingApplies() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1, weight: 2) .MatchOn(s => s.Exclude); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Exclude = "prop1" } }; var filtered = filter.Apply(collection, "prop"); Assert.AreEqual(2, filtered.Count()); Assert.AreSame(collection[0], filtered.OrderByDescending(s => s.Score).First().Item); Assert.AreEqual(2, filtered.OrderByDescending(s => s.Score).First().Score); }
public void CanRemoveMatchEvaluator() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .RemoveEvaluator("="); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "Propane" }, new Subject { Prop1 = "Gas" }, }; var filtered = filter.Apply(collection, "=prop1"); Assert.AreEqual(0, filtered.Count()); }
public void QuotedStringMatch() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1); var collection = new List <Subject> { new Subject { Prop1 = "Prop1", Value = 5, Exclude = "Prop2" }, new Subject { Prop1 = "Prop1 and Prop2", Value = 6, Exclude = "Prop2" }, new Subject { Prop1 = "Prop3", Exclude = "Prop1", Deep = new Subject { Value = 6 } }, new Subject { Prop1 = "Prop4", Value = 8, Exclude = "Prop1" } }; var results = filter.Apply(collection, "\"prop1 and\" prop4"); Assert.AreEqual(2, results.Count()); Assert.AreSame(collection[1], results.First().Item); }
public void NotAnAndQuery() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .MatchOn(s => s.Exclude); var collection = new List <Subject> { new Subject { Prop1 = "prop1", Exclude = "prop2" }, new Subject { Exclude = "and" }, new Subject { Prop1 = "no", Exclude = "prop2" }, }; var filtered = filter.Apply(collection, "prop1 and prop2"); Assert.AreEqual(3, filtered.Count()); }
public void CanAddCustomMatchEvaluator() { var filter = new ThingFilter <Subject>() .MatchOn(s => s.Prop1) .AddEvaluator(new StartsWithMatchEvaluator()); var collection = new List <Subject> { new Subject { Prop1 = "prop1" }, new Subject { Prop1 = "secondProp" }, new Subject { Prop1 = "something else" }, }; var filtered = filter.Apply(collection, "%prop"); Assert.AreEqual(1, filtered.Count()); Assert.AreSame(collection[0], filtered.First().Item); }
public void BooleanSearch() { var filter = new ThingFilter <Subject>() .MatchOnAll(); var collection = new List <Subject> { new Subject { Bool = false }, new Subject { Bool = true }, new Subject { Bool = true }, new Subject { Bool = false } }; var filtered = filter.Apply(collection, "true"); Assert.AreEqual(2, filtered.Count()); }