2022-02-11 01:32:43 +01:00
|
|
|
#include <StringTools/StringTools.h>
|
|
|
|
#include "Catch2.h"
|
2021-11-20 19:24:35 +01:00
|
|
|
|
2022-02-11 01:32:43 +01:00
|
|
|
// Tests that replacing something in an empty string returns an empty string
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/EmptyString", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "burger", "Subst");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing a string to an empty string works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Single_ReplaceToEmpty", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Squarepants";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "Squarepants", "");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing to a single char works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Single_ReplaceToSingleChar", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Squarepants";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "Squarepants", "i");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "i");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing to a single char works, passing a char
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Single_ReplaceToSingleChar_AsChar", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Oilbanger";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "Oilbanger", 'i');
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "i");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing the find to something longer works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Single_ReplaceToLonger", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "LittleDong";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "Dong", "Binger");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "LittleBinger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing a string to an empty string works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Multiple_ReplaceToEmpty", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "The fucking dogs are fucking eating the fucking chicken.";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "fucking ", "");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "The dogs are eating the chicken.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing to a single char works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle.";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "smeary", "i");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing to a single char works, passing a char
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Multiple_ReplaceToSingleChar_AsChar", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Oilbsmearynger, Bsmearyngerfsmearynger, Lsmearyttle brsmearyttle osmearyly sksmearyttle.";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "smeary", 'i');
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "Oilbinger, Bingerfinger, Little brittle oily skittle.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing the find to something longer works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Multiple_ReplaceToLonger", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "honk honk honk honk honk honk honk honk";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "honk", "hallery");
|
2021-11-20 19:24:35 +01:00
|
|
|
|
2022-02-11 01:32:43 +01:00
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "hallery hallery hallery hallery hallery hallery hallery hallery");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the replacer ignores chars put in by the replacer
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/ReplacerIgnoresReplaced", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "honk honk honk honk honk honk honk honk";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "honk", "honka");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "honka honka honka honka honka honka honka honka");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that replacing successive findings works
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Replace_Successive", "[ReplaceString]")
|
2021-11-20 19:24:35 +01:00
|
|
|
{
|
2022-02-11 01:32:43 +01:00
|
|
|
// Setup
|
|
|
|
const std::string in = "honkhonkhonkhonkhonkhonkhonkhonk";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "honk", "hallery");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "halleryhalleryhalleryhalleryhalleryhalleryhalleryhallery");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that if find.length() == 0, it returns just the input
|
2022-02-11 09:36:29 +01:00
|
|
|
TEST_CASE(__FILE__"/Find_Length0_Returns_Input", "[ReplaceString]")
|
2022-02-11 01:32:43 +01:00
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
const std::string in = "Littled";
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
const std::string out = StringTools::Replace(in, "", "binger");
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
REQUIRE(out == "Littled");
|
|
|
|
return;
|
2021-11-20 19:24:35 +01:00
|
|
|
}
|