Make WordPress Core

Changeset 63591


Ignore:
Timestamp:
09/10/2026 08:53:44 PM (2 months ago)
Author:
lancewillett
Message:

Tests: Reduce repeated token map test setup.

Check all HTML5 named character references within each token map test instead of running a separate test for every token. Preserve the per-token assertions and include token names in failure messages.

Reduce test invocations from 4,474 to 14, avoiding repeated test runner setup and teardown.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13478

Props sainathpoojary.
See #66074.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/wp-token-map/wpTokenMap.php

    r58281 r63591  
    307307         *
    308308         * @ticket 60698
    309          *
    310          * @dataProvider data_html5_test_dataset
    311          *
    312          * @param string $token       Token to find.
    313          * @param string $replacement Replacement string for token.
    314          */
    315         public function test_reads_token_at_given_offset( $token, $replacement ) {
    316                 $document = "& another {$token} & then some";
    317                 $map      = self::get_html5_token_map();
    318 
    319                 $skip_bytes = 0;
    320                 $this->assertNull(
    321                         $map->read_token( $document, 0, $skip_bytes ),
    322                         "Shouldn't have found token at start of document."
    323                 );
    324 
    325                 $response = $map->read_token( $document, 10, $skip_bytes );
    326 
    327                 $this->assertSame(
    328                         strlen( $token ),
    329                         $skip_bytes,
    330                         "Found the wrong length for token '{$token}'."
    331                 );
    332 
    333                 $this->assertSame(
    334                         $response,
    335                         $replacement,
    336                         'Found the wrong replacement value for the token.'
    337                 );
     309         */
     310        public function test_reads_token_at_given_offset() {
     311                $map = self::get_html5_token_map();
     312
     313                foreach ( $this->get_html5_test_dataset() as $token => $replacement ) {
     314                        $document = "& another {$token} & then some";
     315
     316                        $skip_bytes = 0;
     317                        $this->assertNull(
     318                                $map->read_token( $document, 0, $skip_bytes ),
     319                                "Shouldn't have found token '{$token}' at start of document."
     320                        );
     321
     322                        $response = $map->read_token( $document, 10, $skip_bytes );
     323
     324                        $this->assertSame(
     325                                strlen( $token ),
     326                                $skip_bytes,
     327                                "Found the wrong length for token '{$token}'."
     328                        );
     329
     330                        $this->assertSame(
     331                                $response,
     332                                $replacement,
     333                                "Found the wrong replacement value for token '{$token}'."
     334                        );
     335                }
    338336        }
    339337
    … …  
    342340         *
    343341         * @ticket 60698
    344          *
    345          * @dataProvider data_html5_test_dataset
    346          *
    347          * @param string $token       Token to find.
    348          * @param string $replacement Not used in this test.
    349          */
    350         public function test_detects_all_tokens( $token, $replacement ) {
     342         */
     343        public function test_detects_all_tokens() {
    351344                $map = self::get_html5_token_map();
    352345
    353                 $this->assertTrue(
    354                         $map->contains( $token ),
    355                         "Should have found '{$token}' inside the Token Map, but didn't."
    356                 );
    357 
    358                 $double_escaped_token = str_replace( '&', '&', $token );
    359                 $this->assertFalse(
    360                         $map->contains( $double_escaped_token ),
    361                         "Should not have found '{$double_escaped_token}' in Token Map, but did."
    362                 );
    363         }
    364 
    365         /**
    366          * Data provider.
    367          *
    368          * @return array[].
    369          */
    370         public function data_html5_test_dataset() {
     346                foreach ( $this->get_html5_test_dataset() as $token => $replacement ) {
     347                        $this->assertTrue(
     348                                $map->contains( $token ),
     349                                "Should have found '{$token}' inside the Token Map, but didn't."
     350                        );
     351
     352                        $double_escaped_token = str_replace( '&', '&', $token );
     353                        $this->assertFalse(
     354                                $map->contains( $double_escaped_token ),
     355                                "Should not have found '{$double_escaped_token}' in Token Map, but did."
     356                        );
     357                }
     358        }
     359
     360        /**
     361         * Gets all HTML5 named character references to check within each test.
     362         *
     363         * @return string[] Replacement strings keyed by token.
     364         */
     365        private function get_html5_test_dataset() {
    371366                $html5 = self::get_test_input_array( 'HTML5' );
    372367
    … …  
    377372                );
    378373
    379                 foreach ( $html5 as $token => $replacement ) {
    380                         yield $token => array( $token, $replacement );
    381                 }
     374                return $html5;
    382375        }
    383376
Note: See TracChangeset for help on using the changeset viewer.