diff --git a/includes/parser/AFPData.php b/includes/parser/AFPData.php index ca4f452d7..07476abeb 100644 --- a/includes/parser/AFPData.php +++ b/includes/parser/AFPData.php @@ -188,13 +188,34 @@ class AFPData { } /** + * @ToDo Should we also build a proper system to compare arrays with different types? * @param AFPData $d1 * @param AFPData $d2 + * @param bool $strict whether to also check types * @return bool */ - public static function equals( $d1, $d2 ) { - return $d1->type != self::DLIST && $d2->type != self::DLIST && - $d1->toString() === $d2->toString(); + public static function equals( $d1, $d2, $strict = false ) { + if ( $d1->type != self::DLIST && $d2->type != self::DLIST ) { + $typecheck = $d1->type == $d2->type || !$strict; + return $typecheck && $d1->toString() === $d2->toString(); + } elseif ( $d1->type == self::DLIST && $d2->type == self::DLIST ) { + $data1 = $d1->data; + $data2 = $d2->data; + if ( count( $data1 ) !== count( $data2 ) ) { + return false; + } + $length = count( $data1 ); + for ( $i = 0; $i < $length; $i++ ) { + $result = self::equals( $data1[$i], $data2[$i], $strict ); + if ( $result === false ) { + return false; + } + } + return true; + } else { + // Trying to compare an array to something else + return false; + } } /** @@ -304,10 +325,10 @@ class AFPData { return new AFPData( self::DBOOL, !self::equals( $a, $b ) ); } if ( $op == '===' ) { - return new AFPData( self::DBOOL, $a->type == $b->type && self::equals( $a, $b ) ); + return new AFPData( self::DBOOL, self::equals( $a, $b, true ) ); } if ( $op == '!==' ) { - return new AFPData( self::DBOOL, $a->type != $b->type || !self::equals( $a, $b ) ); + return new AFPData( self::DBOOL, !self::equals( $a, $b, true ) ); } $a = $a->toString(); $b = $b->toString(); diff --git a/tests/parserTests/list-inequality.r b/tests/parserTests/list-comparisons.r similarity index 100% rename from tests/parserTests/list-inequality.r rename to tests/parserTests/list-comparisons.r diff --git a/tests/parserTests/list-comparisons.t b/tests/parserTests/list-comparisons.t new file mode 100644 index 000000000..db4315737 --- /dev/null +++ b/tests/parserTests/list-comparisons.t @@ -0,0 +1,11 @@ +a := [1, 2, 3]; +b := [1, 2, 3]; +c := [2, 3, 4]; +d := [1, 2, 3, 4]; +e := ['1', '2', '3']; +f := [[['1']]]; +g := [[[1]]]; +h := [[1, 2], 3]; +i := [['1', 2], '3']; + +a == b & a === b & a != c & b != d & a == e & a !== e & f == g & f !== g & h == i & h !== i & e != i diff --git a/tests/parserTests/list-inequality.t b/tests/parserTests/list-inequality.t deleted file mode 100644 index 6ffed4f04..000000000 --- a/tests/parserTests/list-inequality.t +++ /dev/null @@ -1,3 +0,0 @@ -a := [1, 2, 3]; - -a != a