Esempio n. 1
0
		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());
		}
Esempio n. 2
0
        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());
        }
Esempio n. 3
0
        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());
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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());
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
        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]));
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        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());
        }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
        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());
        }
Esempio n. 20
0
        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);
        }
Esempio n. 21
0
        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());
        }