Permalink
Browse files

Allow tests to run in a single executable

  • Loading branch information...
1 parent 17e4f47 commit 12cc07b9975dc620298cc66881f5b9e1f631d65f @mnunberg committed May 19, 2013
Showing with 904 additions and 5 deletions.
  1. +5 −0 .gitignore
  2. +154 −0 tests/all-tests.c
  3. +33 −0 tests/all-tests.h
  4. +579 −0 tests/cliopts.c
  5. +108 −0 tests/cliopts.h
  6. +2 −1 tests/jpr_test.c
  7. +19 −2 tests/json_test.c
  8. +4 −2 tests/unescape.c
View
@@ -6,3 +6,8 @@ share
tests/jpr_test
tests/json_test
tests/unescape
+vs/Debug
+vs/Release
+vs/*.user
+vs/*.suo
+vs/*.filters
View
@@ -0,0 +1,154 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "all-tests.h"
+#include "cliopts.h"
+
+static int WantQuiet = 0;
+static int WantFail = 0;
+const char *mode = "all";
+const char *json_files = NULL;
+const char *file_list = NULL;
+const char **JsonFileList = NULL;
+
+enum {
+ TEST_TYPE_JSON = 0x1,
+ TEST_TYPE_JPR = 0x2,
+ TEST_TYPE_UNESCAPE = 0x4,
+};
+
+#define TEST_TYPE_ALL (TEST_TYPE_JSON|TEST_TYPE_JPR|TEST_TYPE_UNESCAPE)
+
+cliopts_entry entries[] = {
+ { 'q', "quiet", CLIOPTS_ARGT_NONE,
+ &WantQuiet,
+ "Whether to not output verbose test information"
+ },
+ { 'F', "fail", CLIOPTS_ARGT_NONE,
+ &WantFail,
+ "For JSON tests, whether the parser is expected to return an "
+ "error when parsing the inputs"
+ },
+ { 'm', "mode", CLIOPTS_ARGT_STRING,
+ &mode,
+ "Mode to test, can be 'all', 'jpr' 'json', or 'unescape'"
+ },
+ { 'f', "file", CLIOPTS_ARGT_STRING,
+ &json_files,
+ "Path to a single file for the 'json' test"
+ },
+ { 0, "file-list", CLIOPTS_ARGT_STRING,
+ &file_list,
+ "Path to a list of files to pass to the 'json' test"
+ },
+ { 0 }
+};
+
+static int build_list(void)
+{
+ int curalloc = 0;
+ int curpos = 0;
+ FILE *fp;
+ char rbuf[512];
+
+ if (json_files) {
+ JsonFileList = malloc(sizeof(char**), 2);
+ JsonFileList[1] = NULL;
+ JsonFileList[0] = json_files;
+ return 0;
+ }
+
+ if (!file_list) {
+ fprintf(stderr, "Must have file or file list for JPR tests\n");
+ return -1;
+ }
+
+ fp = fopen(file_list, "r");
+ if (!fp) {
+ perror(file_list);
+ return -1;
+ }
+
+ while (fgets(rbuf, sizeof(rbuf), fp)) {
+ char *curbuf;
+ int slen = strlen(rbuf);
+ curbuf = malloc(slen + 1);
+ memcpy(curbuf, rbuf, slen);
+ curbuf[slen-1] = '\0';
+
+ if (curpos == curalloc) {
+ if (!curalloc) {
+ curalloc = 32;
+ } else {
+ curalloc *= 2;
+ }
+ JsonFileList = realloc(JsonFileList,
+ (curalloc + 1) * sizeof(char**));
+ }
+ JsonFileList[curpos++] = curbuf;
+ }
+ fclose(fp);
+ if (!curpos) {
+ fprintf(stderr, "No files in list\n");
+ return -1;
+ }
+ JsonFileList[curpos] = NULL;
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int last_opt = 0;
+ int test_mode = 0;
+ cliopts_parse_options(entries, argc, argv, &last_opt, NULL);
+
+ if (strcmp(mode, "all") == 0) {
+ test_mode = TEST_TYPE_ALL;
+
+ } else if (strcmp(mode, "json") == 0) {
+ test_mode = TEST_TYPE_JSON;
+
+ } else if (strcmp(mode, "jpr") == 0) {
+ test_mode = TEST_TYPE_JPR;
+
+ } else if (strcmp(mode, "unescape") == 0) {
+ test_mode = TEST_TYPE_UNESCAPE;
+
+ } else {
+ fprintf(stderr, "Unrecognized mode '%s'\n", mode);
+ exit(EXIT_FAILURE);
+ }
+
+ if (WantQuiet) {
+ freopen(DEVNULL, "w", stdout);
+ }
+ if (WantFail) {
+ setenv("JSONSL_FAIL_TESTS", "1", 1);
+ }
+
+ if (test_mode & TEST_TYPE_JSON) {
+ char **curbuf;
+ if (build_list() == -1) {
+ return EXIT_FAILURE;
+ }
+ for (curbuf = JsonFileList; *curbuf; curbuf++) {
+ if (jsonsl_test_json(2, curbuf-1) != 0) {
+ return EXIT_FAILURE;
+ }
+ }
+ }
+
+ if (test_mode & TEST_TYPE_UNESCAPE) {
+ if (jsonsl_test_unescape() != 0) {
+ return EXIT_FAILURE;
+ }
+ }
+ if (test_mode & TEST_TYPE_JPR) {
+
+ if (jsonsl_test_jpr() != 0) {
+ return EXIT_FAILURE;
+ }
+ }
+ return 0;
+}
View
@@ -0,0 +1,33 @@
+#ifndef JSONSL_ALLTESTS_H
+#define JSONSL_ALLTESTS_H
+
+/**
+ * I made this file primarily for Windows, where I didn't want to make
+ * 3 separate projects for each executable
+ */
+
+#ifdef JSONSL_SINGLE_TEST_EXE
+#define JSONSL_TEST_UNESCAPE_FUNC int jsonsl_test_unescape(void)
+#define JSONSL_TEST_JPR_FUNC int jsonsl_test_jpr(void)
+#define JSONSL_TEST_JSON_FUNC int jsonsl_test_json(int argc, char **argv)
+
+JSONSL_TEST_UNESCAPE_FUNC;
+JSONSL_TEST_JPR_FUNC;
+JSONSL_TEST_JSON_FUNC;
+
+#else
+#define JSONSL_TEST_UNESCAPE_FUNC int main(void)
+#define JSONSL_TEST_JPR_FUNC int main(void)
+#define JSONSL_TEST_JSON_FUNC int main(int argc, char **argv)
+
+#endif /* SINGLE_TEST_EXE */
+
+#ifdef _WIN32
+#define S_ISDIR(x) ((x & _S_IFMT) == _S_IFDIR)
+#define DEVNULL "nul"
+#define setenv(k, v, o) _putenv_s(k, v)
+#else
+#define DEVNULL "/dev/null"
+#endif
+
+#endif /* JSONSL_ALLTESTS_H */
Oops, something went wrong.

0 comments on commit 12cc07b

Please sign in to comment.