File indexing completed on 2024-05-12 06:02:08

0001 <?php
0002 
0003 require_once __DIR__ . "/../lessc.inc.php";
0004 
0005 class ApiTest extends PHPUnit_Framework_TestCase {
0006   public function setUp() {
0007     $this->less = new lessc();
0008     $this->less->importDir = array(__DIR__ . "/inputs/test-imports");
0009   }
0010 
0011   public function testPreserveComments() {
0012     $input = <<<EOD
0013 // what is going on?
0014 
0015 /** what the heck **/
0016 
0017 /**
0018 
0019 Here is a block comment
0020 
0021 **/
0022 
0023 
0024 // this is a comment
0025 
0026 /*hello*/div /*yeah*/ { //surew
0027   border: 1px solid red; // world
0028   /* another property */
0029   color: url('http://mage-page.com');
0030   string: "hello /* this is not a comment */";
0031   world: "// neither is this";
0032   string: 'hello /* this is not a comment */' /*what if this is a comment */;
0033   world: '// neither is this' // hell world;
0034   ;
0035   what-ever: 100px;
0036   background: url(/*this is not a comment?*/); // uhh what happens here
0037 }
0038 EOD;
0039 
0040 
0041     $outputWithComments = <<<EOD
0042 /** what the heck **/
0043 /**
0044 
0045 Here is a block comment
0046 
0047 **/
0048 /*hello*/
0049 /*yeah*/
0050 div /*yeah*/ {
0051   /* another property */
0052   border: 1px solid red;
0053   color: url('http://mage-page.com');
0054   string: "hello /* this is not a comment */";
0055   world: "// neither is this";
0056   /*what if this is a comment */
0057   string: 'hello /* this is not a comment */';
0058   world: '// neither is this';
0059   what-ever: 100px;
0060   /*this is not a comment?*/
0061   background: url();
0062 }
0063 EOD;
0064 
0065     $outputWithoutComments = <<<EOD
0066 div {
0067   border: 1px solid red;
0068   color: url('http://mage-page.com');
0069   string: "hello /* this is not a comment */";
0070   world: "// neither is this";
0071   string: 'hello /* this is not a comment */';
0072   world: '// neither is this';
0073   what-ever: 100px;
0074   background: url(/*this is not a comment?*/);
0075 }
0076 EOD;
0077 
0078     $this->assertEquals($this->compile($input), trim($outputWithoutComments));
0079     $this->less->setPreserveComments(true);
0080     $this->assertEquals($this->compile($input), trim($outputWithComments));
0081   }
0082 
0083   public function testOldInterface() {
0084     $this->less = new lessc(__DIR__ . "/inputs/hi.less");
0085     $out = $this->less->parse(array("hello" => "10px"));
0086     $this->assertEquals(trim($out), trim('
0087 div:before {
0088   content: "hi!";
0089 }'));
0090 
0091   }
0092 
0093   public function testInjectVars() {
0094     $out = $this->less->parse(".magic { color: @color;  width: @base - 200; }",
0095       array(
0096         'color' => 'red',
0097         'base' => '960px'
0098       ));
0099   
0100     $this->assertEquals(trim($out), trim("
0101 .magic {
0102   color: red;
0103   width: 760px;
0104 }"));
0105 
0106   }
0107 
0108   public function testDisableImport() {
0109     $this->less->importDisabled = true;
0110     $this->assertEquals(
0111       "/* import disabled */",
0112       $this->compile("@import 'file3';"));
0113   }
0114 
0115   public function testUserFunction() {
0116     $this->less->registerFunction("add-two", function($list) {
0117       list($a, $b) = $list[2];
0118       return $a[1] + $b[1];
0119     });
0120 
0121     $this->assertEquals(
0122       $this->compile("result: add-two(10, 20);"),
0123       "result: 30;");
0124     
0125     return $this->less;
0126   }
0127 
0128   /**
0129    * @depends testUserFunction
0130    */
0131   public function testUnregisterFunction($less) {
0132     $less->unregisterFunction("add-two");
0133 
0134     $this->assertEquals(
0135       $this->compile("result: add-two(10, 20);"),
0136       "result: add-two(10,20);");
0137   }
0138 
0139 
0140 
0141   public function testFormatters() {
0142     $src = "
0143       div, pre {
0144         color: blue;
0145         span, .big, hello.world {
0146           height: 20px;
0147           color:#ffffff + #000;
0148         }
0149       }";
0150 
0151     $this->less->setFormatter("compressed");
0152     $this->assertEquals(
0153       $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;}");
0154 
0155     // TODO: fix the output order of tags
0156     $this->less->setFormatter("lessjs");
0157     $this->assertEquals(
0158       $this->compile($src),
0159 "div,
0160 pre {
0161   color: blue;
0162 }
0163 div span,
0164 div .big,
0165 div hello.world,
0166 pre span,
0167 pre .big,
0168 pre hello.world {
0169   height: 20px;
0170   color: #ffffff;
0171 }");
0172 
0173     $this->less->setFormatter("classic");
0174     $this->assertEquals(
0175       $this->compile($src),
0176 trim("div, pre { color:blue; }
0177 div span, div .big, div hello.world, pre span, pre .big, pre hello.world {
0178   height:20px;
0179   color:#ffffff;
0180 }
0181 "));
0182 
0183   }
0184 
0185   public function compile($str) {
0186     return trim($this->less->parse($str));
0187   }
0188 
0189 }