Skip to content

Commit

Permalink
Add more info about how extract_directive_value behaves
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jan 24, 2024
1 parent 6cee3ba commit 6b62b97
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,12 @@ private function extract_prefix_and_suffix( string $directive_name ): array {
* Parses and extracts the namespace and reference path from the given
* directive attribute value.
*
* If the value doesn't contain an explicit namespace, it returns the default
* one. If the value contains a JSON object instead of a reference path, the
* function tries to parse it and return the resulting array.
* If the value doesn't contain an explicit namespace, it returns the
* default one. If the value contains a JSON object instead of a reference
* path, the function tries to parse it and return the resulting array. If
* the value contains strings that reprenset booleans ("true" and "false"),
* numbers ("1" and "1.2") or "null", the function also transform them to
* regular booleans, numbers and `null`.
*
* Example:
*
Expand Down
28 changes: 28 additions & 0 deletions phpunit/interactivity-api/class-wp-interactivity-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ public function test_extract_directive_value() {

$result = $extract_directive_value->invoke( $this->interactivity, 'null', 'myPlugin' );
$this->assertEquals( array( 'myPlugin', null ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, '100', 'myPlugin' );
$this->assertEquals( array( 'myPlugin', 100 ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, '1.2', 'myPlugin' );
$this->assertEquals( array( 'myPlugin', 1.2 ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, '1.2.3', 'myPlugin' );
$this->assertEquals( array( 'myPlugin', '1.2.3' ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::true', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', true ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::false', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', false ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::null', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', null ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::100', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', 100 ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::1.2', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', 1.2 ), $result );

$result = $extract_directive_value->invoke( $this->interactivity, 'otherPlugin::1.2.3', 'myPlugin' );
$this->assertEquals( array( 'otherPlugin', '1.2.3' ), $result );
}

/**
Expand All @@ -276,6 +303,7 @@ public function test_extract_directive_value_empty_values() {
$result = $extract_directive_value->invoke( $this->interactivity, '', 'myPlugin' );
$this->assertEquals( array( 'myPlugin', null ), $result );

// This is a boolean attribute.
$result = $extract_directive_value->invoke( $this->interactivity, true, 'myPlugin' );
$this->assertEquals( array( 'myPlugin', null ), $result );

Expand Down

0 comments on commit 6b62b97

Please sign in to comment.