wibble 0.1.28
|
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net> 00002 (c) 2007 Enrico Zini <enrico@enricozini.org> */ 00003 00004 #include <wibble/commandline/options.h> 00005 00006 #include <wibble/test.h> 00007 00008 using namespace std; 00009 using namespace wibble::commandline; 00010 00011 struct TestCommandlineOptions { 00012 00013 // Happy trick to get access to protected methods we need to use for the tests 00014 template<typename T> 00015 class Public : public T 00016 { 00017 public: 00018 Public(const std::string& name) 00019 : T(name) {} 00020 Public(const std::string& name, 00021 char shortName, 00022 const std::string& longName, 00023 const std::string& usage = std::string(), 00024 const std::string& description = std::string()) 00025 : T(name, shortName, longName, usage, description) {} 00026 00027 virtual ArgList::iterator parse(ArgList& a, ArgList::iterator begin) { 00028 return T::parse(a, begin); 00029 } 00030 virtual bool parse(const std::string& str) { 00031 return T::parse(str); 00032 } 00033 }; 00034 00035 Test boolOpt() { 00036 Public<BoolOption> opt("test"); 00037 00038 assert_eq(opt.name(), string("test")); 00039 assert_eq(opt.isSet(), false); 00040 assert_eq(opt.boolValue(), false); 00041 assert_eq(opt.stringValue(), string("false")); 00042 00043 assert_eq(opt.parse(string()), false); 00044 assert_eq(opt.isSet(), true); 00045 assert_eq(opt.boolValue(), true); 00046 assert_eq(opt.stringValue(), string("true")); 00047 } 00048 00049 Test intOpt() { 00050 Public<IntOption> opt("test"); 00051 00052 assert_eq(opt.name(), string("test")); 00053 assert_eq(opt.isSet(), false); 00054 assert_eq(opt.boolValue(), false); 00055 assert_eq(opt.intValue(), 0); 00056 assert_eq(opt.stringValue(), string("0")); 00057 00058 assert_eq(opt.parse("42"), true); 00059 assert_eq(opt.isSet(), true); 00060 assert_eq(opt.boolValue(), true); 00061 assert_eq(opt.intValue(), 42); 00062 assert_eq(opt.stringValue(), string("42")); 00063 } 00064 00065 Test stringOpt() { 00066 Public<StringOption> opt("test"); 00067 00068 assert_eq(opt.name(), string("test")); 00069 assert_eq(opt.isSet(), false); 00070 assert_eq(opt.boolValue(), false); 00071 assert_eq(opt.stringValue(), string()); 00072 00073 assert_eq(opt.parse("-a"), true); 00074 assert_eq(opt.isSet(), true); 00075 assert_eq(opt.boolValue(), true); 00076 assert_eq(opt.stringValue(), "-a"); 00077 } 00078 00079 Test vectorBoolOpt() { 00080 Public< VectorOption<Bool> > opt("test"); 00081 assert_eq(opt.name(), string("test")); 00082 assert_eq(opt.isSet(), false); 00083 assert_eq(opt.boolValue(), false); 00084 assert_eq(opt.values().size(), 0u); 00085 00086 assert_eq(opt.parse("yes"), true); 00087 assert_eq(opt.isSet(), true); 00088 assert_eq(opt.boolValue(), true); 00089 assert_eq(opt.values().size(), 1u); 00090 assert_eq(opt.values()[0], true); 00091 00092 assert_eq(opt.parse("no"), true); 00093 assert_eq(opt.isSet(), true); 00094 assert_eq(opt.boolValue(), true); 00095 assert_eq(opt.values().size(), 2u); 00096 assert_eq(opt.values()[0], true); 00097 assert_eq(opt.values()[1], false); 00098 } 00099 00100 Test vectorStringOpt() { 00101 Public< VectorOption<String> > opt("test"); 00102 assert_eq(opt.name(), string("test")); 00103 assert_eq(opt.isSet(), false); 00104 assert_eq(opt.boolValue(), false); 00105 assert_eq(opt.values().size(), 0u); 00106 00107 assert_eq(opt.parse("-a"), true); 00108 assert_eq(opt.isSet(), true); 00109 assert_eq(opt.boolValue(), true); 00110 assert_eq(opt.values().size(), 1u); 00111 assert_eq(opt.values()[0], "-a"); 00112 00113 assert_eq(opt.parse("foo"), true); 00114 assert_eq(opt.isSet(), true); 00115 assert_eq(opt.boolValue(), true); 00116 assert_eq(opt.values().size(), 2u); 00117 assert_eq(opt.values()[0], "-a"); 00118 assert_eq(opt.values()[1], "foo"); 00119 } 00120 }; 00121 00122 // vim:set ts=4 sw=4: