aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile16
-rw-r--r--convert-markdown.c39
-rw-r--r--parse-file.c29
-rw-r--r--parse-file.h12
-rw-r--r--test-convert-markdown.c51
6 files changed, 109 insertions, 39 deletions
diff --git a/.gitignore b/.gitignore
index f5da8b6..e1ef24d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
convert-markdown
+test-convert-markdown
*.o
Makefile.configure
config.h
diff --git a/Makefile b/Makefile
index a87bb2c..f7a4931 100644
--- a/Makefile
+++ b/Makefile
@@ -4,15 +4,25 @@ LDADD_PKG != pkg-config --silence-errors --libs lowdown || echo "-llowdown -lm"
CFLAGS_PKG != pkg-config --silence-errors --cflags lowdown || echo ""
LDADD += $(LDADD_PKG) $(LDADD_MD5) $(LDADD_STATIC)
CFLAGS += -O2 $(CFLAGS_PKG)
-OBJS = convert-markdown.o compats.o
+OBJS = parse-file.o compats.o
convert-markdown: $(OBJS)
- $(CC) $(OBJS) -o $@ $(LDFLAGS) $(LDADD)
+ $(CC) $(OBJS) convert-markdown.c -o $@ $(LDFLAGS) $(LDADD)
$(OBJS): config.h
clean:
- rm -f convert-markdown $(OBJS)
+ rm -f convert-markdown $(OBJS) test-convert-markdown
distclean: clean
rm -f Makefile.configure config.h config.log config.h.old config.log.old
+
+check: tests
+ echo "# header" | ./test-convert-markdown
+
+tests: test-convert-markdown
+
+test-convert-markdown: $(OBJS) test-convert-markdown.c
+ $(CC) $(OBJS) test-convert-markdown.c -o $@ $(LDFLAGS) $(LDADD)
+
+.PHONY: tests
diff --git a/convert-markdown.c b/convert-markdown.c
index 15e0be1..6801e47 100644
--- a/convert-markdown.c
+++ b/convert-markdown.c
@@ -15,42 +15,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include "config.h"
-
-#if HAVE_ERR
-#include <err.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/queue.h>
-#include <lowdown.h>
+#include "parse-file.h"
int main(void) {
- // adapted from the example section of lowdown_file(3)
- struct lowdown_opts opts;
- char *buf;
- size_t bufsz;
-
- memset(&opts, 0, sizeof(struct lowdown_opts));
- opts.type = LOWDOWN_HTML;
- opts.feat = LOWDOWN_FOOTNOTES |
- LOWDOWN_AUTOLINK |
- LOWDOWN_TABLES |
- LOWDOWN_SUPER |
- LOWDOWN_STRIKE |
- LOWDOWN_FENCED |
- LOWDOWN_COMMONMARK |
- LOWDOWN_DEFLIST |
- LOWDOWN_IMG_EXT |
- LOWDOWN_METADATA;
- opts.oflags = LOWDOWN_HTML_HEAD_IDS |
- LOWDOWN_HTML_NUM_ENT |
- LOWDOWN_HTML_OWASP |
- LOWDOWN_SMARTY;
- if (!lowdown_file(&opts, stdin, &buf, &bufsz, NULL))
- errx(1, "lowdown_file");
- fwrite(buf, 1, bufsz, stdout);
- free(buf);
- return 0;
+ _parse_file(stdin, stdout);
+ return 0;
}
diff --git a/parse-file.c b/parse-file.c
new file mode 100644
index 0000000..14a737c
--- /dev/null
+++ b/parse-file.c
@@ -0,0 +1,29 @@
+#include "parse-file.h"
+
+void _parse_file(FILE* instream, FILE* outstream) {
+ // adapted from the example section of lowdown_file(3)
+ struct lowdown_opts opts;
+ char *buf;
+ size_t bufsz;
+
+ memset(&opts, 0, sizeof(struct lowdown_opts));
+ opts.type = LOWDOWN_HTML;
+ opts.feat = LOWDOWN_FOOTNOTES |
+ LOWDOWN_AUTOLINK |
+ LOWDOWN_TABLES |
+ LOWDOWN_SUPER |
+ LOWDOWN_STRIKE |
+ LOWDOWN_FENCED |
+ LOWDOWN_COMMONMARK |
+ LOWDOWN_DEFLIST |
+ LOWDOWN_IMG_EXT |
+ LOWDOWN_METADATA;
+ opts.oflags = LOWDOWN_HTML_HEAD_IDS |
+ LOWDOWN_HTML_NUM_ENT |
+ LOWDOWN_HTML_OWASP |
+ LOWDOWN_SMARTY;
+ if (!lowdown_file(&opts, instream, &buf, &bufsz, NULL))
+ errx(1, "lowdown_file");
+ fwrite(buf, 1, bufsz, outstream);
+ free(buf);
+}
diff --git a/parse-file.h b/parse-file.h
new file mode 100644
index 0000000..d0054e4
--- /dev/null
+++ b/parse-file.h
@@ -0,0 +1,12 @@
+#include "config.h"
+
+#if HAVE_ERR
+#include <err.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/queue.h>
+#include <lowdown.h>
+
+void _parse_file(FILE*, FILE*);
diff --git a/test-convert-markdown.c b/test-convert-markdown.c
new file mode 100644
index 0000000..5925184
--- /dev/null
+++ b/test-convert-markdown.c
@@ -0,0 +1,51 @@
+#include "parse-file.h"
+
+static int test_parse_file(const char* testname, const char* input, const char* output) {
+ FILE* instream = tmpfile();
+ FILE* outstream = tmpfile();
+
+ /* for some reason, input isn't properly written to instream */
+ fprintf(instream, "%s\n", input);
+
+ const int length = strlen(output) + 1;
+ char buffer[length];
+
+ /* TODO: use instream instead of stdin */
+ _parse_file(stdin, outstream);
+ const int tmp_length = ftell(outstream) + 1;
+ rewind(outstream);
+
+ fgets(buffer, length, outstream);
+ fclose(instream);
+ fclose(outstream);
+
+ if (strcmp(buffer, output) == 0 && tmp_length == length) {
+ printf("%s test succeeded\n", testname);
+ return 0;
+ } else {
+ printf("%s test failed\n", testname);
+ printf("%s !=\n%s\n", buffer, output);
+ return 1;
+ }
+}
+
+int main(void) {
+ printf("=============\nRunning Tests\n=============\n");
+
+ int errors = 0;
+ errors += test_parse_file("1. Header", "# header", "<h1 id=\"header\">header</h1>\n");
+ //errors += test_parse_file("2. Bold", "*bold*", "<strong>bold</strong>\n");
+
+ printf("\n============\nTest Summary\n============\n");
+
+ switch (errors) {
+ case 0:
+ printf("All tests succeeded\n");
+ break;
+ case 1:
+ printf("One test failed\n");
+ break;
+ default:
+ printf("%i tests failed\n", errors);
+ }
+}