Skip to content

Commit

Permalink
Parser: Runs all parser implementations against the same tests (#11320)
Browse files Browse the repository at this point in the history
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
dmsnell authored Oct 31, 2018
1 parent fae7777 commit b7d8fbf
Show file tree
Hide file tree
Showing 12 changed files with 1,785 additions and 1,703 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ coverage
node_modules
test/e2e/test-plugins
vendor
packages/block-serialization-spec-parser/index.js
packages/block-serialization-spec-parser/parser.js
3 changes: 3 additions & 0 deletions packages/block-serialization-default-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"dependencies": {
"@babel/runtime": "^7.0.0"
},
"devDependencies": {
"@wordpress/block-serialization-spec-parser": "file:../block-serialization-spec-parser"
},
"publishConfig": {
"access": "public"
}
Expand Down
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-->",
},
]
`;
75 changes: 8 additions & 67 deletions packages/block-serialization-default-parser/test/index.js
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' ) );
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' ) ) );
Loading

0 comments on commit b7d8fbf

Please sign in to comment.