@@ -15,45 +15,66 @@ struct Person {
1515 std::vector<Person> children;
1616};
1717
18- TEST (cbor, test_field_error_messages) {
18+ TEST (cbor, test_empty_field_error_messages) {
19+ // Use JSON input to generic parser for convenient flexible test input
20+ const std::string faulty_string =
21+ R"( {})" ;
22+ const auto faulty_generic = rfl::json::read<rfl::Generic>(faulty_string);
23+ const auto faulty_cbor = rfl::cbor::write (faulty_generic);
24+
25+ rfl::Result<Person> result = rfl::error (" result didn't get set" );
26+ EXPECT_NO_THROW ({
27+ result = rfl::cbor::read<Person>(faulty_cbor);
28+ });
29+
30+ const std::string expected = R"( Found 4 errors:
31+ 1) Field named 'firstName' not found.
32+ 2) Field named 'lastName' not found.
33+ 3) Field named 'birthday' not found.
34+ 4) Field named 'children' not found.)" ;
35+ EXPECT_EQ (result.error ().what (), expected);
36+
37+ EXPECT_FALSE (result.has_value ());
38+ }
39+
40+ TEST (cbor, test_field_type_error_messages) {
41+ // Use JSON input to generic parser for convenient flexible test input
1942 const std::string faulty_string =
2043 R"( {"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})" ;
2144 const auto faulty_generic = rfl::json::read<rfl::Generic>(faulty_string);
2245 const auto faulty_cbor = rfl::cbor::write (faulty_generic);
23- const auto result = rfl::cbor::read<Person>(faulty_cbor);
46+
47+ rfl::Result<Person> result = rfl::error (" result didn't get set" );
48+ EXPECT_NO_THROW ({
49+ result = rfl::cbor::read<Person>(faulty_cbor);
50+ });
2451
2552 // Order of errors is different than input JSON because rfl::Generic doesn't preserve order
2653 const std::string expected = R"( Found 3 errors:
27541) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'.
28552) Failed to parse field 'lastName': Could not cast to string.
29563) Field named 'children' not found.)" ;
3057
31- EXPECT_TRUE (! result.has_value () && true );
58+ EXPECT_FALSE ( result.has_value ());
3259
3360 EXPECT_EQ (result.error ().what (), expected);
3461}
3562
3663TEST (cbor, test_decode_error_without_exception) {
37- const std::string good_string =
38- R"( {"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})" ;
39- const auto good_generic = rfl::json::read<rfl::Generic>(good_string);
40- auto faulty_cbor = rfl::cbor::write (good_generic);
41- faulty_cbor[1 ] = ' \xff ' ; // Corrupt structure of CBOR encoding
64+ const Person homer{" Homer" , " Simpson" , " 1987-04-19" };
65+ auto faulty_cbor = rfl::cbor::write (homer);
66+ faulty_cbor[1 ] = ' \xfe ' ; // Corrupt structure of CBOR encoding
4267
4368 rfl::Result<Person> result = rfl::error (" result didn't get set" );
4469 EXPECT_NO_THROW ({
4570 result = rfl::cbor::read<Person>(faulty_cbor);
4671 });
4772
4873 // A proposal: A generic prefix, followed by the underlying library's error output
49- const std::string expected = R"( Found 4 errors:
50- 1) Field named 'firstName' not found.
51- 2) Field named 'lastName' not found.
52- 3) Field named 'birthday' not found.
53- 4) Field named 'children' not found.)" ;
74+ const std::string expected = R"( Could not parse CBOR: An unknown type was found in the stream at position 1)" ;
5475 EXPECT_EQ (result.error ().what (), expected);
5576
56- EXPECT_TRUE (! result.has_value () && true );
77+ EXPECT_FALSE ( result.has_value ());
5778}
5879
5980} // namespace test_error_messages
0 commit comments