function _getExamples() { $file = new SplFileObject(__DIR__ . '/examples.php'); $examples = []; $current = ''; foreach ($file as $line) { if (preg_match('~^(\\w+):$~', $line, $matches)) { $examples[$matches[1]] = ''; $current =& $examples[$matches[1]]; continue; } $current .= $line; } return _::map($examples, function ($ex) { return trim($ex); }); }
function _map($list, $iterator, $context = NULL) { return Underscore::map($list, $iterator, $context); }
/** * @tags collections */ public function testMap() { // it should provide the value, key and list to the iterator (in that order) $test = $this; _::map(['a' => 1, 'b' => 2, 'c' => 3], function ($v, $k, $l) use($test) { $test->integer($v); $test->string($k); $test->array($l); }); // it should work with array, objects and iterators and return an array in every case $this->typeTolerant([1, 2, 3], [2, 4, 6], function ($in, $out) { $this->array(_::map($in, function ($v) { return $v *= 2; }))->isEqualTo($out); }, [0, -1]); // it should be possible to specify a context for iterator function $this->variable(_::map([1, 2, 3], function ($v) { return $v * $this->mult; }, (object) ['mult' => 2]))->isEqualTo([2, 4, 6]); }