Skip to content

Commit

Permalink
Tests: Load: Add test for CYAML_ENUM with fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
tlsa committed Nov 18, 2020
1 parent 868b637 commit dc27b4f
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/units/load.c
Expand Up @@ -907,6 +907,66 @@ static bool test_load_mapping_entry_enum_sparse(
return ttest_pass(&tc);
}

/**
* Test loading an enumeration with numerical fallback.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_load_mapping_entry_enum_fallback(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
enum test_enum {
TEST_ENUM_FIRST = 3,
TEST_ENUM_SECOND = 77,
TEST_ENUM_THIRD = 183,
TEST_ENUM_FOURTH = 9900,
} value = TEST_ENUM_SECOND;
static const cyaml_strval_t strings[] = {
{ "first", TEST_ENUM_FIRST },
{ "second", TEST_ENUM_SECOND },
{ "third", TEST_ENUM_THIRD },
{ "fourth", TEST_ENUM_FOURTH },
};
static const unsigned char yaml[] =
"test_enum: 77\n";
struct target_struct {
enum test_enum test_value_enum;
} *data_tgt = NULL;
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_ENUM("test_enum", CYAML_FLAG_DEFAULT,
struct target_struct, test_value_enum,
strings, CYAML_ARRAY_LEN(strings)),
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;

ttest_ctx_t tc = ttest_start(report, __func__, cyaml_cleanup, &td);

err = cyaml_load_data(yaml, YAML_LEN(yaml), config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_OK) {
return ttest_fail(&tc, cyaml_strerror(err));
}

if (data_tgt->test_value_enum != value) {
return ttest_fail(&tc, "Incorrect value");
}

return ttest_pass(&tc);
}

/**
* Test loading a string to a character array.
*
Expand Down Expand Up @@ -6619,6 +6679,7 @@ bool load_tests(
pass &= test_load_mapping_entry_enum_sparse(rc, &config);
pass &= test_load_mapping_entry_ignore_deep(rc, &config);
pass &= test_load_mapping_entry_ignore_scalar(rc, &config);
pass &= test_load_mapping_entry_enum_fallback(rc, &config);
pass &= test_load_mapping_entry_bool_true_ptr(rc, &config);
pass &= test_load_mapping_entry_bool_false_ptr(rc, &config);
pass &= test_load_mapping_entry_string_ptr_empty(rc, &config);
Expand Down

0 comments on commit dc27b4f

Please sign in to comment.