diff --git a/dse/clib/util/yaml.c b/dse/clib/util/yaml.c index e67619f..d1bb2df 100644 --- a/dse/clib/util/yaml.c +++ b/dse/clib/util/yaml.c @@ -182,14 +182,13 @@ DLL_PUBLIC const char** dse_yaml_get_array( DLL_PUBLIC int dse_yaml_get_bool(YamlNode* node, const char* name, bool* value) { if (node == NULL && name == NULL && value == NULL) return EINVAL; - if (node->node_type != YAML_SCALAR_NODE) return EINVAL; const char* _scalar = dse_yaml_get_scalar(node, name); if (_scalar == NULL) return EINVAL; /* True? */ const char* bool_true[] = { "y", "Y", "yes", "Yes", "YES", "true", "True", "TRUE", "on", "On", "ON", NULL }; for (const char** p = bool_true; *p; p++) { - if (strcmp(*p, node->scalar)) continue; + if (strcmp(*p, _scalar)) continue; *value = true; return 0; } @@ -197,7 +196,7 @@ DLL_PUBLIC int dse_yaml_get_bool(YamlNode* node, const char* name, bool* value) const char* bool_false[] = { "n", "N", "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF", NULL }; for (const char** p = bool_false; *p; p++) { - if (strcmp(*p, node->scalar)) continue; + if (strcmp(*p, _scalar)) continue; *value = false; return 0; } @@ -267,8 +266,8 @@ DLL_PUBLIC int dse_yaml_get_int(YamlNode* node, const char* name, int* value) /* Fallback to bool? */ bool _bool; if (dse_yaml_get_bool(node, name, &_bool) == 0) { - value = NULL; - return EINVAL; + *value = _bool; + return 0; } return EINVAL; } diff --git a/tests/util/CMakeLists.txt b/tests/util/CMakeLists.txt index 1aae587..252036d 100644 --- a/tests/util/CMakeLists.txt +++ b/tests/util/CMakeLists.txt @@ -35,10 +35,11 @@ target_link_libraries(test_util # -Wl,--wrap=strdup ) set(YAML_EXAMPLE_RESOURCE_FILES - data/bool.yaml - data/uint.yaml - data/test.yaml + data/dict_dup.yaml data/empty_doc.yaml + data/test.yaml + data/uint.yaml + data/values.yaml ) install(TARGETS test_util) install( diff --git a/tests/util/data/bool.yaml b/tests/util/data/bool.yaml deleted file mode 100644 index d57af08..0000000 --- a/tests/util/data/bool.yaml +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright 2023 Robert Bosch GmbH -# -# SPDX-License-Identifier: Apache-2.0 - ---- -foo: true -bar: Y -a: on -b: false diff --git a/tests/util/test_yaml.c b/tests/util/test_yaml.c index 089411d..82d1d47 100644 --- a/tests/util/test_yaml.c +++ b/tests/util/test_yaml.c @@ -11,7 +11,6 @@ #define FILE "util/data/values.yaml" #define EMPTY_FILE "util/data/empty_doc.yaml" #define UINT_FILE "util/data/uint.yaml" -#define BOOL_FILE "util/data/bool.yaml" #define DICT_DUP_FILE "util/data/dict_dup.yaml" #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) @@ -46,8 +45,8 @@ void test_yaml_get_uint(void** state) /* Boolean cases. */ { .node = "bools/numeric_true", .ev_uint = 1, .ex_rc = 0 }, { .node = "bools/numeric_false", .ev_uint = 0, .ex_rc = 0 }, - { .node = "bools/lc_true", .ev_uint = 0, .ex_rc = 22 }, - { .node = "bools/lc_false", .ev_uint = 0, .ex_rc = 22 }, + { .node = "bools/lc_true", .ev_uint = 1, .ex_rc = 0 }, + { .node = "bools/lc_false", .ev_uint = 0, .ex_rc = 0 }, }; const char* a = FILE; @@ -82,8 +81,8 @@ void test_yaml_get_int(void** state) /* Boolean cases. */ { .node = "bools/numeric_true", .ev_int = 1, .ex_rc = 0 }, { .node = "bools/numeric_false", .ev_int = 0, .ex_rc = 0 }, - { .node = "bools/lc_true", .ev_int = 0, .ex_rc = 22 }, - { .node = "bools/lc_false", .ev_int = 0, .ex_rc = 22 }, + { .node = "bools/lc_true", .ev_int = 1, .ex_rc = 0 }, + { .node = "bools/lc_false", .ev_int = 0, .ex_rc = 0 }, }; const char* a = FILE; @@ -181,11 +180,11 @@ void test_yaml_get_bool(void** state) test_case tc[] = { /* Good cases. */ - { .node = "lc_true", .ev_bool = 1, .ex_rc = 0 }, - { .node = "lc_false", .ev_bool = 0, .ex_rc = 0 }, + { .node = "bools/lc_true", .ev_bool = 1, .ex_rc = 0 }, + { .node = "bools/lc_false", .ev_bool = 0, .ex_rc = 0 }, /* Boolean Negative cases. */ - { .node = "numeric_true", .ev_bool = 0, .ex_rc = 22 }, - { .node = "numeric_false", .ev_bool = 0, .ex_rc = 22 }, + { .node = "bools/numeric_true", .ev_bool = 0, .ex_rc = 22 }, + { .node = "bools/numeric_false", .ev_bool = 0, .ex_rc = 22 }, /* Negative cases. */ { .node = "integers/positive", .ev_bool = 0, .ex_rc = 22 }, { .node = "integers/zero", .ev_bool = 0, .ex_rc = 22 }, @@ -202,8 +201,8 @@ void test_yaml_get_bool(void** state) bool value; log_debug("Testing node: %s", tc[i].node); - YamlNode* node = dse_yaml_find_node(doc, tc[i].node); - rc = dse_yaml_get_bool(node, tc[i].node, &value); + // YamlNode* node = dse_yaml_find_node(doc, tc[i].node); + rc = dse_yaml_get_bool(doc, tc[i].node, &value); assert_int_equal(rc, tc[i].ex_rc); assert_int_equal(value, tc[i].ev_bool); }