PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/asual/lesscss/LessEngineTest.java

http://github.com/asual/lesscss-engine
Java | 269 lines | 216 code | 29 blank | 24 comment | 2 complexity | 16bfcacf7b40d71b4a0fa0b39cc4fdf1 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.asual.lesscss;
  15. import static org.junit.Assert.assertEquals;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.BufferedReader;
  18. import java.io.DataInputStream;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.net.URL;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. /**
  27. * @author Rostislav Hristov
  28. * @author Uriah Carpenter
  29. * @author Eliot Sykes
  30. */
  31. public class LessEngineTest {
  32. private static LessEngine engine;
  33. @BeforeClass
  34. public static void before() {
  35. LessOptions options = new LessOptions();
  36. options.setCss(true);
  37. engine = new LessEngine(options);
  38. }
  39. @Test
  40. public void testString() throws LessException {
  41. assertEquals("div {\n width: 2;\n}\n",
  42. engine.compile("div { width: 1 + 1 }"));
  43. }
  44. @Test
  45. public void testStringWithImport() throws LessException {
  46. String path = getResource("less/import.less").getPath();
  47. assertEquals(
  48. "body {\n color: #f0f0f0;\n}\n",
  49. engine.compile("@import url('" + path
  50. + "'); body { color: @color; }"));
  51. }
  52. @Test
  53. public void testStringWithLocation() throws LessException {
  54. /*
  55. * A template engine would extract the <style type="text/less"> block
  56. * from META-INF/template.html and compile it. Here, we're skipping the
  57. * extraction and passing the stylesheet body and template location to
  58. * the engine directly.
  59. */
  60. String in = "@import \"less/subdir/import-from-root.less\";\n"
  61. + "@import \"classpath:META-INF/less/import.less\";\n"
  62. + "body { color: @color; }";
  63. String out = "a {\n color: #dddddd;\n"
  64. + " background-image: url(img/logo.png);\n}\n"
  65. + "body {\n color: #f0f0f0;\n}\n";
  66. assertEquals(out, engine.compile(in, getResource("template.html")
  67. .toString(), false));
  68. }
  69. @Test
  70. public void testToString() throws LessException, IOException {
  71. assertEquals("body {\n color: #f0f0f0;\n}\n",
  72. engine.compile(getResource("less/classpath.less")));
  73. }
  74. @Test
  75. public void testToFile() throws LessException, IOException {
  76. File tempDir = new File(System.getProperty("java.io.tmpdir"));
  77. File tempFile = File.createTempFile("classpath.less", null, tempDir);
  78. engine.compile(new File(getResource("less/classpath.less").getPath()),
  79. new File(tempFile.getAbsolutePath()));
  80. assertEquals("body { color: #f0f0f0;}", getContent(tempFile));
  81. tempFile.delete();
  82. }
  83. @Test
  84. public void testToStringForMultipleImports() throws LessException,
  85. IOException {
  86. String expected = "body {\n"
  87. + " font-family: 'Helvetica Neue', Arial, sans-serif;\n}\n"
  88. + "body {\n width: 960px;\n margin: 0;\n}\n"
  89. + "#header {\n border-radius: 5px;\n"
  90. + " -webkit-border-radius: 5px;\n"
  91. + " -moz-border-radius: 5px;\n}\n#footer {\n"
  92. + " border-radius: 10px;\n"
  93. + " -webkit-border-radius: 10px;\n"
  94. + " -moz-border-radius: 10px;\n}\n";
  95. String compile = engine.compile(getResource("css/multiple-imports.css"));
  96. assertEquals(expected,
  97. compile);
  98. }
  99. @Test
  100. public void testToCompressedStringForMultipleImports()
  101. throws LessException, IOException {
  102. String expected = "body{font-family:'Helvetica Neue',Arial,sans-serif}body{width:960px;margin:0}"
  103. + "#header{border-radius:5px;-webkit-border-radius:5px;-moz-border-radius:5px}"
  104. + "#footer{border-radius:10px;-webkit-border-radius:10px;-moz-border-radius:10px}";
  105. assertEquals(expected,
  106. engine.compile(getResource("css/multiple-imports.css"), true));
  107. }
  108. @Test
  109. public void testImages() throws LessException, IOException {
  110. String expected = ".logo {\n background-image: url(../img/logo.png);\n}\n";
  111. assertEquals(expected, engine.compile(getResource("less/img.less")));
  112. }
  113. @Test
  114. public void testSubdirImports() throws LessException, IOException {
  115. engine.compile(getResource("less/root.less"));
  116. engine.compile(getResource("less/subdir/import-from-root.less"));
  117. engine.compile(getResource("less/import-from-subdir.less"));
  118. }
  119. @Test(expected = LessException.class)
  120. public void testNameErrorInput() throws IOException, LessException {
  121. try {
  122. engine.compile(getResource("less/name-error.less"));
  123. } catch (LessException e) {
  124. assertTrue("Name Error", e.getMessage().contains(
  125. "Name Error: .bgColor is undefined (line 2, column 4)"));
  126. throw e;
  127. }
  128. }
  129. @Test(expected = LessException.class)
  130. public void testParseErrorInput() throws IOException, LessException {
  131. try {
  132. engine.compile(getResource("less/parse-error.less"));
  133. } catch (LessException e) {
  134. assertTrue("Parse Error",
  135. e.getMessage().contains("Parse Error: Unrecognised input"));
  136. throw e;
  137. }
  138. }
  139. @Test(expected = LessException.class)
  140. public void testParseUnbalancedInputUnder() throws IOException,
  141. LessException {
  142. try {
  143. engine.compile(getResource("less/unbalanced-under-error.less"));
  144. } catch (LessException e) {
  145. assertTrue("Parse Error",
  146. e.getMessage().contains("Parse Error: missing closing `}`"));
  147. throw e;
  148. }
  149. }
  150. @Test
  151. public void testImportWithUrl() throws LessException, IOException {
  152. String expected = "a {\n color: #dddddd;\n background-image: url(img/logo.png);\n}\n";
  153. String result = engine
  154. .compile(getResource("less/import-from-subdir.less"));
  155. assertEquals(expected, result);
  156. }
  157. @Test(expected = LessException.class)
  158. public void testImportWithMissingUrl() throws Exception {
  159. try {
  160. engine.compile(getResource("less/import-missing.less"));
  161. } catch (Exception e) {
  162. assertTrue("No such file", e.getMessage().contains("No such file"));
  163. throw e;
  164. }
  165. }
  166. @Test
  167. public void testSample() throws LessException, IOException {
  168. String expected = ".box {\n color: #fe33ac;\n"
  169. + " border-color: #fdcdea;\n}\n.box div {\n"
  170. + " box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);\n"
  171. + " -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);\n"
  172. + " -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);\n}\n";
  173. String result = engine.compile(getResource("less/sample.less"));
  174. assertEquals(expected, result);
  175. }
  176. @Test
  177. public void testDebugInfo() throws LessException, IOException {
  178. LessOptions options = new LessOptions();
  179. options.setLineNumbers(LessOptions.LINE_NUMBERS_COMMENTS);
  180. options.setCss(true);
  181. LessEngine debugEngine = new LessEngine(options);
  182. assertEquals("/* line 3",
  183. debugEngine.compile(getResource("css/multiple-imports.css"))
  184. .substring(0, 9));
  185. }
  186. @Test
  187. public void testConfiguredImportPaths() throws LessException, IOException {
  188. LessOptions options = new LessOptions();
  189. options.setPaths(getResource("less2").toString());
  190. LessEngine pathEngine = new LessEngine(options);
  191. String expected = "a {\n color: #dddddd;\n"
  192. + " background-image: url(img/logo.png);\n}\n"
  193. + "p {\n color: #555555;\n}\n"
  194. + "h2 {\n font-size: 20px;\n}\n";
  195. String compiledLess = pathEngine.compile(getResource("less/import-from-multi-paths.less"));
  196. assertEquals(expected, compiledLess);
  197. options.setPaths(new File(getResource("less2").getPath()).getAbsolutePath());
  198. pathEngine = new LessEngine(options);
  199. compiledLess = pathEngine.compile(getResource("less/import-from-multi-paths.less"));
  200. assertEquals(expected, compiledLess);
  201. }
  202. @Test
  203. public void testSourceMap() throws LessException {
  204. LessOptions options = new LessOptions();
  205. options.setSourceMap(true);
  206. LessEngine engine = new LessEngine(options);
  207. assertEquals("div {\n width: 2;\n}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImlucHV0Il0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0VBQU0sUUFBQSJ9 */",
  208. engine.compile("div { width: 1 + 1 }"));
  209. }
  210. @Test
  211. public void testSourceMapUrl() throws LessException, IOException {
  212. String sourceMapUrl = "test.map.css";
  213. LessOptions options = new LessOptions();
  214. options.setSourceMap(true);
  215. options.setSourceMapUrl(sourceMapUrl);
  216. LessEngine engine = new LessEngine(options);
  217. assertEquals("div {\n width: 2;\n}\n/*# sourceMappingURL=" + sourceMapUrl + " */",
  218. engine.compile("div { width: 1 + 1 }"));
  219. File file = new File(sourceMapUrl);
  220. assertEquals("{\"version\":3,\"sources\":[\"input\"],\"names\":[],\"mappings\":\"AAAA;EAAM,QAAA\"}",
  221. getContent(file));
  222. file.delete();
  223. }
  224. private URL getResource(String path) {
  225. return getClass().getClassLoader().getResource("META-INF/" + path);
  226. }
  227. private String getContent(File file) throws IOException {
  228. FileInputStream fstream = new FileInputStream(
  229. file.getAbsolutePath());
  230. DataInputStream in = new DataInputStream(fstream);
  231. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  232. String strLine;
  233. StringBuilder sb = new StringBuilder();
  234. while ((strLine = br.readLine()) != null) {
  235. sb.append(strLine);
  236. }
  237. in.close();
  238. return sb.toString();
  239. }
  240. }