From c6e839271a178d41bf24327a879dfa01f2fa55cb Mon Sep 17 00:00:00 2001 From: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> Date: Mon, 9 Dec 2019 20:48:46 -0800 Subject: [PATCH] This test brake was introduced in the following fix: https://github.com/microsoft/PowerToys/pull/647/commits/1efe5bff9fcf8216814e12a1045df2668beb1709 It looks to me like the test cases just was also wrong and just wasn't updated with the fix. I've modified some of the test cases to verify the expected behavior: 1) Slight refactor of the tests to pass in the SearchReplaceExpected and flags for the tests. 2) Using Assert::AreEqual instead of Assert::IsTrue for better error meesaging when failed. 3) Verifying that the behavior is the same with or without match all occurances when using *. 4) Verifying that without the `UseRegularExpressionsFlag` the `.*` characters get replaced, including when MatchAllOccurances is set. --- .../unittests/PowerRenameRegExTests.cpp | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/modules/powerrename/unittests/PowerRenameRegExTests.cpp b/src/modules/powerrename/unittests/PowerRenameRegExTests.cpp index 1bca68642c..c1cf77ccb0 100644 --- a/src/modules/powerrename/unittests/PowerRenameRegExTests.cpp +++ b/src/modules/powerrename/unittests/PowerRenameRegExTests.cpp @@ -275,28 +275,62 @@ TEST_METHOD(VerifyMatchAllWildcardUseRegEx) } } -TEST_METHOD(VerifyReplaceFirstWildcardUseRegEx) +void VerifyReplaceFirstWildcard(SearchReplaceExpected sreTable[], int tableSize, DWORD flags) { CComPtr renameRegEx; Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK); - DWORD flags = UseRegularExpressions; Assert::IsTrue(renameRegEx->put_flags(flags) == S_OK); - SearchReplaceExpected sreTable[] = { - { L".*", L"Foo", L"AAAAAA", L"FooAAAA" }, - }; - - for (int i = 0; i < ARRAYSIZE(sreTable); i++) + for (int i = 0; i < tableSize; i++) { PWSTR result = nullptr; Assert::IsTrue(renameRegEx->put_searchTerm(sreTable[i].search) == S_OK); Assert::IsTrue(renameRegEx->put_replaceTerm(sreTable[i].replace) == S_OK); Assert::IsTrue(renameRegEx->Replace(sreTable[i].test, &result) == S_OK); - Assert::IsTrue(wcscmp(result, sreTable[i].expected) == 0); + Assert::AreEqual(sreTable[i].expected, result); CoTaskMemFree(result); } } +TEST_METHOD(VerifyReplaceFirstWildCardUseRegex) +{ + SearchReplaceExpected sreTable[] = { + //search, replace, test, result + { L".*", L"Foo", L"AAAAAA", L"Foo" }, + }; + VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), UseRegularExpressions); +} + +TEST_METHOD(VerifyReplaceFirstWildCardUseRegexMatchAllOccurances) +{ + SearchReplaceExpected sreTable[] = { + //search, replace, test, result + { L".*", L"Foo", L"AAAAAA", L"Foo" }, + }; + VerifyReplaceFirstWildcard(sreTable, ARRAYSIZE(sreTable), UseRegularExpressions | MatchAllOccurences); +} + +TEST_METHOD(VerifyReplaceFirstWildCardMatchAllOccurances) +{ + 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); +} + TEST_METHOD(VerifyEventsFire) { CComPtr renameRegEx; @@ -318,4 +352,4 @@ TEST_METHOD(VerifyEventsFire) } } ; -} \ No newline at end of file +}