Fix unitest build issue

This commit is contained in:
Jack251970 2025-01-13 10:21:14 +08:00
parent c3f71c213e
commit 3bebb69093
2 changed files with 18 additions and 10 deletions

View file

@ -19,6 +19,12 @@ namespace Flow.Launcher.Infrastructure
_alphabet = Ioc.Default.GetRequiredService<IAlphabet>();
}
// This is a workaround to allow unit tests to set the instance
public StringMatcher(IAlphabet alphabet)
{
_alphabet = alphabet;
}
public static StringMatcher Instance { get; internal set; }
public static MatchResult FuzzySearch(string query, string stringToCompare)

View file

@ -21,6 +21,8 @@ namespace Flow.Launcher.Test
private const string MicrosoftSqlServerManagementStudio = "Microsoft SQL Server Management Studio";
private const string VisualStudioCode = "Visual Studio Code";
private readonly IAlphabet alphabet = null;
public List<string> GetSearchStrings()
=> new List<string>
{
@ -59,7 +61,7 @@ namespace Flow.Launcher.Test
};
var results = new List<Result>();
var matcher = new StringMatcher();
var matcher = new StringMatcher(alphabet);
foreach (var str in sources)
{
results.Add(new Result
@ -81,7 +83,7 @@ namespace Flow.Launcher.Test
public void WhenNotAllCharactersFoundInSearchString_ThenShouldReturnZeroScore(string searchString)
{
var compareString = "Can have rum only in my glass";
var matcher = new StringMatcher();
var matcher = new StringMatcher(alphabet);
var scoreResult = matcher.FuzzyMatch(searchString, compareString).RawScore;
Assert.True(scoreResult == 0);
@ -97,7 +99,7 @@ namespace Flow.Launcher.Test
string searchTerm)
{
var results = new List<Result>();
var matcher = new StringMatcher();
var matcher = new StringMatcher(alphabet);
foreach (var str in GetSearchStrings())
{
results.Add(new Result
@ -147,7 +149,7 @@ namespace Flow.Launcher.Test
string queryString, string compareString, int expectedScore)
{
// When, Given
var matcher = new StringMatcher {UserSettingSearchPrecision = SearchPrecisionScore.Regular};
var matcher = new StringMatcher(alphabet) {UserSettingSearchPrecision = SearchPrecisionScore.Regular};
var rawScore = matcher.FuzzyMatch(queryString, compareString).RawScore;
// Should
@ -181,7 +183,7 @@ namespace Flow.Launcher.Test
bool expectedPrecisionResult)
{
// When
var matcher = new StringMatcher {UserSettingSearchPrecision = expectedPrecisionScore};
var matcher = new StringMatcher(alphabet) {UserSettingSearchPrecision = expectedPrecisionScore};
// Given
var matchResult = matcher.FuzzyMatch(queryString, compareString);
@ -232,7 +234,7 @@ namespace Flow.Launcher.Test
bool expectedPrecisionResult)
{
// When
var matcher = new StringMatcher {UserSettingSearchPrecision = expectedPrecisionScore};
var matcher = new StringMatcher(alphabet) {UserSettingSearchPrecision = expectedPrecisionScore};
// Given
var matchResult = matcher.FuzzyMatch(queryString, compareString);
@ -260,7 +262,7 @@ namespace Flow.Launcher.Test
string queryString, string compareString1, string compareString2)
{
// When
var matcher = new StringMatcher {UserSettingSearchPrecision = SearchPrecisionScore.Regular};
var matcher = new StringMatcher(alphabet) {UserSettingSearchPrecision = SearchPrecisionScore.Regular};
// Given
var compareString1Result = matcher.FuzzyMatch(queryString, compareString1);
@ -293,7 +295,7 @@ namespace Flow.Launcher.Test
string queryString, string compareString1, string compareString2)
{
// When
var matcher = new StringMatcher { UserSettingSearchPrecision = SearchPrecisionScore.Regular };
var matcher = new StringMatcher(alphabet) { UserSettingSearchPrecision = SearchPrecisionScore.Regular };
// Given
var compareString1Result = matcher.FuzzyMatch(queryString, compareString1);
@ -323,7 +325,7 @@ namespace Flow.Launcher.Test
string secondName, string secondDescription, string secondExecutableName)
{
// Act
var matcher = new StringMatcher();
var matcher = new StringMatcher(alphabet);
var firstNameMatch = matcher.FuzzyMatch(queryString, firstName).RawScore;
var firstDescriptionMatch = matcher.FuzzyMatch(queryString, firstDescription).RawScore;
var firstExecutableNameMatch = matcher.FuzzyMatch(queryString, firstExecutableName).RawScore;
@ -358,7 +360,7 @@ namespace Flow.Launcher.Test
public void WhenGivenAnAcronymQuery_ShouldReturnAcronymScore(string queryString, string compareString,
int desiredScore)
{
var matcher = new StringMatcher();
var matcher = new StringMatcher(alphabet);
var score = matcher.FuzzyMatch(queryString, compareString).Score;
Assert.IsTrue(score == desiredScore,
$@"Query: ""{queryString}""