-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parser: Runs all parser implementations against the same tests (#11320)
So far we haven't added too many tests to the parsers but the ones we _have_ added are in separate places and each implementation runs against a different set of tests. In this patch we're creating `shared-tests.js` in the spec parser that defines a suite of conformance tests for the parser and then we're using that base test-builder to dynamically create test suites for each implementation such that they all run the same suite. It's probably easier to understand by reading the code than this summary. Of note: by calling to `php` directly we're able to run the PHP parsers against the same tests as we are the JavaScript implementations. We should be able to do this for any implementation as long as the required binaries are available in the CI environment (Rust, for example).
- Loading branch information
Showing
12 changed files
with
1,785 additions
and
1,703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/block-serialization-default-parser/test/__snapshots__/index.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`block-serialization-default-parser-js basic parsing parse() works properly 1`] = ` | ||
Array [ | ||
Object { | ||
"attrs": Object {}, | ||
"blockName": "core/more", | ||
"innerBlocks": Array [], | ||
"innerHTML": "<!--more-->", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`block-serialization-default-parser-php basic parsing parse() works properly 1`] = ` | ||
Array [ | ||
Object { | ||
"attrs": Object {}, | ||
"blockName": "core/more", | ||
"innerBlocks": Array [], | ||
"innerHTML": "<!--more-->", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,14 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import path from 'path'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { jsTester, phpTester } from '@wordpress/block-serialization-spec-parser'; | ||
import { parse } from '../'; | ||
|
||
describe( 'block-serialization-spec-parser', () => { | ||
test( 'parse() accepts inputs with multiple Reusable blocks', () => { | ||
const result = parse( | ||
'<!-- wp:block {"ref":313} /--><!-- wp:block {"ref":482} /-->' | ||
); | ||
|
||
expect( result ).toEqual( [ | ||
{ | ||
blockName: 'core/block', | ||
attrs: { ref: 313 }, | ||
innerBlocks: [], | ||
innerHTML: '', | ||
}, | ||
{ | ||
blockName: 'core/block', | ||
attrs: { ref: 482 }, | ||
innerBlocks: [], | ||
innerHTML: '', | ||
}, | ||
] ); | ||
} ); | ||
|
||
test( 'treats void blocks and empty blocks identically', () => { | ||
expect( parse( | ||
'<!-- wp:block /-->' | ||
) ).toEqual( parse( | ||
'<!-- wp:block --><!-- /wp:block -->' | ||
) ); | ||
|
||
expect( parse( | ||
'<!-- wp:my/bus { "is": "fast" } /-->' | ||
) ).toEqual( parse( | ||
'<!-- wp:my/bus { "is": "fast" } --><!-- /wp:my/bus -->' | ||
) ); | ||
} ); | ||
|
||
test( 'should grab HTML soup before block openers', () => { | ||
[ | ||
'<p>Break me</p><!-- wp:block /-->', | ||
'<p>Break me</p><!-- wp:block --><!-- /wp:block -->', | ||
].forEach( ( input ) => expect( parse( input ) ).toEqual( [ | ||
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ), | ||
expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ), | ||
] ) ); | ||
} ); | ||
|
||
test( 'should grab HTML soup before inner block openers', () => { | ||
[ | ||
'<!-- wp:outer --><p>Break me</p><!-- wp:block /--><!-- /wp:outer -->', | ||
'<!-- wp:outer --><p>Break me</p><!-- wp:block --><!-- /wp:block --><!-- /wp:outer -->', | ||
].forEach( ( input ) => expect( parse( input ) ).toEqual( [ | ||
expect.objectContaining( { | ||
innerBlocks: [ expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ) ], | ||
innerHTML: '<p>Break me</p>', | ||
} ), | ||
] ) ); | ||
} ); | ||
describe( 'block-serialization-default-parser-js', jsTester( parse ) ); | ||
|
||
test( 'should grab HTML soup after blocks', () => { | ||
[ | ||
'<!-- wp:block /--><p>Break me</p>', | ||
'<!-- wp:block --><!-- /wp:block --><p>Break me</p>', | ||
].forEach( ( input ) => expect( parse( input ) ).toEqual( [ | ||
expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ), | ||
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ), | ||
] ) ); | ||
} ); | ||
} ); | ||
phpTester( 'block-serialization-default-parser-php', path.join( __dirname, 'test-parser.php' ) ); |
7 changes: 7 additions & 0 deletions
7
packages/block-serialization-default-parser/test/test-parser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../parser.php'; | ||
|
||
$parser = new WP_Block_Parser(); | ||
|
||
echo json_encode( $parser->parse( file_get_contents( 'php://stdin' ) ) ); |
Oops, something went wrong.