/src/UnitTests/Finders/FinderTests.cs
C# | 161 lines | 94 code | 37 blank | 30 comment | 1 complexity | fbfb4781348d17e042731b1df5de0ad5 MD5 | raw file
1using System.Collections.Generic; 2using LinFu.Finders; 3using LinFu.Finders.Interfaces; 4using Moq; 5using Xunit; 6 7namespace LinFu.UnitTests.Finders 8{ 9 public class FinderTests : BaseTestFixture 10 { 11 private Mock<ICriteria<object>> GetMockCriteria(bool predicateResult, CriteriaType criteriaType, int weight) 12 { 13 var criteria = new Mock<ICriteria<object>>(); 14 criteria.Expect(c => c.Predicate).Returns(predicate => predicateResult); 15 criteria.Expect(c => c.Type).Returns(criteriaType); 16 criteria.Expect(c => c.Weight).Returns(weight); 17 return criteria; 18 } 19 20 [Fact] 21 public void ShouldBeAbleToAddCriteriaToList() 22 { 23 // Return a predicate that always returns true 24 var mockCriteria = new Mock<ICriteria<object>>(); 25 var criteria = mockCriteria.Object; 26 27 var mockFuzzyItem = new Mock<IFuzzyItem<object>>(); 28 var fuzzyItem = mockFuzzyItem.Object; 29 30 // The Test method must be called on the fuzzy item 31 mockFuzzyItem.Expect(fuzzy => fuzzy.Test(criteria)); 32 33 // Initialize the list of fuzzy items 34 var fuzzyList = new List<IFuzzyItem<object>>(); 35 fuzzyList.Add(fuzzyItem); 36 37 // Apply the criteria 38 fuzzyList.AddCriteria(criteria); 39 40 mockCriteria.VerifyAll(); 41 mockFuzzyItem.VerifyAll(); 42 } 43 44 [Fact] 45 public void ShouldBeAbleToAddLambdasAsCriteria() 46 { 47 var fuzzyList = new List<IFuzzyItem<int>>(); 48 fuzzyList.Add(5); 49 50 // Directly apply the predicate instead of 51 // having to manually construct the criteria 52 fuzzyList.AddCriteria(item => item == 5); 53 54 Assert.Equal(5, fuzzyList.BestMatch().Item); 55 } 56 57 [Fact] 58 public void ShouldBeAbleToDetermineBestFuzzyMatch() 59 { 60 var mockFuzzyItem = new Mock<IFuzzyItem<object>>(); 61 var fuzzyItem = mockFuzzyItem.Object; 62 63 // This should be the best match 64 mockFuzzyItem.Expect(f => f.Confidence).Returns(.8); 65 66 var otherMockFuzzyItem = new Mock<IFuzzyItem<object>>(); 67 var fauxFuzzyItem = otherMockFuzzyItem.Object; 68 69 // This fuzzy item should be ignored since it has 70 // a lower confidence rate 71 otherMockFuzzyItem.Expect(f => f.Confidence).Returns(.1); 72 73 var fuzzyList = new List<IFuzzyItem<object>> {fuzzyItem, fauxFuzzyItem}; 74 75 var bestMatch = fuzzyList.BestMatch(); 76 Assert.Same(bestMatch, fuzzyItem); 77 } 78 79 [Fact] 80 public void ShouldBeAbleToIgnoreFailedOptionalCriteria() 81 { 82 // The criteria will be set up to fail by default 83 var falseCriteria = GetMockCriteria(false, CriteriaType.Optional, 1); 84 85 // Use the true criteria as the control result 86 var trueCriteria = GetMockCriteria(true, CriteriaType.Optional, 1); 87 88 var fuzzyList = new List<IFuzzyItem<object>>(); 89 var fuzzyItem = new FuzzyItem<object>(new object()); 90 91 fuzzyList.Add(fuzzyItem); 92 93 // Apply the criteria 94 fuzzyList.AddCriteria(trueCriteria.Object); 95 fuzzyList.AddCriteria(falseCriteria.Object); 96 97 // The score must be left unchanged 98 // since the criteria is optional and 99 // the failed predicate does not count 100 // against the current fuzzy item. 101 Assert.Equal(1.0, fuzzyItem.Confidence); 102 } 103 104 [Fact] 105 public void ShouldNotBeAbleToIgnoreFailedCriticalCriteria() 106 { 107 var fuzzyList = new List<IFuzzyItem<object>>(); 108 var fuzzyItem = new FuzzyItem<object>(new object()); 109 110 fuzzyList.Add(fuzzyItem); 111 112 var trueCriteria = GetMockCriteria(true, CriteriaType.Standard, 2); 113 var failedCriteria = GetMockCriteria(false, CriteriaType.Critical, 1); 114 115 // Boost the first item results so that the best match 116 // should be biased towards the first item 117 fuzzyItem.Test(trueCriteria.Object); 118 119 // Make both items pass the first test 120 var secondItem = new FuzzyItem<object>(new object()); 121 fuzzyList.Add(secondItem); 122 123 fuzzyList.AddCriteria(trueCriteria.Object); 124 125 // The first item should be the best match at this point 126 var bestMatch = fuzzyList.BestMatch(); 127 Assert.Same(bestMatch, fuzzyItem); 128 129 // Remove the second item from the list to avoid the 130 // failed critical match 131 fuzzyList.Remove(secondItem); 132 133 // The failed critical criteria should eliminate 134 // the first match as the best possible match 135 fuzzyList.AddCriteria(failedCriteria.Object); 136 137 // Reinsert the second value into the list 138 // so that it can be chosen as the best match 139 fuzzyList.Add(secondItem); 140 141 // Run the test again 142 bestMatch = fuzzyList.BestMatch(); 143 144 // The second item should be the best possible match, 145 // and the first item should be ignored 146 // because of the failed criteria 147 Assert.Same(bestMatch, secondItem); 148 } 149 150 [Fact] 151 public void ShouldReturnNullIfAllMatchScoresAreZero() 152 { 153 var fuzzyItem = new FuzzyItem<object>(new object()); 154 var fuzzyList = new List<IFuzzyItem<object>> {fuzzyItem}; 155 156 var bestMatch = fuzzyList.BestMatch(); 157 158 Assert.Null(bestMatch); 159 } 160 } 161}