Add precision score tests for new function

This commit is contained in:
Jeremy Wu
2019-09-29 13:27:28 +10:00
parent 2301478b3f
commit 9ab9ea4c2e

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@@ -24,12 +25,16 @@ namespace Wox.Test
}; };
public List<int> GetPrecisionScores() public List<int> GetPrecisionScores()
=> new List<int> {
{ var listToReturn = new List<int>();
0, //no precision
20, //low Enum.GetValues(typeof(StringMatcher.SearchPrecisionScore))
50 //regular .Cast<StringMatcher.SearchPrecisionScore>()
}; .ToList()
.ForEach(x => listToReturn.Add((int)x));
return listToReturn;
}
[Test] [Test]
public void MatchTest() public void MatchTest()
@@ -50,7 +55,7 @@ namespace Wox.Test
results.Add(new Result results.Add(new Result
{ {
Title = str, Title = str,
Score = FuzzyMatcher.Create("inst").Evaluate(str).Score Score = StringMatcher.FuzzySearch("inst", str, new MatchOption()).Score
}); });
} }
@@ -67,7 +72,7 @@ namespace Wox.Test
{ {
var compareString = "Can have rum only in my glass"; var compareString = "Can have rum only in my glass";
var scoreResult = FuzzyMatcher.Create(compareString).Evaluate(searchString).Score; var scoreResult = StringMatcher.FuzzySearch(searchString, compareString, new MatchOption()).Score;
Assert.True(scoreResult == 0); Assert.True(scoreResult == 0);
} }
@@ -75,7 +80,7 @@ namespace Wox.Test
//[TestCase("c", 50)] //[TestCase("c", 50)]
//[TestCase("ch", 50)] //[TestCase("ch", 50)]
//[TestCase("chr", 50)] [TestCase("chr")]
[TestCase("chrom")] [TestCase("chrom")]
[TestCase("chrome")] [TestCase("chrome")]
//[TestCase("chrom", 0)] //[TestCase("chrom", 0)]
@@ -91,19 +96,10 @@ namespace Wox.Test
results.Add(new Result results.Add(new Result
{ {
Title = str, Title = str,
Score = FuzzyMatcher.Create(searchTerm).Evaluate(str).Score Score = StringMatcher.FuzzySearch(searchTerm, str, new MatchOption()).Score
}); });
} }
StringMatcher.UserSettingSearchPrecision = "None";
StringMatcher.FuzzySearch(searchTerm, "blah", new MatchOption()).IsPreciciseMatch();
StringMatcher.UserSettingSearchPrecision = "Regular";
StringMatcher.FuzzySearch(searchTerm, "blah", new MatchOption()).IsPreciciseMatch();
StringMatcher.FuzzySearch(searchTerm, "blah", new MatchOption()).IsPreciciseMatch();
foreach (var precisionScore in GetPrecisionScores()) foreach (var precisionScore in GetPrecisionScores())
{ {
var filteredResult = results.Where(result => result.Score >= precisionScore).Select(result => result).OrderByDescending(x => x.Score).ToList(); var filteredResult = results.Where(result => result.Score >= precisionScore).Select(result => result).OrderByDescending(x => x.Score).ToList();
@@ -123,7 +119,7 @@ namespace Wox.Test
} }
[TestCase("chrome")] [TestCase("chrome")]
public void WhenGivenStringsForCalScoreMethodThenShouldAlwaysReturnSpecificScore(string searchTerm) public void WhenGivenStringsForCalScoreMethodThenShouldReturnCurrentScoring(string searchTerm)
{ {
var searchStrings = new List<string> var searchStrings = new List<string>
{ {
@@ -165,5 +161,22 @@ namespace Wox.Test
Assert.IsTrue(orderedResults[3].Score == 107 && orderedResults[3].Title == searchStrings[3]); Assert.IsTrue(orderedResults[3].Score == 107 && orderedResults[3].Title == searchStrings[3]);
Assert.IsTrue(orderedResults[4].Score == 0 && orderedResults[4].Title == searchStrings[4]); Assert.IsTrue(orderedResults[4].Score == 0 && orderedResults[4].Title == searchStrings[4]);
} }
[TestCase("chr", "Chrome", (int)StringMatcher.SearchPrecisionScore.Regular, true)]
[TestCase("chr", "Help cure hope raise on mind entity Chrome", (int)StringMatcher.SearchPrecisionScore.Regular, false)]
[TestCase("chr", "Help cure hope raise on mind entity Chrome", (int)StringMatcher.SearchPrecisionScore.Low, true)]
[TestCase("chr", "Candy Crush Saga from King", (int)StringMatcher.SearchPrecisionScore.Regular, false)]
[TestCase("chr", "Candy Crush Saga from King", (int)StringMatcher.SearchPrecisionScore.Low, true)]
public void WhenGivenDesiredPrecisionIsRegularShouldReturnAllResultsEqualGreaterThanRegular(string queryString, string compareString,
int expectedPrecisionScore, bool expectedPrecisionResult)
{
var expectedPrecisionString = (StringMatcher.SearchPrecisionScore)expectedPrecisionScore;
StringMatcher.UserSettingSearchPrecision = expectedPrecisionString.ToString();
var matchResult = StringMatcher.FuzzySearch(queryString, compareString, new MatchOption());
var matchScore = matchResult.Score;
var matchPrecisionResult = matchResult.IsPreciciseMatch();
Debug.WriteLine(matchScore);
Assert.IsTrue(matchPrecisionResult == expectedPrecisionResult);
}
} }
} }