2020-04-29 13:02:18 -07:00
|
|
|
#include "pch.h"
|
2019-10-17 20:57:19 -07:00
|
|
|
#include "CppUnitTest.h"
|
2020-11-09 19:13:43 +01:00
|
|
|
#include "powerrename/lib/Settings.h"
|
2019-10-17 20:57:19 -07:00
|
|
|
#include <PowerRenameInterfaces.h>
|
|
|
|
|
#include <PowerRenameRegEx.h>
|
|
|
|
|
#include "MockPowerRenameRegExEvents.h"
|
|
|
|
|
|
|
|
|
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
|
|
|
|
|
|
|
|
namespace PowerRenameRegExTests
|
|
|
|
|
{
|
|
|
|
|
struct SearchReplaceExpected
|
|
|
|
|
{
|
|
|
|
|
PCWSTR search;
|
|
|
|
|
PCWSTR replace;
|
|
|
|
|
PCWSTR test;
|
|
|
|
|
PCWSTR expected;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-10 10:28:24 +03:00
|
|
|
TEST_CLASS(SimpleTests){
|
|
|
|
|
public:
|
2020-11-09 19:13:43 +01:00
|
|
|
TEST_CLASS_INITIALIZE(ClassInitialize)
|
|
|
|
|
{
|
|
|
|
|
CSettingsInstance().SetUseBoostLib(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(GeneralReplaceTest)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(L"foo") == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(L"big") == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, L"bigbar") == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(ReplaceNoMatch)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(L"notfound") == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(L"big") == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, L"foobar") == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(ReplaceNoSearchOrReplaceTerm)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
PWSTR result = nullptr;
|
2020-12-14 12:28:12 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(result == nullptr);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(ReplaceNoReplaceTerm)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(L"foo") == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, L"bar") == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(ReplaceEmptyStringReplaceTerm)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(L"foo") == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(L"") == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, L"bar") == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyDefaultFlags)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = 0;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->GetFlags(&flags) == S_OK);
|
2022-02-02 17:46:22 +01:00
|
|
|
Assert::IsTrue(flags == 0);
|
2019-12-10 10:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyCaseSensitiveSearch)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = CaseSensitive;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"Foo", L"Foo", L"FooBar", L"FooBar" },
|
|
|
|
|
{ L"Foo", L"boo", L"FooBar", L"booBar" },
|
|
|
|
|
{ L"Foo", L"boo", L"foobar", L"foobar" },
|
|
|
|
|
{ L"123", L"654", L"123456", L"654456" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceFirstOnly)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = 0;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AABBA" },
|
|
|
|
|
{ L"B", L"BBB", L"ABABAB", L"ABBBABAB" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceAll)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AAAAA" },
|
|
|
|
|
{ L"B", L"BBB", L"ABABAB", L"ABBBABBBABBB" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceAllCaseInsensitive)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | CaseSensitive;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AAAAA" },
|
|
|
|
|
{ L"B", L"BBB", L"ABABAB", L"ABBBABBBABBB" },
|
|
|
|
|
{ L"b", L"BBB", L"AbABAb", L"ABBBABABBB" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceFirstOnlyUseRegEx)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = UseRegularExpressions;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AABBA" },
|
|
|
|
|
{ L"B", L"BBB", L"ABABAB", L"ABBBABAB" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
2019-10-17 20:57:19 -07:00
|
|
|
{
|
2019-12-10 10:28:24 +03:00
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceAllUseRegEx)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AAAAA" },
|
|
|
|
|
{ L"B", L"BBB", L"ABABAB", L"ABBBABBBABBB" },
|
2019-10-17 20:57:19 -07:00
|
|
|
};
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceAllUseRegExCaseSensitive)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions | CaseSensitive;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L"B", L"BB", L"ABA", L"ABBA" },
|
|
|
|
|
{ L"B", L"A", L"ABBBA", L"AAAAA" },
|
|
|
|
|
{ L"b", L"BBB", L"AbABAb", L"ABBBABABBB" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyMatchAllWildcardUseRegEx)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
{ L".*", L"Foo", L"AAAAAA", L"Foo" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 20:48:46 -08:00
|
|
|
void VerifyReplaceFirstWildcard(SearchReplaceExpected sreTable[], int tableSize, DWORD flags)
|
2019-12-10 10:28:24 +03:00
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
|
2019-12-09 20:48:46 -08:00
|
|
|
for (int i = 0; i < tableSize; i++)
|
2019-12-10 10:28:24 +03:00
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
2019-12-09 20:48:46 -08:00
|
|
|
Assert::AreEqual(sreTable[i].expected, result);
|
2019-12-10 10:28:24 +03:00
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 20:48:46 -08:00
|
|
|
TEST_METHOD(VerifyReplaceFirstWildCardUseRegex)
|
|
|
|
|
{
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L".*", L"Foo", L"AAAAAA", L"Foo" },
|
|
|
|
|
};
|
|
|
|
|
VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), UseRegularExpressions);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:43:59 -04:00
|
|
|
TEST_METHOD(VerifyReplaceFirstWildCardUseRegexMatchAllOccurrences)
|
2019-12-09 20:48:46 -08:00
|
|
|
{
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L".*", L"Foo", L"AAAAAA", L"Foo" },
|
|
|
|
|
};
|
|
|
|
|
VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), UseRegularExpressions | MatchAllOccurences);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:43:59 -04:00
|
|
|
TEST_METHOD(VerifyReplaceFirstWildCardMatchAllOccurrences)
|
2019-12-09 20:48:46 -08:00
|
|
|
{
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L".*", L"Foo", L"AAAAAA", L"AAAAAA" },
|
|
|
|
|
{ L".*", L"Foo", L".*", L"Foo" },
|
|
|
|
|
{ L".*", L"Foo", L".*Bar.*", L"FooBarFoo" },
|
|
|
|
|
};
|
|
|
|
|
VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), MatchAllOccurences);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD(VerifyReplaceFirstWildNoFlags)
|
|
|
|
|
{
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L".*", L"Foo", L"AAAAAA", L"AAAAAA" },
|
|
|
|
|
{ L".*", L"Foo", L".*", L"Foo" },
|
|
|
|
|
};
|
|
|
|
|
VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 00:12:46 +03:00
|
|
|
TEST_METHOD(VerifyHandleCapturingGroups)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions | CaseSensitive;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
2020-07-23 00:12:46 +03:00
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L"(foo)(bar)", L"$1_$002_$223_$001021_$00001", L"foobar", L"foo_$002_bar23_$001021_$00001" },
|
|
|
|
|
{ L"(foo)(bar)", L"_$1$2_$123$040", L"foobar", L"_foobar_foo23$040" },
|
|
|
|
|
{ L"(foo)(bar)", L"$$$1", L"foobar", L"$foo" },
|
|
|
|
|
{ L"(foo)(bar)", L"$$1", L"foobar", L"$1" },
|
|
|
|
|
{ L"(foo)(bar)", L"$12", L"foobar", L"foo2" },
|
|
|
|
|
{ L"(foo)(bar)", L"$10", L"foobar", L"foo0" },
|
|
|
|
|
{ L"(foo)(bar)", L"$01", L"foobar", L"$01" },
|
|
|
|
|
{ L"(foo)(bar)", L"$$$11", L"foobar", L"$foo1" },
|
|
|
|
|
{ L"(foo)(bar)", L"$$$$113a", L"foobar", L"$$113a" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
2020-07-23 00:12:46 +03:00
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
2020-11-09 19:13:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 12:28:12 +03:00
|
|
|
TEST_METHOD (VerifyFileAttributesNoPadding)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions ;
|
|
|
|
|
SYSTEMTIME fileTime = SYSTEMTIME{ 2020, 7, 3, 22, 15, 6, 42, 453 };
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
|
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L"foo", L"bar$YY-$M-$D-$h-$m-$s-$f", L"foo", L"bar20-7-22-15-6-42-4" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFileTime(fileTime) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD (VerifyFileAttributesPadding)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
|
|
|
|
SYSTEMTIME fileTime = SYSTEMTIME{ 2020, 7, 3, 22, 15, 6, 42, 453 };
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L"foo", L"bar$YYYY-$MM-$DD-$hh-$mm-$ss-$fff", L"foo", L"bar2020-07-22-15-06-42-453" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFileTime(fileTime) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_METHOD (VerifyFileAttributesMonthandDayNames)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
|
|
|
|
|
|
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
|
SYSTEMTIME fileTime = { 2020, 1, 3, 1, 15, 6, 42, 453 };
|
|
|
|
|
wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
|
|
|
|
|
wchar_t result[MAX_PATH] = L"bar";
|
|
|
|
|
wchar_t formattedDate[MAX_PATH];
|
|
|
|
|
if (GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) == 0)
|
|
|
|
|
StringCchCopy(localeName, LOCALE_NAME_MAX_LENGTH, L"en_US");
|
|
|
|
|
|
|
|
|
|
GetDateFormatEx(localeName, NULL, &fileTime, L"MMM", formattedDate, MAX_PATH, NULL);
|
|
|
|
|
formattedDate[0] = towupper(formattedDate[0]);
|
|
|
|
|
StringCchPrintf(result, MAX_PATH, TEXT("%s%s"), result, formattedDate);
|
|
|
|
|
|
|
|
|
|
GetDateFormatEx(localeName, NULL, &fileTime, L"MMMM", formattedDate, MAX_PATH, NULL);
|
|
|
|
|
formattedDate[0] = towupper(formattedDate[0]);
|
|
|
|
|
StringCchPrintf(result, MAX_PATH, TEXT("%s-%s"), result, formattedDate);
|
|
|
|
|
|
|
|
|
|
GetDateFormatEx(localeName, NULL, &fileTime, L"ddd", formattedDate, MAX_PATH, NULL);
|
|
|
|
|
formattedDate[0] = towupper(formattedDate[0]);
|
|
|
|
|
StringCchPrintf(result, MAX_PATH, TEXT("%s-%s"), result, formattedDate);
|
|
|
|
|
|
|
|
|
|
GetDateFormatEx(localeName, NULL, &fileTime, L"dddd", formattedDate, MAX_PATH, NULL);
|
|
|
|
|
formattedDate[0] = towupper(formattedDate[0]);
|
|
|
|
|
StringCchPrintf(result, MAX_PATH, TEXT("%s-%s"), result, formattedDate);
|
|
|
|
|
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L"foo", L"bar$MMM-$MMMM-$DDD-$DDDD", L"foo", result },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFileTime(fileTime) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK);
|
|
|
|
|
Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0);
|
|
|
|
|
CoTaskMemFree(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 19:13:43 +01:00
|
|
|
TEST_METHOD(VerifyLookbehindFails)
|
|
|
|
|
{
|
|
|
|
|
// Standard Library Regex Engine does not support lookbehind, thus test should fail.
|
|
|
|
|
SearchReplaceExpected sreTable[] = {
|
|
|
|
|
//search, replace, test, result
|
|
|
|
|
{ L"(?<=E12).*", L"Foo", L"AAAAAA", nullptr },
|
|
|
|
|
{ L"(?<!E12).*", L"Foo", L"AAAAAA", nullptr },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(UseRegularExpressions) == S_OK);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ARRAYSIZE(sreTable); i++)
|
|
|
|
|
{
|
|
|
|
|
PWSTR result = nullptr;
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(sreTable[i].search) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(sreTable[i].replace) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == E_FAIL);
|
|
|
|
|
Assert::AreEqual(sreTable[i].expected, result);
|
|
|
|
|
CoTaskMemFree(result);
|
2020-07-23 00:12:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 10:28:24 +03:00
|
|
|
TEST_METHOD(VerifyEventsFire)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IPowerRenameRegEx> renameRegEx;
|
|
|
|
|
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
|
|
|
|
CMockPowerRenameRegExEvents* mockEvents = new CMockPowerRenameRegExEvents();
|
|
|
|
|
CComPtr<IPowerRenameRegExEvents> regExEvents;
|
|
|
|
|
Assert::IsTrue(mockEvents->QueryInterface(IID_PPV_ARGS(®ExEvents)) == S_OK);
|
|
|
|
|
DWORD cookie = 0;
|
|
|
|
|
Assert::IsTrue(renameRegEx->Advise(regExEvents, &cookie) == S_OK);
|
|
|
|
|
DWORD flags = MatchAllOccurences | UseRegularExpressions | CaseSensitive;
|
2020-08-25 08:22:05 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutSearchTerm(L"FOO") == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->PutReplaceTerm(L"BAR") == S_OK);
|
2020-12-14 12:28:12 +03:00
|
|
|
Assert::IsTrue(renameRegEx->PutFileTime(SYSTEMTIME{ 0 }) == S_OK);
|
|
|
|
|
Assert::IsTrue(renameRegEx->ResetFileTime() == S_OK);
|
2019-12-10 10:28:24 +03:00
|
|
|
Assert::IsTrue(lstrcmpi(L"FOO", mockEvents->m_searchTerm) == 0);
|
|
|
|
|
Assert::IsTrue(lstrcmpi(L"BAR", mockEvents->m_replaceTerm) == 0);
|
|
|
|
|
Assert::IsTrue(flags == mockEvents->m_flags);
|
|
|
|
|
Assert::IsTrue(renameRegEx->UnAdvise(cookie) == S_OK);
|
|
|
|
|
mockEvents->Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
;
|
2019-12-09 20:48:46 -08:00
|
|
|
}
|