From d0e35e15f798cd2c1bd8df46de047ab5803e910d Mon Sep 17 00:00:00 2001 From: Travis Yaeger Date: Thu, 23 May 2019 21:30:26 -0700 Subject: [PATCH] Add more Tests for variable_substitute --- tests/test-common.cc | 94 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/tests/test-common.cc b/tests/test-common.cc index f9ed6414..11ddab59 100644 --- a/tests/test-common.cc +++ b/tests/test-common.cc @@ -34,11 +34,45 @@ #include #include +std::string get_valid_environment_variable_name() { + if (getenv("HOME") != nullptr) { return "HOME"; } + + // If HOME is not a valid environment variable name, try to get a valid one. + char *env_var = *environ; + + for (int i = 1; env_var; i++) { + std::string variable_name(env_var); + int variable_name_length = variable_name.find('='); + variable_name = variable_name.substr(0, variable_name_length); + + if (getenv(variable_name.c_str()) != nullptr) { return variable_name; } + + env_var = *(environ + i); + } + + return ""; +} + +std::string get_invalid_environment_variable_name() { + std::string variable_name = "INVALIDVARIABLENAME"; + + while (getenv(variable_name.c_str()) != nullptr) { + variable_name += std::to_string(variable_name.length()); + } + + return variable_name; +} + TEST_CASE("to_real_path becomes homedir", "[to_real_path]") { REQUIRE(to_real_path("~/test") == std::string(getenv("HOME")) + "/test"); } -TEST_CASE("variables are substituted correctly", "[variable_substitute]") { +TEST_CASE("environment variables are substituted correctly", + "[variable_substitute]") { + std::string valid_name = get_valid_environment_variable_name(); + std::string valid_value = getenv(valid_name.c_str()); + std::string invalid_name = get_invalid_environment_variable_name(); + SECTION("an empty string input returns an empty string") { REQUIRE(variable_substitute("") == ""); } @@ -47,10 +81,68 @@ TEST_CASE("variables are substituted correctly", "[variable_substitute]") { std::string string_alpha = "abcdefghijklmnopqrstuvwxyz"; std::string string_numbers = "1234567890"; std::string string_special = "`~!@#$%^&*()-=_+[]{}\\|;:'\",<.>/?"; + std::string string_valid_name = valid_name; REQUIRE(variable_substitute(string_alpha) == string_alpha); REQUIRE(variable_substitute(string_numbers) == string_numbers); REQUIRE(variable_substitute(string_special) == string_special); + REQUIRE(variable_substitute(string_valid_name) == string_valid_name); + } + + SECTION("invalid variables are removed from return string") { + std::string string_in_1 = "a$" + invalid_name + " z"; + std::string string_in_2 = "a${" + invalid_name + "} z"; + std::string string_in_3 = "a${" + invalid_name + " " + valid_name + "} z"; + std::string string_in_4 = "a${ " + valid_name + "} z"; + std::string string_in_5 = "a${" + valid_name + " } z"; + std::string string_in_6 = "a$" + valid_name + "z z"; + std::string string_in_7 = "a$" + invalid_name + "# z"; + + REQUIRE(variable_substitute(string_in_1) == "a z"); + REQUIRE(variable_substitute(string_in_2) == "a z"); + REQUIRE(variable_substitute(string_in_3) == "a z"); + REQUIRE(variable_substitute(string_in_4) == "a z"); + REQUIRE(variable_substitute(string_in_5) == "a z"); + REQUIRE(variable_substitute(string_in_6) == "a z"); + REQUIRE(variable_substitute(string_in_7) == "a# z"); + } + + SECTION("valid variable gets replaced in the return string") { + std::string string_in_1 = "a$" + valid_name + " z"; + std::string string_in_2 = "a${" + valid_name + "} z"; + std::string string_in_3 = "a$" + valid_name + "# z"; + + std::string string_var_replaced_1 = "a" + valid_value + " z"; + std::string string_var_replaced_2 = "a" + valid_value + " z"; + std::string string_var_replaced_3 = "a" + valid_value + "# z"; + + REQUIRE(variable_substitute(string_in_1) == string_var_replaced_1); + REQUIRE(variable_substitute(string_in_2) == string_var_replaced_2); + REQUIRE(variable_substitute(string_in_3) == string_var_replaced_3); + } + + SECTION("$ without variable is ignored") { + std::string string_in_1 = "a$#z"; + std::string string_in_2 = "a$2z"; + + REQUIRE(variable_substitute(string_in_1) == string_in_1); + REQUIRE(variable_substitute(string_in_2) == string_in_2); + } + + SECTION("double $ gets converted to single $ and is passed over") { + std::string string_in_1 = "a$$sz"; + std::string string_in_2 = "a$$" + valid_name + "z"; + std::string string_out_1 = "a$sz"; + std::string string_out_2 = "a$" + valid_name + "z"; + + REQUIRE(variable_substitute(string_in_1) == string_out_1); + REQUIRE(variable_substitute(string_in_2) == string_out_2); + } + + SECTION("incomplete variable does not get replaced in return string") { + std::string string_in = "a${" + valid_name + " z"; + + REQUIRE(variable_substitute(string_in) == string_in); } }