From: Kevin Day Date: Sun, 16 Jan 2022 20:30:42 +0000 (-0600) Subject: Update: Add f_color unit tests. X-Git-Tag: 0.5.8~111 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=f3679d0b217f8581ab493598f27770c739be11f7;p=fll Update: Add f_color unit tests. --- diff --git a/level_0/f_color/data/build/dependencies-tests b/level_0/f_color/data/build/dependencies-tests new file mode 100644 index 0000000..dea3179 --- /dev/null +++ b/level_0/f_color/data/build/dependencies-tests @@ -0,0 +1,3 @@ +# fss-0001 + +cmocka 1.* diff --git a/level_0/f_color/data/build/settings-mocks b/level_0/f_color/data/build/settings-mocks new file mode 100644 index 0000000..dac285d --- /dev/null +++ b/level_0/f_color/data/build/settings-mocks @@ -0,0 +1,78 @@ +# fss-0001 + +project_name f_color + +version_major 0 +version_minor 5 +version_micro 8 +version_file micro +version_target minor + +environment + +process_pre +process_post + +modes individual +modes_default individual + +build_compiler gcc +build_indexer ar +build_indexer_arguments rcs +build_language c +build_libraries -lc +build_libraries-individual -lf_memory -lf_string +build_libraries_shared +build_libraries_static +build_sources_library color.c color-common.c ../../tests/c/mock-color.c +build_sources_library_shared +build_sources_library_static +build_sources_program +build_sources_program_shared +build_sources_program_static +build_sources_headers color.h color-common.h +build_sources_headers_shared +build_sources_headers_static +build_sources_script +build_sources_setting +build_script yes +build_shared yes +build_static no + +path_headers fll/level_0 +path_headers_preserve no +path_library_script script +path_library_shared shared +path_library_static static +path_program_script script +path_program_shared shared +path_program_static static +path_sources +path_standard yes + +search_exclusive yes +search_shared yes +search_static yes + +defines +defines_library +defines_library_shared +defines_library_static +defines_program +defines_program_shared +defines_program_static +defines_static +defines_shared + +flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-logical-op-parentheses -Wno-parentheses +flags_library -fPIC +flags_library_shared +flags_library_static +flags_program -fPIE +flags_program_shared +flags_program_static +flags_shared +flags_static + +# Inject mocks. +flags -Wl,--wrap=getenv diff --git a/level_0/f_color/data/build/settings-tests b/level_0/f_color/data/build/settings-tests new file mode 100644 index 0000000..75ea8f1 --- /dev/null +++ b/level_0/f_color/data/build/settings-tests @@ -0,0 +1,45 @@ +# fss-0001 + +project_name test-f_color + +version_major 0 +version_minor 5 +version_micro 8 +version_file major +version_target major + +modes individual level monolithic +modes_default individual + +build_compiler gcc +build_indexer ar +build_indexer_arguments rcs +build_language c +build_libraries -lc -lcmocka +build_libraries-individual -lf_memory -lf_string -lf_color +build_libraries-level -lfll_0 +build_libraries-monolithic -lfll +build_sources_program test-color-load_context.c test-color-save.c test-color.c +build_script no +build_shared yes +build_static no + +path_headers tests/c +path_headers_preserve no +path_sources tests/c +path_standard no + +search_exclusive yes +search_shared yes +search_static yes + +defines -Ibuild/includes +defines_static -Lbuild/libraries/static +defines_shared -Lbuild/libraries/shared + +flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-logical-op-parentheses -Wno-parentheses +flags_program -fPIE +flags_program_shared +flags_program_static +flags_shared +flags_static diff --git a/level_0/f_color/data/build/testfile b/level_0/f_color/data/build/testfile new file mode 100644 index 0000000..64b0d9e --- /dev/null +++ b/level_0/f_color/data/build/testfile @@ -0,0 +1,45 @@ +# fss-0005 iki-0002 + +settings: + load_build yes + fail exit + + environment LD_LIBRARY_PATH + +main: + build settings-mocks + build settings-tests + + operate ld_library_path + + if exists build/programs/shared/test-f_color + shell build/programs/shared/test-f_color + + if exists build/programs/static/test-f_color + shell build/programs/static/test-f_color + + if not exists build/programs/shared/test-f_color + and not exists build/programs/static/test-f_color + operate not_created + +not_created: + print + print context:"error"Failed to test due to being unable to find either a shared or static test binary to perform tests. context:"error" + + exit failure + +ld_library_path: + if defined environment LD_LIBRARY_PATH + and defined parameter work + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:"work:value"libraries/shared:define:"LD_LIBRARY_PATH"' + + else + if defined environment LD_LIBRARY_PATH + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:define:"LD_LIBRARY_PATH"' + + else + if defined parameter work + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:"work:value"libraries/shared' + + else + define LD_LIBRARY_PATH build/libraries/shared diff --git a/level_0/f_color/tests/c/mock-color.c b/level_0/f_color/tests/c/mock-color.c new file mode 100644 index 0000000..2e2a5a6 --- /dev/null +++ b/level_0/f_color/tests/c/mock-color.c @@ -0,0 +1,22 @@ +#include "mock-color.h" + +#ifdef __cplusplus +extern "C" { +#endif + +char * __wrap_getenv(char *name) { + + bool failure = mock_type(bool); + + if (failure) { + errno = mock_type(int); + + return (char *) 0; + } + + return mock_type(char *); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/mock-color.h b/level_0/f_color/tests/c/mock-color.h new file mode 100644 index 0000000..969301b --- /dev/null +++ b/level_0/f_color/tests/c/mock-color.h @@ -0,0 +1,37 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _MOCK__color_h +#define _MOCK__color_h + +// libc includes. +#include +#include +#include +#include + +// cmocka includes. +#include + +// fll-0 includes. +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const static int mock_errno_generic = 32767; + +extern char * __wrap_getenv(char *name); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _MOCK__color_h diff --git a/level_0/f_color/tests/c/test-color-load_context.c b/level_0/f_color/tests/c/test-color-load_context.c new file mode 100644 index 0000000..ff9cdb3 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-load_context.c @@ -0,0 +1,83 @@ +#include "test-color.h" +#include "test-color-load_context.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_load_context__parameter_checking(void **state) { + + { + const f_status_t status = f_color_load_context(0, F_true); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_load_context__works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + + f_string_t terms[3] = { + "linux", + "xterm-256color", + 0, + }; + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + for (uint8_t i = 0; i < 2; ++i) { + + for (uint8_t j = 0; j < 3; ++j) { + + will_return(__wrap_getenv, i); + will_return(__wrap_getenv, terms[j]); + + const f_status_t status = f_color_load_context(&context, F_true); + + assert_int_equal(status, F_none); + + assert_ptr_equal(context.set.reset.before, &context.reset); + assert_ptr_equal(context.set.reset.after, &context.reset); + + assert_ptr_equal(context.set.warning.before, &context.warning); + assert_ptr_equal(context.set.warning.after, &context.reset); + + assert_ptr_equal(context.set.error.before, &context.error); + assert_ptr_equal(context.set.error.after, &context.reset); + + assert_ptr_equal(context.set.title.before, &context.title); + assert_ptr_equal(context.set.title.after, &context.reset); + + assert_ptr_equal(context.set.notable.before, &context.notable); + assert_ptr_equal(context.set.notable.after, &context.reset); + + assert_ptr_equal(context.set.important.before, &context.important); + assert_ptr_equal(context.set.important.after, &context.reset); + + assert_ptr_equal(context.set.standout.before, &context.standout); + assert_ptr_equal(context.set.standout.after, &context.reset); + + assert_ptr_equal(context.set.success.before, &context.success); + assert_ptr_equal(context.set.success.after, &context.reset); + + assert_ptr_equal(context.set.normal.before, &context.normal); + assert_ptr_equal(context.set.normal.after, &context.reset); + + assert_ptr_equal(context.set.normal_reset.before, &context.normal_reset); + assert_ptr_equal(context.set.normal_reset.after, &context.reset); + } // for + } // for + + macro_f_color_context_t_delete_simple(context); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-load_context.h b/level_0/f_color/tests/c/test-color-load_context.h new file mode 100644 index 0000000..30fb256 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-load_context.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__load_context +#define _TEST__F_color__load_context + +/** + * Test that parameter checking works as expected. + * + * @see f_color_load_context() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_load_context__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +// f_color_load_context() only returns failures by other functions that have their own tests. + +/** + * Test that function works. + * + * @see f_color_load_context() + */ +extern void test__f_color_load_context__works(void **state); + +#endif // _TEST__F_color__load_context diff --git a/level_0/f_color/tests/c/test-color-save.c b/level_0/f_color/tests/c/test-color-save.c new file mode 100644 index 0000000..7c3f19e --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save.c @@ -0,0 +1,150 @@ +#include "test-color.h" +#include "test-color-save.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save__parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const char *color = "color"; + + { + const f_status_t status = f_color_save(0, format, 0, 0, 0, 0, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, 0, 0, 0, 0, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, color, 0, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, color, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, color, 0, color); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, color, color, color); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, 0, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, 0, 0, 0, color); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, color, 0, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, color, 0, 0, color); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + const f_status_t status = f_color_save(&buffer, format, color, color, color, 0, color); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save__works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_t color1 = "color1"; + const f_string_t color2 = "color2"; + const f_string_t color3 = "color3"; + const f_string_t color4 = "color4"; + const f_string_t color5 = "color5"; + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save(&buffer, format, color1, 0, 0, 0, 0); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1"); + } + + { + buffer.used = 0; + + const f_status_t status = f_color_save(&buffer, format, color1, color2, 0, 0, 0); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2"); + } + + { + buffer.used = 0; + + const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, 0, 0); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3"); + } + + { + buffer.used = 0; + + const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, color4, 0); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3color4"); + } + + { + buffer.used = 0; + + const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, color4, color5); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3color4color5"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save.h b/level_0/f_color/tests/c/test-color-save.h new file mode 100644 index 0000000..92efe98 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save +#define _TEST__F_color__save + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +// f_color_save() only returns failures by other functions that have their own tests. + +/** + * Test that function works. + * + * @see f_color_save() + */ +extern void test__f_color_save__works(void **state); + +#endif // _TEST__F_color__save diff --git a/level_0/f_color/tests/c/test-color.c b/level_0/f_color/tests/c/test-color.c new file mode 100644 index 0000000..6aeae66 --- /dev/null +++ b/level_0/f_color/tests/c/test-color.c @@ -0,0 +1,39 @@ +#include "test-color.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int setup(void **state) { + + return 0; +} + +int setdown(void **state) { + + errno = 0; + + return 0; +} + +int main(void) { + + const struct CMUnitTest tests[] = { + // f_color_load_context() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_load_context__works), + + // f_color_save() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save__works), + + #ifndef _di_level_0_parameter_checking_ + cmocka_unit_test(test__f_color_load_context__parameter_checking), + cmocka_unit_test(test__f_color_save__parameter_checking), + #endif // _di_level_0_parameter_checking_ + }; + + return cmocka_run_group_tests(tests, setup, setdown); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color.h b/level_0/f_color/tests/c/test-color.h new file mode 100644 index 0000000..dab50a5 --- /dev/null +++ b/level_0/f_color/tests/c/test-color.h @@ -0,0 +1,73 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color_h +#define _TEST__F_color_h + +// libc includes. +#include +#include +#include +#include + +// cmocka includes. +#include + +// fll-0 includes. +#include + +// mock includes. +#include "mock-color.h" + +// test includes. +#include "test-color-load_context.h" +#include "test-color-save.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Perform any setup operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setup(void **state); + +/** + * Peform any setdown operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setdown(void **state); + +/** + * Run all tests. + * + * @return + * The final result of the tests. + * + * @see cmocka_run_group_tests() + * @see cmocka_unit_test() + */ +extern int main(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _TEST__F_color_h