Throw if constructing a non-English multilingual message

This matches the behaviour for when converting from YAML, ensuring that
a non-English multilingual message can never be created.
This commit is contained in:
Oliver Hamlet
2016-03-28 11:16:15 +01:00
parent a48a936c22
commit 7d3080c948
2 changed files with 20 additions and 1 deletions
+12 -1
View File
@@ -24,6 +24,7 @@
#include "message.h"
#include "../helpers/language.h"
#include "../error.h"
#include <boost/log/trivial.hpp>
@@ -42,7 +43,17 @@ namespace loot {
}
Message::Message(const unsigned int type, const std::vector<MessageContent>& content,
const std::string& condition) : _type(type), _content(content), ConditionalMetadata(condition) {}
const std::string& condition) : _type(type), _content(content), ConditionalMetadata(condition) {
if (content.size() > 1) {
bool englishStringExists = false;
for (const auto &mc : content) {
if (mc.Language() == loot::Language::english)
englishStringExists = true;
}
if (!englishStringExists)
throw loot::error(error::invalid_args, "bad conversion: multilingual messages must contain an English content string");
}
}
bool Message::operator < (const Message& rhs) const {
if (!_content.empty() && !rhs.Content().empty())
@@ -71,6 +71,14 @@ namespace loot {
EXPECT_EQ("condition1", message.Condition());
}
TEST_P(MessageTest, vectorContentConstructorShouldThrowIfMultipleContentStringsAreGivenAndNoneAreEnglish) {
MessageContents contents({
MessageContent("content1", Language::german),
MessageContent("content2", Language::french),
});
EXPECT_ANY_THROW(Message(Message::error, contents, "condition1"));
}
TEST_P(MessageTest, messagesWithDifferentContentStringsShouldBeUnequal) {
Message message1(Message::say, "content1", "condition1");
Message message2(Message::say, "content2", "condition1");