From d1ea5d11c015bbad698af0dd300eb658fa532c5e Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 16 Apr 2013 21:40:35 -0400 Subject: Stubbed out the TraverserTest and moved the SerielizerTest to the Serielizer subdirectory. --- test/HTML5/Serializer/SerializerTest.php | 125 +++++++++++++++++++++++++++++++ test/HTML5/Serializer/TraverserTest.php | 10 +++ test/HTML5/SerializerTest.php | 125 ------------------------------- 3 files changed, 135 insertions(+), 125 deletions(-) create mode 100644 test/HTML5/Serializer/SerializerTest.php create mode 100644 test/HTML5/Serializer/TraverserTest.php delete mode 100644 test/HTML5/SerializerTest.php (limited to 'test') diff --git a/test/HTML5/Serializer/SerializerTest.php b/test/HTML5/Serializer/SerializerTest.php new file mode 100644 index 0000000..a06fd50 --- /dev/null +++ b/test/HTML5/Serializer/SerializerTest.php @@ -0,0 +1,125 @@ +saveHTML(); + + return $out; + } + + public function testSaveHTML() { + $html = 'test'; + + $dom = \HTML5::parse($html); + $this->assertTrue($dom instanceof \DOMDocument, "Canary"); + + $ser = new Serializer($dom, FALSE); + $out = $ser->saveHTML(); + + $this->assertTrue(count($out) >= count($html), 'Byte counts'); + $this->assertRegExp('//', $out, 'Has DOCTYPE.'); + $this->assertRegExp('/test<\/body>/', $out, 'Has body text.'); + + } + + public function testSave() { + $html = 'test'; + + $dom = \HTML5::parse($html); + $this->assertTrue($dom instanceof \DOMDocument, "Canary"); + + $ser = new Serializer($dom, FALSE); + $out = fopen("php://temp", "w"); + $ser->save($out); + + rewind($out); + $res = stream_get_contents($out); + $this->assertTrue(count($res) >= count($html)); + } + + public function testElements() { + // Should have content. + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + // Should be empty + $res = $this->cycle(''); + $this->assertRegExp('||', $res); + + // Should have no closing tag. + $res = $this->cycle('
'); + $this->assertRegExp('|
|', $res); + + } + + public function testAttributes() { + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + // XXX: Note that spec does NOT require attrs in the same order. + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + + $res = $this->cycle('
FOO
'); + $this->assertRegExp('|
FOO
|', $res); + } + + public function testPCData() { + $res = $this->cycle('This is a test.'); + $this->assertRegExp('|This is a test.|', $res); + + $res = $this->cycle('This + is + a + test.'); + + // Check that newlines are there, but don't count spaces. + $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); + + $res = $this->cycle('This is a test.'); + $this->assertRegExp('|This is a test.|', $res); + } + + public function testUnescaped() { + $res = $this->cycle(''); + $this->assertRegExp('|2 < 1|', $res); + + $res = $this->cycle(''); + $this->assertRegExp('|div>div>div|', $res); + } + + public function testEntities() { + $res = $this->cycle('Apples & bananas.'); + $this->assertRegExp('|Apples & bananas.|', $res); + } + + public function testComment() { + $res = $this->cycle('ab'); + $this->assertRegExp('||', $res); + } + + // FAILS because the parser converts CDATA to a comment. Issue #2. + public function testCDATA() { + $res = $this->cycle('a a test. ]]>b'); + $this->assertRegExp('| a test. ]]>|', $res); + } +} diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php new file mode 100644 index 0000000..c7e43d8 --- /dev/null +++ b/test/HTML5/Serializer/TraverserTest.php @@ -0,0 +1,10 @@ +saveHTML(); - - return $out; - } - - public function testSaveHTML() { - $html = 'test'; - - $dom = \HTML5::parse($html); - $this->assertTrue($dom instanceof \DOMDocument, "Canary"); - - $ser = new \HTML5\Serializer($dom, FALSE); - $out = $ser->saveHTML(); - - $this->assertTrue(count($out) >= count($html), 'Byte counts'); - $this->assertRegExp('//', $out, 'Has DOCTYPE.'); - $this->assertRegExp('/test<\/body>/', $out, 'Has body text.'); - - } - - public function testSave() { - $html = 'test'; - - $dom = \HTML5::parse($html); - $this->assertTrue($dom instanceof \DOMDocument, "Canary"); - - $ser = new \HTML5\Serializer($dom, FALSE); - $out = fopen("php://temp", "w"); - $ser->save($out); - - rewind($out); - $res = stream_get_contents($out); - $this->assertTrue(count($res) >= count($html)); - } - - public function testElements() { - // Should have content. - $res = $this->cycle('
FOO
'); - $this->assertRegExp('|
FOO
|', $res); - - // Should be empty - $res = $this->cycle(''); - $this->assertRegExp('||', $res); - - // Should have no closing tag. - $res = $this->cycle('
'); - $this->assertRegExp('|
|', $res); - - } - - public function testAttributes() { - $res = $this->cycle('
FOO
'); - $this->assertRegExp('|
FOO
|', $res); - - // XXX: Note that spec does NOT require attrs in the same order. - $res = $this->cycle('
FOO
'); - $this->assertRegExp('|
FOO
|', $res); - - $res = $this->cycle('
FOO
'); - $this->assertRegExp('|
FOO
|', $res); - } - - public function testPCData() { - $res = $this->cycle('This is a test.'); - $this->assertRegExp('|This is a test.|', $res); - - $res = $this->cycle('This - is - a - test.'); - - // Check that newlines are there, but don't count spaces. - $this->assertRegExp('|This\n\s*is\n\s*a\n\s*test.|', $res); - - $res = $this->cycle('This is a test.'); - $this->assertRegExp('|This is a test.|', $res); - } - - public function testUnescaped() { - $res = $this->cycle(''); - $this->assertRegExp('|2 < 1|', $res); - - $res = $this->cycle(''); - $this->assertRegExp('|div>div>div|', $res); - } - - public function testEntities() { - $res = $this->cycle('Apples & bananas.'); - $this->assertRegExp('|Apples & bananas.|', $res); - } - - public function testComment() { - $res = $this->cycle('ab'); - $this->assertRegExp('||', $res); - } - - // FAILS because the parser converts CDATA to a comment. Issue #2. - public function testCDATA() { - $res = $this->cycle('a a test. ]]>b'); - $this->assertRegExp('| a test. ]]>|', $res); - } -} -- cgit v1.2.3 From 09de792de309eaaffe6153bf12e7a9b2bfe97f82 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 16 Apr 2013 22:44:43 -0400 Subject: Converted the isBlock method to use a case insensitive mask and added tests for it. --- test/HTML5/Serializer/TraverserTest.php | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'test') diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php index c7e43d8..8638e22 100644 --- a/test/HTML5/Serializer/TraverserTest.php +++ b/test/HTML5/Serializer/TraverserTest.php @@ -7,4 +7,47 @@ require_once __DIR__ . '/../TestCase.php'; class TraverserTest extends \HTML5\Tests\TestCase { + /** + * Using reflection we make a protected method accessible for testing. + * + * @param string $name + * The name of the method on the Traverser class to test. + * + * @return \ReflectionMethod + * \ReflectionMethod for the specified method + */ + function getProtectedMethod($name) { + $class = new \ReflectionClass('\HTML5\Serializer\Traverser'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + public function testIsBlock() { + $blocks = array('html', 'body', 'head', 'p', 'div', 'h1', 'h2', 'h3', 'h4', + 'h5', 'h6', 'title', 'script', 'link', 'meta', 'section', 'article', + 'table', 'tbody', 'tr', 'th', 'td', + //'form', + ); + + // Mocking the required input because there is no checking. + $t = new Traverser('', ''); + $method = $this->getProtectedMethod('isBlock'); + + foreach ($blocks as $block) { + $this->assertTrue($method->invoke($t, $block), 'Block test failed on: ' . $block); + + // Also test the uppercase version. + $this->assertTrue($method->invoke($t, strtoupper($block)), 'Block test failed on: ' . $block); + } + + $nonblocks = array('span', 'a', 'img'); + foreach ($nonblocks as $tag) { + $this->assertFalse($method->invoke($t, $tag), 'Block test failed on: ' . $tag); + + // Also test the uppercase version. + $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Block test failed on: ' . $tag); + } + } + } \ No newline at end of file -- cgit v1.2.3 From fcbc66f9b8bd957b6878af0e0fed33457d47f072 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Wed, 17 Apr 2013 09:22:16 -0400 Subject: Rewrote isUnary testing and added tests. --- test/HTML5/Serializer/TraverserTest.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php index 8638e22..0717a75 100644 --- a/test/HTML5/Serializer/TraverserTest.php +++ b/test/HTML5/Serializer/TraverserTest.php @@ -38,7 +38,7 @@ class TraverserTest extends \HTML5\Tests\TestCase { $this->assertTrue($method->invoke($t, $block), 'Block test failed on: ' . $block); // Also test the uppercase version. - $this->assertTrue($method->invoke($t, strtoupper($block)), 'Block test failed on: ' . $block); + $this->assertTrue($method->invoke($t, strtoupper($block)), 'Block test failed on: ' . strtoupper($block)); } $nonblocks = array('span', 'a', 'img'); @@ -46,7 +46,32 @@ class TraverserTest extends \HTML5\Tests\TestCase { $this->assertFalse($method->invoke($t, $tag), 'Block test failed on: ' . $tag); // Also test the uppercase version. - $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Block test failed on: ' . $tag); + $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Block test failed on: ' . strtoupper($tag)); + } + } + + public function testIsUnary() { + $elements = array( 'area', 'base', 'basefont', 'bgsound', 'br', 'col', + 'command', 'embed', 'frame', 'hr', 'img', + ); + + // Mocking the required input because there is no checking. + $t = new Traverser('', ''); + $method = $this->getProtectedMethod('isUnary'); + + foreach ($elements as $element) { + $this->assertTrue($method->invoke($t, $element), 'Unary test failed on: ' . $element); + + // Also test the uppercase version. + $this->assertTrue($method->invoke($t, strtoupper($element)), 'Unary test failed on: ' . strtoupper($element)); + } + + $nonblocks = array('span', 'a', 'div'); + foreach ($nonblocks as $tag) { + $this->assertFalse($method->invoke($t, $tag), 'Unary test failed on: ' . $tag); + + // Also test the uppercase version. + $this->assertFalse($method->invoke($t, strtoupper($tag)), 'Unary test failed on: ' . strtoupper($tag)); } } -- cgit v1.2.3