diff options
author | Martin Braun <martin.braun@ettus.com> | 2017-11-10 21:43:09 -0800 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2018-02-01 06:35:37 +0100 |
commit | 4ebfea45f32cc845d7887e552e8c6084d8ef3067 (patch) | |
tree | 03072dbab4870b9d648d154331d0ee7911da1b24 /host/tests | |
parent | 21be43d40a0fe33af9597e77fb3465caa7079033 (diff) | |
download | uhd-4ebfea45f32cc845d7887e552e8c6084d8ef3067.tar.gz uhd-4ebfea45f32cc845d7887e552e8c6084d8ef3067.tar.bz2 uhd-4ebfea45f32cc845d7887e552e8c6084d8ef3067.zip |
lib: Add config_parser class
This class is not publicly exported. It is meant to read config files in
the INI format.
Reviewed-by: Brent Stapleton <brent.stapleton@ettus.com>
Diffstat (limited to 'host/tests')
-rw-r--r-- | host/tests/CMakeLists.txt | 13 | ||||
-rw-r--r-- | host/tests/config_parser_test.cpp | 97 |
2 files changed, 110 insertions, 0 deletions
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 0741191f2..b22f5722c 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -111,6 +111,19 @@ TARGET_LINK_LIBRARIES(nocscript_parser_test uhd ${Boost_LIBRARIES}) UHD_ADD_TEST(nocscript_parser_test nocscript_parser_test) UHD_INSTALL(TARGETS nocscript_parser_test RUNTIME DESTINATION ${PKG_LIB_DIR}/tests COMPONENT tests) +ADD_EXECUTABLE(config_parser_test + config_parser_test.cpp + ${CMAKE_SOURCE_DIR}/lib/utils/config_parser.cpp +) +TARGET_LINK_LIBRARIES(config_parser_test uhd ${Boost_LIBRARIES}) +UHD_ADD_TEST(config_parser_test config_parser_test) +UHD_INSTALL(TARGETS + config_parser_test + RUNTIME + DESTINATION ${PKG_LIB_DIR}/tests + COMPONENT tests +) + ######################################################################## # demo of a loadable module ######################################################################## diff --git a/host/tests/config_parser_test.cpp b/host/tests/config_parser_test.cpp new file mode 100644 index 000000000..e0ad3e919 --- /dev/null +++ b/host/tests/config_parser_test.cpp @@ -0,0 +1,97 @@ +// +// Copyright 2017 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#include <uhdlib/utils/config_parser.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/assign/list_of.hpp> +#include <fstream> +#include <cstdio> + +const std::string INI1_FILENAME = "test1.ini"; +const std::string INI1 = + "[section1]\n" + "key1=value1\n" + "key2=4\n" + "\n" + "; This is a comment\n" + "[section2]\n" + "key3=value with spaces\n" + "key4= leading and trailing spaces \n" +; + +const std::string INI2_FILENAME = "test2.ini"; +const std::string INI2 = + "[section2]\n" + "key3=value with even more spaces\n" + "\n" + "[section3]\n" + "key4=\"with quotes\"\n"; + +namespace { + //! Create files that can be read by the CUT + void make_config_parsers() + { + std::ofstream ini1(INI1_FILENAME); + ini1 << INI1 << std::endl; + ini1.close(); + std::ofstream ini2(INI2_FILENAME); + ini2 << INI2 << std::endl; + ini2.close(); + } + + //! Tidy up after us + void cleanup_config_parsers() + { + std::remove(INI1_FILENAME.c_str()); + std::remove(INI2_FILENAME.c_str()); + } +} + +BOOST_AUTO_TEST_CASE(test_config_parser){ + make_config_parsers(); + uhd::config_parser I(INI1_FILENAME); + BOOST_CHECK_EQUAL(I.sections().size(), 2); + BOOST_CHECK_EQUAL(I.options("section1").size(), 2); + BOOST_CHECK_EQUAL( + I.get<std::string>("section1", "key1"), + "value1" + ); + BOOST_CHECK_EQUAL( + I.get<int>("section1", "key2"), + 4 + ); + BOOST_CHECK_EQUAL( + I.get<std::string>("section2", "key3"), + "value with spaces" + ); + BOOST_CHECK_EQUAL( + I.get<std::string>("section2", "key4"), + "leading and trailing spaces" + ); + BOOST_CHECK_EQUAL( + I.get<std::string>("section2", "non_existent_key", "default"), + "default" + ); + BOOST_REQUIRE_THROW( + I.get<std::string>("section2", "non_existent_key"), + uhd::key_error + ); + I.read_file(INI2_FILENAME); + BOOST_CHECK_EQUAL( + I.get<std::string>("section2", "key3"), + "value with even more spaces" + ); + BOOST_CHECK_EQUAL( + I.get<std::string>("section1", "key1"), + "value1" + ); + BOOST_CHECK_EQUAL( + I.get<std::string>("section3", "key4"), + "\"with quotes\"" + ); + + cleanup_config_parsers(); +} |