PageRenderTime 65ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/_core_/tags/spip-3.0.4/plugins/compresseur/lib/csstidy/testing/unit-tests/class.csstidy_csst.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 135 lines | 96 code | 13 blank | 26 comment | 12 complexity | 8167253b606276d804c66b426584840b MD5 | raw file
  1. <?php
  2. require_once 'class.Text_Diff_Renderer_parallel.php';
  3. /**
  4. * CSSTidy CSST expectation, for testing CSS parsing.
  5. */
  6. class csstidy_csst extends SimpleExpectation
  7. {
  8. /** Filename of test */
  9. var $filename;
  10. /** Test name */
  11. var $test;
  12. /** CSS for test to parse */
  13. var $css = '';
  14. /** Settings for csstidy */
  15. var $settings = array();
  16. /** Expected var_export() output of $css->css[41] (no at block) */
  17. var $expect = '';
  18. /** Boolean whether or not to use $css->css instead for $expect */
  19. var $fullexpect = false;
  20. /** Print form of CSS that can be tested **/
  21. var $print = false;
  22. var $default_media = "";
  23. /** Actual result */
  24. var $actual;
  25. /**
  26. * Loads this class from a file.
  27. * @param $filename String filename to load
  28. */
  29. function load($filename) {
  30. $this->filename = $filename;
  31. $fh = fopen($filename, 'r');
  32. $state = '';
  33. while (($line = fgets($fh)) !== false) {
  34. $line = rtrim($line, "\n\r"); // normalize newlines
  35. if (substr($line, 0, 2) == '--') {
  36. // detected section
  37. $state = $line;
  38. continue;
  39. }
  40. if ($state === null) continue;
  41. switch ($state) {
  42. case '--TEST--':
  43. $this->test = trim($line);
  44. break;
  45. case '--CSS--':
  46. $this->css .= $line . "\n";
  47. break;
  48. case '--FULLEXPECT--':
  49. $this->fullexpect = true;
  50. $this->expect .= $line . "\n";
  51. break;
  52. case '--EXPECT--':
  53. $this->expect .= $line . "\n";
  54. break;
  55. case '--SETTINGS--':
  56. list($n, $v) = array_map('trim',explode('=', $line, 2));
  57. $v = eval("return $v;");
  58. if ($n=="default_media" AND $this->print)
  59. $this->default_media = $v;
  60. else
  61. $this->settings[$n] = $v;
  62. break;
  63. case '--PRINT--':
  64. $this->print = true;
  65. $this->expect .= $line . "\n";
  66. break;
  67. }
  68. }
  69. if ($this->print) {
  70. $this->expect = trim($this->expect);
  71. }
  72. else {
  73. if ($this->expect)
  74. $this->expect = eval("return ".$this->expect.";");
  75. if (!$this->fullexpect)
  76. $this->expect = array(41=>$this->expect);
  77. }
  78. fclose($fh);
  79. }
  80. /**
  81. * Implements SimpleExpectation::test().
  82. * @param $filename Filename of test file to test.
  83. */
  84. function test($filename = false) {
  85. if ($filename) $this->load($filename);
  86. $css = new csstidy();
  87. $css->set_cfg($this->settings);
  88. $css->parse($this->css);
  89. if ($this->print){
  90. $this->actual = $css->print->plain($this->default_media);
  91. }
  92. else{
  93. $this->actual = $css->css;
  94. }
  95. return $this->expect === $this->actual;
  96. }
  97. /**
  98. * Implements SimpleExpectation::testMessage().
  99. */
  100. function testMessage() {
  101. $message = $this->test . ' test at '. htmlspecialchars($this->filename);
  102. return $message;
  103. }
  104. /**
  105. * Renders the test with an HTML diff table.
  106. */
  107. function render() {
  108. $message = '<pre>'. htmlspecialchars($this->css) .'</pre>';
  109. $diff = new Text_Diff(
  110. 'auto',
  111. array(
  112. explode("\n", $this->print?$this->expect:var_export($this->expect,true)),
  113. explode("\n", $this->print?$this->actual:var_export($this->actual,true))
  114. )
  115. );
  116. $renderer = new Text_Diff_Renderer_parallel();
  117. $renderer->original = 'Expected';
  118. $renderer->final = 'Actual';
  119. $message .= $renderer->render($diff);
  120. return $message;
  121. }
  122. }