Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindGuid helper function #3762

Merged
merged 7 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/cascadia/LocalTests_TerminalApp/SettingsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace TerminalAppLocalTests
TEST_METHOD(TestExplodingNameOnlyProfiles);
TEST_METHOD(TestHideAllProfiles);
TEST_METHOD(TestInvalidColorSchemeName);
TEST_METHOD(TestHelperFunctions);

TEST_METHOD(TestLayerGlobalsOnRoot);

Expand Down Expand Up @@ -1264,6 +1265,67 @@ namespace TerminalAppLocalTests
VERIFY_ARE_EQUAL(L"Campbell", settings._profiles.at(2)._schemeName.value());
}

void SettingsTests::TestHelperFunctions()
{
const std::string settings0String{ R"(
{
"defaultProfile" : "{2C4DE342-38B7-51CF-B940-2309A097F518}",
"profiles": [
{
"name" : "profile0",
"guid": "{6239a42c-5555-49a3-80bd-e8fdd045185c}"
},
{
"name" : "profile1",
"guid": "{6239a42c-6666-49a3-80bd-e8fdd045185c}"
},
{
"name" : "Ubuntu",
"guid" : "{2C4DE342-38B7-51CF-B940-2309A097F518}"
},
{
"name" : "ThisProfileShouldNotCrash"
}
]
})" };

auto name0{ L"profile0" };
auto name1{ L"profile1" };
auto name2{ L"Ubuntu" };
auto badName{ L"DoesNotExist" };

auto guid0{ Microsoft::Console::Utils::GuidFromString(L"{6239a42c-5555-49a3-80bd-e8fdd045185c}") };
auto guid1{ Microsoft::Console::Utils::GuidFromString(L"{6239a42c-6666-49a3-80bd-e8fdd045185c}") };
auto guid2{ Microsoft::Console::Utils::GuidFromString(L"{2C4DE342-38B7-51CF-B940-2309A097F518}") };
std::optional<GUID> badGuid{};

VerifyParseSucceeded(settings0String);

CascadiaSettings settings;
settings._ParseJsonString(settings0String, false);
settings.LayerJson(settings._userSettings);

VERIFY_ARE_EQUAL(guid0, settings.FindGuid(name0));
VERIFY_ARE_EQUAL(guid1, settings.FindGuid(name1));
VERIFY_ARE_EQUAL(guid2, settings.FindGuid(name2));
VERIFY_ARE_EQUAL(badGuid, settings.FindGuid(badName));
// Following test will fail because GetGuid throws (despite being noexcept)
// VERIFY_ARE_EQUAL(badGuid, settings.FindGuid(L"ThisProfileShouldNotCrash"));

// Following lines only work because the GUID-less profile is last
auto prof0{ settings.FindProfile(guid0) };
auto prof1{ settings.FindProfile(guid1) };
auto prof2{ settings.FindProfile(guid2) };
// Following line will fail because GetGuid throws (despite being noexcept)
// auto badProf{ settings.FindProfile(badGuid) };

VERIFY_ARE_EQUAL(name0, prof0->GetName());
VERIFY_ARE_EQUAL(name1, prof1->GetName());
VERIFY_ARE_EQUAL(name2, prof2->GetName());
// Following test will fail because GetGuid throws (despite being noexcept)
// VERIFY_ARE_EQUAL(badName, badProf->GetName());
}

void SettingsTests::TestLayerGlobalsOnRoot()
{
// Test for microsoft/terminal#2906. We added the ability for the root
Expand Down
23 changes: 23 additions & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ CascadiaSettings::CascadiaSettings(const bool addDynamicProfiles)
}
}

// Method Description:
// - Finds a GUID associated with the given profile name. If no profile matches
// the profile name, returns a std::nullopt.
// Arguments:
// - profileName: the name of the profile's GUID to return.
// Return Value:
// - the GUID associated with the profile name.
std::optional<GUID> CascadiaSettings::FindGuid(const std::wstring& profileName) const noexcept
{
std::optional<GUID> profileGuid{};

for (const auto& profile : _profiles)
{
if (profileName == profile.GetName())
{
profileGuid = profile.GetGuid();
break;
}
}

return profileGuid;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my only real comment is that I think I'd prefer that we make this function return a std::optional<GUID>, and return std::nullopt when we didn't find a match instead of returning the null GUID, since the null GUID is technically a valid GUID.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing

}

// Method Description:
// - Finds a profile that matches the given GUID. If there is no profile in this
// settings object that matches, returns nullptr.
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class TerminalApp::CascadiaSettings final
static std::wstring GetSettingsPath(const bool useRoamingPath = false);
static std::wstring GetDefaultSettingsPath();

std::optional<GUID> FindGuid(const std::wstring& profileName) const noexcept;
const Profile* FindProfile(GUID profileGuid) const noexcept;

std::vector<TerminalApp::SettingsLoadWarnings>& GetWarnings();
Expand Down