From dc6a0150d9c74785c2ea4c4eaebb2d38afe017f9 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 29 Oct 2023 19:04:02 -0400 Subject: [PATCH] Add second, more complicated gemtext test --- gemtext-parser.c | 1 + test/Makefile | 1 + test/parse-gemtext1.c | 101 ++++++++++++++++++++++++++++++++++++++++++ test/test1.gmi | 17 +++++++ 4 files changed, 120 insertions(+) create mode 100644 test/parse-gemtext1.c create mode 100644 test/test1.gmi diff --git a/gemtext-parser.c b/gemtext-parser.c index 72b830f..60c8953 100644 --- a/gemtext-parser.c +++ b/gemtext-parser.c @@ -362,6 +362,7 @@ int parsePreformatted(gemtextParser *parser, gemtextLineQueue *lq, char c) { switch (parser->state) { case trimStart: if (c == '\n') { + lineBufferReset(&parser->buffer); parser->state = lineStart; parser->altText = NULL; } else if (c == ' ' || c == '\t') { diff --git a/test/Makefile b/test/Makefile index a78791b..46b28a6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -37,6 +37,7 @@ LDLIBS += ../libgemtext.a LDLIBS += $(LIBS) tests += parse-gemtext0 +tests += parse-gemtext1 total != echo $(tests) | wc -w | awk '{ print $$1 }' diff --git a/test/parse-gemtext1.c b/test/parse-gemtext1.c new file mode 100644 index 0000000..6159563 --- /dev/null +++ b/test/parse-gemtext1.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include "gemtext-parser.h" + +gemtextLineQueue lq; +gemtextParser parser; + +char * preBlk = + "Just a regular preformatted block.\n" + "Nothing special"; + +int main() { + int ret = 0; + FILE *stream = NULL; + gemtextLine *line = NULL; + + stream = fopen("test1.gmi", "r"); + assert(stream != NULL); + ret = gemtextLineQueueInit(&lq); + assert(ret == 0); + ret = gemtextParserInit(&parser, stream); + assert(ret == 0); + ret = parseGemtext(&parser, &lq); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType = h1Line); + assert(memcmp(line->str, "A more complicated example", 26) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == listLine); + assert(memcmp(line->str, "list item with no leading space", 30) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == listLine); + assert(memcmp(line->str, "list item with several leading spaces", 37) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == h2Line); + assert(memcmp(line->str, "After this H2, an empty quote", 29) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "\n", 1) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == h3Line); + assert(memcmp(line->str, "Now we'll", 9) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "``", 2) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "=", 1) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "And maybe", 9) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == linkLine); + assert(memcmp(line->link->url, "spartan://example.org", 21) == 0); + assert(line->link->display == NULL); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "Let's enter", 11) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == preformattedLine); + assert(line->node->altText == NULL); + assert(memcmp(line->node->body, preBlk, 50) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == normalLine); + assert(memcmp(line->str, "And we'll finish", 16) == 0); + gemtextLineDeinit(line); + + line = gemtextLineQueueTryPop(&lq); + assert(line->lineType == linkLine); + assert(line->link->display == NULL); + assert(memcmp(line->link->url, "finger://example.org/joe", 24) == 0); + gemtextLineDeinit(line); + + return ret; +} diff --git a/test/test1.gmi b/test/test1.gmi new file mode 100644 index 0000000..a0d2861 --- /dev/null +++ b/test/test1.gmi @@ -0,0 +1,17 @@ +# A more complicated example +*list item with no leading space +* list item with several leading spaces +##After this H2, an empty quote +> +### Now we'll mess with incomplete linetype signifiers +`` += +And maybe a link with no display element.. +=> spartan://example.org/ +Let's enter preformatted for reals this time. +``` +Just a regular preformatted block. +Nothing special +``` +And we'll finish with a link. +=> finger://example.org/joe