mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
* Added CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo) * Check arguments and throw ArgumentNullException (CA1062: Validate arguments of public methods) * Changed url parameter from string to System.Uri and added null checks (CA1054: URI parameters should not be strings) * Rethrow exception without specifying the exception explicitly (CA2200: Rethrow to preserve stack details) * Changed from Collection property to methods for PluginMetadata::ActionKeywords (CA2227: Collection properties should be read only) * Changed from Collection property to methods for Result::GetTitleHighlightData (CA2227: Collection properties should be read only) * Made Collection property read-only and added parameter in constructor for Result::SubTitleHighlightData (CA2227: Collection properties should be read only) * Made Collection property read only and added parameter in constructor for ResultUpdatedEventArgs::Results (CA2227: Collection properties should be read only) * CA1507: Use nameof in place of string * Removed initialization for ThemeManager::_disposed (CA1805: Do not initialize unnecessarily) * Changed Query::Terms array property to ReadOnlyCollection and added private set (CA1819: Properties should not return arrays) * CA1060: Move P/Invokes to NativeMethods class * CA1806: Do not ignore method results * CA2101: Specify marshaling for P/Invoke string arguments * Removed unnecessary empty interface IFeatures (CA1040: Avoid empty interfaces) - Removed IFeatures interface and references - Renamed IFeatures.cs to IContextMenu.cs according to guidelines * Added comments for CultureInfo (CA1307: Specify StringComparison for clarity / CA1304: Specify CultureInfo) * Added localization for Wox.Plugin and localized strings in FilesFolders.cs
207 lines
8.7 KiB
C#
207 lines
8.7 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Mono.Collections.Generic;
|
|
using NUnit.Framework;
|
|
using Wox.Core.Plugin;
|
|
using Wox.Plugin;
|
|
|
|
namespace Wox.Test
|
|
{
|
|
public class QueryBuilderTest
|
|
{
|
|
private static bool AreEqual(Query firstQuery, Query secondQuery)
|
|
{
|
|
// Using Ordinal since this is used internally
|
|
return firstQuery.ActionKeyword.Equals(secondQuery.ActionKeyword, StringComparison.Ordinal)
|
|
&& firstQuery.Search.Equals(secondQuery.Search, StringComparison.Ordinal)
|
|
&& firstQuery.RawQuery.Equals(secondQuery.RawQuery, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldRemoveExtraSpacesForNonGlobalPlugin()
|
|
{
|
|
// Arrange
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ ">", new PluginPair { Metadata = new PluginMetadata(new List<string> { ">" } ) } },
|
|
};
|
|
string searchQuery = "> file.txt file2 file3";
|
|
|
|
// Act
|
|
var pluginQueryPairs = QueryBuilder.Build(ref searchQuery, nonGlobalPlugins);
|
|
|
|
// Assert
|
|
Assert.AreEqual("> file.txt file2 file3", searchQuery);
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldRemoveExtraSpacesForDisabledNonGlobalPlugin()
|
|
{
|
|
// Arrange
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ ">", new PluginPair { Metadata = new PluginMetadata(new List<string> { ">" }) { Disabled = true } } },
|
|
};
|
|
string searchQuery = "> file.txt file2 file3";
|
|
|
|
// Act
|
|
var pluginQueryPairs = QueryBuilder.Build(ref searchQuery, nonGlobalPlugins);
|
|
|
|
// Assert
|
|
Assert.AreEqual("> file.txt file2 file3", searchQuery);
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldRemoveExtraSpacesForGlobalPlugin()
|
|
{
|
|
// Arrange
|
|
string searchQuery = "file.txt file2 file3";
|
|
|
|
// Act
|
|
var pluginQueryPairs = QueryBuilder.Build(ref searchQuery, new Dictionary<string, PluginPair>());
|
|
|
|
// Assert
|
|
Assert.AreEqual("file.txt file2 file3", searchQuery);
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldGenerateSameQueryIfEitherActionKeywordOrActionKeywordsListIsSet()
|
|
{
|
|
// Arrange
|
|
string searchQuery = "> query";
|
|
var firstPlugin = new PluginPair { Metadata = new PluginMetadata(new List<string> { ">" } ) };
|
|
var secondPlugin = new PluginPair { Metadata = new PluginMetadata { ActionKeyword = ">" } };
|
|
|
|
var nonGlobalPluginWithActionKeywords = new Dictionary<string, PluginPair>
|
|
{
|
|
{ ">", firstPlugin },
|
|
};
|
|
|
|
var nonGlobalPluginWithActionKeyword = new Dictionary<string, PluginPair>
|
|
{
|
|
{ ">", secondPlugin },
|
|
};
|
|
string[] terms = { ">", "query" };
|
|
Query expectedQuery = new Query("> query", "query", new ReadOnlyCollection<string>(terms), ">");
|
|
|
|
// Act
|
|
var queriesForPluginsWithActionKeywords = QueryBuilder.Build(ref searchQuery, nonGlobalPluginWithActionKeywords);
|
|
var queriesForPluginsWithActionKeyword = QueryBuilder.Build(ref searchQuery, nonGlobalPluginWithActionKeyword);
|
|
|
|
var firstQuery = queriesForPluginsWithActionKeyword.GetValueOrDefault(firstPlugin);
|
|
var secondQuery = queriesForPluginsWithActionKeywords.GetValueOrDefault(secondPlugin);
|
|
|
|
// Assert
|
|
Assert.IsTrue(AreEqual(firstQuery, expectedQuery));
|
|
Assert.IsTrue(AreEqual(firstQuery, secondQuery));
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldGenerateCorrectQueriesForPluginsWithMultipleActionKeywords()
|
|
{
|
|
// Arrange
|
|
var plugin = new PluginPair { Metadata = new PluginMetadata(new List<string> { "a", "b" } ) };
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ "a", plugin },
|
|
{ "b", plugin },
|
|
};
|
|
|
|
var firstQueryText = "asearch";
|
|
var secondQueryText = "bsearch";
|
|
|
|
// Act
|
|
var firstPluginQueryPair = QueryBuilder.Build(ref firstQueryText, nonGlobalPlugins);
|
|
var firstQuery = firstPluginQueryPair.GetValueOrDefault(plugin);
|
|
|
|
var secondPluginQueryPairs = QueryBuilder.Build(ref secondQueryText, nonGlobalPlugins);
|
|
var secondQuery = secondPluginQueryPairs.GetValueOrDefault(plugin);
|
|
|
|
// Assert
|
|
Assert.IsTrue(AreEqual(firstQuery, new Query { ActionKeyword = "a", RawQuery = "asearch", Search = "search" }));
|
|
Assert.IsTrue(AreEqual(secondQuery, new Query { ActionKeyword = "b", RawQuery = "bsearch", Search = "search" }));
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuildShouldGenerateSameSearchQueryWithOrWithoutSpaceAfterActionKeyword()
|
|
{
|
|
// Arrange
|
|
var plugin = new PluginPair { Metadata = new PluginMetadata(new List<string> { "a" } ) };
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ "a", plugin },
|
|
};
|
|
|
|
var firstQueryText = "asearch";
|
|
var secondQueryText = "a search";
|
|
|
|
// Act
|
|
var firstPluginQueryPair = QueryBuilder.Build(ref firstQueryText, nonGlobalPlugins);
|
|
var firstQuery = firstPluginQueryPair.GetValueOrDefault(plugin);
|
|
|
|
var secondPluginQueryPairs = QueryBuilder.Build(ref secondQueryText, nonGlobalPlugins);
|
|
var secondQuery = secondPluginQueryPairs.GetValueOrDefault(plugin);
|
|
|
|
// Assert
|
|
// Using Ordinal since this is used internally
|
|
Assert.IsTrue(firstQuery.Search.Equals(secondQuery.Search, StringComparison.Ordinal));
|
|
Assert.IsTrue(firstQuery.ActionKeyword.Equals(secondQuery.ActionKeyword, StringComparison.Ordinal));
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuildShouldGenerateCorrectQueryForPluginsWhoseActionKeywordsHaveSamePrefix()
|
|
{
|
|
// Arrange
|
|
string searchQuery = "abcdefgh";
|
|
var firstPlugin = new PluginPair { Metadata = new PluginMetadata { ActionKeyword = "ab", ID = "plugin1" } };
|
|
var secondPlugin = new PluginPair { Metadata = new PluginMetadata { ActionKeyword = "abcd", ID = "plugin2" } };
|
|
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ "ab", firstPlugin },
|
|
{ "abcd", secondPlugin },
|
|
};
|
|
|
|
// Act
|
|
var pluginQueryPairs = QueryBuilder.Build(ref searchQuery, nonGlobalPlugins);
|
|
|
|
var firstQuery = pluginQueryPairs.GetValueOrDefault(firstPlugin);
|
|
var secondQuery = pluginQueryPairs.GetValueOrDefault(secondPlugin);
|
|
|
|
// Assert
|
|
Assert.IsTrue(AreEqual(firstQuery, new Query { RawQuery = searchQuery, Search = searchQuery.Substring(firstPlugin.Metadata.ActionKeyword.Length), ActionKeyword = firstPlugin.Metadata.ActionKeyword } ));
|
|
Assert.IsTrue(AreEqual(secondQuery, new Query { RawQuery = searchQuery, Search = searchQuery.Substring(secondPlugin.Metadata.ActionKeyword.Length), ActionKeyword = secondPlugin.Metadata.ActionKeyword }));
|
|
}
|
|
|
|
[Test]
|
|
public void QueryBuilderShouldSetTermsCorrentlyWhenCalled()
|
|
{
|
|
// Arrange
|
|
string searchQuery = "abcd efgh";
|
|
var firstPlugin = new PluginPair { Metadata = new PluginMetadata { ActionKeyword = "ab", ID = "plugin1" } };
|
|
var secondPlugin = new PluginPair { Metadata = new PluginMetadata { ActionKeyword = "abcd", ID = "plugin2" } };
|
|
|
|
var nonGlobalPlugins = new Dictionary<string, PluginPair>
|
|
{
|
|
{ "ab", firstPlugin },
|
|
{ "abcd", secondPlugin },
|
|
};
|
|
|
|
// Act
|
|
var pluginQueryPairs = QueryBuilder.Build(ref searchQuery, nonGlobalPlugins);
|
|
|
|
var firstQuery = pluginQueryPairs.GetValueOrDefault(firstPlugin);
|
|
var secondQuery = pluginQueryPairs.GetValueOrDefault(secondPlugin);
|
|
|
|
// Assert
|
|
// Using Ordinal since this is used internally
|
|
Assert.IsTrue(firstQuery.Terms[0].Equals("cd", StringComparison.Ordinal) && firstQuery.Terms[1].Equals("efgh", StringComparison.Ordinal) && firstQuery.Terms.Count == 2);
|
|
Assert.IsTrue(secondQuery.Terms[0].Equals("efgh", StringComparison.Ordinal) && secondQuery.Terms.Count == 1);
|
|
}
|
|
}
|
|
}
|