/* * blogc: A blog compiler. * Copyright (C) 2015 Rafael G. Martins * * This program can be distributed under the terms of the BSD License. * See the file COPYING. */ #ifdef HAVE_CONFIG_H #include #endif /* HAVE_CONFIG_H */ #include #include #include #include #include #include "../src/content-parser.h" #include "../src/error.h" #include "../src/utils/utils.h" static void test_content_parse(void **state) { const char *a = "# um\n" "## dois\n" "### tres\n" "#### quatro\n" "##### cinco\n" "###### seis\n" "\n" "bola\n" "chunda\n" "\n" "> bola\n" "> guda\n" "> buga\n" "> \n" "> asd\n" "\n" " bola\n" " asd\n" " qwewer\n" "\n" "+++\n" "1. chunda\n" "3. fuuuu\n" "\n" "+ chunda2\n" "+ fuuuu2\n" "\n" "\n" "\n" "guda\n" "yay\n" "\n" "**bola**\n"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

um

\n" "

dois

\n" "

tres

\n" "

quatro

\n" "
cinco
\n" "
seis
\n" "

bola\n" "chunda

\n" "

bola\n" "guda\n" "buga

\n" "
asd
\n" "
\n" "
bola\n"
        " asd\n"
        "qwewer
\n" "
\n" "
    \n" "
  1. chunda
  2. \n" "
  3. fuuuu
  4. \n" "
\n" "
    \n" "
  • chunda2
  • \n" "
  • fuuuu2
  • \n" "
\n" "\n" "

guda\n" "yay

\n" "

bola

\n"); free(html); } void test_content_parse_header(void **state) { const char *a = "## bola"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n"); free(html); a = "## bola\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n"); free(html); a = "bola\n" "\n" "## bola\n" "\n" "guda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n" "

bola

\n" "

guda

\n"); free(html); } void test_content_parse_html(void **state) { const char *a = "
\n
"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "
\n
\n"); free(html); a = "
\n
\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "
\n
\n"); free(html); a = "bola\n" "\n" "
\n" "
\n" "\n" "chunda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n" "
\n
\n" "

chunda

\n"); free(html); } void test_content_parse_blockquote(void **state) { const char *a = "> bola\n> guda"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola\n" "guda

\n" "
\n"); free(html); a = "> bola\n> guda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola\n" "guda

\n" "
\n"); free(html); a = "bola\n" "\n" "> bola\n" "> guda\n" "\n" "chunda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n" "

bola\n" "guda

\n" "
\n" "

chunda

\n"); free(html); } void test_content_parse_code(void **state) { const char *a = " bola\n guda"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "
bola\n"
        "guda
\n"); free(html); a = " bola\n guda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "
bola\n"
        "guda
\n"); free(html); a = "bola\n" "\n" " bola\n" " guda\n" "\n" "chunda\n"; html = blogc_content_parse(a, strlen(a), &err); assert_null(err); assert_non_null(html); assert_string_equal(html, "

bola

\n" "
bola\n"
        "guda
\n" "

chunda

\n"); free(html); } void test_content_parse_invalid_header(void **state) { const char *a = "asd\n" "\n" "##bola\n"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_non_null(err); assert_null(html); assert_int_equal(err->type, BLOGC_ERROR_CONTENT_PARSER); assert_string_equal(err->msg, "Malformed header, no space or tab after '#'\n" "Error occurred near to 'bola'"); blogc_error_free(err); } void test_content_parse_invalid_header_empty(void **state) { const char *a = "asd\n" "\n" "##\n" "\n" "qwe\n"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_non_null(err); assert_null(html); assert_int_equal(err->type, BLOGC_ERROR_CONTENT_PARSER); assert_string_equal(err->msg, "Malformed header, no space or tab after '#'"); blogc_error_free(err); } void test_content_parse_invalid_blockquote(void **state) { const char *a = "> asd\n" "> bola\n" "> foo\n"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_non_null(err); assert_null(html); assert_int_equal(err->type, BLOGC_ERROR_CONTENT_PARSER); assert_string_equal(err->msg, "Malformed blockquote, must use same prefix as previous line(s): > "); blogc_error_free(err); } void test_content_parse_invalid_code(void **state) { const char *a = " asd\n" " bola\n" " foo\n"; blogc_error_t *err = NULL; char *html = blogc_content_parse(a, strlen(a), &err); assert_non_null(err); assert_null(html); assert_int_equal(err->type, BLOGC_ERROR_CONTENT_PARSER); assert_string_equal(err->msg, "Malformed code block, must use same prefix as previous line(s): ' '"); blogc_error_free(err); } void test_content_parse_inline(void **state) { char *html = blogc_content_parse_inline( "**bola***asd* [![lol](http://google.com/lol.png) **lol** " "\\[asd\\]\\(qwe\\)](http://google.com) ``chunda``"); assert_string_equal(html, "bolaasd " " lol [asd](qwe) " "chunda"); free(html); html = blogc_content_parse_inline("*bola*"); assert_string_equal(html, "bola"); free(html); } int main(void) { const UnitTest tests[] = { unit_test(test_content_parse), unit_test(test_content_parse_header), unit_test(test_content_parse_html), unit_test(test_content_parse_blockquote), unit_test(test_content_parse_code), unit_test(test_content_parse_invalid_header), unit_test(test_content_parse_invalid_header_empty), unit_test(test_content_parse_invalid_blockquote), unit_test(test_content_parse_invalid_code), unit_test(test_content_parse_inline), }; return run_tests(tests); }