function get_test_result($name, $verbose = false) { $path = __DIR__ . DIRECTORY_SEPARATOR . $name; $html = file_get_contents($path . '.html'); if ($verbose) { echo "* rendering test '{$name}'\n"; } try { $new = show_php($path . '.jade'); } catch (Exception $err) { if ($verbose) { echo "! FATAL: php exception: " . str_replace("\n", "\n\t", $err) . "\n"; } $new = null; } if ($new !== null) { $code = get_generated_html($new); // automatically compare $code and $html here $from = array("\n", "\r", "\t", " ", '"', "<!DOCTYPEhtml>"); $to = array('', '', '', '', "'", ''); $html = str_replace($from, $to, $html); $code = str_replace($from, $to, $code); $result = array($name, $html, $code); if (strcmp($html, $code)) { if ($verbose) { echo " -{$html}\n"; echo " +{$code}\n\n"; } return array(false, $result); } return array(true, $result); } }
function get_test_result($name, $verbose = false, $moreVerbose = false) { $mergeSpace = IGNORE_INDENT && strpos($name, 'indent.') === false; $path = TEMPLATES_DIRECTORY . DIRECTORY_SEPARATOR . $name; $expectedHtml = @file_get_contents($path . '.html'); if ($expectedHtml === false) { if ($verbose) { echo "! sample for test '{$name}' not found.\n"; } return array(false, array($name, null, "! sample for test '{$name}' not found.\n")); } if ($verbose) { echo "* rendering test '{$name}'\n"; } try { $new = get_php_code($path . '.jade'); } catch (Exception $err) { if ($verbose) { echo "! FATAL: php exception: " . str_replace("\n", "\n\t", $err) . "\n"; } return array(false, array($name, null, "! FATAL: php exception: " . str_replace("\n", "\n\t", $err) . "\n")); } if (is_null($new)) { return array(false, array($name, null, "! FATAL: " . $path . ".jade returns null\n")); } $actualHtml = get_generated_html($new); $from = array("'", "\r", "<!DOCTYPEhtml>"); $to = array('"', '', ''); if ($mergeSpace) { array_push($from, "\n", "\t", " "); array_push($to, '', '', ''); } $expectedHtml = preg_replace_callback('`class\\s*=\\s*(["\'])([^"\']+)\\1`', 'orderWords', $expectedHtml); $actualHtml = preg_replace_callback('`class\\s*=\\s*(["\'])([^"\']+)\\1`', 'orderWords', $actualHtml); if ($mergeSpace) { $expectedHtml = preg_replace('`(?<=[\'"])\\s(?=>)|(?<=[a-zA-Z0-9:])\\s(?=(>|\\s[a-zA-Z0-9:]))`', '', $expectedHtml); $actualHtml = preg_replace('`(?<=[\'"])\\s(?=>)|(?<=[a-zA-Z0-9:])\\s(?=(>|\\s[a-zA-Z0-9:]))`', '', $actualHtml); } $minifiedExpectedHtml = str_replace($from, $to, trim($expectedHtml)); $minifiedActualHtml = str_replace($from, $to, trim($actualHtml)); $result = array($name, $minifiedExpectedHtml, $minifiedActualHtml); if (strcmp($minifiedExpectedHtml, $minifiedActualHtml)) { if ($verbose) { include_once __DIR__ . '/diff.php'; $actualHtml = preg_replace('`(\\r\\n|\\r|\\n)([\\t ]*(\\r\\n|\\r|\\n))+`', "\n", $actualHtml); $expectedHtml = preg_replace('`(\\r\\n|\\r|\\n)([\\t ]*(\\r\\n|\\r|\\n))+`', "\n", $expectedHtml); echo Diff::toString(Diff::compare($expectedHtml, $actualHtml)) . "\n"; /* echo " Expected: $expectedHtml\n"; echo " Actual : $actualHtml\n\n"; */ } if ($moreVerbose) { echo " PHP : " . compile_php($name); } return array(false, $result); } return array(true, $result); }