diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/context.c | 120 | ||||
| -rw-r--r-- | src/context/get.c | 45 | ||||
| -rw-r--r-- | src/context/meson.build | 1 | ||||
| -rw-r--r-- | src/context/set.c | 34 | ||||
| -rw-r--r-- | src/meson.build | 3 |
5 files changed, 203 insertions, 0 deletions
diff --git a/src/context.c b/src/context.c new file mode 100644 index 0000000..dabe9ab --- /dev/null +++ b/src/context.c @@ -0,0 +1,120 @@ +/* + * This file is part of Tatl <https://github.com/Fuwn/tatl>. + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include <malloc.h> +#include <stdio.h> +#include <string.h> + +#include <tatl/context.h> + +void tatl_new(struct tatl_context *context) { + context->_mute = 0; + context->_exit_code = 0; + context->_total = 0; + context->_passed = 0; + context->_failed = 0; + context->_names_size = 0; + context->_tests_size = 0; + context->_timer = clock(); + context->_tag = malloc(1 * sizeof(char)); + context->_names = malloc(1 * sizeof(char *)); + context->_tests = malloc(1 * sizeof(int (*)(void))); +} + +void tatl_destroy(struct tatl_context context) { + free(context._tag); + free(context._names); + free(context._tests); +} + +void tatl_summary(struct tatl_context context) { + if (!context._mute) { + printf("test result: %s. %zd passed (%zd steps); %zd failed (%ldms)\n", + context._total == context._passed ? "ok" : "FAILED", context._passed, + context._total, context._failed, + (clock() - context._timer) * 1000 / CLOCKS_PER_SEC); + } +} + +void tatl_add(struct tatl_context *context, const char *const tag, + int (*test)(void)) { + context->_names_size += 1; + context->_tests_size += 1; + context->_names = + realloc(context->_names, (strlen(tag) + 1) * sizeof(char *)); + context->_tests = + realloc(context->_tests, (sizeof(test) + 1) * sizeof(int (*)(void))); + /* context->names[context->names_size - 1] = (char *)strdup(tag); */ + context->_names[context->_names_size - 1] = tag; + context->_tests[context->_tests_size - 1] = test; +} + +void tatl_run(struct tatl_context *context) { + size_t i; + + if (!context->_mute) { + char *tag = malloc(strlen(context->_tag)); + size_t tag_length; + + strcpy(tag, context->_tag); + + tag_length = strlen(tag); + + printf("running %zd tests%s%s\n", context->_tests_size, + tag_length > 0 ? " from " : "", tag_length > 0 ? tag : ""); + + free(tag); + } + + for (i = 0; i != context->_names_size; ++i) { + clock_t timer = clock(); + int test_result = context->_tests[i](); + + context->_total += 1; + + if (test_result == 1) { + context->_passed += 1; + } else { + context->_failed += 1; + context->_exit_code = 1; + } + + if (!context->_mute) { + printf("%s ... %s (%ldms)\n", context->_names[i], + test_result == 1 ? "ok" : "FAILED", + (clock() - timer) * 1000 / CLOCKS_PER_SEC); + } + } +} + +void tatl_finish(struct tatl_context *context) { + tatl_run(context); + + if (!context->_mute) { + printf("\n"); + } + + tatl_summary(*context); + tatl_destroy(*context); + + if (!context->_mute) { + printf("\n"); + } +} diff --git a/src/context/get.c b/src/context/get.c new file mode 100644 index 0000000..a1bcc83 --- /dev/null +++ b/src/context/get.c @@ -0,0 +1,45 @@ +/* + * This file is part of Tatl <https://github.com/Fuwn/tatl>. + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include <tatl/context/get.h> + +size_t *tatl_context_get_total(struct tatl_context *context) { + return &context->_total; +} + +size_t *tatl_context_get_passed(struct tatl_context *context) { + return &context->_passed; +} + +size_t *tatl_context_get_failed(struct tatl_context *context) { + return &context->_failed; +} + +char *tatl_context_get_tag(struct tatl_context *context) { + return context->_tag; +} + +int *tatl_context_get_mute(struct tatl_context *context) { + return &context->_mute; +} + +int *tatl_context_get_exit_code(struct tatl_context *context) { + return &context->_exit_code; +} diff --git a/src/context/meson.build b/src/context/meson.build new file mode 100644 index 0000000..a852e86 --- /dev/null +++ b/src/context/meson.build @@ -0,0 +1 @@ +project_source_files += files('get.c', 'set.c') diff --git a/src/context/set.c b/src/context/set.c new file mode 100644 index 0000000..f3aff9d --- /dev/null +++ b/src/context/set.c @@ -0,0 +1,34 @@ +/* + * This file is part of Tatl <https://github.com/Fuwn/tatl>. + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Copyright (C) 2022-2022 Fuwn <[email protected]> + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include <malloc.h> +#include <string.h> + +#include <tatl/context/set.h> + +void tatl_context_set_mute(struct tatl_context *context, int mute) { + context->_mute = mute; +} + +void tatl_context_set_tag(struct tatl_context *context, const char *const tag) { + context->_tag = malloc(strlen(tag) * sizeof(char)); + + strcpy(context->_tag, tag); +} diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..0e1d8d4 --- /dev/null +++ b/src/meson.build @@ -0,0 +1,3 @@ +project_source_files = files('context.c') + +subdir('context') |