/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include "tlibtoml.h" #ifndef PROJECT_SOURCE_DIR #define PROJECT_SOURCE_DIR ".." #endif void print_table(const TomlTable* table); void print_value(const TomlValue* value); void print_array(const TomlArray *array) { printf("["); for (u64 i = 0; i < array->len; i++) { if (i > 0) { printf(", "); } print_value(&array->data[i]); } printf("]"); } void print_value(const TomlValue* value) { switch (value->type) { default: assert(false && "invalid type"); break; case TLIBTOML_TABLE: print_table(value->table); break; case TLIBTOML_ARRAY: print_array(value->array); break; case TLIBTOML_STRING: printf("'" FMT_str "'", value->s->len, value->s->data); break; case TLIBTOML_INTEGER: printf("%" PRId64, value->i); break; case TLIBTOML_FLOAT: printf("%f", value->f); break; case TLIBTOML_DATETIME: printf("(datetime)"); break; case TLIBTOML_BOOLEAN: printf("%s", value->b ? "true" : "false"); break; } } void print_keyval(HashMapKeyValue* keyval) { printf("\"%s\": ", keyval->key.data); print_value(keyval->value); } void print_table(const TomlTable* table) { HashMapIter it = HashMapIter_create(table); printf("{"); u64 i = 0; HashMapKeyValue keyval; while (HashMapIter_moveNext(&it)) { assert(HashMapIter_getCurrent(&it, &keyval)); if (i > 0) printf(", "); print_keyval(&keyval); i++; } printf("}"); } Result(void) test_run(cstr filename) { Deferral(1); try(TomlTable* table, p, toml_load_filename(filename)); Defer(TomlTable_free(table)); print_table(table); printf("\n"); Return RESULT_VOID; } i32 main(void) { Deferral(32); try_fatal_void(tlibc_init()); Defer(tlibc_deinit()); try_fatal_void(tlibtoml_init()); Defer(tlibtoml_deinit()); static cstr const filenames[] = { /* should parse */ PROJECT_SOURCE_DIR "/tests/key-values.toml", PROJECT_SOURCE_DIR "/tests/complex-structure.toml", PROJECT_SOURCE_DIR "/tests/long_config.toml", /* should not parse */ /* tests from https://github.com/toml-lang/toml */ PROJECT_SOURCE_DIR "/tests/example.toml", PROJECT_SOURCE_DIR "/tests/fruit.toml", PROJECT_SOURCE_DIR "/tests/hard_example.toml", PROJECT_SOURCE_DIR "/tests/hard_example_unicode.toml" }; i32 total_tests = sizeof(filenames) / sizeof(char*); i32 num_passed = 0; i32 num_failed = 0; for (i32 i = 0; i < total_tests; i++) { ResultVar(void) r = test_run(filenames[i]); if (!r.error) { printf("test %d success\n", i); num_passed++; } else { str err_str = Error_toStr(r.error); printf("%s\ntest %d failed\n", err_str.data, i); str_destroy(err_str); num_failed++; } } printf("total %d tests, %d passed, %d failed\n", total_tests, num_passed, num_failed); Return num_failed; }