Node equality test now takes tag into account, as the

specification requires.
This commit is contained in:
Ferdinand Majerech 2011-08-20 22:38:32 +02:00
parent 7192503fe6
commit dde7d2f64f
2 changed files with 39 additions and 15 deletions

View file

@ -102,16 +102,31 @@ struct Node
///Pair of YAML nodes, used in mappings. ///Pair of YAML nodes, used in mappings.
struct Pair struct Pair
{ {
///Key node. public:
Node key; ///Key node.
///Value node. Node key;
Node value; ///Value node.
Node value;
///Test for equality with another Pair. public:
bool equals(ref Pair rhs) ///Equality test with another Pair.
{ bool equals(ref Pair rhs)
return key == rhs.key && value == rhs.value; {
} return equals_!true(rhs);
}
private:
/*
* Equality test with another Pair.
*
* useTag determines whether or not we consider node tags
* in the test.
*/
bool equals_(bool useTag)(ref Pair rhs)
{
return key.equals!(Node, useTag)(rhs.key) &&
value.equals!(Node, useTag)(rhs.value);
}
} }
package: package:
@ -165,7 +180,7 @@ struct Node
*/ */
bool opEquals(T)(ref T rhs) bool opEquals(T)(ref T rhs)
{ {
return valueEquals(rhs); return equals!(T, true)(rhs);
} }
/** /**
@ -566,11 +581,20 @@ struct Node
tag_ = tag; tag_ = tag;
} }
///Equality test without taking tag into account. /*
bool valueEquals(T)(ref T rhs) * Equality test with any value.
*
* useTag determines whether or not to consider tags in node-node comparisons.
*/
bool equals(T, bool useTag)(ref T rhs)
{ {
static if(is(T == Node)) static if(is(T == Node))
{ {
static if(useTag)
{
if(tag_ != rhs.tag_){return false;}
}
if(!isValid){return !rhs.isValid;} if(!isValid){return !rhs.isValid;}
if(!rhs.isValid || !hasEqualType(rhs)) if(!rhs.isValid || !hasEqualType(rhs))
{ {
@ -583,7 +607,7 @@ struct Node
if(seq1.length != seq2.length){return false;} if(seq1.length != seq2.length){return false;}
foreach(node; 0 .. seq1.length) foreach(node; 0 .. seq1.length)
{ {
if(seq1[node] != seq2[node]){return false;} if(!seq1[node].equals!(T, useTag)(seq2[node])){return false;}
} }
return true; return true;
} }
@ -594,7 +618,7 @@ struct Node
if(map1.length != map2.length){return false;} if(map1.length != map2.length){return false;}
foreach(pair; 0 .. map1.length) foreach(pair; 0 .. map1.length)
{ {
if(!map1[pair].equals(map2[pair])){return false;} if(!map1[pair].equals_!useTag(map2[pair])){return false;}
} }
return true; return true;
} }

View file

@ -384,7 +384,7 @@ void testConstructor(bool verbose, string dataFilename, string codeDummy)
size_t i = 0; size_t i = 0;
foreach(node; loader) foreach(node; loader)
{ {
if(!node.valueEquals(expected[base][i])) if(!node.equals!(Node, false)(expected[base][i]))
{ {
if(verbose) if(verbose)
{ {