utils: add helpers for assoc-pairs
Add some unit-test, too.
This commit is contained in:
@ -298,6 +298,33 @@ test_error()
|
||||
g_clear_error(&err);
|
||||
}
|
||||
|
||||
enum struct TestEnum { A, B, C };
|
||||
constexpr AssocPairs<TestEnum, std::string_view, 3>
|
||||
test_epairs = {{
|
||||
{TestEnum::A, "a"},
|
||||
{TestEnum::B, "b"},
|
||||
{TestEnum::C, "c"},
|
||||
}};
|
||||
|
||||
static constexpr Option<std::string_view>
|
||||
to_name(TestEnum te)
|
||||
{
|
||||
return to_second(test_epairs, te);
|
||||
}
|
||||
|
||||
static constexpr Option<TestEnum>
|
||||
to_type(std::string_view name)
|
||||
{
|
||||
return to_first(test_epairs, name);
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
test_enum_pairs(void)
|
||||
{
|
||||
assert_equal(to_name(TestEnum::A).value(), "a");
|
||||
g_assert_true(to_type("c").value() == TestEnum::C);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
@ -317,6 +344,7 @@ main(int argc, char* argv[])
|
||||
g_test_add_func("/utils/to-from-lexnum", test_to_from_lexnum);
|
||||
g_test_add_func("/utils/locale-workaround", test_locale_workaround);
|
||||
g_test_add_func("/utils/error", test_error);
|
||||
g_test_add_func("/utils/enum-pairs", test_enum_pairs);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user