2011-09-11 08:22:30 +04:00
# include "nodetests.h"
# include "yaml-cpp/yaml.h"
2011-11-14 02:12:39 +04:00
# include <iostream>
2011-09-11 08:22:30 +04:00
namespace {
struct TEST {
TEST ( ) : ok ( false ) { }
TEST ( bool ok_ ) : ok ( ok_ ) { }
TEST ( const char * error_ ) : ok ( false ) , error ( error_ ) { }
bool ok ;
std : : string error ;
} ;
}
# define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false)
2012-01-13 09:49:05 +04:00
# define YAML_ASSERT_THROWS(cond, exc) do { try { (cond); return " Expression did not throw: " #cond; } catch(const exc&) {} catch(...) { return " Expression threw something other than " #exc ": " #cond; } } while(false)
2011-09-11 08:22:30 +04:00
namespace Test
{
namespace Node
{
TEST SimpleScalar ( )
{
YAML : : Node node = YAML : : Node ( " Hello, World! " ) ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsScalar ( ) ) ;
2011-09-11 08:22:30 +04:00
YAML_ASSERT ( node . as < std : : string > ( ) = = " Hello, World! " ) ;
return true ;
}
2011-09-11 08:31:12 +04:00
TEST IntScalar ( )
{
YAML : : Node node = YAML : : Node ( 15 ) ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsScalar ( ) ) ;
2011-09-11 08:31:12 +04:00
YAML_ASSERT ( node . as < int > ( ) = = 15 ) ;
return true ;
}
2011-09-12 06:51:04 +04:00
TEST SimpleAppendSequence ( )
2011-09-11 08:31:12 +04:00
{
YAML : : Node node ;
2012-01-21 22:11:40 +04:00
node . push_back ( 10 ) ;
node . push_back ( " foo " ) ;
node . push_back ( " monkey " ) ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsSequence ( ) ) ;
2011-09-11 08:31:12 +04:00
YAML_ASSERT ( node . size ( ) = = 3 ) ;
YAML_ASSERT ( node [ 0 ] . as < int > ( ) = = 10 ) ;
YAML_ASSERT ( node [ 1 ] . as < std : : string > ( ) = = " foo " ) ;
2011-09-12 01:21:36 +04:00
YAML_ASSERT ( node [ 2 ] . as < std : : string > ( ) = = " monkey " ) ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsSequence ( ) ) ;
2011-09-11 08:31:12 +04:00
return true ;
}
2011-09-12 01:02:31 +04:00
2011-09-12 06:51:04 +04:00
TEST SimpleAssignSequence ( )
{
YAML : : Node node ;
node [ 0 ] = 10 ;
node [ 1 ] = " foo " ;
node [ 2 ] = " monkey " ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsSequence ( ) ) ;
2011-09-12 06:51:04 +04:00
YAML_ASSERT ( node . size ( ) = = 3 ) ;
YAML_ASSERT ( node [ 0 ] . as < int > ( ) = = 10 ) ;
YAML_ASSERT ( node [ 1 ] . as < std : : string > ( ) = = " foo " ) ;
YAML_ASSERT ( node [ 2 ] . as < std : : string > ( ) = = " monkey " ) ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsSequence ( ) ) ;
2011-09-12 06:51:04 +04:00
return true ;
}
2011-09-12 01:02:31 +04:00
TEST SimpleMap ( )
{
YAML : : Node node ;
node [ " key " ] = " value " ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-12 01:02:31 +04:00
YAML_ASSERT ( node [ " key " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node . size ( ) = = 1 ) ;
return true ;
}
TEST MapWithUndefinedValues ( )
{
YAML : : Node node ;
node [ " key " ] = " value " ;
node [ " undefined " ] ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-12 01:02:31 +04:00
YAML_ASSERT ( node [ " key " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node . size ( ) = = 1 ) ;
2011-09-12 02:36:08 +04:00
2011-09-12 02:16:26 +04:00
node [ " undefined " ] = " monkey " ;
YAML_ASSERT ( node [ " undefined " ] . as < std : : string > ( ) = = " monkey " ) ;
YAML_ASSERT ( node . size ( ) = = 2 ) ;
2011-09-12 02:36:08 +04:00
return true ;
}
TEST MapIteratorWithUndefinedValues ( )
{
YAML : : Node node ;
node [ " key " ] = " value " ;
node [ " undefined " ] ;
std : : size_t count = 0 ;
for ( YAML : : const_iterator it = node . begin ( ) ; it ! = node . end ( ) ; + + it )
count + + ;
YAML_ASSERT ( count = = 1 ) ;
2011-09-12 01:02:31 +04:00
return true ;
}
2011-09-12 08:14:52 +04:00
TEST SimpleSubkeys ( )
{
YAML : : Node node ;
node [ " device " ] [ " udid " ] = " 12345 " ;
node [ " device " ] [ " name " ] = " iPhone " ;
node [ " device " ] [ " os " ] = " 4.0 " ;
node [ " username " ] = " monkey " ;
YAML_ASSERT ( node [ " device " ] [ " udid " ] . as < std : : string > ( ) = = " 12345 " ) ;
YAML_ASSERT ( node [ " device " ] [ " name " ] . as < std : : string > ( ) = = " iPhone " ) ;
YAML_ASSERT ( node [ " device " ] [ " os " ] . as < std : : string > ( ) = = " 4.0 " ) ;
YAML_ASSERT ( node [ " username " ] . as < std : : string > ( ) = = " monkey " ) ;
return true ;
}
TEST StdVector ( )
{
std : : vector < int > primes ;
primes . push_back ( 2 ) ;
primes . push_back ( 3 ) ;
primes . push_back ( 5 ) ;
primes . push_back ( 7 ) ;
primes . push_back ( 11 ) ;
primes . push_back ( 13 ) ;
YAML : : Node node ;
node [ " primes " ] = primes ;
YAML_ASSERT ( node [ " primes " ] . as < std : : vector < int > > ( ) = = primes ) ;
return true ;
}
2011-09-12 08:18:19 +04:00
TEST StdList ( )
{
std : : list < int > primes ;
primes . push_back ( 2 ) ;
primes . push_back ( 3 ) ;
primes . push_back ( 5 ) ;
primes . push_back ( 7 ) ;
primes . push_back ( 11 ) ;
primes . push_back ( 13 ) ;
YAML : : Node node ;
node [ " primes " ] = primes ;
YAML_ASSERT ( node [ " primes " ] . as < std : : list < int > > ( ) = = primes ) ;
return true ;
}
2011-09-12 09:29:39 +04:00
TEST StdMap ( )
{
std : : map < int , int > squares ;
squares [ 0 ] = 0 ;
squares [ 1 ] = 1 ;
squares [ 2 ] = 4 ;
squares [ 3 ] = 9 ;
squares [ 4 ] = 16 ;
YAML : : Node node ;
node [ " squares " ] = squares ;
YAML_ASSERT ( ( node [ " squares " ] . as < std : : map < int , int > > ( ) = = squares ) ) ;
return true ;
}
2011-09-12 21:42:23 +04:00
TEST SimpleAlias ( )
{
YAML : : Node node ;
node [ " foo " ] = " value " ;
node [ " bar " ] = node [ " foo " ] ;
YAML_ASSERT ( node [ " foo " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node [ " bar " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node [ " foo " ] = = node [ " bar " ] ) ;
YAML_ASSERT ( node . size ( ) = = 2 ) ;
return true ;
}
TEST AliasAsKey ( )
{
YAML : : Node node ;
node [ " foo " ] = " value " ;
YAML : : Node value = node [ " foo " ] ;
node [ value ] = " foo " ;
YAML_ASSERT ( node [ " foo " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node [ value ] . as < std : : string > ( ) = = " foo " ) ;
YAML_ASSERT ( node [ " value " ] . as < std : : string > ( ) = = " foo " ) ;
YAML_ASSERT ( node . size ( ) = = 2 ) ;
return true ;
}
2011-09-12 21:48:51 +04:00
TEST SelfReferenceSequence ( )
{
YAML : : Node node ;
node [ 0 ] = node ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsSequence ( ) ) ;
2011-09-12 21:48:51 +04:00
YAML_ASSERT ( node . size ( ) = = 1 ) ;
YAML_ASSERT ( node [ 0 ] = = node ) ;
YAML_ASSERT ( node [ 0 ] [ 0 ] = = node ) ;
YAML_ASSERT ( node [ 0 ] [ 0 ] = = node [ 0 ] ) ;
return true ;
}
TEST ValueSelfReferenceMap ( )
{
YAML : : Node node ;
node [ " key " ] = node ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-12 21:48:51 +04:00
YAML_ASSERT ( node . size ( ) = = 1 ) ;
YAML_ASSERT ( node [ " key " ] = = node ) ;
YAML_ASSERT ( node [ " key " ] [ " key " ] = = node ) ;
YAML_ASSERT ( node [ " key " ] [ " key " ] = = node [ " key " ] ) ;
return true ;
}
TEST KeySelfReferenceMap ( )
{
YAML : : Node node ;
node [ node ] = " value " ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-12 21:48:51 +04:00
YAML_ASSERT ( node . size ( ) = = 1 ) ;
YAML_ASSERT ( node [ node ] . as < std : : string > ( ) = = " value " ) ;
return true ;
}
TEST SelfReferenceMap ( )
{
YAML : : Node node ;
node [ node ] = node ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-12 21:48:51 +04:00
YAML_ASSERT ( node . size ( ) = = 1 ) ;
YAML_ASSERT ( node [ node ] = = node ) ;
YAML_ASSERT ( node [ node ] [ node ] = = node ) ;
YAML_ASSERT ( node [ node ] [ node ] = = node [ node ] ) ;
return true ;
}
2011-09-13 07:09:16 +04:00
TEST TempMapVariable ( )
{
YAML : : Node node ;
YAML : : Node tmp = node [ " key " ] ;
tmp = " value " ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-13 07:09:16 +04:00
YAML_ASSERT ( node . size ( ) = = 1 ) ;
YAML_ASSERT ( node [ " key " ] . as < std : : string > ( ) = = " value " ) ;
return true ;
}
TEST TempMapVariableAlias ( )
{
YAML : : Node node ;
YAML : : Node tmp = node [ " key " ] ;
tmp = node [ " other " ] ;
node [ " other " ] = " value " ;
2011-09-13 23:10:27 +04:00
YAML_ASSERT ( node . IsMap ( ) ) ;
2011-09-13 07:09:16 +04:00
YAML_ASSERT ( node . size ( ) = = 2 ) ;
YAML_ASSERT ( node [ " key " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node [ " other " ] . as < std : : string > ( ) = = " value " ) ;
YAML_ASSERT ( node [ " other " ] = = node [ " key " ] ) ;
return true ;
}
2011-09-14 10:48:36 +04:00
TEST Bool ( )
{
YAML : : Node node ;
node [ true ] = false ;
YAML_ASSERT ( node . IsMap ( ) ) ;
YAML_ASSERT ( node [ true ] . as < bool > ( ) = = false ) ;
return true ;
}
2011-11-14 02:05:42 +04:00
TEST AutoBoolConversion ( )
{
YAML : : Node node ;
node [ " foo " ] = " bar " ;
YAML_ASSERT ( static_cast < bool > ( node [ " foo " ] ) ) ;
YAML_ASSERT ( ! node [ " monkey " ] ) ;
YAML_ASSERT ( ! ! node [ " foo " ] ) ;
return true ;
}
2011-12-21 08:19:54 +04:00
TEST Reassign ( )
{
YAML : : Node node = YAML : : Load ( " foo " ) ;
node = YAML : : Node ( ) ;
return true ;
}
2012-01-12 11:03:31 +04:00
TEST FallbackValues ( )
{
YAML : : Node node = YAML : : Load ( " foo: bar \n x: 2 " ) ;
YAML_ASSERT ( node [ " foo " ] . as < std : : string > ( ) = = " bar " ) ;
YAML_ASSERT ( node [ " foo " ] . as < std : : string > ( " hello " ) = = " bar " ) ;
YAML_ASSERT ( node [ " baz " ] . as < std : : string > ( " hello " ) = = " hello " ) ;
YAML_ASSERT ( node [ " x " ] . as < int > ( ) = = 2 ) ;
YAML_ASSERT ( node [ " x " ] . as < int > ( 5 ) = = 2 ) ;
YAML_ASSERT ( node [ " y " ] . as < int > ( 5 ) = = 5 ) ;
return true ;
}
2012-01-13 09:49:05 +04:00
TEST NumericConversion ( )
{
2012-01-13 09:55:15 +04:00
YAML : : Node node = YAML : : Load ( " [1.5, 1, .nan, .inf, -.inf, 0x15, 015] " ) ;
2012-01-13 09:49:05 +04:00
YAML_ASSERT ( node [ 0 ] . as < float > ( ) = = 1.5f ) ;
YAML_ASSERT ( node [ 0 ] . as < double > ( ) = = 1.5 ) ;
YAML_ASSERT_THROWS ( node [ 0 ] . as < int > ( ) , std : : runtime_error ) ;
YAML_ASSERT ( node [ 1 ] . as < int > ( ) = = 1 ) ;
YAML_ASSERT ( node [ 1 ] . as < float > ( ) = = 1.0f ) ;
YAML_ASSERT ( node [ 2 ] . as < float > ( ) ! = node [ 2 ] . as < float > ( ) ) ;
YAML_ASSERT ( node [ 3 ] . as < float > ( ) = = std : : numeric_limits < float > : : infinity ( ) ) ;
YAML_ASSERT ( node [ 4 ] . as < float > ( ) = = - std : : numeric_limits < float > : : infinity ( ) ) ;
2012-01-13 09:55:15 +04:00
YAML_ASSERT ( node [ 5 ] . as < int > ( ) = = 21 ) ;
YAML_ASSERT ( node [ 6 ] . as < int > ( ) = = 13 ) ;
2012-01-13 09:49:05 +04:00
return true ;
}
2012-01-21 11:54:54 +04:00
TEST Binary ( )
{
YAML : : Node node = YAML : : Load ( " [!!binary \" SGVsbG8sIFdvcmxkIQ== \" , !!binary \" TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K \" ] " ) ;
YAML_ASSERT ( node [ 0 ] . as < YAML : : Binary > ( ) = = YAML : : Binary ( reinterpret_cast < const unsigned char * > ( " Hello, World! " ) , 13 ) ) ;
YAML_ASSERT ( node [ 1 ] . as < YAML : : Binary > ( ) = = YAML : : Binary ( reinterpret_cast < const unsigned char * > ( " Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure. \n " ) , 270 ) ) ;
return true ;
}
2011-09-11 08:22:30 +04:00
}
void RunNodeTest ( TEST ( * test ) ( ) , const std : : string & name , int & passed , int & total ) {
TEST ret ;
try {
ret = test ( ) ;
} catch ( . . . ) {
ret . ok = false ;
}
if ( ret . ok ) {
passed + + ;
} else {
std : : cout < < " Node test failed: " < < name < < " \n " ;
if ( ret . error ! = " " )
std : : cout < < ret . error < < " \n " ;
}
total + + ;
}
bool RunNodeTests ( )
{
int passed = 0 ;
int total = 0 ;
RunNodeTest ( & Node : : SimpleScalar , " simple scalar " , passed , total ) ;
2011-09-11 08:31:12 +04:00
RunNodeTest ( & Node : : IntScalar , " int scalar " , passed , total ) ;
2011-09-12 06:51:04 +04:00
RunNodeTest ( & Node : : SimpleAppendSequence , " simple append sequence " , passed , total ) ;
RunNodeTest ( & Node : : SimpleAssignSequence , " simple assign sequence " , passed , total ) ;
2011-09-12 01:02:31 +04:00
RunNodeTest ( & Node : : SimpleMap , " simple map " , passed , total ) ;
RunNodeTest ( & Node : : MapWithUndefinedValues , " map with undefined values " , passed , total ) ;
2011-09-12 02:36:08 +04:00
RunNodeTest ( & Node : : MapIteratorWithUndefinedValues , " map iterator with undefined values " , passed , total ) ;
2011-09-12 08:14:52 +04:00
RunNodeTest ( & Node : : SimpleSubkeys , " simple subkey " , passed , total ) ;
RunNodeTest ( & Node : : StdVector , " std::vector " , passed , total ) ;
2011-09-12 08:18:19 +04:00
RunNodeTest ( & Node : : StdList , " std::list " , passed , total ) ;
2011-09-12 09:29:39 +04:00
RunNodeTest ( & Node : : StdMap , " std::map " , passed , total ) ;
2011-09-12 21:42:23 +04:00
RunNodeTest ( & Node : : SimpleAlias , " simple alias " , passed , total ) ;
RunNodeTest ( & Node : : AliasAsKey , " alias as key " , passed , total ) ;
2011-09-12 21:48:51 +04:00
RunNodeTest ( & Node : : SelfReferenceSequence , " self reference sequence " , passed , total ) ;
RunNodeTest ( & Node : : ValueSelfReferenceMap , " value self reference map " , passed , total ) ;
RunNodeTest ( & Node : : KeySelfReferenceMap , " key self reference map " , passed , total ) ;
RunNodeTest ( & Node : : SelfReferenceMap , " self reference map " , passed , total ) ;
2011-09-13 07:09:16 +04:00
RunNodeTest ( & Node : : TempMapVariable , " temp map variable " , passed , total ) ;
RunNodeTest ( & Node : : TempMapVariableAlias , " temp map variable alias " , passed , total ) ;
2011-09-14 10:48:36 +04:00
RunNodeTest ( & Node : : Bool , " bool " , passed , total ) ;
2011-11-14 02:05:42 +04:00
RunNodeTest ( & Node : : AutoBoolConversion , " auto bool conversion " , passed , total ) ;
2011-12-21 08:19:54 +04:00
RunNodeTest ( & Node : : Reassign , " reassign " , passed , total ) ;
2012-01-12 11:03:31 +04:00
RunNodeTest ( & Node : : FallbackValues , " fallback values " , passed , total ) ;
2012-01-13 09:49:05 +04:00
RunNodeTest ( & Node : : NumericConversion , " numeric conversion " , passed , total ) ;
2012-01-21 11:54:54 +04:00
RunNodeTest ( & Node : : Binary , " binary " , passed , total ) ;
2011-09-11 08:22:30 +04:00
std : : cout < < " Node tests: " < < passed < < " / " < < total < < " passed \n " ;
return passed = = total ;
}
}