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
This commit is contained in:
parent
a40c36ef49
commit
49622a6a30
@ -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()
|
||||
|
||||
|
@ -21,43 +21,86 @@
|
||||
* shipped with each module for correctness -- well, for parseability.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <QFile>
|
||||
#include <QByteArray>
|
||||
|
||||
using std::cerr;
|
||||
|
||||
static const char usage[] = "Usage: test_conf [-v] [-b] <file> ...\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 <file.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<std::string>() << '\n';
|
||||
}
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
cerr << "WARNING:" << argv[1] << '\n';
|
||||
cerr << "WARNING:" << filename << '\n';
|
||||
cerr << "WARNING: YAML parser error " << e.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user