Author: mlandres Date: Thu Nov 6 14:30:51 2008 New Revision: 11589 URL: http://svn.opensuse.org/viewcvs/zypp?rev=11589&view=rev Log: - enhance string to bool conversion and add tests. Modified: trunk/libzypp/tests/zypp/base/String_test.cc trunk/libzypp/zypp/base/String.cc trunk/libzypp/zypp/base/String.h Modified: trunk/libzypp/tests/zypp/base/String_test.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/String_te... ============================================================================== --- trunk/libzypp/tests/zypp/base/String_test.cc (original) +++ trunk/libzypp/tests/zypp/base/String_test.cc Thu Nov 6 14:30:51 2008 @@ -92,7 +92,7 @@ BOOST_CHECK_EQUAL( escaped, "bad\|ass\\\|worse" ); } -BOOST_AUTO_TEST_CASE(convertions) +BOOST_AUTO_TEST_CASE(conversions) { BOOST_CHECK_EQUAL(str::numstring(42), "42"); BOOST_CHECK_EQUAL(str::numstring(42, 6), " 42"); @@ -111,6 +111,62 @@ BOOST_CHECK_EQUAL(str::toLower("This IS A TeST"), "this is a test"); BOOST_CHECK_EQUAL(str::toUpper("This IS A TeST"), "THIS IS A TEST"); BOOST_CHECK_EQUAL(str::compareCI("TeST", "test"), 0); + + BOOST_CHECK_EQUAL(str::compareCI("TeST", "test"), 0); + BOOST_CHECK_EQUAL(str::compareCI("TeST", "test"), 0); +} + +BOOST_AUTO_TEST_CASE(conversions_to_bool) +{ + // true iff true-string {1,on,yes,true} + BOOST_CHECK_EQUAL( str::strToTrue("1"), true ); + BOOST_CHECK_EQUAL( str::strToTrue("42"), true ); + BOOST_CHECK_EQUAL( str::strToTrue("ON"), true ); + BOOST_CHECK_EQUAL( str::strToTrue("YES"), true ); + BOOST_CHECK_EQUAL( str::strToTrue("TRUE"), true ); + BOOST_CHECK_EQUAL( str::strToTrue("0"), false ); + BOOST_CHECK_EQUAL( str::strToTrue("OFF"), false ); + BOOST_CHECK_EQUAL( str::strToTrue("NO"), false ); + BOOST_CHECK_EQUAL( str::strToTrue("FALSE"), false ); + BOOST_CHECK_EQUAL( str::strToTrue(""), false ); + BOOST_CHECK_EQUAL( str::strToTrue("foo"), false ); + + // false iff false-string {0,off,no,false} + BOOST_CHECK_EQUAL( str::strToFalse("1"), true ); + BOOST_CHECK_EQUAL( str::strToFalse("42"), true ); + BOOST_CHECK_EQUAL( str::strToFalse("ON"), true ); + BOOST_CHECK_EQUAL( str::strToFalse("YES"), true ); + BOOST_CHECK_EQUAL( str::strToFalse("TRUE"), true ); + BOOST_CHECK_EQUAL( str::strToFalse("0"), false ); + BOOST_CHECK_EQUAL( str::strToFalse("OFF"), false ); + BOOST_CHECK_EQUAL( str::strToFalse("NO"), false ); + BOOST_CHECK_EQUAL( str::strToFalse("FALSE"), false ); + BOOST_CHECK_EQUAL( str::strToFalse(""), true ); + BOOST_CHECK_EQUAL( str::strToFalse("foo"), true ); + + // true iff true-string + BOOST_CHECK_EQUAL( str::strToBool("TRUE", false), true ); + BOOST_CHECK_EQUAL( str::strToBool("FALSE", false), false ); + BOOST_CHECK_EQUAL( str::strToBool("", false), false ); + BOOST_CHECK_EQUAL( str::strToBool("foo", false), false ); + + // false iff false-string + BOOST_CHECK_EQUAL( str::strToBool("TRUE", true), true ); + BOOST_CHECK_EQUAL( str::strToBool("FALSE", true), false ); + BOOST_CHECK_EQUAL( str::strToBool("", true), true ); + BOOST_CHECK_EQUAL( str::strToBool("foo", true), true ); + + // true/false iff true/false-string, else unchanged + bool ret; + ret = true; BOOST_CHECK_EQUAL( str::strToBoolNodefault("TRUE", ret), true ); + ret = true; BOOST_CHECK_EQUAL( str::strToBoolNodefault("FALSE", ret), false ); + ret = true; BOOST_CHECK_EQUAL( str::strToBoolNodefault("", ret), true ); + ret = true; BOOST_CHECK_EQUAL( str::strToBoolNodefault("foo", ret), true ); + + ret = false; BOOST_CHECK_EQUAL( str::strToBoolNodefault("TRUE", ret), true ); + ret = false; BOOST_CHECK_EQUAL( str::strToBoolNodefault("FALSE", ret), false ); + ret = false; BOOST_CHECK_EQUAL( str::strToBoolNodefault("", ret), false ); + ret = false; BOOST_CHECK_EQUAL( str::strToBoolNodefault("foo", ret), false ); } BOOST_AUTO_TEST_CASE(operations) Modified: trunk/libzypp/zypp/base/String.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/String.cc?rev=1... ============================================================================== --- trunk/libzypp/zypp/base/String.cc (original) +++ trunk/libzypp/zypp/base/String.cc Thu Nov 6 14:30:51 2008 @@ -64,6 +64,7 @@ || t == "yes" || t == "true" || t == "on" + || strtonum<long long>( str ) ); } Modified: trunk/libzypp/zypp/base/String.h URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/String.h?rev=11... ============================================================================== --- trunk/libzypp/zypp/base/String.h (original) +++ trunk/libzypp/zypp/base/String.h Thu Nov 6 14:30:51 2008 @@ -274,7 +274,7 @@ /** Parsing boolean from string. */ //@{ - /** Return \c true if str is <tt>1, true, yes, on</tt>. */ + /** Return \c true if str is <tt>1, true, yes, on</tt> (or a nonzero number). */ bool strToTrue( const C_Str & str ); /** Return \c false if str is <tt>0, false, no, off</tt>. */ @@ -283,9 +283,21 @@ /** Parse \c str into a bool depending on the default value. * If the \c default is true, look for a legal \c false string. * If the \c default is false, look for a legal \c true string. - */ + */ inline bool strToBool( const C_Str & str, bool default_r ) { return( default_r ? strToFalse( str ) : strToTrue( str ) ); } + + /** Parse \c str into a bool if it's a legal \c true or \c false string. + * If \c str is not a recognized \c true or \c false string, \a return_r + * is left unchanged. + */ + inline bool strToBoolNodefault( const C_Str & str, bool & return_r ) + { + if ( strToTrue( str ) ) return (return_r = true); + if ( !strToFalse( str ) ) return (return_r = false); + return return_r; + } + //@} /** -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org