2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/parse/enum.hpp>
|
|
|
|
#include <cruft/util/parse/value.hpp>
|
|
|
|
#include <cruft/util/std.hpp>
|
|
|
|
#include <cruft/util/tap.hpp>
|
2019-05-30 11:54:56 +10:00
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
cruft::TAP::logger tap;
|
2019-08-26 12:18:37 +10:00
|
|
|
|
|
|
|
{
|
2019-08-29 13:33:18 +10:00
|
|
|
enum enumeration_t : u16 { FOO, BAR = 2, QUX = 257 };
|
|
|
|
|
2019-08-26 12:18:37 +10:00
|
|
|
auto const cookie = cruft::parse::enumeration::setup<enumeration_t> ({
|
|
|
|
{ "FOO", FOO },
|
|
|
|
{ "BAR", BAR },
|
|
|
|
{ "QUX", QUX },
|
|
|
|
});
|
|
|
|
(void)cookie;
|
|
|
|
|
|
|
|
tap.expect_eq (FOO, cruft::parse::from_string<enumeration_t> ("FOO"), "enumeration, FOO");
|
|
|
|
tap.expect_eq (BAR, cruft::parse::from_string<enumeration_t> ("BAR"), "enumeration, BAR");
|
|
|
|
tap.expect_eq (QUX, cruft::parse::from_string<enumeration_t> ("QUX"), "enumeration, QUX");
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
u16 {QUX},
|
|
|
|
cruft::parse::enumeration::from_string<u16> (
|
|
|
|
cruft::typeidx<enumeration_t> (),
|
|
|
|
"QUX"
|
|
|
|
),
|
|
|
|
"u16, QUX"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-29 13:33:18 +10:00
|
|
|
enum class enum_class { VALUE };
|
|
|
|
|
2019-08-26 12:18:37 +10:00
|
|
|
auto const cookie = cruft::parse::enumeration::setup<enum_class> ({
|
|
|
|
{ "VALUE", enum_class::VALUE },
|
|
|
|
});
|
|
|
|
(void)cookie;
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
enum_class::VALUE,
|
|
|
|
cruft::parse::from_string<enum_class> ("VALUE"), "enum class, VALUE"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:33:18 +10:00
|
|
|
{
|
|
|
|
enum class func_enum { ASDF };
|
|
|
|
|
|
|
|
auto const cookie = cruft::parse::enumeration::setup<func_enum> ([] (auto &str) {
|
|
|
|
if (equal (str, "ASDF")) {
|
|
|
|
str = str.consume (4);
|
|
|
|
return func_enum::ASDF;
|
|
|
|
}
|
|
|
|
throw std::runtime_error ("Invalid enum value");
|
|
|
|
});
|
|
|
|
|
|
|
|
(void)cookie;
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
func_enum::ASDF,
|
|
|
|
cruft::parse::from_string<func_enum> ("ASDF"),
|
|
|
|
"function enum lookup"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:54:56 +10:00
|
|
|
return tap.status ();
|
|
|
|
}
|