/* * splinter -- markdown tables. * * The chat is monospace, so a table is only a table when the pipes line up. * These cases pin the shape a model emits and the cases that must stay text: * no separator, a pipe in prose, ragged rows, text before and after the block. */ #include #include #include "splinter.h" #include "md.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static void check_format(const char *in, const char *want) { struct sp_buf b; int rc; if (sp_buf_init(&b) != 0) { fprintf(stderr, "FAIL init\n"); failures++; return; } rc = sp_md_table_format(in, &b); if (rc != 1) { fprintf(stderr, "FAIL %s:%d: rc %d for:\n%s\n", __FILE__, __LINE__, rc, in); failures++; } else if (b.p == NULL || strcmp(b.p, want) != 0) { fprintf(stderr, "FAIL %s:%d:\n--- in ---\n%s" "--- want ---\n%s--- got ---\n%s\n", __FILE__, __LINE__, in, want, b.p != NULL ? b.p : "(null)"); failures++; } sp_buf_free(&b); } static void check_no_table(const char *in) { struct sp_buf b; int rc; if (sp_buf_init(&b) != 0) { failures++; return; } rc = sp_md_table_format(in, &b); if (rc != 0 || b.len != 0) { fprintf(stderr, "FAIL %s:%d: expected no table, rc %d, %zu B\n", __FILE__, __LINE__, rc, b.len); failures++; } sp_buf_free(&b); } int main(void) { /* header, separator, body: widths come from the longest cell */ check_format("| a | b |\n| --- | --- |\n| 1 | 22 |\n", "| a | b |\n" "|---|----|\n" "| 1 | 22 |\n"); /* alignment markers are syntax, not text */ check_format("| a | b |\n|:---|---:|\n| 1 | 2 |\n", "| a | b |\n" "|---|---|\n" "| 1 | 2 |\n"); /* text before and after the block survives verbatim */ check_format("vor\n| a | b |\n|---|---|\n| 1 | 2 |\nnach\n", "vor\n" "| a | b |\n" "|---|---|\n" "| 1 | 2 |\n" "nach\n"); /* ragged rows: missing cells are empty, extra cells get a column */ check_format("| a | b |\n|---|---|\n| 1 |\n| 2 | 3 | 4 |\n", "| a | b | |\n" "|---|---|---|\n" "| 1 | | |\n" "| 2 | 3 | 4 |\n"); /* UTF-8: the width is the bytes the screen will consume */ check_format("| \xc3\xa4 | b |\n|---|---|\n| 1 | 2 |\n", "| \xc3\xa4 | b |\n" "|----|---|\n" "| 1 | 2 |\n"); /* a table at the very end gets the newline the row needs */ check_format("| a |\n|---|\n| 1 |", "| a |\n" "|---|\n" "| 1 |\n"); /* two blocks in one answer are both formatted */ check_format("| a |\n|---|\n| 1 |\ntext\n| bb |\n|----|\n| 22 |\n", "| a |\n" "|---|\n" "| 1 |\n" "text\n" "| bb |\n" "|----|\n" "| 22 |\n"); /* a pipe in prose and no separator line is not a table */ check_no_table("a | b\nno separator here\n"); check_no_table("| a | b |\n| 1 | 2 |\n"); check_no_table("plain text\n"); /* the first line alone is not a table */ check_no_table("| a | b |\n"); /* detection: only a header at the very start counts */ CHECK(sp_md_table_starts("| a | b |\n|---|---|\n| 1 | 2 |\n") == 1); CHECK(sp_md_table_starts("text\n| a |\n|---|\n") == 0); CHECK(sp_md_table_starts("| a |\n|---|\n") == 1); CHECK(sp_md_table_starts("| a |\nno separator\n") == 0); CHECK(sp_md_table_starts(NULL) == 0); if (failures == 0) printf("test_md: ok\n"); return failures == 0 ? 0 : 1; }