Skip to content

Commit

Permalink
Merge "test: move tests for parser options to ParserTest.php"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jan 18, 2024
2 parents 9205429 + d184d6f commit 2083ffd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 106 deletions.
106 changes: 0 additions & 106 deletions test/phpunit/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,110 +152,4 @@ public function testFixture( $cssFile, $lessFile ) {
$css = trim( $css );
$this->assertEquals( $expectedCSS, $css, "Using cache" );
}

public function testOptionRootpath() {
// When CSS refers to a URL that is only a hash fragment, it is a
// dynamic reference to something in the current DOM, thus it must
// not be remapped. https://phabricator.wikimedia.org/T331649
$lessCode = '
div {
--a10: url("./images/icon.svg");
--a11: url("./images/icon.svg#myid");
--a20: url(icon.svg);
--a21: url(icon.svg#myid);
--a30: url(#myid);
}
';

$parser = new Less_Parser();
$parser->parse( $lessCode, '/x/fake.css' );
$css = trim( $parser->getCss() );

$expected = <<<CSS
div {
--a10: url("/x/images/icon.svg");
--a11: url("/x/images/icon.svg#myid");
--a20: url(/x/icon.svg);
--a21: url(/x/icon.svg#myid);
--a30: url(#myid);
}
CSS;
$this->assertEquals( $expected, $css );
}

public function testOptionFunctions() {
// Add options
$lessCode = '
#test{
border-width: add(7, 6);
}
';

$options = [ 'functions' => [ 'add' => [ __CLASS__, 'fooBar2' ] ] ];
$parser = new Less_Parser( $options );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$expected = <<<CSS
#test {
border-width: 13;
}
CSS;
$this->assertSame( $expected, $css );

// test with closure
$parser = new Less_Parser( [
'functions' => [
'add' => static function ( $a, $b ) {
return new Less_Tree_Dimension( $a->value + $b->value );
}
]
] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css );

// test with directly with registerFunction()
$parser = new Less_Parser();
$parser->registerFunction( 'add', [ __CLASS__, 'fooBar2' ] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css );

// test with both passing options and using registerFunction()
$lessCode = '
#test{
border-width: add(2, 3);
border-color: _color("evil red");
width: increment(15);
}
';

$options = [ 'functions' => [ '_color' => [ __CLASS__, 'fooBar1' ], 'add' => [ __CLASS__, 'fooBar2' ] ] ];
$parser = new Less_Parser( $options );
$parser->registerFunction( 'increment', [ __CLASS__, 'fooBar3' ] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$expected = <<<CSS
#test {
border-width: 5;
border-color: #660000;
width: 16;
}
CSS;
$this->assertSame( $expected, $css );
}

public static function fooBar1( $str ) {
if ( $str->value === "evil red" ) {
return new Less_Tree_Color( "600" );
}
}

public static function fooBar2( $a, $b ) {
return new Less_Tree_Dimension( $a->value + $b->value );
}

public static function fooBar3( $a ) {
return new Less_Tree_Dimension( $a->value + 1 );
}
}
97 changes: 97 additions & 0 deletions test/phpunit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,101 @@ public function testOperationException() {
);
}
}

public function testOptionRootpath() {
// When CSS refers to a URL that is only a hash fragment, it is a
// dynamic reference to something in the current DOM, thus it must
// not be remapped. https://phabricator.wikimedia.org/T331649
$lessCode = '
div {
--a10: url("./images/icon.svg");
--a11: url("./images/icon.svg#myid");
--a20: url(icon.svg);
--a21: url(icon.svg#myid);
--a30: url(#myid);
}
';

$parser = new Less_Parser();
$parser->parse( $lessCode, '/x/fake.css' );
$css = trim( $parser->getCss() );

$expected = <<<CSS
div {
--a10: url("/x/images/icon.svg");
--a11: url("/x/images/icon.svg#myid");
--a20: url(/x/icon.svg);
--a21: url(/x/icon.svg#myid);
--a30: url(#myid);
}
CSS;
$this->assertEquals( $expected, $css );
}

public function testOptionFunctions() {
$lessCode = <<<CSS
#test {
border-width: add(7, 6);
}
CSS;
$expected = <<<CSS
#test {
border-width: 13;
}
CSS;

// test with static callback
$options = [ 'functions' => [ 'add' => [ __CLASS__, 'fnAdd' ] ] ];
$parser = new Less_Parser( $options );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css, 'static callback' );

// test with closure
$parser = new Less_Parser( [
'functions' => [
'add' => static function ( $a, $b ) {
return new Less_Tree_Dimension( $a->value + $b->value );
}
]
] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css, 'closure' );

// test directly with registerFunction()
$parser = new Less_Parser();
$parser->registerFunction( 'add', [ __CLASS__, 'fnAdd' ] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css, 'registerFunction' );

// test with both passing options and using registerFunction()
$lessCode = <<<CSS
#test{
border-width: add(2, 3);
width: increment(15);
}
CSS;
$expected = <<<CSS
#test {
border-width: 5;
width: 16;
}
CSS;
$options = [ 'functions' => [ 'add' => [ __CLASS__, 'fnAdd' ] ] ];
$parser = new Less_Parser( $options );
$parser->registerFunction( 'increment', [ __CLASS__, 'fnIncrement' ] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css, 'both' );
}

public static function fnAdd( $a, $b ) {
return new Less_Tree_Dimension( $a->value + $b->value );
}

public static function fnIncrement( $a ) {
return new Less_Tree_Dimension( $a->value + 1 );
}
}

0 comments on commit 2083ffd

Please sign in to comment.