commit 97c4eab1680b740b6b21e11cff9983f66f12d6bc
parent 66459fadd8ebf94fd0b6347acfe04e6725dec7c3
Author: Chris Bracken <chris@bracken.jp>
Date: Thu, 24 Jul 2025 19:21:50 -0700
Add tests for print_gopher_text_len
No tests added for print_gopher_text since it just delegates to
print_gopher_text_len with a length of -1.
Diffstat:
M | format.c | | | 2 | +- |
M | format_tests.c | | | 98 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 99 insertions(+), 1 deletion(-)
diff --git a/format.c b/format.c
@@ -129,7 +129,7 @@ void print_gopher_text_len(FILE* out,
} else if (c == '\t') {
fprintf(out, " ");
} else if (c == '\r' || (!output_lf && c == '\n')) {
- // Ignore.
+ /* Ignore. */
} else {
fprintf(out, "%c", c);
}
diff --git a/format_tests.c b/format_tests.c
@@ -237,3 +237,101 @@ UTEST(print_xml_encoded_len, ZeroLength) {
free(buf);
}
+
+UTEST(print_gopher_text_len, BasicWithLf) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "hello\nworld", -1, true);
+ fclose(out);
+
+ EXPECT_STREQ("hello\nworld", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, BasicNoLf) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "hello\nworld\r", -1, false);
+ fclose(out);
+
+ EXPECT_STREQ("helloworld", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, EscapesWithLf) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "[hello]\n[world]", -1, true);
+ fclose(out);
+
+ EXPECT_STREQ("[|hello]\n[|world]", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, DoesNotEscapeIfBracketNotImmediatelyAfterLf) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "[hello]\n\t[world]", -1, true);
+ fclose(out);
+
+ EXPECT_STREQ("[|hello]\n [world]", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, NoEscapesWithoutLf) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "[hello]\n\t[world]", -1, false);
+ fclose(out);
+
+ EXPECT_STREQ("[hello] [world]", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, PartialLen) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "[hello]\n\t[world]", 8, true);
+ fclose(out);
+
+ EXPECT_STREQ("[|hello]\n", buf);
+
+ free(buf);
+}
+
+UTEST(print_gopher_text_len, EmptyString) {
+ char* buf = NULL;
+ size_t size = 0;
+ FILE* out = open_memstream(&buf, &size);
+ ASSERT_NE(NULL, out);
+
+ print_gopher_text_len(out, "", -1, true);
+ fclose(out);
+
+ EXPECT_STREQ("", buf);
+
+ free(buf);
+}