Make File::GetDisplayName() a trivial accessor

Don't escape any markdown characters or return the name field if the
display name is empty. They're really presentation issues so are left to
the UI.
This commit is contained in:
Oliver Hamlet
2022-02-19 13:45:53 +00:00
parent 77decb26c4
commit d4de531de5
6 changed files with 7 additions and 76 deletions
+1 -6
View File
@@ -84,12 +84,7 @@ public:
/**
* Get the display name of the file.
*
* If the File was constructed with an empty display string, the name field
* will be returned instead, with any `ASCII punctuation characters
* <https://github.github.com/gfm/#ascii-punctuation-character>`_ escaped.
* Escaping is not performed if returning the value of the display string.
* @return The file's display name or filename.
* @return The file's display name.
*/
LOOT_API std::string GetDisplayName() const;
-8
View File
@@ -58,14 +58,6 @@ static constexpr const char* pseudosemVersionRegex =
'v' or 'version:. */
static constexpr const char* digitsVersionRegex = R"((?:^|v|version:\s*)(\d+))";
std::string EscapeMarkdownASCIIPunctuation(const std::string& text) {
// As defined by <https://github.github.com/gfm/#ascii-punctuation-character>.
static const regex asciiPunctuationCharacters(
"([!\"#$%&'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~])");
return std::regex_replace(text, asciiPunctuationCharacters, "\\$1");
}
std::vector<Tag> ExtractBashTags(const std::string& description) {
std::vector<Tag> tags;
-2
View File
@@ -36,8 +36,6 @@ static constexpr const char* GHOST_FILE_EXTENSION = ".ghost";
static constexpr std::size_t GHOST_FILE_EXTENSION_LENGTH =
std::char_traits<char>::length(GHOST_FILE_EXTENSION);
std::string EscapeMarkdownASCIIPunctuation(const std::string& text);
std::vector<Tag> ExtractBashTags(const std::string& description);
std::optional<std::string> ExtractVersion(const std::string& text);
-4
View File
@@ -73,10 +73,6 @@ bool File::operator==(const File& rhs) const {
Filename File::GetName() const { return name_; }
std::string File::GetDisplayName() const {
if (display_.empty()) {
return EscapeMarkdownASCIIPunctuation(std::string(name_));
}
return display_;
}
+3 -8
View File
@@ -45,9 +45,7 @@ struct convert<loot::File> {
if (rhs.IsConditional())
node["condition"] = rhs.GetCondition();
auto escapedName =
loot::EscapeMarkdownASCIIPunctuation(std::string(rhs.GetName()));
if (rhs.GetDisplayName() != escapedName) {
if (!rhs.GetDisplayName().empty()) {
node["display"] = rhs.GetDisplayName();
}
@@ -118,11 +116,8 @@ struct convert<loot::File> {
};
inline Emitter& operator<<(Emitter& out, const loot::File& rhs) {
auto escapedName =
loot::EscapeMarkdownASCIIPunctuation(std::string(rhs.GetName()));
if (!rhs.IsConditional() && rhs.GetDetail().empty() &&
(rhs.GetDisplayName().empty() || rhs.GetDisplayName() == escapedName))
rhs.GetDisplayName().empty())
out << YAML::SingleQuoted << std::string(rhs.GetName());
else {
out << BeginMap << Key << "name" << Value << YAML::SingleQuoted
@@ -132,7 +127,7 @@ inline Emitter& operator<<(Emitter& out, const loot::File& rhs) {
out << Key << "condition" << Value << YAML::SingleQuoted
<< rhs.GetCondition();
if (rhs.GetDisplayName() != escapedName)
if (!rhs.GetDisplayName().empty())
out << Key << "display" << Value << YAML::SingleQuoted
<< rhs.GetDisplayName();
+3 -48
View File
@@ -342,32 +342,12 @@ TEST(
EXPECT_TRUE(file1 >= file2);
}
TEST(File, getDisplayNameShouldReturnDisplayStringIfItIsNotEmpty) {
TEST(File, getDisplayNameShouldReturnDisplayString) {
File file("name", "display");
EXPECT_EQ("display", file.GetDisplayName());
}
TEST(File, getDisplayNameShouldReturnNameStringIfDisplayStringIsEmpty) {
File file("name", "");
EXPECT_EQ("name", file.GetDisplayName());
}
TEST(File, getDisplayNameShouldNotEscapeASCIIPunctuationInDisplayString) {
auto display = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
File file("name", display);
EXPECT_EQ(display, file.GetDisplayName());
}
TEST(File, getDisplayNameShouldEscapeASCIIPunctuationInNameString) {
File file("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
EXPECT_EQ(
R"raw(\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~)raw",
file.GetDisplayName());
}
TEST(File, emittingAsYamlShouldSingleQuoteValues) {
File file(
"name1", "display1", "condition1", {MessageContent("english", "en")});
@@ -389,16 +369,6 @@ TEST(File, emittingAsYamlShouldOutputAsAScalarIfOnlyTheNameStringIsNotEmpty) {
EXPECT_EQ("'" + std::string(file.GetName()) + "'", emitter.c_str());
}
TEST(
File,
emittingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameFieldAfterEscapingASCIIPunctuation) {
File file("file.esp", "file\\.esp");
YAML::Emitter emitter;
emitter << file;
EXPECT_STREQ("'file.esp'", emitter.c_str());
}
TEST(File, emittingAsYamlShouldOmitAnEmptyConditionString) {
File file("name1", "display1");
YAML::Emitter emitter;
@@ -453,19 +423,6 @@ TEST(File, encodingAsYamlShouldOmitEmptyFields) {
EXPECT_FALSE(node["detail"]);
}
TEST(
File,
encodingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameFieldAfterEscapingASCIIPunctuation) {
File file("file.esp", "file\\.esp");
YAML::Node node;
node = file;
EXPECT_EQ(std::string(file.GetName()), node["name"].as<std::string>());
EXPECT_FALSE(node["display"]);
EXPECT_FALSE(node["condition"]);
EXPECT_FALSE(node["detail"]);
}
TEST(File, decodingFromYamlShouldSetDataCorrectly) {
YAML::Node node = YAML::Load(
"{name: name1, display: display1, condition: 'file(\"Foo.esp\")', "
@@ -528,14 +485,12 @@ TEST(
EXPECT_THROW(node.as<File>(), YAML::RepresentationException);
}
TEST(
File,
decodingFromYamlScalarShouldUseNameValueForDisplayNameAndLeaveConditionEmpty) {
TEST(File, decodingFromYamlScalarShouldLeaveDisplayNameAndConditionEmpty) {
YAML::Node node = YAML::Load("name1");
File file = node.as<File>();
EXPECT_EQ(node.as<std::string>(), std::string(file.GetName()));
EXPECT_EQ(node.as<std::string>(), file.GetDisplayName());
EXPECT_TRUE(file.GetDisplayName().empty());
EXPECT_TRUE(file.GetCondition().empty());
EXPECT_TRUE(file.GetDetail().empty());
}