namespace {
+#define TAB "\t"
+#define NEWLINE "\n"
+#define BACKSLASH R"(\)"
+#define QUOTE R"(")"
+
TEST(TextWordGet, NullTerminator) {
char textStr[] = "Word";
spv_text_t text = {textStr, strlen(textStr)};
}
}
+TEST(TextWordGet, QuotesAreKept) {
+ AutoText input(R"("quotes" "around words")");
+ const char *expected[] = {R"("quotes")", R"("around words")"};
+
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ EXPECT_EQ(8, endPosition.column);
+ EXPECT_EQ(0, endPosition.line);
+ EXPECT_EQ(8, endPosition.index);
+ EXPECT_STREQ(expected[0], word.c_str());
+
+ // Move to the next word.
+ startPosition = endPosition;
+ startPosition.index++;
+ startPosition.column++;
+
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ EXPECT_EQ(23, endPosition.column);
+ EXPECT_EQ(0, endPosition.line);
+ EXPECT_EQ(23, endPosition.index);
+ EXPECT_STREQ(expected[1], word.c_str());
+}
+
+TEST(TextWordGet, QuotesBetweenWordsActLikeGlue) {
+ AutoText input(R"(quotes" "between words)");
+ const char *expected[] = {R"(quotes" "between)", "words"};
+
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ EXPECT_EQ(16, endPosition.column);
+ EXPECT_EQ(0, endPosition.line);
+ EXPECT_EQ(16, endPosition.index);
+ EXPECT_STREQ(expected[0], word.c_str());
+
+ // Move to the next word.
+ startPosition = endPosition;
+ startPosition.index++;
+ startPosition.column++;
+
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ EXPECT_EQ(22, endPosition.column);
+ EXPECT_EQ(0, endPosition.line);
+ EXPECT_EQ(22, endPosition.index);
+ EXPECT_STREQ(expected[1], word.c_str());
+}
+
+TEST(TextWordGet, QuotingWhitespace) {
+ // Whitespace surrounded by quotes acts like glue.
+ AutoText input(QUOTE "white " NEWLINE TAB " space" QUOTE);
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ EXPECT_EQ(input.str.length(), endPosition.column);
+ EXPECT_EQ(0, endPosition.line);
+ EXPECT_EQ(input.str.length(), endPosition.index);
+ EXPECT_EQ(input.str, word);
+}
+
+TEST(TextWordGet, QuoteAlone) {
+ AutoText input(QUOTE);
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ ASSERT_EQ(1, endPosition.column);
+ ASSERT_EQ(0, endPosition.line);
+ ASSERT_EQ(1, endPosition.index);
+ ASSERT_STREQ(QUOTE, word.c_str());
+}
+
+TEST(TextWordGet, EscapeAlone) {
+ AutoText input(BACKSLASH);
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ ASSERT_EQ(1, endPosition.column);
+ ASSERT_EQ(0, endPosition.line);
+ ASSERT_EQ(1, endPosition.index);
+ ASSERT_STREQ(BACKSLASH, word.c_str());
+}
+
+TEST(TextWordGet, EscapeAtEndOfInput) {
+ AutoText input("word" BACKSLASH);
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ ASSERT_EQ(5, endPosition.column);
+ ASSERT_EQ(0, endPosition.line);
+ ASSERT_EQ(5, endPosition.index);
+ ASSERT_STREQ("word" BACKSLASH, word.c_str());
+}
+
+TEST(TextWordGet, Escaping) {
+ AutoText input("w" BACKSLASH QUOTE "o" BACKSLASH NEWLINE "r" BACKSLASH ";d");
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ ASSERT_EQ(10, endPosition.column);
+ ASSERT_EQ(0, endPosition.line);
+ ASSERT_EQ(10, endPosition.index);
+ ASSERT_EQ(input.str, word);
+}
+
+TEST(TextWordGet, EscapingEscape) {
+ AutoText input("word" BACKSLASH BACKSLASH " abc");
+ std::string word;
+ spv_position_t startPosition = {};
+ spv_position_t endPosition = {};
+ ASSERT_EQ(SPV_SUCCESS,
+ spvTextWordGet(input, &startPosition, word, &endPosition));
+ ASSERT_EQ(6, endPosition.column);
+ ASSERT_EQ(0, endPosition.line);
+ ASSERT_EQ(6, endPosition.index);
+ ASSERT_STREQ("word" BACKSLASH BACKSLASH, word.c_str());
+}
+
} // anonymous namespace