Skip to content

Commit

Permalink
fix up new tests (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Oct 23, 2020
1 parent 28c50a4 commit 14bf652
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 68 deletions.
178 changes: 120 additions & 58 deletions src/cascadia/LocalTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ namespace SettingsModelLocalTests

void SerializationTests::GlobalSettings()
{
DebugBreak();
// This needs to be in alphabetical order.
const std::string globalsString{
"{"
Expand All @@ -70,7 +69,6 @@ namespace SettingsModelLocalTests
"\"confirmCloseAllTabs\": true,"
"\"copyFormatting\": \"all\","
"\"copyOnSelect\": false,"
"\"debugFeatures\": true,"
"\"defaultProfile\": \"{61c54bbd-c2c6-5271-96e7-009a87ff44bf}\","
"\"experimental.input.forceVT\": false,"
"\"experimental.rendering.forceFullRepaint\": false,"
Expand Down Expand Up @@ -113,42 +111,41 @@ namespace SettingsModelLocalTests

void SerializationTests::Profile()
{
DebugBreak();
// This needs to be in alphabetical order.
const std::string profileString{
"{"
"\"name\": \"Windows PowerShell\","
"\"guid\": \"{61c54bbd-c2c6-5271-96e7-009a87ff44bf}\","
"\"hidden\": false,"
"\"foreground\": \"#AABBCC\","
"\"altGrAliasing\": true,"
"\"antialiasingMode\": \"grayscale\","
"\"background\": \"#BBCCAA\","
"\"selectionBackground\": \"#CCAABB\","
"\"cursorColor\": \"#CCBBAA\","
"\"backgroundImage\": \"made_you_look.jpeg\","
"\"backgroundImageAlignment\": \"center\","
"\"backgroundImageOpacity\": 1.0,"
"\"backgroundImageStretchMode\": \"uniformToFill\","
"\"closeOnExit\": \"graceful\","
"\"colorScheme\": \"Campbell\","
"\"historySize\": 9001,"
"\"snapOnInput\": true,"
"\"altGrAliasing\": true,"
"\"cursorHeight\": null,"
"\"commandline\": \"%SystemRoot%\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe\","
"\"cursorColor\": \"#CCBBAA\","
"\"cursorHeight\": 10,"
"\"cursorShape\": \"bar\","
"\"tabTitle\": null,"
"\"fontWeight\": \"normal\","
//"\"connectionType\": null,"
"\"commandline\": \"%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\","
"\"experimental.retroTerminalEffect\": false,"
"\"fontFace\": \"Cascadia Mono\","
"\"fontSize\": 12,"
"\"useAcrylic\": false,"
"\"suppressApplicationTitle\": false,"
"\"closeOnExit\": \"graceful\","
"\"fontWeight\": \"normal\","
"\"foreground\": \"#AABBCC\","
"\"guid\": \"{61c54bbd-c2c6-5271-96e7-009a87ff44bf}\","
"\"hidden\": false,"
"\"historySize\": 9001,"
"\"icon\": \"ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png\","
"\"name\": \"Windows PowerShell\","
"\"padding\": \"8, 8, 8, 8\","
"\"scrollbarState\": \"visible\","
"\"selectionBackground\": \"#CCAABB\","
"\"snapOnInput\": true,"
"\"startingDirectory\": \"%USERPROFILE%\","
"\"icon\": \"ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png\","
"\"backgroundImage\": null,"
"\"backgroundImageOpacity\": 1.0,"
"\"backgroundImageStretchMode\": \"uniformToFill\","
"\"backgroundImageAlignment\": \"center\","
"\"experimental.retroTerminalEffect\": false,"
"\"antialiasingMode\": \"grayscale\""
"\"suppressApplicationTitle\": false,"
"\"tabColor\": \"#0C0C0C\","
"\"tabTitle\": \"Cool Tab\","
"\"useAcrylic\": false"
"}"
};

Expand All @@ -160,9 +157,9 @@ namespace SettingsModelLocalTests

auto roundtripTest = [this](auto jsonString) {
const auto json{ VerifyParseSucceeded(jsonString) };
const auto globals{ implementation::Profile::FromJson(json) };
const auto profile{ implementation::Profile::FromJson(json) };

const auto result{ globals->ToJson() };
const auto result{ profile->ToJson() };

VERIFY_ARE_EQUAL(jsonString, toString(result));
};
Expand All @@ -173,47 +170,112 @@ namespace SettingsModelLocalTests

void SerializationTests::ColorScheme()
{
DebugBreak();
// This needs to be in alphabetical order.
const std::string schemeString{ "{"
"\"name\" : \"Campbell\","
"\"foreground\" : \"#F2F2F2\","
"\"background\" : \"#0C0C0C\","
"\"selectionBackground\" : \"#131313\","
"\"cursorColor\" : \"#FFFFFF\","
"\"black\" : \"#0C0C0C\","
"\"red\" : \"#C50F1F\","
"\"green\" : \"#13A10E\","
"\"yellow\" : \"#C19C00\""
"\"blue\" : \"#0037DA\","
"\"purple\" : \"#881798\","
"\"cyan\" : \"#3A96DD\","
"\"white\" : \"#CCCCCC\","
"\"brightBlack\" : \"#767676\","
"\"brightRed\" : \"#E74856\","
"\"brightGreen\" : \"#16C60C\","
"\"brightYellow\" : \"#F9F1A5\","
"\"brightBlue\" : \"#3B78FF\","
"\"brightPurple\" : \"#B4009E\","
"\"brightCyan\" : \"#61D6D6\","
"\"brightWhite\" : \"#F2F2F2\""
"\"background\": \"#0C0C0C\","
"\"black\": \"#0C0C0C\","
"\"blue\": \"#0037DA\","
"\"brightBlack\": \"#767676\","
"\"brightBlue\": \"#3B78FF\","
"\"brightCyan\": \"#61D6D6\","
"\"brightGreen\": \"#16C60C\","
"\"brightPurple\": \"#B4009E\","
"\"brightRed\": \"#E74856\","
"\"brightWhite\": \"#F2F2F2\","
"\"brightYellow\": \"#F9F1A5\","
"\"cursorColor\": \"#FFFFFF\","
"\"cyan\": \"#3A96DD\","
"\"foreground\": \"#F2F2F2\","
"\"green\": \"#13A10E\","
"\"name\": \"Campbell\","
"\"purple\": \"#881798\","
"\"red\": \"#C50F1F\","
"\"selectionBackground\": \"#131313\","
"\"white\": \"#CCCCCC\","
"\"yellow\": \"#C19C00\""
"}" };

const auto json{ VerifyParseSucceeded(schemeString) };
const auto profile{ implementation::ColorScheme::FromJson(json) };
const auto scheme{ implementation::ColorScheme::FromJson(json) };

const auto result{ profile->ToJson() };
VERIFY_ARE_EQUAL(json, result);
const auto result{ scheme->ToJson() };
VERIFY_ARE_EQUAL(schemeString, toString(result));
}

void SerializationTests::CascadiaSettings()
{
DebugBreak();
const std::string settingsString{ "" };
// clang-format off
// This needs to be in alphabetical order.
const std::string settingsString{ "{"
"\"$schema\": \"https://aka.ms/terminal-profiles-schema\","
"\"actions\": ["
"{\"command\": {\"action\": \"renameTab\",\"input\": \"Liang Tab\"},\"keys\": \"ctrl+t\"}"
"],"
"\"defaultProfile\": \"{61c54bbd-1111-5271-96e7-009a87ff44bf}\","
"\"keybindings\": ["
"{\"command\": {\"action\": \"sendInput\",\"input\": \"VT Griese Mode\"},\"keys\": \"ctrl+k\"}"
"],"
"\"profiles\": {"
"\"defaults\": {"
"\"fontFace\": \"Zamora Code\""
"},"
"\"list\": ["
"{"
"\"fontFace\": \"Cascadia Code\","
"\"guid\": \"{61c54bbd-1111-5271-96e7-009a87ff44bf}\","
"\"name\": \"HowettShell\""
"},"
"{"
"\"antialiasingMode\": \"aliased\","
"\"name\": \"NiksaShell\""
"},"
"{"
"\"name\": \"BhojwaniShell\","
"\"source\": \"local\""
"}"
"]"
"},"
"\"schemes\": ["
"{"
"\"background\": \"#3C0315\","
"\"black\": \"#282A2E\","
"\"blue\": \"#0170C5\","
"\"brightBlack\": \"#676E7A\","
"\"brightBlue\": \"#5C98C5\","
"\"brightCyan\": \"#8ABEB7\","
"\"brightGreen\": \"#B5D680\","
"\"brightPurple\": \"#AC79BB\","
"\"brightRed\": \"#BD6D85\","
"\"brightWhite\": \"#FFFFFD\","
"\"brightYellow\": \"#FFFD76\","
"\"cursorColor\": \"#FFFFFD\","
"\"cyan\": \"#3F8D83\","
"\"foreground\": \"#FFFFFD\","
"\"green\": \"#76AB23\","
"\"name\": \"Cinnamon Roll\","
"\"purple\": \"#7D498F\","
"\"red\": \"#BD0940\","
"\"selectionBackground\": \"#FFFFFF\","
"\"white\": \"#FFFFFD\","
"\"yellow\": \"#E0DE48\""
"}"
"]"
"}" };
// clang-format on

const auto json{ VerifyParseSucceeded(settingsString) };
const auto settings{ implementation::CascadiaSettings::FromJson(json) };
auto settings{ winrt::make_self<implementation::CascadiaSettings>(false) };
settings->_ParseJsonString(settingsString, false);
settings->_ApplyDefaultsFromUserSettings();
settings->LayerJson(settings->_userSettings);
settings->_ValidateSettings();

// TODO CARLOS:
// - temp/DebugBreak is only for testing. Remove after we're done here.
// - NiksaShell prints out a guid, when it shouldn't be
// - BhojwaniShell is missing entirely
const auto result{ settings->ToJson() };
VERIFY_ARE_EQUAL(json, result);
const auto temp{ toString(result) };
VERIFY_ARE_EQUAL(settingsString, toString(result));
}
}
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ void CascadiaSettings::_ValidateAllSchemesExist()
const auto schemeName = profile.ColorSchemeName();
if (!_globals->ColorSchemes().HasKey(schemeName))
{
profile.ColorSchemeName({ L"Campbell" });
// Clear the user set color scheme. We'll just fallback instead.
profile.ClearColorSchemeName();
foundInvalidScheme = true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Author(s):
// fwdecl unittest classes
namespace SettingsModelLocalTests
{
class SerializationTests;
class DeserializationTests;
class ProfileTests;
class ColorSchemeTests;
Expand Down Expand Up @@ -142,6 +143,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void _ValidateKeybindings();
void _ValidateNoGlobalsKey();

friend class SettingsModelLocalTests::SerializationTests;
friend class SettingsModelLocalTests::DeserializationTests;
friend class SettingsModelLocalTests::ProfileTests;
friend class SettingsModelLocalTests::ColorSchemeTests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,20 +1049,21 @@ Json::Value CascadiaSettings::ToJson() const
// "profiles" will always be serialized as an object
Json::Value profiles{ Json::ValueType::objectValue };
profiles[JsonKey(DefaultSettingsKey)] = _userDefaultProfileSettings->ToJson();
profiles[JsonKey(ProfilesListKey)] = { Json::ValueType::arrayValue };
Json::Value profilesList{ Json::ValueType::arrayValue };
for (const auto& entry : _profiles)
{
const auto prof{ winrt::get_self<implementation::Profile>(entry) };
json[JsonKey(ProfilesKey)].append(prof->ToJson());
profilesList.append(prof->ToJson());
}
profiles[JsonKey(ProfilesListKey)] = profilesList;
json[JsonKey(ProfilesKey)] = profiles;

// "schemes" will be an accumulation of _all_ the color schemes
Json::Value schemes{ Json::ValueType::arrayValue };
for (const auto& entry : _globals->ColorSchemes())
{
const auto scheme{ winrt::get_self<implementation::ColorScheme>(entry.Value()) };
json[JsonKey(SchemesKey)].append(scheme->ToJson());
schemes.append(scheme->ToJson());
}
json[JsonKey(SchemesKey)] = schemes;

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Json::Value GlobalAppSettings::ToJson() const
{
Json::Value json{ Json::ValueType::objectValue };

// Exclude DebugFeatures from the list below
// clang-format off
JsonUtils::SetValueForKey(json, DefaultProfileKey, _UnparsedDefaultProfile);
JsonUtils::SetValueForKey(json, AlwaysShowTabsKey, _AlwaysShowTabs);
Expand All @@ -370,7 +371,6 @@ Json::Value GlobalAppSettings::ToJson() const
JsonUtils::SetValueForKey(json, ThemeKey, _Theme);
JsonUtils::SetValueForKey(json, TabWidthModeKey, _TabWidthMode);
JsonUtils::SetValueForKey(json, SnapToGridOnResizeKey, _SnapToGridOnResize);
JsonUtils::SetValueForKey(json, DebugFeaturesKey, _DebugFeaturesEnabled);
JsonUtils::SetValueForKey(json, ForceFullRepaintRenderingKey, _ForceFullRepaintRendering);
JsonUtils::SetValueForKey(json, SoftwareRenderingKey, _SoftwareRendering);
JsonUtils::SetValueForKey(json, ForceVTInputKey, _ForceVTInput);
Expand Down
18 changes: 13 additions & 5 deletions src/cascadia/TerminalSettingsModel/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ namespace Microsoft::Terminal::Settings::Model::JsonUtils
template<typename T, typename Converter>
void SetValueForKey(Json::Value& json, std::string_view key, const T& target, Converter&& conv)
{
// demand guarantees that it will return a value or throw an exception
auto out{ json.demand(&*key.cbegin(), (&*key.cbegin()) + key.size()) };
std::optional<Json::Value> out;
if constexpr (Detail::DeduceOptional<T>::IsOptional)
{
// FOR OPTION TYPES
Expand All @@ -720,19 +719,28 @@ namespace Microsoft::Terminal::Settings::Model::JsonUtils
// We consider them optional so that they can be zero-constructed
// and we store it as an empty string
// However, we need to differentiate between hstring and optional<hstring>
*out = conv.ToJson(target);
out = conv.ToJson(target);
}
else
{
if (target)
{
*out = conv.ToJson(*target);
out = conv.ToJson(*target);
}
}
}
else
{
*out = conv.ToJson(target);
out = conv.ToJson(target);
}

if (out)
{
// demand guarantees that it will return a value or throw an exception
// _Only_ call it if we are writing a value. Otherwise, we'll write the key
// in the json, and initialize it's value to "null". Instead, we use "out"
// to completely omit writing this setting if we don't have a value.
*json.demand(&*key.cbegin(), (&*key.cbegin()) + key.size()) = *out;
}
}

Expand Down

0 comments on commit 14bf652

Please sign in to comment.