WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (17 months ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/shortcode.php

    r47198 r48937  
    9898    function test_noatts() {
    9999        do_shortcode( '[test-shortcode-tag /]' );
    100         $this->assertEquals( '', $this->atts );
    101         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     100        $this->assertSame( '', $this->atts );
     101        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    102102    }
    103103
    104104    function test_one_att() {
    105105        do_shortcode( '[test-shortcode-tag foo="asdf" /]' );
    106         $this->assertEquals( array( 'foo' => 'asdf' ), $this->atts );
    107         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     106        $this->assertSame( array( 'foo' => 'asdf' ), $this->atts );
     107        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    108108    }
    109109
    110110    function test_not_a_tag() {
    111111        $out = do_shortcode( '[not-a-shortcode-tag]' );
    112         $this->assertEquals( '[not-a-shortcode-tag]', $out );
     112        $this->assertSame( '[not-a-shortcode-tag]', $out );
    113113    }
    114114
     
    118118    function test_tag_hyphen_not_tag() {
    119119        $out = do_shortcode( '[dumptag-notreal]' );
    120         $this->assertEquals( '[dumptag-notreal]', $out );
     120        $this->assertSame( '[dumptag-notreal]', $out );
    121121    }
    122122
    123123    function test_tag_underscore_not_tag() {
    124124        $out = do_shortcode( '[dumptag_notreal]' );
    125         $this->assertEquals( '[dumptag_notreal]', $out );
     125        $this->assertSame( '[dumptag_notreal]', $out );
    126126    }
    127127
    128128    function test_tag_not_tag() {
    129129        $out = do_shortcode( '[dumptagnotreal]' );
    130         $this->assertEquals( '[dumptagnotreal]', $out );
     130        $this->assertSame( '[dumptagnotreal]', $out );
    131131    }
    132132
     
    135135     */
    136136    function test_tag_hyphen() {
    137         $this->assertEquals( '_shortcode_hyphen', do_shortcode( '[hyphen]' ) );
    138         $this->assertEquals( '_shortcode_hyphen_foo', do_shortcode( '[hyphen-foo]' ) );
    139         $this->assertEquals( '_shortcode_hyphen_foo_bar', do_shortcode( '[hyphen-foo-bar]' ) );
    140         $this->assertEquals( '[hyphen-baz]', do_shortcode( '[hyphen-baz]' ) );
    141         $this->assertEquals( '[hyphen-foo-bar-baz]', do_shortcode( '[hyphen-foo-bar-baz]' ) );
     137        $this->assertSame( '_shortcode_hyphen', do_shortcode( '[hyphen]' ) );
     138        $this->assertSame( '_shortcode_hyphen_foo', do_shortcode( '[hyphen-foo]' ) );
     139        $this->assertSame( '_shortcode_hyphen_foo_bar', do_shortcode( '[hyphen-foo-bar]' ) );
     140        $this->assertSame( '[hyphen-baz]', do_shortcode( '[hyphen-baz]' ) );
     141        $this->assertSame( '[hyphen-foo-bar-baz]', do_shortcode( '[hyphen-foo-bar-baz]' ) );
    142142    }
    143143
     
    157157            'foo--bar'     => 'foo--bar',
    158158        );
    159         $this->assertEquals( $expected_attrs, $this->atts );
     159        $this->assertSame( $expected_attrs, $this->atts );
    160160    }
    161161
    162162    function test_two_atts() {
    163163        do_shortcode( '[test-shortcode-tag foo="asdf" bar="bing" /]' );
    164         $this->assertEquals(
     164        $this->assertSame(
    165165            array(
    166166                'foo' => 'asdf',
     
    169169            $this->atts
    170170        );
    171         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     171        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    172172    }
    173173
    174174    function test_noatts_enclosing() {
    175175        do_shortcode( '[test-shortcode-tag]content[/test-shortcode-tag]' );
    176         $this->assertEquals( '', $this->atts );
    177         $this->assertEquals( 'content', $this->content );
    178         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     176        $this->assertSame( '', $this->atts );
     177        $this->assertSame( 'content', $this->content );
     178        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    179179    }
    180180
    181181    function test_one_att_enclosing() {
    182182        do_shortcode( '[test-shortcode-tag foo="bar"]content[/test-shortcode-tag]' );
    183         $this->assertEquals( array( 'foo' => 'bar' ), $this->atts );
    184         $this->assertEquals( 'content', $this->content );
    185         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     183        $this->assertSame( array( 'foo' => 'bar' ), $this->atts );
     184        $this->assertSame( 'content', $this->content );
     185        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    186186    }
    187187
    188188    function test_two_atts_enclosing() {
    189189        do_shortcode( '[test-shortcode-tag foo="bar" baz="bing"]content[/test-shortcode-tag]' );
    190         $this->assertEquals(
     190        $this->assertSame(
    191191            array(
    192192                'foo' => 'bar',
     
    195195            $this->atts
    196196        );
    197         $this->assertEquals( 'content', $this->content );
    198         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     197        $this->assertSame( 'content', $this->content );
     198        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    199199    }
    200200
    201201    function test_unclosed() {
    202202        $out = do_shortcode( '[test-shortcode-tag]' );
    203         $this->assertEquals( '', $out );
    204         $this->assertEquals( '', $this->atts );
    205         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     203        $this->assertSame( '', $out );
     204        $this->assertSame( '', $this->atts );
     205        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    206206    }
    207207
    208208    function test_positional_atts_num() {
    209209        $out = do_shortcode( '[test-shortcode-tag 123]' );
    210         $this->assertEquals( '', $out );
    211         $this->assertEquals( array( 0 => '123' ), $this->atts );
    212         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     210        $this->assertSame( '', $out );
     211        $this->assertSame( array( 0 => '123' ), $this->atts );
     212        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    213213    }
    214214
    215215    function test_positional_atts_url() {
    216216        $out = do_shortcode( '[test-shortcode-tag http://www.youtube.com/watch?v=eBGIQ7ZuuiU]' );
    217         $this->assertEquals( '', $out );
    218         $this->assertEquals( array( 0 => 'http://www.youtube.com/watch?v=eBGIQ7ZuuiU' ), $this->atts );
    219         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     217        $this->assertSame( '', $out );
     218        $this->assertSame( array( 0 => 'http://www.youtube.com/watch?v=eBGIQ7ZuuiU' ), $this->atts );
     219        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    220220    }
    221221
    222222    function test_positional_atts_quotes() {
    223223        $out = do_shortcode( '[test-shortcode-tag "something in quotes" "something else"]' );
    224         $this->assertEquals( '', $out );
    225         $this->assertEquals(
     224        $this->assertSame( '', $out );
     225        $this->assertSame(
    226226            array(
    227227                0 => 'something in quotes',
     
    230230            $this->atts
    231231        );
    232         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     232        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    233233    }
    234234
    235235    function test_positional_atts_mixed() {
    236236        $out = do_shortcode( '[test-shortcode-tag 123 https://wordpress.org/ 0 "foo" bar]' );
    237         $this->assertEquals( '', $out );
    238         $this->assertEquals(
     237        $this->assertSame( '', $out );
     238        $this->assertSame(
    239239            array(
    240240                0 => '123',
     
    246246            $this->atts
    247247        );
    248         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     248        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    249249    }
    250250
    251251    function test_positional_and_named_atts() {
    252252        $out = do_shortcode( '[test-shortcode-tag 123 url=https://wordpress.org/ foo bar="baz"]' );
    253         $this->assertEquals( '', $out );
    254         $this->assertEquals(
     253        $this->assertSame( '', $out );
     254        $this->assertSame(
    255255            array(
    256256                0     => '123',
     
    261261            $this->atts
    262262        );
    263         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     263        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    264264    }
    265265
    266266    function test_footag_default() {
    267267        $out = do_shortcode( '[footag]' );
    268         $this->assertEquals( 'foo = ', $out );
     268        $this->assertSame( 'foo = ', $out );
    269269    }
    270270
     
    272272        $val = rand_str();
    273273        $out = do_shortcode( '[footag foo="' . $val . '"]' );
    274         $this->assertEquals( 'foo = ' . $val, $out );
     274        $this->assertSame( 'foo = ' . $val, $out );
    275275    }
    276276
     
    278278        $out      = do_shortcode( '[baztag][dumptag abc="foo" def=123 https://wordpress.org/][/baztag]' );
    279279        $expected = "content = abc = foo\ndef = 123\n0 = https://wordpress.org\n";
    280         $this->assertEquals( $expected, $out );
     280        $this->assertSame( $expected, $out );
    281281    }
    282282
     
    286286    function test_tag_escaped() {
    287287        $out = do_shortcode( '[[footag]] [[bartag foo="bar"]]' );
    288         $this->assertEquals( '[footag] [bartag foo="bar"]', $out );
     288        $this->assertSame( '[footag] [bartag foo="bar"]', $out );
    289289
    290290        $out = do_shortcode( '[[footag /]] [[bartag foo="bar" /]]' );
    291         $this->assertEquals( '[footag /] [bartag foo="bar" /]', $out );
     291        $this->assertSame( '[footag /] [bartag foo="bar" /]', $out );
    292292
    293293        $out = do_shortcode( '[[baztag foo="bar"]the content[/baztag]]' );
    294         $this->assertEquals( '[baztag foo="bar"]the content[/baztag]', $out );
     294        $this->assertSame( '[baztag foo="bar"]the content[/baztag]', $out );
    295295
    296296        // Double escaped.
    297297        $out = do_shortcode( '[[[footag]]] [[[bartag foo="bar"]]]' );
    298         $this->assertEquals( '[[footag]] [[bartag foo="bar"]]', $out );
     298        $this->assertSame( '[[footag]] [[bartag foo="bar"]]', $out );
    299299    }
    300300
     
    302302        // These have square brackets on either end but aren't actually escaped.
    303303        $out = do_shortcode( '[[footag] [bartag foo="bar"]]' );
    304         $this->assertEquals( '[foo =  foo = bar]', $out );
     304        $this->assertSame( '[foo =  foo = bar]', $out );
    305305
    306306        $out = do_shortcode( '[[footag /] [bartag foo="bar" /]]' );
    307         $this->assertEquals( '[foo =  foo = bar]', $out );
     307        $this->assertSame( '[foo =  foo = bar]', $out );
    308308
    309309        $out = do_shortcode( '[[baztag foo="bar"]the content[/baztag]' );
    310         $this->assertEquals( '[content = the content', $out );
     310        $this->assertSame( '[content = the content', $out );
    311311
    312312        $out = do_shortcode( '[[not-a-tag]]' );
    313         $this->assertEquals( '[[not-a-tag]]', $out );
     313        $this->assertSame( '[[not-a-tag]]', $out );
    314314
    315315        $out = do_shortcode( '[[[footag] [bartag foo="bar"]]]' );
    316         $this->assertEquals( '[[foo =  foo = bar]]', $out );
     316        $this->assertSame( '[[foo =  foo = bar]]', $out );
    317317    }
    318318
     
    351351EOF;
    352352        $out      = do_shortcode( $in );
    353         $this->assertEquals( strip_ws( $expected ), strip_ws( $out ) );
     353        $this->assertSame( strip_ws( $expected ), strip_ws( $out ) );
    354354    }
    355355
     
    360360        // NO-BREAK SPACE: U+00A0.
    361361        do_shortcode( "[test-shortcode-tag foo=\"bar\" \xC2\xA0baz=\"123\"]" );
    362         $this->assertEquals(
     362        $this->assertSame(
    363363            array(
    364364                'foo' => 'bar',
     
    367367            $this->atts
    368368        );
    369         $this->assertEquals( '', $this->content );
     369        $this->assertSame( '', $this->content );
    370370    }
    371371
     
    376376        // ZERO WIDTH SPACE: U+200B.
    377377        do_shortcode( "[test-shortcode-tag foo=\"bar\" \xE2\x80\x8Babc=\"def\"]" );
    378         $this->assertEquals(
     378        $this->assertSame(
    379379            array(
    380380                'foo' => 'bar',
     
    383383            $this->atts
    384384        );
    385         $this->assertEquals( '', $this->content );
     385        $this->assertSame( '', $this->content );
    386386    }
    387387
     
    392392        // A blank line is added at the end, so test with it already there.
    393393        $test_string = "[footag]\n";
    394         $this->assertEquals( $test_string, shortcode_unautop( wpautop( $test_string ) ) );
     394        $this->assertSame( $test_string, shortcode_unautop( wpautop( $test_string ) ) );
    395395    }
    396396
     
    419419     */
    420420    function test_strip_shortcodes( $expected, $content ) {
    421         $this->assertEquals( $expected, strip_shortcodes( $content ) );
     421        $this->assertSame( $expected, strip_shortcodes( $content ) );
    422422    }
    423423
     
    427427    function test_strip_shortcodes_filter() {
    428428        add_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
    429         $this->assertEquals( 'beforemiddle [footag]after', strip_shortcodes( 'before[gallery]middle [footag]after' ) );
     429        $this->assertSame( 'beforemiddle [footag]after', strip_shortcodes( 'before[gallery]middle [footag]after' ) );
    430430        remove_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
    431431    }
     
    463463
    464464        do_shortcode( '[bartag foo="foo1" /]' );
    465         $this->assertEquals(
     465        $this->assertSame(
    466466            array(
    467467                'foo' => 'foo1',
     
    470470            $this->filter_atts_out
    471471        );
    472         $this->assertEquals(
     472        $this->assertSame(
    473473            array(
    474474                'foo' => 'no foo',
     
    477477            $this->filter_atts_pairs
    478478        );
    479         $this->assertEquals( array( 'foo' => 'foo1' ), $this->filter_atts_atts );
     479        $this->assertSame( array( 'foo' => 'foo1' ), $this->filter_atts_atts );
    480480
    481481        remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 );
     
    486486
    487487        $out = do_shortcode( '[bartag foo="foo1" baz="baz1" /]' );
    488         $this->assertEquals( array( 'foo' => 'no foo' ), $this->filter_atts_out );
    489         $this->assertEquals( 'foo = no foo', $out );
     488        $this->assertSame( array( 'foo' => 'no foo' ), $this->filter_atts_out );
     489        $this->assertSame( 'foo = no foo', $out );
    490490
    491491        $out = do_shortcode( '[bartag foo="foo2" /]' );
    492         $this->assertEquals( 'foo = foo2', $out );
     492        $this->assertSame( 'foo = foo2', $out );
    493493
    494494        remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 );
     
    513513
    514514        foreach ( $input as $in ) {
    515             $this->assertEquals( $output, shortcode_unautop( $in ) );
     515            $this->assertSame( $output, shortcode_unautop( $in ) );
    516516        }
    517517    }
     
    523523     */
    524524    function test_escaping( $input, $output ) {
    525         return $this->assertEquals( $output, do_shortcode( $input ) );
     525        return $this->assertSame( $output, do_shortcode( $input ) );
    526526    }
    527527
     
    601601     */
    602602    function test_escaping2( $input, $output ) {
    603         return $this->assertEquals( $output, strip_shortcodes( $input ) );
     603        return $this->assertSame( $output, strip_shortcodes( $input ) );
    604604    }
    605605
     
    672672        add_shortcode( $input, '' );
    673673        $actual = shortcode_exists( $input );
    674         $test   = $this->assertEquals( $expected, $actual );
     674        $test   = $this->assertSame( $expected, $actual );
    675675        if ( $actual ) {
    676676            remove_shortcode( $input );
     
    766766        $out      = do_shortcode( '[dumptag=https://wordpress.org/]' );
    767767        $expected = "0 = =https://wordpress.org\n";
    768         $this->assertEquals( $expected, $out );
     768        $this->assertSame( $expected, $out );
    769769    }
    770770
     
    775775        $out      = apply_filters( 'the_content', '[img alt="Hello :-) World"]' );
    776776        $expected = "<img alt=\"Hello :-) World\" />\n";
    777         $this->assertEquals( $expected, $out );
     777        $this->assertSame( $expected, $out );
    778778    }
    779779
     
    925925    function test_empty_single_quote_attribute() {
    926926        $out = do_shortcode( '[test-shortcode-tag a="foo" b=\'bar\' c=baz foo \'bar\' "baz" ]test empty atts[/test-shortcode-tag]' );
    927         $this->assertEquals(
     927        $this->assertSame(
    928928            array(
    929929                'a' => 'foo',
     
    943943    function test_positional_atts_single_quotes() {
    944944        $out = do_shortcode( "[test-shortcode-tag 'something in quotes' 'something else']" );
    945         $this->assertEquals( '', $out );
    946         $this->assertEquals(
     945        $this->assertSame( '', $out );
     946        $this->assertSame(
    947947            array(
    948948                0 => 'something in quotes',
     
    951951            $this->atts
    952952        );
    953         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     953        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    954954    }
    955955
     
    959959    function test_positional_atts_mixed_quotes() {
    960960        $out = do_shortcode( "[test-shortcode-tag 'something in quotes' \"something else\" 123 foo bar='baz' example=\"test\" ]" );
    961         $this->assertEquals( '', $out );
    962         $this->assertEquals(
     961        $this->assertSame( '', $out );
     962        $this->assertSame(
    963963            array(
    964964                0         => 'something in quotes',
     
    971971            $this->atts
    972972        );
    973         $this->assertEquals( 'test-shortcode-tag', $this->tagname );
     973        $this->assertSame( 'test-shortcode-tag', $this->tagname );
    974974    }
    975975}
Note: See TracChangeset for help on using the changeset viewer.