From 49622a6a3079172b8ba86f9be0e4f3f6d4819ba2 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 11 Jun 2018 06:28:31 -0400 Subject: [PATCH] Tests: expand test-application test_conf - Add -v (verbose) and -b (load via bytearray) - Verbose prints the keys read from the file, - Bytes reads via an indirection through QByteArray, like Settings does --- src/modules/CMakeLists.txt | 2 +- src/modules/test_conf.cpp | 55 +++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 7f93c555a..514d6b4f6 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -6,7 +6,7 @@ set( LIST_SKIPPED_MODULES "" ) if( BUILD_TESTING ) add_executable( test_conf test_conf.cpp ) - target_link_libraries( test_conf ${YAMLCPP_LIBRARY} ) + target_link_libraries( test_conf ${YAMLCPP_LIBRARY} Qt5::Core ) target_include_directories( test_conf PUBLIC ${YAMLCPP_INCLUDE_DIR} ) endif() diff --git a/src/modules/test_conf.cpp b/src/modules/test_conf.cpp index 7ef557a3c..082f626c8 100644 --- a/src/modules/test_conf.cpp +++ b/src/modules/test_conf.cpp @@ -21,43 +21,86 @@ * shipped with each module for correctness -- well, for parseability. */ +#include +#include + #include + #include +#include +#include + using std::cerr; +static const char usage[] = "Usage: test_conf [-v] [-b] ...\n"; + int main(int argc, char** argv) { - if (argc != 2) + bool verbose = false; + bool bytes = false; + + int opt; + while ((opt = getopt(argc, argv, "vb")) != -1) { + switch (opt) { + case 'v': + verbose = true; + break; + case 'b': + bytes = true; + break; + default: /* '?' */ + cerr << usage; + return 1; + } + } + + if ( optind >= argc ) { - cerr << "Usage: test_conf \n"; + cerr << usage; return 1; } + const char* filename = argv[optind]; try { - YAML::Node doc = YAML::LoadFile( argv[1] ); + YAML::Node doc; + if ( bytes ) + { + QFile f( filename ); + if ( f.open( QFile::ReadOnly | QFile::Text ) ) + doc = YAML::Load( f.readAll().constData() ); + } + else + doc = YAML::LoadFile( filename ); if ( doc.IsNull() ) { // Special case: empty config files are valid, // but aren't a map. For the example configs, // this is still an error. - cerr << "WARNING:" << argv[1] << '\n'; + cerr << "WARNING:" << filename << '\n'; cerr << "WARNING: empty YAML\n"; return 1; } if ( !doc.IsMap() ) { - cerr << "WARNING:" << argv[1] << '\n'; + cerr << "WARNING:" << filename << '\n'; cerr << "WARNING: not-a-YAML-map\n"; return 1; } + + if ( verbose ) + { + cerr << "Keys:\n"; + for ( auto i = doc.begin(); i != doc.end(); ++i ) + cerr << i->first.as() << '\n'; + } } catch ( YAML::Exception& e ) { - cerr << "WARNING:" << argv[1] << '\n'; + cerr << "WARNING:" << filename << '\n'; cerr << "WARNING: YAML parser error " << e.what() << '\n'; return 1; }