Debian Codegraph @

libs/config/src/yaml2config_unittest.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include <gtest/gtest.h>
11 #include <mrpt/config.h>
12 #include <mrpt/config/CConfigFileMemory.h>
13
14 #if MRPT_HAS_FYAML
15
16 // clang-format off
17 const auto sampleYamlTxt = std::string(R"xxx(
18 # example YAML config file from a std::string
19 unscoped_key1: 42
20 maps:
21 pointMaps: 1
22 gridMaps: 2
23 pointMap_00:
24 max_x: 10.0
25 min_x: -10.0
26 gridmap_00: {max_x: 25.0, min_x: -25.0}
27 gridmap_01:
28 foo1: 0
29 layerName: lidar
30 # end of YAMLexample
31 )xxx");
32 // clang-format on
33
34 const std::string sampleCfgTxt =
35 "# example config file from std::string\n"
36 "[test]\n"
37 "key_num = 4\n"
38 "key_str = pepe\n";
39
40 const std::string sampleCfgTxt_as_yaml_correct =
41 "test:\n"
42 " key_num: 4\n"
43 " key_str: pepe\n";
44
45 TEST(yaml2ini, parse)
46 {
47 try
48 {
49 mrpt::config::CConfigFileMemory c;
50 c.setContentFromYAML(sampleYamlTxt);
51
52 // std::cout << "INI-liKe:\n" << c.getContent() << "\n";
53
54 EXPECT_EQ(1, c.read_int("maps", "pointMaps", 0));
55 EXPECT_EQ(2, c.read_int("maps", "gridMaps", 0));
56 EXPECT_EQ(25.0, c.read_int("gridmap_00", "max_x", 0));
57 EXPECT_EQ(42, c.read_int("", "unscoped_key1", 0));
58 }
59 catch (const std::exception& e)
60 {
61 std::cerr << mrpt::exception_to_str(e);
62 GTEST_FAIL();
63 }
64 }
65
66 TEST(ini2yaml, parse)
67 {
68 mrpt::config::CConfigFileMemory c;
69 c.setContent(sampleCfgTxt);
70
71 // Note: we don't compare the exact strings since order of keys may vary (?)
72 EXPECT_EQ(sampleCfgTxt_as_yaml_correct.size(), c.getContentAsYAML().size());
73 }
74 #endif