mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[PowerRename] Fix tests inconsistency, improve test performance (#8129)
* Move retrieveing file attibutes to PowerRenameRegex Move file attributes unit tests to PowerRenameRegexTests Add file time field to MockPowerRenameItem * Add file attributes unittests to PowerRenameManagerTests * Change variable name * Rearrange function arguments * Check if file attributes are used only once * Change variable name LocalTime -> fileTime, date -> time * Set fileTime as a member of PowerRenameRegEx rather than passing as an argument * Change function name isFileAttributesUsed() -> isFileTimeUsed() Check before resetting fileTime * Fix small bugs * Fix typos * Refactor for readability, move free calls to reachable places * Fix search for area empty bug searchTerm being empty is not an invalid argument rather it must return OK without any operation Tests must check if Replace() returns S_OK becuase later it checks its result * Check return values of method calls in PowerRenameManager Remove received argments checks from some methods because argument being null or empty string doesnt mean it is invalid or method fails * Fix formatting. Remove overlooked comment. Fix error message. * Change HRESULT declarations according to coding style * Fix unhandled case. Refactor.
This commit is contained in:
committed by
GitHub
parent
4403876320
commit
da22e21a0e
@@ -53,7 +53,7 @@ TEST_METHOD(ReplaceNoSearchOrReplaceTerm)
|
||||
CComPtr<IPowerRenameRegEx> renameRegEx;
|
||||
Assert::IsTrue(CPowerRenameRegEx::s_CreateInstance(&renameRegEx) == S_OK);
|
||||
PWSTR result = nullptr;
|
||||
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) != S_OK);
|
||||
Assert::IsTrue(renameRegEx->Replace(L"foobar", &result) == S_OK);
|
||||
Assert::IsTrue(result == nullptr);
|
||||
CoTaskMemFree(result);
|
||||
}
|
||||
@@ -369,6 +369,103 @@ TEST_METHOD(VerifyHandleCapturingGroups)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_METHOD(VerifyLookbehindFails)
|
||||
{
|
||||
// Standard Library Regex Engine does not support lookbehind, thus test should fail.
|
||||
@@ -406,6 +503,8 @@ TEST_METHOD(VerifyEventsFire)
|
||||
Assert::IsTrue(renameRegEx->PutFlags(flags) == S_OK);
|
||||
Assert::IsTrue(renameRegEx->PutSearchTerm(L"FOO") == S_OK);
|
||||
Assert::IsTrue(renameRegEx->PutReplaceTerm(L"BAR") == S_OK);
|
||||
Assert::IsTrue(renameRegEx->PutFileTime(SYSTEMTIME{ 0 }) == S_OK);
|
||||
Assert::IsTrue(renameRegEx->ResetFileTime() == S_OK);
|
||||
Assert::IsTrue(lstrcmpi(L"FOO", mockEvents->m_searchTerm) == 0);
|
||||
Assert::IsTrue(lstrcmpi(L"BAR", mockEvents->m_replaceTerm) == 0);
|
||||
Assert::IsTrue(flags == mockEvents->m_flags);
|
||||
|
||||
Reference in New Issue
Block a user