rhubarb-lip-sync/tests/stringToolsTests.cpp

74 lines
2.9 KiB
C++
Raw Normal View History

#include <gmock/gmock.h>
#include "stringTools.h"
using namespace testing;
// splitIntoLines
TEST(splitIntoLines, splitsOnLineBreaks) {
EXPECT_THAT(splitIntoLines("this\nis\r\na\r\ntest"), ElementsAre("this", "is", "a", "test"));
}
TEST(splitIntoLines, handlesEmptyElements) {
EXPECT_THAT(splitIntoLines("\n1\n\n\n2\n"), ElementsAre("", "1", "", "", "2", ""));
EXPECT_THAT(splitIntoLines("\n"), ElementsAre("", ""));
EXPECT_THAT(splitIntoLines(""), ElementsAre(""));
}
// wrapSingleLineString
TEST(wrapSingleLineString, basic) {
const char* lipsum = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.";
EXPECT_THAT(wrapSingleLineString(lipsum, 30), ElementsAre("Lorem ipsum dolor sit amet,", "consectetur adipisici elit,", "sed eiusmod tempor incidunt ut", "labore et dolore magna aliqua."));
}
TEST(wrapSingleLineString, preciseWrapPosition) {
const char* test = "a b c";
EXPECT_THAT(wrapSingleLineString(test, 5), ElementsAre("a b c"));
EXPECT_THAT(wrapSingleLineString(test, 4), ElementsAre("a b", "c"));
EXPECT_THAT(wrapSingleLineString(test, 3), ElementsAre("a b", "c"));
EXPECT_THAT(wrapSingleLineString(test, 2), ElementsAre("a", "b", "c"));
EXPECT_THAT(wrapSingleLineString(test, 1), ElementsAre("a", "b", "c"));
}
TEST(wrapSingleLineString, overlongLines) {
EXPECT_THAT(wrapSingleLineString("aaa bbbb ccc", 3), ElementsAre("aaa", "bbb", "b", "ccc"));
EXPECT_THAT(wrapSingleLineString("aaa bbbb c", 3), ElementsAre("aaa", "bbb", "b c"));
EXPECT_THAT(wrapSingleLineString("a bbbb c", 5), ElementsAre("a", "bbbb", "c"));
EXPECT_THAT(wrapSingleLineString("aa b", 1), ElementsAre("a", "a", "b"));
}
TEST(wrapSingleLineString, discardingSpacesAtWrapPositionsAndEnd) {
const char* test = " a b c ";
EXPECT_THAT(wrapSingleLineString(test, 20), ElementsAre(" a b c"));
EXPECT_THAT(wrapSingleLineString(test, 6), ElementsAre(" a b", "c"));
EXPECT_THAT(wrapSingleLineString(test, 5), ElementsAre(" a", "b c"));
EXPECT_THAT(wrapSingleLineString(test, 4), ElementsAre(" a", "b c"));
EXPECT_THAT(wrapSingleLineString(test, 3), ElementsAre(" a", "b", "c"));
EXPECT_THAT(wrapSingleLineString(test, 2), ElementsAre("", "a", "b", "c"));
}
TEST(wrapSingleLineString, errorHandling) {
EXPECT_NO_THROW(wrapSingleLineString("test", 1));
EXPECT_NO_THROW(wrapSingleLineString("", 1));
// Throw if lineLength < 1
EXPECT_ANY_THROW(wrapSingleLineString("test", 0));
EXPECT_ANY_THROW(wrapSingleLineString("", 0));
EXPECT_ANY_THROW(wrapSingleLineString("test", -1));
EXPECT_ANY_THROW(wrapSingleLineString("", -1));
// Throw if string contains tabs
EXPECT_ANY_THROW(wrapSingleLineString("a\tb", 10));
// Throw if string contains line breaks
EXPECT_ANY_THROW(wrapSingleLineString("a\nb", 10));
}
// wrapString
TEST(wrapString, basic) {
EXPECT_THAT(wrapString("\n\nLine no 3\n\nLine no 4\n", 8), ElementsAre("", "", "Line no", "3", "", "Line no", "4", ""));
}