From 519f1f8031687ebf3853817a8b2e3557c2443d67 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 27 Jun 2016 03:01:20 +0200 Subject: content-parser: rewrote inline parser. parser is stricter now, and won't produce invalid HTML anymore. --- tests/check_content_parser.c | 162 ++++++++++++++++++++++++++++++++++--------- tests/check_utils.c | 33 +++++++++ 2 files changed, 161 insertions(+), 34 deletions(-) (limited to 'tests') diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c index 783d3f3..b0272fb 100644 --- a/tests/check_content_parser.c +++ b/tests/check_content_parser.c @@ -184,7 +184,7 @@ test_content_parse(void **state) "
seis
\n" "

bola\n" "chunda

\n" - "

bola
\n" + "

bola
\n" "guda\n" "buga

\n" "
asd
\n" @@ -276,7 +276,7 @@ test_content_parse_crlf(void **state) "
seis
\r\n" "

bola\r\n" "chunda

\r\n" - "

bola
\r\n" + "

bola
\r\n" "guda\r\n" "buga

\r\n" "
asd
\r\n" @@ -1485,14 +1485,13 @@ test_content_parse_invalid_code(void **state) static void test_content_parse_invalid_horizontal_rule(void **state) { - // this generates invalid html, but... char *html = blogc_content_parse("** asd", NULL, NULL); assert_non_null(html); - assert_string_equal(html, "

asd

\n"); + assert_string_equal(html, "

** asd

\n"); free(html); html = blogc_content_parse("** asd\n", NULL, NULL); assert_non_null(html); - assert_string_equal(html, "

asd

\n"); + assert_string_equal(html, "

** asd

\n"); free(html); } @@ -1500,13 +1499,12 @@ test_content_parse_invalid_horizontal_rule(void **state) static void test_content_parse_invalid_unordered_list(void **state) { - // more invalid html char *html = blogc_content_parse( "* asd\n" "1. qwe", NULL, NULL); assert_non_null(html); assert_string_equal(html, - "

asd\n" + "

* asd\n" "1. qwe

\n"); free(html); html = blogc_content_parse( @@ -1515,7 +1513,7 @@ test_content_parse_invalid_unordered_list(void **state) "\n", NULL, NULL); assert_non_null(html); assert_string_equal(html, - "

asd\n" + "

* asd\n" "1. qwe

\n"); free(html); html = blogc_content_parse( @@ -1523,7 +1521,7 @@ test_content_parse_invalid_unordered_list(void **state) "1. qwe\n", NULL, NULL); assert_non_null(html); assert_string_equal(html, - "

asd\n" + "

* asd\n" "1. qwe" "

\n"); free(html); @@ -1532,7 +1530,7 @@ test_content_parse_invalid_unordered_list(void **state) "1. qwe\n", NULL, NULL); assert_non_null(html); assert_string_equal(html, - "

asd\n" + "

* asd\n" "1. qwe" "

\n"); free(html); @@ -1546,7 +1544,7 @@ test_content_parse_invalid_unordered_list(void **state) assert_non_null(html); assert_string_equal(html, "

chunda

\n" - "

asd\n" + "

* asd\n" "1. qwe

\n" "

poi

\n"); free(html); @@ -1556,14 +1554,13 @@ test_content_parse_invalid_unordered_list(void **state) static void test_content_parse_invalid_ordered_list(void **state) { - // more invalid html char *html = blogc_content_parse( "1. asd\n" "* qwe", NULL, NULL); assert_non_null(html); assert_string_equal(html, "

1. asd\n" - " qwe

\n"); + "* qwe

\n"); free(html); html = blogc_content_parse( "1. asd\n" @@ -1572,7 +1569,7 @@ test_content_parse_invalid_ordered_list(void **state) assert_non_null(html); assert_string_equal(html, "

1. asd\n" - " qwe

\n"); + "* qwe

\n"); free(html); html = blogc_content_parse( "1. asd\n" @@ -1580,7 +1577,7 @@ test_content_parse_invalid_ordered_list(void **state) assert_non_null(html); assert_string_equal(html, "

