2019-09-29 11:27:28 +08:00
|
|
|
using System;
|
2019-09-28 22:09:03 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2014-02-09 00:33:10 +08:00
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using Wox.Infrastructure;
|
2016-01-07 05:34:42 +08:00
|
|
|
using Wox.Plugin;
|
2014-02-09 00:33:10 +08:00
|
|
|
|
|
|
|
namespace Wox.Test
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class FuzzyMatcherTest
|
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
private const string Chrome = "Chrome";
|
|
|
|
private const string CandyCrushSagaFromKing = "Candy Crush Saga from King";
|
|
|
|
private const string HelpCureHopeRaiseOnMindEntityChrome = "Help cure hope raise on mind entity Chrome";
|
|
|
|
private const string UninstallOrChangeProgramsOnYourComputer = "Uninstall or change programs on your computer";
|
|
|
|
private const string LastIsChrome = "Last is chrome";
|
|
|
|
private const string OneOneOneOne = "1111";
|
|
|
|
private const string MicrosoftSqlServerManagementStudio = "Microsoft SQL Server Management Studio";
|
|
|
|
|
2019-12-10 04:08:17 +08:00
|
|
|
public List<string> GetSearchStrings()
|
2019-09-28 22:09:03 +08:00
|
|
|
=> new List<string>
|
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
Chrome,
|
2019-09-28 22:09:03 +08:00
|
|
|
"Choose which programs you want Windows to use for activities like web browsing, editing photos, sending e-mail, and playing music.",
|
2019-12-30 07:13:33 +08:00
|
|
|
HelpCureHopeRaiseOnMindEntityChrome,
|
|
|
|
CandyCrushSagaFromKing,
|
|
|
|
UninstallOrChangeProgramsOnYourComputer,
|
2019-09-28 22:09:03 +08:00
|
|
|
"Add, change, and manage fonts on your computer",
|
2019-12-30 07:13:33 +08:00
|
|
|
LastIsChrome,
|
|
|
|
OneOneOneOne
|
2019-09-28 22:09:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
public List<int> GetPrecisionScores()
|
2019-09-29 11:27:28 +08:00
|
|
|
{
|
|
|
|
var listToReturn = new List<int>();
|
|
|
|
|
|
|
|
Enum.GetValues(typeof(StringMatcher.SearchPrecisionScore))
|
|
|
|
.Cast<StringMatcher.SearchPrecisionScore>()
|
|
|
|
.ToList()
|
|
|
|
.ForEach(x => listToReturn.Add((int)x));
|
|
|
|
|
|
|
|
return listToReturn;
|
|
|
|
}
|
2019-09-28 22:09:03 +08:00
|
|
|
|
2014-02-09 00:33:10 +08:00
|
|
|
[Test]
|
|
|
|
public void MatchTest()
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
var sources = new List<string>
|
2014-02-09 00:33:10 +08:00
|
|
|
{
|
|
|
|
"file open in browser-test",
|
|
|
|
"Install Package",
|
|
|
|
"add new bsd",
|
|
|
|
"Inste",
|
2016-01-07 05:34:42 +08:00
|
|
|
"aac"
|
2014-02-09 00:33:10 +08:00
|
|
|
};
|
|
|
|
|
2016-01-07 05:34:42 +08:00
|
|
|
var results = new List<Result>();
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher();
|
2014-02-09 00:33:10 +08:00
|
|
|
foreach (var str in sources)
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
results.Add(new Result
|
2014-02-09 00:33:10 +08:00
|
|
|
{
|
|
|
|
Title = str,
|
2020-01-20 07:06:16 +08:00
|
|
|
Score = matcher.FuzzyMatch("inst", str).RawScore
|
2014-02-09 00:33:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
results = results.Where(x => x.Score > 0).OrderByDescending(x => x.Score).ToList();
|
|
|
|
|
|
|
|
Assert.IsTrue(results.Count == 3);
|
|
|
|
Assert.IsTrue(results[0].Title == "Inste");
|
|
|
|
Assert.IsTrue(results[1].Title == "Install Package");
|
|
|
|
Assert.IsTrue(results[2].Title == "file open in browser-test");
|
|
|
|
}
|
2019-09-28 22:09:03 +08:00
|
|
|
|
|
|
|
[TestCase("Chrome")]
|
|
|
|
public void WhenGivenNotAllCharactersFoundInSearchStringThenShouldReturnZeroScore(string searchString)
|
|
|
|
{
|
|
|
|
var compareString = "Can have rum only in my glass";
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher();
|
|
|
|
var scoreResult = matcher.FuzzyMatch(searchString, compareString).RawScore;
|
2019-09-28 22:09:03 +08:00
|
|
|
|
|
|
|
Assert.True(scoreResult == 0);
|
|
|
|
}
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2019-09-29 11:27:28 +08:00
|
|
|
[TestCase("chr")]
|
2019-09-28 22:09:03 +08:00
|
|
|
[TestCase("chrom")]
|
2019-12-30 07:13:33 +08:00
|
|
|
[TestCase("chrome")]
|
2019-09-30 19:51:15 +08:00
|
|
|
[TestCase("cand")]
|
|
|
|
[TestCase("cpywa")]
|
2019-09-28 22:09:03 +08:00
|
|
|
[TestCase("ccs")]
|
|
|
|
public void WhenGivenStringsAndAppliedPrecisionFilteringThenShouldReturnGreaterThanPrecisionScoreResults(string searchTerm)
|
|
|
|
{
|
|
|
|
var results = new List<Result>();
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher();
|
2019-09-28 22:09:03 +08:00
|
|
|
foreach (var str in GetSearchStrings())
|
|
|
|
{
|
|
|
|
results.Add(new Result
|
|
|
|
{
|
|
|
|
Title = str,
|
2020-01-20 07:06:16 +08:00
|
|
|
Score = matcher.FuzzyMatch(searchTerm, str).Score
|
2019-09-28 22:09:03 +08:00
|
|
|
});
|
2019-12-30 07:13:33 +08:00
|
|
|
}
|
2019-09-28 22:09:03 +08:00
|
|
|
|
|
|
|
foreach (var precisionScore in GetPrecisionScores())
|
|
|
|
{
|
|
|
|
var filteredResult = results.Where(result => result.Score >= precisionScore).Select(result => result).OrderByDescending(x => x.Score).ToList();
|
|
|
|
|
|
|
|
Debug.WriteLine("");
|
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine("SEARCHTERM: " + searchTerm + ", GreaterThanSearchPrecisionScore: " + precisionScore);
|
|
|
|
foreach (var item in filteredResult)
|
|
|
|
{
|
|
|
|
Debug.WriteLine("SCORE: " + item.Score.ToString() + ", FoundString: " + item.Title);
|
|
|
|
}
|
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine("");
|
|
|
|
|
|
|
|
Assert.IsFalse(filteredResult.Any(x => x.Score < precisionScore));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 04:36:53 +08:00
|
|
|
[TestCase(Chrome, Chrome, 137)]
|
|
|
|
[TestCase(Chrome, LastIsChrome, 83)]
|
2020-01-07 17:26:26 +08:00
|
|
|
[TestCase(Chrome, HelpCureHopeRaiseOnMindEntityChrome, 21)]
|
|
|
|
[TestCase(Chrome, UninstallOrChangeProgramsOnYourComputer, 15)]
|
|
|
|
[TestCase(Chrome, CandyCrushSagaFromKing, 0)]
|
|
|
|
[TestCase("sql", MicrosoftSqlServerManagementStudio, 56)]
|
2020-01-14 04:36:53 +08:00
|
|
|
[TestCase("sql manag", MicrosoftSqlServerManagementStudio, 79)]//double spacing intended
|
2020-01-07 17:26:26 +08:00
|
|
|
public void WhenGivenQueryStringThenShouldReturnCurrentScoring(string queryString, string compareString, int expectedScore)
|
2019-09-28 22:09:03 +08:00
|
|
|
{
|
2020-01-07 17:26:26 +08:00
|
|
|
// When, Given
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher();
|
|
|
|
var rawScore = matcher.FuzzyMatch(queryString, compareString).RawScore;
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2020-01-07 17:26:26 +08:00
|
|
|
// Should
|
|
|
|
Assert.AreEqual(expectedScore, rawScore, $"Expected score for compare string '{compareString}': {expectedScore}, Actual: {rawScore}");
|
2019-09-28 22:09:03 +08:00
|
|
|
}
|
2019-09-29 12:27:07 +08:00
|
|
|
|
2020-01-07 19:30:36 +08:00
|
|
|
[TestCase("goo", "Google Chrome", StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("chr", "Google Chrome", StringMatcher.SearchPrecisionScore.Low, true)]
|
|
|
|
[TestCase("chr", "Chrome", StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("chr", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("chr", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Low, true)]
|
|
|
|
[TestCase("chr", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("chr", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.None, true)]
|
|
|
|
[TestCase("ccs", "Candy Crush Saga from King", StringMatcher.SearchPrecisionScore.Low, true)]
|
|
|
|
[TestCase("cand", "Candy Crush Saga from King",StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("cand", "Help cure hope raise on mind entity Chrome", StringMatcher.SearchPrecisionScore.Regular, false)]
|
2019-12-03 22:04:02 +08:00
|
|
|
public void WhenGivenDesiredPrecisionThenShouldReturnAllResultsGreaterOrEqual(
|
2019-12-30 07:13:33 +08:00
|
|
|
string queryString,
|
|
|
|
string compareString,
|
2020-01-07 19:30:36 +08:00
|
|
|
StringMatcher.SearchPrecisionScore expectedPrecisionScore,
|
2019-12-03 22:04:02 +08:00
|
|
|
bool expectedPrecisionResult)
|
2019-09-29 11:27:28 +08:00
|
|
|
{
|
2020-01-07 17:26:26 +08:00
|
|
|
// When
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher {UserSettingSearchPrecision = expectedPrecisionScore};
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2020-01-07 17:26:26 +08:00
|
|
|
// Given
|
2020-01-20 07:06:16 +08:00
|
|
|
var matchResult = matcher.FuzzyMatch(queryString, compareString);
|
2019-09-29 12:27:07 +08:00
|
|
|
|
2020-01-02 05:04:16 +08:00
|
|
|
Debug.WriteLine("");
|
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine($"QueryString: {queryString} CompareString: {compareString}");
|
2020-01-07 19:30:36 +08:00
|
|
|
Debug.WriteLine($"RAW SCORE: {matchResult.RawScore.ToString()}, PrecisionLevelSetAt: {expectedPrecisionScore} ({(int)expectedPrecisionScore})");
|
2020-01-02 05:04:16 +08:00
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine("");
|
|
|
|
|
2020-01-07 17:26:26 +08:00
|
|
|
// Should
|
2019-12-30 07:13:33 +08:00
|
|
|
Assert.AreEqual(expectedPrecisionResult, matchResult.IsSearchPrecisionScoreMet(),
|
|
|
|
$"Query:{queryString}{Environment.NewLine} " +
|
|
|
|
$"Compare:{compareString}{Environment.NewLine}" +
|
|
|
|
$"Raw Score: {matchResult.RawScore}{Environment.NewLine}" +
|
2020-01-07 19:30:36 +08:00
|
|
|
$"Precision Score: {(int)expectedPrecisionScore}");
|
2019-12-30 07:13:33 +08:00
|
|
|
}
|
|
|
|
|
2020-01-07 19:30:36 +08:00
|
|
|
[TestCase("exce", "OverLeaf-Latex: An online LaTeX editor", StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("term", "Windows Terminal (Preview)", StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("sql s managa", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("sql' s manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("sql s manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("sql manag", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("sql", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("sql serv", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
2020-01-14 04:53:59 +08:00
|
|
|
[TestCase("sqlserv", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("sql servman", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
|
|
|
|
[TestCase("sql serv man", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
2020-01-07 19:30:36 +08:00
|
|
|
[TestCase("sql studio", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("mic", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("chr", "Shutdown", StringMatcher.SearchPrecisionScore.Regular, false)]
|
2020-01-14 04:53:59 +08:00
|
|
|
[TestCase("mssms", MicrosoftSqlServerManagementStudio, StringMatcher.SearchPrecisionScore.Regular, false)]
|
2020-01-07 19:30:36 +08:00
|
|
|
[TestCase("chr", "Change settings for text-to-speech and for speech recognition (if installed).", StringMatcher.SearchPrecisionScore.Regular, false)]
|
2020-01-14 04:53:59 +08:00
|
|
|
[TestCase("ch r", "Change settings for text-to-speech and for speech recognition (if installed).", StringMatcher.SearchPrecisionScore.Regular, true)]
|
2020-01-07 19:30:36 +08:00
|
|
|
[TestCase("a test", "This is a test", StringMatcher.SearchPrecisionScore.Regular, true)]
|
|
|
|
[TestCase("test", "This is a test", StringMatcher.SearchPrecisionScore.Regular, true)]
|
2019-12-30 07:13:33 +08:00
|
|
|
public void WhenGivenQueryShouldReturnResultsContainingAllQuerySubstrings(
|
|
|
|
string queryString,
|
|
|
|
string compareString,
|
2020-01-07 19:30:36 +08:00
|
|
|
StringMatcher.SearchPrecisionScore expectedPrecisionScore,
|
2019-12-30 07:13:33 +08:00
|
|
|
bool expectedPrecisionResult)
|
|
|
|
{
|
2020-01-07 17:26:26 +08:00
|
|
|
// When
|
2020-01-20 07:06:16 +08:00
|
|
|
var matcher = new StringMatcher { UserSettingSearchPrecision = expectedPrecisionScore };
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2020-01-07 17:26:26 +08:00
|
|
|
// Given
|
2020-01-20 07:06:16 +08:00
|
|
|
var matchResult = matcher.FuzzyMatch(queryString, compareString);
|
2019-09-29 12:27:07 +08:00
|
|
|
|
2020-01-02 05:04:16 +08:00
|
|
|
Debug.WriteLine("");
|
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine($"QueryString: {queryString} CompareString: {compareString}");
|
2020-01-07 19:30:36 +08:00
|
|
|
Debug.WriteLine($"RAW SCORE: {matchResult.RawScore.ToString()}, PrecisionLevelSetAt: {expectedPrecisionScore} ({(int)expectedPrecisionScore})");
|
2020-01-02 05:04:16 +08:00
|
|
|
Debug.WriteLine("###############################################");
|
|
|
|
Debug.WriteLine("");
|
|
|
|
|
2020-01-07 17:26:26 +08:00
|
|
|
// Should
|
2019-12-30 07:13:33 +08:00
|
|
|
Assert.AreEqual(expectedPrecisionResult, matchResult.IsSearchPrecisionScoreMet(),
|
|
|
|
$"Query:{queryString}{Environment.NewLine} " +
|
|
|
|
$"Compare:{compareString}{Environment.NewLine}" +
|
|
|
|
$"Raw Score: {matchResult.RawScore}{Environment.NewLine}" +
|
2020-01-07 19:30:36 +08:00
|
|
|
$"Precision Score: {(int)expectedPrecisionScore}");
|
2019-09-29 11:27:28 +08:00
|
|
|
}
|
2014-02-09 00:33:10 +08:00
|
|
|
}
|
2019-12-30 07:13:33 +08:00
|
|
|
}
|