[CSSTidy] SF.net SVN: csstidy: [93] trunk
Status: Inactive
Brought to you by:
floele
From: <edw...@us...> - 2008-01-05 20:44:49
|
Revision: 93 http://csstidy.svn.sourceforge.net/csstidy/?rev=93&view=rev Author: edwardzyang Date: 2008-01-05 12:44:47 -0800 (Sat, 05 Jan 2008) Log Message: ----------- Make unit tests use SimpleTest, integrate in the framework. We have a custom reporter. Modified Paths: -------------- trunk/testing/unit-tests.inc trunk/testing/unit-tests.php Added Paths: ----------- trunk/testing/unit-tests/_files.php trunk/testing/unit-tests/class.Text_Diff_Renderer_parallel.php trunk/testing/unit-tests/class.csstidy_csst.php trunk/testing/unit-tests/class.csstidy_reporter.php trunk/testing/unit-tests/csst/README trunk/testing/unit-tests/test.csst.php Property Changed: ---------------- trunk/ Property changes on: trunk ___________________________________________________________________ Name: svn:ignore - temp *.bak + temp *.bak test-settings.php Added: trunk/testing/unit-tests/_files.php =================================================================== --- trunk/testing/unit-tests/_files.php (rev 0) +++ trunk/testing/unit-tests/_files.php 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,4 @@ +<?php + +$test_files = array(); +$test_files[] = 'test.csst.php'; Property changes on: trunk/testing/unit-tests/_files.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/testing/unit-tests/class.Text_Diff_Renderer_parallel.php =================================================================== --- trunk/testing/unit-tests/class.Text_Diff_Renderer_parallel.php (rev 0) +++ trunk/testing/unit-tests/class.Text_Diff_Renderer_parallel.php 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,30 @@ +<?php + +class Text_Diff_Renderer_parallel extends Text_Diff_Renderer +{ + var $original = 'Original'; + var $final = 'Final'; + var $_leading_context_lines = 10000; + var $_trailing_context_lines = 10000; + function _blockHeader() {} + function _startDiff() { + return '<table class="diff"><thead><tr><th>'. $this->original .'</th><th>'. $this->final .'</th></tr></thead><tbody>'; + } + function _endDiff() { + return '</tbody></table>'; + } + function _context($lines) { + return '<tr><td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td> + <td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>'; + } + function _added($lines) { + return '<tr><td> </td><td class="added"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>'; + } + function _deleted($lines) { + return '<tr><td class="deleted"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td><td> </td></tr>'; + } + function _changed($orig, $final) { + return '<tr class="changed"><td><pre>'. htmlspecialchars(implode("\n", $orig)) .'</pre></td> + <td><pre>'. htmlspecialchars(implode("\n", $final)) .'</pre></td></tr>'; + } +} Property changes on: trunk/testing/unit-tests/class.Text_Diff_Renderer_parallel.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/testing/unit-tests/class.csstidy_csst.php =================================================================== --- trunk/testing/unit-tests/class.csstidy_csst.php (rev 0) +++ trunk/testing/unit-tests/class.csstidy_csst.php 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,84 @@ +<?php + +require_once 'class.Text_Diff_Renderer_parallel.php'; + +class csstidy_csst extends SimpleExpectation +{ + /** Filename of test */ + var $filename; + + /** Test name */ + var $test; + + /** CSS for test to parse */ + var $css = ''; + + /** Expected var_export() output of $css->css[41] (no at block) */ + var $expect = ''; + + /** Expected var_export() output of $css->css (with at block) */ + var $fullexpect = ''; + + /** Actual result */ + var $actual; + + /** + * Loads this class from a file + */ + function load($filename) { + $this->filename = $filename; + $fh = fopen($filename, 'r'); + $state = null; + while (($line = fgets($fh)) !== false) { + $line = rtrim($line, "\n\r"); // normalize newlines + if (substr($line, 0, 2) == '--') { + // detected section + $state = $line; + continue; + } + if ($state === null) continue; + switch ($state) { + case '--TEST--': + $this->test = trim($line); + continue; + case '--CSS--': + $this->css .= $line . "\n"; + continue; + case '--EXPECT--': + $this->expect .= $line . "\n"; + continue; + case '--FULLEXPECT--': + $this->fullexpect .= $line . "\n"; + continue; + } + } + $this->expect = trim($this->expect, "\n"); // trim trailing/leading newlines + fclose($fh); + } + + function test($filename) { + $this->load($filename); + $css = new csstidy(); + $css->parse($this->css); + $this->actual = var_export($css->css[41], true); + return $this->expect === $this->actual; + } + + function testMessage() { + $message = $this->test . ' test at '. htmlspecialchars($this->filename); + return $message; + } + + /** + * Renders the test + */ + function render() { + $message = '<pre>'. htmlspecialchars($this->css) .'</pre>'; + $diff = new Text_Diff('auto', array(explode("\n", $this->expect), explode("\n", $this->actual))); + $renderer = new Text_Diff_Renderer_parallel(); + $renderer->original = 'Expected'; + $renderer->final = 'Actual'; + $message .= $renderer->render($diff); + return $message; + } +} Property changes on: trunk/testing/unit-tests/class.csstidy_csst.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/testing/unit-tests/class.csstidy_reporter.php =================================================================== --- trunk/testing/unit-tests/class.csstidy_reporter.php (rev 0) +++ trunk/testing/unit-tests/class.csstidy_reporter.php 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,18 @@ +<?php + +class csstidy_reporter extends HTMLReporter +{ + + function _getCss() { + $css = parent::_getCss(); + $css .= ' +.diff {margin-bottom: 1em;} +.diff th {width:50%;} +.diff pre {margin:0; padding:0; background:none;} +.diff .changed, .diff .deleted, .diff .added {background: #FF5;} + '; + return $css; + } + +} + Property changes on: trunk/testing/unit-tests/class.csstidy_reporter.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/testing/unit-tests/csst/README =================================================================== --- trunk/testing/unit-tests/csst/README (rev 0) +++ trunk/testing/unit-tests/csst/README 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,22 @@ +CSST + +These test are for CSSTidy's parsing algorithms. They take this form: + +--TEST-- +Test name +--CSS-- +CSS to parse +--EXPECT-- +var_export() representation of csstidy->css[41] + +Note carefully that EXPECT is for csstidy->css[41], not csstidy->css. This +is because, by default, all declarations are placed inside the +DEFAULT_AT section. For tests that need to make use of at selectors, use + +--FULLEXPECT-- +var_export() representation of csstidy->css + +...instead. + +See also: class.csstidy_csst.php (the implementation of these tests) and +test.csst.php (the caller stub for SimpleTest) Property changes on: trunk/testing/unit-tests/csst/README ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/testing/unit-tests/test.csst.php =================================================================== --- trunk/testing/unit-tests/test.csst.php (rev 0) +++ trunk/testing/unit-tests/test.csst.php 2008-01-05 20:44:47 UTC (rev 93) @@ -0,0 +1,19 @@ +<?php + +require_once 'class.csstidy_csst.php'; + +/** + * Performs the csst tests in csst/ folder + */ +class csstidy_test_csst extends UnitTestCase +{ + function testAll() { + foreach (globr(dirname(__FILE__) . '/csst', '*.csst') as $filename) { + $expectation = new csstidy_csst(); + $result = $this->assert($expectation, $filename, '%s'); + // this is necessary because SimpleTest doesn't support + // HTML messages; this probably should be in the reporter + if (!$result) echo $expectation->render(); + } + } +} Property changes on: trunk/testing/unit-tests/test.csst.php ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/testing/unit-tests.inc =================================================================== --- trunk/testing/unit-tests.inc 2008-01-05 19:28:57 UTC (rev 92) +++ trunk/testing/unit-tests.inc 2008-01-05 20:44:47 UTC (rev 93) @@ -1,7 +1,7 @@ <?php /**@file - * Utility functions and classes for unit testing + * Utility functions for unit testing */ function globr($sDir, $sPattern, $nFlags = NULL) { @@ -33,119 +33,3 @@ } return $aDirs; } - -class csstidy_test_csst -{ - /** Filename of test */ - var $filename; - - /** Test name */ - var $test; - - /** CSS for test to parse */ - var $css = ''; - - /** Expected var_export() output of $css->css[41] (no at block) */ - var $expect = ''; - - /** Expected var_export() output of $css->css (with at block) */ - var $fullexpect = ''; - - /** Actual result */ - var $actual; - - /** - * Loads this class from a file - */ - function load($filename) { - $this->filename = $filename; - $fh = fopen($filename, 'r'); - $state = null; - while (($line = fgets($fh)) !== false) { - $line = rtrim($line, "\n\r"); // normalize newlines - if (substr($line, 0, 2) == '--') { - // detected section - $state = $line; - continue; - } - if ($state === null) continue; - switch ($state) { - case '--TEST--': - $this->test = trim($line); - continue; - case '--CSS--': - $this->css .= $line . "\n"; - continue; - case '--EXPECT--': - $this->expect .= $line . "\n"; - continue; - case '--FULLEXPECT--': - $this->fullexpect .= $line . "\n"; - continue; - } - } - $this->expect = trim($this->expect, "\n"); // trim trailing/leading newlines - fclose($fh); - } - - /** - * Runs test - * @return TRUE if success - */ - function run() { - $css = new csstidy(); - $css->parse($this->css); - $this->actual = var_export($css->css[41], true); - return $this->expect === $this->actual; - } - - /** - * Renders the test - */ - function render() { - if ($this->actual === null) $this->run(); - $heading = '<strong>'. htmlspecialchars($this->test) .'</strong> test (<tt>'. htmlspecialchars($this->filename) .'</tt>)'; - if ($this->expect === $this->actual) { - return; - // comment the above return to show passes - echo "<div><span style=\"color:green;\">Passed:</span> $heading</div>"; - return; - } - echo "<div><span style=\"color:red;\">Failed:</span> $heading</div>"; - echo '<pre>'. htmlspecialchars($this->css) .'</pre>'; - $diff = new Text_Diff('auto', array(explode("\n", $this->expect), explode("\n", $this->actual))); - $renderer = new Text_Diff_Renderer_parallel(); - $renderer->original = 'Expected'; - $renderer->final = 'Actual'; - echo $renderer->render($diff); - } -} - -class Text_Diff_Renderer_parallel extends Text_Diff_Renderer -{ - var $original = 'Original'; - var $final = 'Final'; - var $_leading_context_lines = 10000; - var $_trailing_context_lines = 10000; - function _blockHeader() {} - function _startDiff() { - return '<table class="diff"><thead><tr><th>'. $this->original .'</th><th>'. $this->final .'</th></tr></thead><tbody>'; - } - function _endDiff() { - return '</tbody></table>'; - } - function _context($lines) { - return '<tr><td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td> - <td><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>'; - } - function _added($lines) { - return '<tr><td> </td><td class="added"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td></tr>'; - } - function _deleted($lines) { - return '<tr><td class="deleted"><pre>'. htmlspecialchars(implode("\n", $lines)) .'</pre></td><td> </td></tr>'; - } - function _changed($orig, $final) { - return '<tr class="changed"><td><pre>'. htmlspecialchars(implode("\n", $orig)) .'</pre></td> - <td><pre>'. htmlspecialchars(implode("\n", $final)) .'</pre></td></tr>'; - } -} Modified: trunk/testing/unit-tests.php =================================================================== --- trunk/testing/unit-tests.php 2008-01-05 19:28:57 UTC (rev 92) +++ trunk/testing/unit-tests.php 2008-01-05 20:44:47 UTC (rev 93) @@ -8,59 +8,32 @@ error_reporting(E_ALL); -require_once '../class.csstidy.php'; - +// Configuration $simpletest_location = 'simpletest/'; if (file_exists('../test-settings.php')) include_once '../test-settings.php'; -// PEAR +// Includes +require_once '../class.csstidy.php'; require_once 'Text/Diff.php'; require_once 'Text/Diff/Renderer.php'; - require_once $simpletest_location . 'unit_tester.php'; require_once $simpletest_location . 'reporter.php'; -// require_once $simpletest_location . 'mock_objects.php'; // we don't need it yet - +require_once 'unit-tests/class.csstidy_reporter.php'; require_once 'unit-tests.inc'; -chdir(dirname(__FILE__)); -$passed_tests = 0; -$failed_tests = 0; +// Test files +$test_files = array(); +require 'unit-tests/_files.php'; -?> -<html> -<title>CSSTidy unit tests</title> -<style type="text/css"> -.box {color:white; font-weight:bold; padding: 8px; margin-top: 1em;} -.diff {margin-bottom: 1em;} -.diff th {width:50%;} -.diff pre {margin:0; padding:0;} -.diff .changed, .diff .deleted, .diff .added {background: #FF5;} -del {background: red; color: white; font-weight: bold;} -ins {background: green; color: white; font-weight: bold;} -</style> -<body> -<h1>CSSTidy unit tests</h1> -<?php - -// allow only one directory deep, I guess otherwise, we need a recursive glob -foreach (globr('unit-tests/csst', '*.csst') as $filename) { - $test = new csstidy_test_csst(); - $test->load($filename); - if (!$test->run()) $failed_tests++; - else $passed_tests++; - $test->render(); +// Setup test files +$test = new GroupTest('CSSTidy unit tests'); +foreach ($test_files as $test_file) { + require_once "unit-tests/$test_file"; + list($x, $class_suffix) = explode('.', $test_file); + $test->addTestClass("csstidy_test_$class_suffix"); } -// output test results -if (!$failed_tests) { - echo '<div style="background:green;" class="box">'; -} else { - echo '<div style="background:red;" class="box">'; -} +if (SimpleReporter::inCli()) $reporter = new TextReporter(); +else $reporter = new csstidy_reporter('UTF-8'); -echo $passed_tests .' tests passed, ' . $failed_tests .' tests failed</div>' - -?> -</body> -</html> \ No newline at end of file +$test->run($reporter); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |