InputTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. require_once __DIR__ . "/../lessc.inc.php";
  3. // Runs all the tests in inputs/ and compares their output to ouputs/
  4. function _dump($value) {
  5. fwrite(STDOUT, print_r($value, true));
  6. }
  7. function _quote($str) {
  8. return preg_quote($str, "/");
  9. }
  10. class InputTest extends PHPUnit_Framework_TestCase {
  11. protected static $importDirs = array("inputs/test-imports");
  12. protected static $testDirs = array(
  13. "inputs" => "outputs",
  14. "inputs_lessjs" => "outputs_lessjs",
  15. );
  16. public function setUp() {
  17. $this->less = new lessc();
  18. $this->less->importDir = array_map(function($path) {
  19. return __DIR__ . "/" . $path;
  20. }, self::$importDirs);
  21. }
  22. /**
  23. * @dataProvider fileNameProvider
  24. */
  25. public function testInputFile($inFname) {
  26. if ($pattern = getenv("BUILD")) {
  27. return $this->buildInput($inFname);
  28. }
  29. $outFname = self::outputNameFor($inFname);
  30. if (!is_readable($outFname)) {
  31. $this->fail("$outFname is missing, ".
  32. "consider building tests with BUILD=true");
  33. }
  34. $input = file_get_contents($inFname);
  35. $output = file_get_contents($outFname);
  36. $this->assertEquals($output, $this->less->parse($input));
  37. }
  38. public function fileNameProvider() {
  39. return array_map(function($a) { return array($a); },
  40. self::findInputNames());
  41. }
  42. // only run when env is set
  43. public function buildInput($inFname) {
  44. $css = $this->less->parse(file_get_contents($inFname));
  45. file_put_contents(self::outputNameFor($inFname), $css);
  46. }
  47. static public function findInputNames($pattern="*.less") {
  48. $files = array();
  49. foreach (self::$testDirs as $inputDir => $outputDir) {
  50. $files = array_merge($files, glob(__DIR__ . "/" . $inputDir . "/" . $pattern));
  51. }
  52. return array_filter($files, "is_file");
  53. }
  54. static public function outputNameFor($input) {
  55. $front = _quote(__DIR__ . "/");
  56. $out = preg_replace("/^$front/", "", $input);
  57. foreach (self::$testDirs as $inputDir => $outputDir) {
  58. $in = _quote($inputDir . "/");
  59. $rewritten = preg_replace("/$in/", $outputDir . "/", $out);
  60. if ($rewritten != $out) {
  61. $out = $rewritten;
  62. break;
  63. }
  64. }
  65. $out = preg_replace("/.less$/", ".css", $out);
  66. return __DIR__ . "/" . $out;
  67. }
  68. }