gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

commit 66459fadd8ebf94fd0b6347acfe04e6725dec7c3
parent 3a5781e8fa75436b0932a283aeb9e331ce5df9d8
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 24 Jul 2025 14:50:29 -0700

Add tests for print_xml_encoded_len

No tests added for print_xml_encoded, since it just delegates to
print_xml_encoded_len with length of -1.

Diffstat:
Mformat_tests.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 88 insertions(+), 0 deletions(-)

diff --git a/format_tests.c b/format_tests.c @@ -149,3 +149,91 @@ UTEST(print_percent_encoded, EmptyString) { free(buf); } + +UTEST(print_xml_encoded_len, FullEncoding) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + const char* test_str = "<tag attr='value'> & \"text\""; + print_xml_encoded_len(out, test_str, -1, true); + fclose(out); + + EXPECT_STREQ("&lt;tag attr=&#39;value&#39;&gt; &amp; &quot;text&quot;", buf); + + free(buf); +} + +UTEST(print_xml_encoded_len, PartialEncoding) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + const char* test_str = "<tag>hello</tag>"; + print_xml_encoded_len(out, test_str, 5, true); + fclose(out); + + EXPECT_STREQ("&lt;tag&gt;", buf); + + free(buf); +} + +UTEST(print_xml_encoded_len, WithCrlf) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + const char* test_str = "line1\r\nline2"; + print_xml_encoded_len(out, test_str, -1, true); + fclose(out); + + EXPECT_STREQ("line1\r\nline2", buf); + + free(buf); +} + +UTEST(print_xml_encoded_len, WithoutCrlf) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + const char* test_str = "line1\r\nline2"; + print_xml_encoded_len(out, test_str, -1, false); + fclose(out); + + EXPECT_STREQ("line1line2", buf); + + free(buf); +} + +UTEST(print_xml_encoded_len, EmptyString) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + print_xml_encoded_len(out, "", -1, true); + fclose(out); + + EXPECT_STREQ("", buf); + + free(buf); +} + +UTEST(print_xml_encoded_len, ZeroLength) { + char* buf = NULL; + size_t size = 0; + FILE* out = open_memstream(&buf, &size); + ASSERT_NE(NULL, out); + + print_xml_encoded_len(out, "some text", 0, true); + fclose(out); + + EXPECT_STREQ("", buf); + + free(buf); +}