1. asd\n" - " qwe" + "* qwe" "

\n"); free(html); html = blogc_content_parse( @@ -1589,7 +1586,7 @@ test_content_parse_invalid_ordered_list(void **state) assert_non_null(html); assert_string_equal(html, "

1. asd\n" - " qwe" + "* qwe" "

\n"); free(html); html = blogc_content_parse( @@ -1603,7 +1600,7 @@ test_content_parse_invalid_ordered_list(void **state) assert_string_equal(html, "

chunda

\n" "

1. asd\n" - " qwe

\n" + "* qwe

\n" "

poi

\n"); free(html); html = blogc_content_parse( @@ -1612,7 +1609,7 @@ test_content_parse_invalid_ordered_list(void **state) assert_non_null(html); assert_string_equal(html, "

1 asd\n" - " qwe

\n"); + "* qwe

\n"); free(html); html = blogc_content_parse( "a. asd\n" @@ -1677,6 +1674,10 @@ test_content_parse_inline_em(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); + html = blogc_content_parse_inline("*bo\\*la*\n"); + assert_non_null(html); + assert_string_equal(html, "bo*la\n"); + free(html); html = blogc_content_parse_inline("_bola_"); assert_non_null(html); assert_string_equal(html, "bola"); @@ -1685,14 +1686,25 @@ test_content_parse_inline_em(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); + html = blogc_content_parse_inline("_bo\\*la_\n"); + assert_non_null(html); + assert_string_equal(html, "bo*la\n"); + free(html); html = blogc_content_parse_inline("_**bola**_\n"); assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); - // this is not really valid + html = blogc_content_parse_inline("_**bo\\_\\*la**_\n"); + assert_non_null(html); + assert_string_equal(html, "bo_*la\n"); + free(html); html = blogc_content_parse_inline("_**bola\n"); assert_non_null(html); - assert_string_equal(html, "bola\n"); + assert_string_equal(html, "_**bola\n"); + free(html); + html = blogc_content_parse_inline("**_bola\\*\n"); + assert_non_null(html); + assert_string_equal(html, "**_bola*\n"); free(html); } @@ -1708,6 +1720,10 @@ test_content_parse_inline_strong(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); + html = blogc_content_parse_inline("**bo\*la**\n"); + assert_non_null(html); + assert_string_equal(html, "bo*la\n"); + free(html); html = blogc_content_parse_inline("__bola__"); assert_non_null(html); assert_string_equal(html, "bola"); @@ -1716,14 +1732,25 @@ test_content_parse_inline_strong(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); + html = blogc_content_parse_inline("__bo\*la__\n"); + assert_non_null(html); + assert_string_equal(html, "bo*la\n"); + free(html); html = blogc_content_parse_inline("__*bola*__\n"); assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); - // this is not really valid + html = blogc_content_parse_inline("__*bo\\_\\*la*__\n"); + assert_non_null(html); + assert_string_equal(html, "bo_*la\n"); + free(html); html = blogc_content_parse_inline("__*bola\n"); assert_non_null(html); - assert_string_equal(html, "bola\n"); + assert_string_equal(html, "__*bola\n"); + free(html); + html = blogc_content_parse_inline("__*bola\\_\n"); + assert_non_null(html); + assert_string_equal(html, "__*bola_\n"); free(html); } @@ -1751,18 +1778,29 @@ test_content_parse_inline_code(void **state) assert_non_null(html); assert_string_equal(html, "bo*la\n"); free(html); - // invalid + html = blogc_content_parse_inline("``bobo<la\n"); + free(html); + html = blogc_content_parse_inline("`bo\\`\\`la`\n"); + assert_non_null(html); + assert_string_equal(html, "bo``la\n"); + free(html); + html = blogc_content_parse_inline("``bo\\`\\`la``\n"); + assert_non_null(html); + assert_string_equal(html, "bo``la\n"); + free(html); html = blogc_content_parse_inline("``bola\n"); assert_non_null(html); - assert_string_equal(html, "bola\n"); + assert_string_equal(html, "``bola\n"); free(html); html = blogc_content_parse_inline("`bola\n"); assert_non_null(html); - assert_string_equal(html, "bola\n"); + assert_string_equal(html, "`bola\n"); free(html); html = blogc_content_parse_inline("``bola`\n"); assert_non_null(html); - assert_string_equal(html, "bola\n"); + assert_string_equal(html, "``bola`\n"); free(html); } @@ -1802,9 +1840,9 @@ test_content_parse_inline_link(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); - html = blogc_content_parse_inline("[``bola(2)[3]**!\\``](http://example.org/)\n"); + html = blogc_content_parse_inline("[``bola(2)[3]**!\\```](http://example.org/)\n"); assert_non_null(html); - assert_string_equal(html, "bola(2)[3]**!\\\n"); + assert_string_equal(html, "bola(2)[3]**!`\n"); free(html); html = blogc_content_parse_inline("test suite!)\n" "depends on [cmocka](http://cmocka.org/), though.\n"); @@ -1821,6 +1859,10 @@ test_content_parse_inline_link(void **state) assert_non_null(html); assert_string_equal(html, "bola\n"); free(html); + html = blogc_content_parse_inline("[bo[]\\[\\]()la](http://example.org/?\\(\\))\n"); + assert_non_null(html); + assert_string_equal(html, "bo[][]()la\n"); + free(html); html = blogc_content_parse_inline("[bola](http://example.org/\n"); assert_non_null(html); assert_string_equal(html, "[bola](http://example.org/\n"); @@ -1863,13 +1905,21 @@ test_content_parse_inline_link_auto(void **state) assert_non_null(html); assert_string_equal(html, "guda\n"); free(html); + html = blogc_content_parse_inline("[[http://example.org/?\\[\\]]]\n"); + assert_non_null(html); + assert_string_equal(html, "http://example.org/?[]\n"); + free(html); + html = blogc_content_parse_inline("[[http://example.org/?\\[\\]a]]\n"); + assert_non_null(html); + assert_string_equal(html, "http://example.org/?[]a\n"); + free(html); html = blogc_content_parse_inline("[[guda]asd]"); assert_non_null(html); - assert_string_equal(html, "guda"); + assert_string_equal(html, "[[guda]asd]"); free(html); html = blogc_content_parse_inline("[[guda]asd]\n"); assert_non_null(html); - assert_string_equal(html, "guda\n"); + assert_string_equal(html, "[[guda]asd]\n"); free(html); html = blogc_content_parse_inline("[[guda]asd"); assert_non_null(html); @@ -1942,6 +1992,10 @@ test_content_parse_inline_image(void **state) assert_non_null(html); assert_string_equal(html, "\"bola\"\n"); free(html); + html = blogc_content_parse_inline("![bo\\[\\]()la](http://example.org/?\\(\\))\n"); + assert_non_null(html); + assert_string_equal(html, "\"bo[]()la\"\n"); + free(html); html = blogc_content_parse_inline("![bola](http://example.org/\n"); assert_non_null(html); assert_string_equal(html, "![bola](http://example.org/\n"); @@ -1994,15 +2048,15 @@ test_content_parse_inline_line_break(void **state) { char *html = blogc_content_parse_inline("asd \n"); assert_non_null(html); - assert_string_equal(html, "asd
\n"); + assert_string_equal(html, "asd
\n"); free(html); html = blogc_content_parse_inline("asd "); assert_non_null(html); - assert_string_equal(html, "asd
"); + assert_string_equal(html, "asd
"); free(html); html = blogc_content_parse_inline("asd "); assert_non_null(html); - assert_string_equal(html, "asd
"); + assert_string_equal(html, "asd
"); free(html); // invalid html = blogc_content_parse_inline("asd "); @@ -2021,7 +2075,7 @@ test_content_parse_inline_line_break_crlf(void **state) { char *html = blogc_content_parse_inline("asd \r\n"); assert_non_null(html); - assert_string_equal(html, "asd
\r\n"); + assert_string_equal(html, "asd
\r\n"); free(html); html = blogc_content_parse_inline("asd \r\n"); assert_non_null(html); @@ -2041,6 +2095,46 @@ test_content_parse_inline_endash_emdash(void **state) assert_non_null(html); assert_string_equal(html, "foo — bar"); free(html); + html = blogc_content_parse_inline("foo --"); + assert_non_null(html); + assert_string_equal(html, "foo –"); + free(html); + html = blogc_content_parse_inline("foo ---"); + assert_non_null(html); + assert_string_equal(html, "foo —"); + free(html); + html = blogc_content_parse_inline("foo \\-\\-"); + assert_non_null(html); + assert_string_equal(html, "foo --"); + free(html); + html = blogc_content_parse_inline("foo \\-\\-\\-"); + assert_non_null(html); + assert_string_equal(html, "foo ---"); + free(html); + html = blogc_content_parse_inline("foo \\---"); + assert_non_null(html); + assert_string_equal(html, "foo -–"); + free(html); + html = blogc_content_parse_inline("foo \\----"); + assert_non_null(html); + assert_string_equal(html, "foo -—"); + free(html); + html = blogc_content_parse_inline("foo \\-\\- bar"); + assert_non_null(html); + assert_string_equal(html, "foo -- bar"); + free(html); + html = blogc_content_parse_inline("foo \\-\\-\\- bar"); + assert_non_null(html); + assert_string_equal(html, "foo --- bar"); + free(html); + html = blogc_content_parse_inline("foo \\--- bar"); + assert_non_null(html); + assert_string_equal(html, "foo -– bar"); + free(html); + html = blogc_content_parse_inline("foo \\---- bar"); + assert_non_null(html); + assert_string_equal(html, "foo -— bar"); + free(html); html = blogc_content_parse_inline("`foo -- bar`"); assert_non_null(html); assert_string_equal(html, "foo -- bar"); diff --git a/tests/check_utils.c b/tests/check_utils.c index 6a6ceca..31087f1 100644 --- a/tests/check_utils.c +++ b/tests/check_utils.c @@ -256,6 +256,18 @@ test_str_replace(void **state) } +static void +test_str_find(void **state) +{ + assert_null(sb_str_find(NULL, 'c')); + assert_string_equal(sb_str_find("bola", 'l'), "la"); + assert_string_equal(sb_str_find("bo\\lalala", 'l'), "lala"); + assert_string_equal(sb_str_find("bola", '\0'), ""); + assert_null(sb_str_find("bola", 'g')); + assert_null(sb_str_find("bo\\la", 'l')); +} + + static void test_strv_join(void **state) { @@ -528,6 +540,25 @@ test_string_append_printf(void **state) } +static void +test_string_append_escaped(void **state) +{ + sb_string_t *str = sb_string_new(); + str = sb_string_append_escaped(str, NULL); + assert_non_null(str); + assert_string_equal(str->str, ""); + assert_int_equal(str->len, 0); + assert_int_equal(str->allocated_len, SB_STRING_CHUNK_SIZE); + str = sb_string_append_escaped(str, "foo \\a bar \\\\ lol"); + assert_non_null(str); + assert_string_equal(str->str, "foo a bar \\ lol"); + assert_int_equal(str->len, 15); + assert_int_equal(str->allocated_len, SB_STRING_CHUNK_SIZE); + assert_null(sb_string_free(str, true)); + assert_null(sb_string_append_escaped(NULL, "asd")); +} + + static void test_trie_new(void **state) { @@ -934,6 +965,7 @@ main(void) unit_test(test_str_strip), unit_test(test_str_split), unit_test(test_str_replace), + unit_test(test_str_find), unit_test(test_strv_join), unit_test(test_strv_length), @@ -945,6 +977,7 @@ main(void) unit_test(test_string_append), unit_test(test_string_append_c), unit_test(test_string_append_printf), + unit_test(test_string_append_escaped), // trie unit_test(test_trie_new), -- cgit v1.2.3-18-g5258