Merge "Make TexArray iterable"

This commit is contained in:
jenkins-bot 2024-05-27 13:18:29 +00:00 committed by Gerrit Code Review
commit 024cdbb00f
2 changed files with 20 additions and 3 deletions

View file

@ -4,6 +4,7 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\WikiTexVC\Nodes;
use Generator;
use InvalidArgumentException;
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\BaseMappings;
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLParsingUtil;
@ -14,7 +15,7 @@ use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmstyle;
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmsup;
use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
class TexArray extends TexNode {
class TexArray extends TexNode implements \IteratorAggregate {
protected bool $curly = false;
public static function newCurly( ...$args ) {
@ -427,7 +428,7 @@ class TexArray extends TexNode {
}
/**
* @return TexNode|string|null first value
* @return TexNode|null first value
*/
public function first() {
if ( isset( $this->args[0] ) ) {
@ -438,7 +439,7 @@ class TexArray extends TexNode {
}
/**
* @return TexNode|string|null second value
* @return TexNode|null second value
*/
public function second() {
if ( isset( $this->args[1] ) ) {
@ -482,4 +483,10 @@ class TexArray extends TexNode {
return $this;
}
/**
* @return Generator<TexNode>
*/
public function getIterator(): Generator {
yield from $this->args;
}
}

View file

@ -79,4 +79,14 @@ class TexArrayTest extends MediaWikiUnitTestCase {
$n->unshift( 'test1', 'test2' );
$this->assertEquals( 3, $n->getLength(), 'Should unshift elements' );
}
public function testGenerator() {
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) );
foreach ( $ta as $item ) {
$this->assertInstanceOf(
'MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal',
$item,
'Should iterate over the elements' );
}
}
}