Browse Source

Add test for evaluation order of nested list() keys

pull/1730/head
Andrea Faulds 10 years ago
parent
commit
70942e4c3c
  1. 60
      Zend/tests/list_keyed_evaluation_order.inc
  2. 68
      Zend/tests/list_keyed_evaluation_order.phpt
  3. 77
      Zend/tests/list_keyed_evaluation_order_nested.phpt

60
Zend/tests/list_keyed_evaluation_order.inc

@ -0,0 +1,60 @@
<?php declare(strict_types=1);
// Observer objects for the Zend/tests/list_keyed_evaluation_order.* tests
class Stringable
{
private $name;
public function __construct(string $name) {
$this->name = $name;
}
public function __toString(): string {
echo "$this->name evaluated.", PHP_EOL;
return $this->name;
}
}
class Indexable implements ArrayAccess
{
private $array;
public function __construct(array $array) {
$this->array = $array;
}
public function offsetExists($offset): bool {
echo "Existence of offset $offset checked for.", PHP_EOL;
return isset($this->array[$offset]);
}
public function offsetUnset($offset): void {
unset($this->array[$offset]);
echo "Offset $offset removed.", PHP_EOL;
}
public function offsetGet($offset) {
echo "Offset $offset retrieved.", PHP_EOL;
return $this->array[$offset];
}
public function offsetSet($offset, $value): void {
$this->array[$offset] = $value;
echo "Offset $offset set to $value.", PHP_EOL;
}
}
class IndexableRetrievable
{
private $label;
private $indexable;
public function __construct(string $label, Indexable $indexable) {
$this->label = $label;
$this->indexable = $indexable;
}
public function getIndexable(): Indexable {
echo "Indexable $this->label retrieved.", PHP_EOL;
return $this->indexable;
}
}

68
Zend/tests/list_keyed_evaluation_order.phpt

@ -3,6 +3,15 @@ list() with keys, evaluation order
--FILE--
<?php
require_once "list_keyed_evaluation_order.inc";
$a = new Stringable("A");
$c = new Stringable("C");
$e = new IndexableRetrievable("E", new Indexable(["A" => "value for offset A", "C" => "value for offset C"]));
$store = new Indexable([]);
// list($a => $b, $c => $d) = $e;
// Should be evaluated in the order:
// 1. Evaluate $e
@ -13,65 +22,6 @@ list() with keys, evaluation order
// 6. Evaluate $e[$c]
// 7. Assign $c from $e[$a]
// In order to observe this evaluation order, let's use some observer objects!
class Stringable
{
private $name;
public function __construct(string $name) {
$this->name = $name;
}
public function __toString(): string {
echo "$this->name evaluated.", PHP_EOL;
return $this->name;
}
}
class Indexable implements ArrayAccess
{
public function offsetExists($offset): bool {
echo "Existence of offset $offset checked for.", PHP_EOL;
return true;
}
public function offsetUnset($offset): void {
echo "Offset $offset removed.", PHP_EOL;
}
public function offsetGet($offset) {
echo "Offset $offset retrieved.", PHP_EOL;
return "value for offset $offset";
}
public function offsetSet($offset, $value): void {
echo "Offset $offset set to $value.", PHP_EOL;
}
}
class IndexableRetrievable
{
private $label;
private $indexable;
public function __construct(string $label, Indexable $indexable) {
$this->label = $label;
$this->indexable = $indexable;
}
public function getIndexable(): Indexable {
echo "Indexable $this->label retrieved.", PHP_EOL;
return $this->indexable;
}
}
$a = new Stringable("A");
$c = new Stringable("C");
$e = new IndexableRetrievable("E", new Indexable(["A" => "value for A", "C" => "value for C"]));
$store = new Indexable;
list((string)$a => $store["B"], (string)$c => $store["D"]) = $e->getIndexable();
?>

77
Zend/tests/list_keyed_evaluation_order_nested.phpt

@ -0,0 +1,77 @@
--TEST--
list() with keys, evaluation order: nested
--FILE--
<?php
require_once "list_keyed_evaluation_order.inc";
$a = new Stringable("A");
$c = new Stringable("C");
$f = new Stringable("F");
$g = new Stringable("G");
$i = new Stringable("I");
$k = new IndexableRetrievable("K", new Indexable([
"A" => "offset value for A",
"C" => new Indexable([
0 => "offset value for 0",
1 => "offset value for 1"
]),
"F" => new Indexable([
"G" => "offset value for G",
"I" => "offset value for I"
])
]));
$store = new Indexable([]);
// list($a => $b, $c => list($d, $e), $f => list($g => $h, $i => $j)) = $k;
// Should be evaluated in the order:
// 1. Evaluate $k
// 2. Evaluate $a
// 3. Evaluate $k[$a]
// 4. Assign $b from $k[$a]
// 5. Evaluate $c
// 6. Evaluate $k[$c]
// 7. Evaluate $k[$c][0]
// 8. Assign $d from $k[$c][0]
// 9. Evaluate $k[$c][1]
// 10. Assign $e from $k[$c][1]
// 11. Evaluate $f
// 12. Evaluate $k[$f]
// 13. Evaluate $g
// 14. Evaluate $k[$f][$g]
// 15. Assign $h from $k[$f][$g]
// 16. Evaluate $i
// 17. Evaluate $k[$f][$i]
// 18. Assign $j from $k[$f][$i]
list(
(string)$a => $store["B"],
(string)$c => list($store["D"], $store["E"]),
(string)$f => list(
(string)$g => $store["H"],
(string)$i => $store["J"]
)
) = $k->getIndexable();
?>
--EXPECT--
Indexable K retrieved.
A evaluated.
Offset A retrieved.
Offset B set to offset value for A.
C evaluated.
Offset C retrieved.
Offset 0 retrieved.
Offset D set to offset value for 0.
Offset 1 retrieved.
Offset E set to offset value for 1.
F evaluated.
Offset F retrieved.
G evaluated.
Offset G retrieved.
Offset H set to offset value for G.
I evaluated.
Offset I retrieved.
Offset J set to offset value for I.
Loading…
Cancel
Save