[complement-svn] SF.net SVN: complement: [1878] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-05-21 08:37:24
|
Revision: 1878 http://complement.svn.sourceforge.net/complement/?rev=1878&view=rev Author: complement Date: 2008-05-21 01:37:08 -0700 (Wed, 21 May 2008) Log Message: ----------- separate unit tests for type_traits and options; partially fixed exceptions usage; sample how to use generic container (options storage) Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cpp trunk/complement/explore/lib/misc/ut/misc_test_suite.cc trunk/complement/explore/lib/misc/ut/misc_test_suite.h trunk/complement/explore/lib/misc/ut/opts_test.cc trunk/complement/explore/lib/misc/ut/unit_test.cc Modified: trunk/complement/explore/include/misc/opts.h =================================================================== --- trunk/complement/explore/include/misc/opts.h 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/include/misc/opts.h 2008-05-21 08:37:08 UTC (rev 1878) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/05/01 12:02:26 ptr> +// -*- C++ -*- Time-stamp: <08/05/21 12:17:39 yeti> #ifndef __OPTS_H__ #define __OPTS_H__ @@ -76,46 +76,44 @@ std::string get_author() const; std::string get_copyright() const; - struct error : public std::logic_error - { - error(const std::string& what) : - std::logic_error(what) - { } - }; - // error handling - struct invalid_opt : public error + struct unknown_option : + public std::invalid_argument { - invalid_opt(const std::string& _optname) : - error(std::string("invalid opt: ").append(_optname)) + unknown_option( const std::string& _optname ) : + std::invalid_argument( std::string("unknown option ").append(_optname) ) { } }; - struct missing_arg : public error + struct missing_arg : + public std::invalid_argument { - missing_arg( const std::string& _optname) : - error(std::string("missing argument for option ").append(_optname)) + missing_arg( const std::string& _optname ) : + std::invalid_argument( std::string("missing argument for option ").append(_optname) ) { } }; - struct invalid_arg : public error + struct invalid_arg : + public std::invalid_argument { invalid_arg( const std::string& _optname, const std::string& _argname) : - error(std::string("invalid argument [").append(_argname).append("] for option ").append(_optname)) + std::invalid_argument(std::string("invalid argument [").append(_argname).append("] for option ").append(_optname)) { } }; - struct bad_usage : public error + struct bad_usage : + public std::invalid_argument { - bad_usage( const std::string& what) : - error(what) - { } + bad_usage( const std::string& descr ) : + std::invalid_argument( descr ) + { } }; //std::vector< std::string > args; private: // data - std::vector< Opt > storage; + typedef std::vector< Opt > options_container_type; + options_container_type storage; std::string pname; std::string brief; @@ -142,30 +140,30 @@ template <class T> T Opts::get( char _shortname ) { - int i; + options_container_type::const_iterator i; T res; - for (i = 0;i < storage.size();i++) - if (storage[i].shortname == _shortname) - { - if (!storage[i].has_arg) + for ( i = storage.begin(); i != storage.end(); ++i ) { + if ( i->shortname == _shortname ) { + if ( !i->has_arg ) { throw bad_usage("using Opts::get for option without arguments"); + } std::stringstream ss; - if (!storage[i].args.empty()) - ss << storage[i].args[0]; - else - ss << storage[i].default_v; - + ss << (i->args.empty() ? i->default_v : i->args[0]); ss >> res; - if (ss.fail()) - throw invalid_arg(std::string("-") + std::string(1,_shortname),storage[i].args[0]); + if (ss.fail()) { + throw invalid_arg(std::string("-") + std::string(1,_shortname), i->args[0]); + } break; } + } - if (i == storage.size()) - throw invalid_opt(std::string("-") + std::string(1,_shortname)); + if ( i == storage.end() ) { + throw unknown_option( std::string("-") + _shortname ); + } + return res; } @@ -192,7 +190,7 @@ } if (i == storage.size()) - throw invalid_opt(std::string("-") + std::string(1,_shortname)); + throw unknown_option(std::string("-") + std::string(1,_shortname)); return res; } @@ -222,7 +220,7 @@ } if (i == storage.size()) - throw invalid_opt(std::string("--") + _longname); + throw unknown_option(std::string("--") + _longname); return res; } @@ -248,7 +246,7 @@ } if (i == storage.size()) - throw invalid_opt(std::string("--") + _longname); + throw unknown_option(std::string("--") + _longname); return res; } @@ -290,7 +288,7 @@ } if (i == storage.size()) - throw invalid_opt(std::string("-") + std::string(1,_shortname)); + throw unknown_option(std::string("-") + std::string(1,_shortname)); } template <class BackInsertIterator> @@ -331,7 +329,7 @@ } if (i == storage.size()) - throw invalid_opt(std::string("-") + _longname); + throw unknown_option(std::string("-") + _longname); } #endif Modified: trunk/complement/explore/lib/misc/opts.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts.cpp 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/lib/misc/opts.cpp 2008-05-21 08:37:08 UTC (rev 1878) @@ -166,7 +166,7 @@ int p = get_opt_index(opt); if (p == storage.size()) - throw invalid_opt(opt); + throw unknown_option(opt); else { storage[p].is_set = true; @@ -194,7 +194,7 @@ { int p = get_opt_index(string("-") + optgroup[j]); if (p == storage.size()) - throw invalid_opt( "-" + string(1,optgroup[j]) ); + throw unknown_option( "-" + string(1,optgroup[j]) ); else { storage[p].is_set = true; Modified: trunk/complement/explore/lib/misc/ut/misc_test_suite.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-05-21 08:37:08 UTC (rev 1878) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/05/01 15:18:28 ptr> +// -*- C++ -*- Time-stamp: <08/05/21 12:30:07 yeti> /* * Copyright (c) 2007, 2008 @@ -16,7 +16,7 @@ int EXAM_IMPL(misc_test_suite) { - exam::test_suite t( "libmisc? test" ); + exam::test_suite t( "libmisc type_traits test" ); misc_test test; exam::test_suite::test_case_type tc[10]; @@ -55,8 +55,13 @@ t.add( &misc_test::type_traits_is_pod, test, "is_pod", tc[0] ); t.add( &misc_test::type_traits_is_empty, test, "is_empty", tc[0] ); + return t.girdle(); +} +int EXAM_IMPL(options_test_suite) +{ // test for options parsing + exam::test_suite t( "libmisc, options test" ); opts_test opts; @@ -94,4 +99,4 @@ t.add( &opts_test::multiple_args, opts,"multiple_args"); return t.girdle(); -}; +} Modified: trunk/complement/explore/lib/misc/ut/misc_test_suite.h =================================================================== --- trunk/complement/explore/lib/misc/ut/misc_test_suite.h 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/lib/misc/ut/misc_test_suite.h 2008-05-21 08:37:08 UTC (rev 1878) @@ -1,7 +1,7 @@ -// -*- C++ -*- Time-stamp: <07/07/16 22:08:39 ptr> +// -*- C++ -*- Time-stamp: <08/05/21 12:31:01 yeti> /* - * Copyright (c) 2007 + * Copyright (c) 2007, 2008 * Petr Ovtchenkov * * Licensed under the Academic Free License Version 3.0 @@ -14,5 +14,6 @@ #include <exam/suite.h> int EXAM_DECL(misc_test_suite); +int EXAM_DECL(options_test_suite); #endif // __MISC_TEST_SUITE_H Modified: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-21 08:37:08 UTC (rev 1878) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/05/01 15:16:10 ptr> +// -*- C++ -*- Time-stamp: <08/05/21 12:20:14 yeti> /* * Copyright (c) 2008 @@ -32,7 +32,7 @@ EXAM_CHECK( opts.is_set( 'h' ) ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { } catch ( const Opts::invalid_arg& e ) { } @@ -54,7 +54,7 @@ EXAM_CHECK( opts.is_set( 'h' ) ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { } catch ( const Opts::invalid_arg& e ) { } @@ -78,7 +78,7 @@ EXAM_CHECK( opts.is_set( 'p' ) ); EXAM_CHECK( opts.get<int>( 'p' ) == 80 ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { } catch ( const Opts::invalid_arg& e ) { } @@ -102,7 +102,7 @@ EXAM_CHECK( opts.is_set( 'p' ) ); EXAM_CHECK( opts.get<int>( 'p' ) == 80 ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { } catch ( const Opts::invalid_arg& e ) { } @@ -126,7 +126,7 @@ EXAM_CHECK( !opts.is_set( 'p' ) ); EXAM_CHECK( opts.get<int>( 'p' ) == 0 ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { } catch ( const Opts::invalid_arg& e ) { } @@ -150,7 +150,7 @@ EXAM_ERROR( "exception expected" ); } - catch ( const Opts::invalid_opt& e ) { + catch ( const Opts::unknown_option& e ) { exception_happens = true; } catch ( const Opts::invalid_arg& e ) { @@ -491,7 +491,7 @@ { opts.parse( argc, argv ); } - catch(const Opts::invalid_opt& e) + catch(const Opts::unknown_option& e) { exception_happens = true; } Modified: trunk/complement/explore/lib/misc/ut/unit_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/unit_test.cc 2008-05-16 15:18:52 UTC (rev 1877) +++ trunk/complement/explore/lib/misc/ut/unit_test.cc 2008-05-21 08:37:08 UTC (rev 1878) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/07/16 22:12:10 ptr> +// -*- C++ -*- Time-stamp: <08/05/21 12:33:01 yeti> /* * Copyright (c) 2007 @@ -15,5 +15,9 @@ int main( int, char ** ) { - return misc_test_suite(0); + int res1 = misc_test_suite(0); + + int res2 = options_test_suite(0); + + return res1 || res2; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |