ApiTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. require_once __DIR__ . "/../lessc.inc.php";
  3. class ApiTest extends PHPUnit_Framework_TestCase {
  4. public function setUp() {
  5. $this->less = new lessc();
  6. $this->less->importDir = array(__DIR__ . "/inputs/test-imports");
  7. }
  8. public function testPreserveComments() {
  9. $input = <<<EOD
  10. // what is going on?
  11. /** what the heck **/
  12. /**
  13. Here is a block comment
  14. **/
  15. // this is a comment
  16. /*hello*/div /*yeah*/ { //surew
  17. border: 1px solid red; // world
  18. /* another property */
  19. color: url('http://mage-page.com');
  20. string: "hello /* this is not a comment */";
  21. world: "// neither is this";
  22. string: 'hello /* this is not a comment */' /*what if this is a comment */;
  23. world: '// neither is this' // hell world;
  24. ;
  25. what-ever: 100px;
  26. background: url(/*this is not a comment?*/); // uhh what happens here
  27. }
  28. EOD;
  29. $outputWithComments = <<<EOD
  30. /** what the heck **/
  31. /**
  32. Here is a block comment
  33. **/
  34. /*hello*/
  35. /*yeah*/
  36. div /*yeah*/ {
  37. /* another property */
  38. border: 1px solid red;
  39. color: url('http://mage-page.com');
  40. string: "hello /* this is not a comment */";
  41. world: "// neither is this";
  42. /*what if this is a comment */
  43. string: 'hello /* this is not a comment */';
  44. world: '// neither is this';
  45. what-ever: 100px;
  46. /*this is not a comment?*/
  47. background: url();
  48. }
  49. EOD;
  50. $outputWithoutComments = <<<EOD
  51. div {
  52. border: 1px solid red;
  53. color: url('http://mage-page.com');
  54. string: "hello /* this is not a comment */";
  55. world: "// neither is this";
  56. string: 'hello /* this is not a comment */';
  57. world: '// neither is this';
  58. what-ever: 100px;
  59. background: url(/*this is not a comment?*/);
  60. }
  61. EOD;
  62. $this->assertEquals($this->compile($input), trim($outputWithoutComments));
  63. $this->less->setPreserveComments(true);
  64. $this->assertEquals($this->compile($input), trim($outputWithComments));
  65. }
  66. public function testOldInterface() {
  67. $this->less = new lessc(__DIR__ . "/inputs/hi.less");
  68. $out = $this->less->parse(array("hello" => "10px"));
  69. $this->assertEquals(trim($out), trim('
  70. div:before {
  71. content: "hi!";
  72. }'));
  73. }
  74. public function testInjectVars() {
  75. $out = $this->less->parse(".magic { color: @color; width: @base - 200; }",
  76. array(
  77. 'color' => 'red',
  78. 'base' => '960px'
  79. ));
  80. $this->assertEquals(trim($out), trim("
  81. .magic {
  82. color: red;
  83. width: 760px;
  84. }"));
  85. }
  86. public function testDisableImport() {
  87. $this->less->importDisabled = true;
  88. $this->assertEquals(
  89. "/* import disabled */",
  90. $this->compile("@import 'file3';"));
  91. }
  92. public function testUserFunction() {
  93. $this->less->registerFunction("add-two", function($list) {
  94. list($a, $b) = $list[2];
  95. return $a[1] + $b[1];
  96. });
  97. $this->assertEquals(
  98. $this->compile("result: add-two(10, 20);"),
  99. "result: 30;");
  100. return $this->less;
  101. }
  102. /**
  103. * @depends testUserFunction
  104. */
  105. public function testUnregisterFunction($less) {
  106. $less->unregisterFunction("add-two");
  107. $this->assertEquals(
  108. $this->compile("result: add-two(10, 20);"),
  109. "result: add-two(10,20);");
  110. }
  111. public function testFormatters() {
  112. $src = "
  113. div, pre {
  114. color: blue;
  115. span, .big, hello.world {
  116. height: 20px;
  117. color:#ffffff + #000;
  118. }
  119. }";
  120. $this->less->setFormatter("compressed");
  121. $this->assertEquals(
  122. $this->compile($src), "div,pre{color:blue;}div span,div .big,div hello.world,pre span,pre .big,pre hello.world{height:20px;color:#fff;}");
  123. // TODO: fix the output order of tags
  124. $this->less->setFormatter("lessjs");
  125. $this->assertEquals(
  126. $this->compile($src),
  127. "div,
  128. pre {
  129. color: blue;
  130. }
  131. div span,
  132. div .big,
  133. div hello.world,
  134. pre span,
  135. pre .big,
  136. pre hello.world {
  137. height: 20px;
  138. color: #ffffff;
  139. }");
  140. $this->less->setFormatter("classic");
  141. $this->assertEquals(
  142. $this->compile($src),
  143. trim("div, pre { color:blue; }
  144. div span, div .big, div hello.world, pre span, pre .big, pre hello.world {
  145. height:20px;
  146. color:#ffffff;
  147. }
  148. "));
  149. }
  150. public function compile($str) {
  151. return trim($this->less->parse($str));
  152. }
  153. }