PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 2ms app.codeStats 0ms

/src/UnitTests/Finders/FinderTests.cs

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