3. Test Parser

This test program can be run inside of main, and is designed to be run as a command line program.

3.1. Top Level Program

<<funcdefs>>=
#ifdef ORGPARSE_TEST
int orgparse_test(int argc, char *argv[]);
#endif
<<test_program>>=
<<test_callbacks>>
static void setup_orgparse(orgparse *op)
{
<<setup_test_callbacks>>
}
int orgparse_test_parser(int argc, char *argv[])
{
    orgparse op;
    FILE *fp;
    char *buf;
    size_t sz;

    if (argc == 1) {
        fprintf(stderr, "Usage: %s file.org\n", argv[0]);
        return 1;
    }

    fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr,
                "Could not open %s for reading.\n",
                argv[1]);
        return 1;
    }
    fseek(fp, 0, SEEK_END);
    sz = ftell(fp) + 1;
    buf = calloc(1, sz);
    fseek(fp, 0, SEEK_SET);
    fread(buf, 1, sz, fp);
    orgparse_init(&op);
    setup_orgparse(&op);
    orgparse_run(&op, buf, sz, NULL);
    free(buf);
    return 0;
}

3.2. Test Callbacks

3.2.1. Header

<<test_callbacks>>=
static void test_header(void *ud,
                        const char *h,
                        size_t sz,
                        int lvl)
{
    printf("header: '");
    fwrite(h, 1, sz, stdout);
    printf("', lvl: %d\n", lvl);
}
<<setup_test_callbacks>>=
orgparse_set_header(op, test_header);

3.2.2. Text

<<test_callbacks>>=
static void test_text(void *ud,
                      const char *str,
                      size_t sz)
{
    printf("text: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_text(op, test_text);

3.2.3. Bold

<<test_callbacks>>=
static void test_bold(void *ud,
                      const char *str,
                      size_t sz)
{
    printf("bold: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_bold(op, test_bold);

3.2.4. Code

<<test_callbacks>>=
static void test_code(void *ud,
                      const char *str,
                      size_t sz)
{
    printf("code: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_code(op, test_code);

3.2.5. Underline

<<test_callbacks>>=
static void test_uline(void *ud,
                       const char *str,
                       size_t sz)
{
    printf("underline: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_underline(op, test_uline);

3.2.6. Newline

<<test_callbacks>>=
static void test_newline(void *ud,
                         const char *str,
                         size_t sz)
{
    printf("NEWLINE\n");
}
<<setup_test_callbacks>>=
orgparse_set_newline(op, test_newline);

3.2.7. Codeblock

<<test_callbacks>>=
static void test_codeblock(void *ud,
                           const char *str,
                           size_t sz)
{
    printf("codeblock: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_codeblock(op, test_codeblock);

3.2.8. Name

<<test_callbacks>>=
static void test_name(void *ud,
                           const char *str,
                           size_t sz)
{
    printf("name: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_name(op, test_name);

3.2.9. Aux Block

<<test_callbacks>>=
static void test_aux(void *ud,
                     const char *str,
                     size_t sz)
{
    printf("aux: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_aux(op, test_aux);

3.2.10. Title

<<test_callbacks>>=
static void test_title(void *ud,
                       const char *str,
                       size_t sz)
{
    printf("title: '");
    fwrite(str, 1, sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_title(op, test_title);

3.2.11. Link

<<test_callbacks>>=
static void test_link(void *ud,
                      const char *link,
                      size_t link_sz,
                      const char *name,
                      size_t name_sz)
{
    printf("link_ref: '");
    fwrite(link, 1, link_sz, stdout);
    printf("', '");
    fwrite(name, 1, name_sz, stdout);
    printf("'\n");
}
<<setup_test_callbacks>>=
orgparse_set_link(op, test_link);

3.2.12. Paragraph

<<test_callbacks>>=
static void test_pgrph(void *ud, int mode)
{
    if (mode) {
        printf("PARAGRAPH END\n");
    } else {
        printf("PARAGRAPH BEGIN\n");
    }
}
<<setup_test_callbacks>>=
orgparse_set_pgrph(op, test_pgrph);



prev | home | next