package press;

import org.junit.Before;
import org.junit.Test;
import play.Play;
import play.cache.Cache;
import play.cache.CacheImpl;

import java.io.IOException;
import java.io.StringWriter;

import static org.apache.commons.io.FileUtils.readFileToString;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

public class StyleCompressorTest extends AbstractPressTest {
  StyleCompressor compressor = new StyleCompressor();

  @Before
  public void mockCache() {
    Cache.cacheImpl = mock(CacheImpl.class);
    Play.mode = Play.Mode.DEV;
    doReturn(true).when(Cache.cacheImpl).safeSet(anyString(), any(), anyInt());
    PluginConfig.inMemoryStorage = true;
  }

  @Test
  public void canOutputCssFileAsIs() throws IOException {
    StringWriter out = new StringWriter();
    compressor.compress(rawCssFile, out, false);
    assertEquals(readFileToString(rawCssFile), out.toString());
  }

  @Test
  public void compressDoesNotModifyCssFile_becauseItIsCompressedDuringBuild() throws IOException {
    StringWriter out = new StringWriter();
    compressor.compress(rawCssFile, out, true);
    String compressedCss = out.toString();
    assertEquals(readFileToString(rawCssFile), compressedCss);
  }

  @Test
  public void canConvertLessToCssWithoutCompression() throws IOException {
    StringWriter out = new StringWriter();
    compressor.compress(rawLessFile, out, false);
    assertEquals(readFileToString(rawCssFile), out.toString());
  }

  String warning = "The compress option has been deprecated. We recommend you use a dedicated css minifier, for instance see less-plugin-clean-css.\n";
  
  @Test
  public void canConvertLessToCssWithCompression() throws IOException {
    StringWriter out = new StringWriter();
    compressor.compress(rawLessFile, out, true);
    String compressedLess = out.toString();
    assertEquals(warning + readFileToString(minCssFile), compressedLess);
  }
  
  @Test
  public void detectsLessByFileExtension() {
    assertTrue(compressor.isLess("some.less"));
    assertFalse(compressor.isLess("some.css"));
    assertFalse(compressor.isLess("some.js"));
  }
}

@@ -1546,16 +1546,98 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void canConvertLessToCssWithCompression() throws IOException %7B%0A  
   String
@@ -1784,90 +1784,8 @@
 n%22;%0A
-  %0A  @Test%0A  public void canConvertLessToCssWithCompression() throws IOException %7B
 %0A   
@@ -1938,18 +1938,8 @@
 als(
-warning + 
 read
@@ -1978,16 +1978,44 @@
 ssedLess
+.replace(warning, %22%22).trim()
 );%0A  %7D%0A 

fix failing unit-tests in play-press module