From 626e0807684cb78b51bbf185ba085cb846ece659 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 11 Nov 2022 20:19:28 -0600 Subject: [PATCH] Bugfix: The f_color tests are failing due to test design problems. The mock function is incorrectly setting errno mock. There is no errno as far as I can tell for getenv(). Improve tests by: - Adding additional "unknown" string to match. - Actually test the different modes. --- level_0/f_color/tests/unit/c/mock-color.c | 2 - .../f_color/tests/unit/c/test-color-load_context.c | 107 +++++++++++++++------ 2 files changed, 80 insertions(+), 29 deletions(-) diff --git a/level_0/f_color/tests/unit/c/mock-color.c b/level_0/f_color/tests/unit/c/mock-color.c index 6d9464d..5fc76ce 100644 --- a/level_0/f_color/tests/unit/c/mock-color.c +++ b/level_0/f_color/tests/unit/c/mock-color.c @@ -9,8 +9,6 @@ char * __wrap_getenv(char *name) { const bool failure = mock_type(bool); if (failure) { - errno = mock_type(int); - return (char *) 0; } diff --git a/level_0/f_color/tests/unit/c/test-color-load_context.c b/level_0/f_color/tests/unit/c/test-color-load_context.c index 3799bda..6ca7684 100644 --- a/level_0/f_color/tests/unit/c/test-color-load_context.c +++ b/level_0/f_color/tests/unit/c/test-color-load_context.c @@ -18,58 +18,111 @@ void test__f_color_load_context__works(void **state) { f_color_context_t context = f_color_context_t_initialize; - f_string_t terms[3] = { + f_string_t terms[4] = { "linux", "xterm-256color", + "unknown", 0, }; + uint8_t modes[4] = { + f_color_mode_none_e, + f_color_mode_color_not_e, + f_color_mode_dark_e, + f_color_mode_light_e, + }; + + f_status_t statuss[4] = { + F_data_not, + F_none, + F_none, + F_none, + }; + { 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 i = 0; i < 4; ++i) { + + for (uint8_t j = 0; j < 4; ++j) { + + if (modes[j] == f_color_mode_dark_e || modes[j] == f_color_mode_light_e) { + will_return(__wrap_getenv, 0); + will_return(__wrap_getenv, terms[i]); + } + + const f_status_t status = f_color_load_context(modes[j], &context); + + assert_int_equal(status, statuss[j]); + + if (modes[j] == f_color_mode_color_not_e) { + assert_ptr_equal(context.set.reset.before, &f_string_empty_s); + assert_ptr_equal(context.set.reset.after, &f_string_empty_s); + + assert_ptr_equal(context.set.warning.before, &f_string_empty_s); + assert_ptr_equal(context.set.warning.after, &f_string_empty_s); + + assert_ptr_equal(context.set.error.before, &f_string_empty_s); + assert_ptr_equal(context.set.error.after, &f_string_empty_s); + + assert_ptr_equal(context.set.title.before, &f_string_empty_s); + assert_ptr_equal(context.set.title.after, &f_string_empty_s); + + assert_ptr_equal(context.set.notable.before, &f_string_empty_s); + assert_ptr_equal(context.set.notable.after, &f_string_empty_s); - for (uint8_t j = 0; j < 3; ++j) { + assert_ptr_equal(context.set.important.before, &f_string_empty_s); + assert_ptr_equal(context.set.important.after, &f_string_empty_s); - will_return(__wrap_getenv, i); - will_return(__wrap_getenv, terms[j]); + assert_ptr_equal(context.set.standout.before, &f_string_empty_s); + assert_ptr_equal(context.set.standout.after, &f_string_empty_s); - const f_status_t status = f_color_load_context(F_true, &context); + assert_ptr_equal(context.set.success.before, &f_string_empty_s); + assert_ptr_equal(context.set.success.after, &f_string_empty_s); - assert_int_equal(status, F_none); + assert_ptr_equal(context.set.normal.before, &f_string_empty_s); + assert_ptr_equal(context.set.normal.after, &f_string_empty_s); - assert_ptr_equal(context.set.reset.before, &context.reset); - assert_ptr_equal(context.set.reset.after, &context.reset); + assert_ptr_equal(context.set.normal_reset.before, &f_string_empty_s); + assert_ptr_equal(context.set.normal_reset.after, &f_string_empty_s); + } + else if (statuss[j] == F_data_not) { + // In this case, the data is not touched. + } + else { + 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.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.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.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.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.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.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.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.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); + assert_ptr_equal(context.set.normal_reset.before, &context.normal_reset); + assert_ptr_equal(context.set.normal_reset.after, &context.reset); + } } // for } // for -- 1.8.3.1