package org.zmlx.hg4idea.test;

import com.intellij.openapi.vfs.CharsetToolkit;
import org.junit.Test;
import org.zmlx.hg4idea.HgRevisionNumber;
import org.zmlx.hg4idea.command.HgCatCommand;
import org.zmlx.hg4idea.command.HgRevertCommand;
import org.zmlx.hg4idea.execution.HgCommandResult;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class HgRevertTest extends HgSingleUserTest {
  @Test
  public void testRevertToCurrentRevision() throws Exception {
    fillFile(myProjectDir, new String[]{"file.txt"}, "initial contents");
    runHgOnProjectRepo("add", ".");
    runHgOnProjectRepo("commit", "-m", "initial contents");

    fillFile(myProjectDir, new String[]{"file.txt"}, "new contents");

    HgRevertCommand revertCommand = new HgRevertCommand(myProject);
    revertCommand.execute(myRepo.getDir(), Collections.singleton(new File(myProjectDir, "file.txt").getPath()), null, false);

    HgCatCommand catCommand = new HgCatCommand(myProject);
    HgCommandResult result = catCommand.execute(getHgFile("file.txt"), null, Charset.defaultCharset());
    assertNotNull(result);
    assertEquals("initial contents", new String(result.getBytesOutput(), CharsetToolkit.UTF8_CHARSET));
  }


  @Test
  public void testRevertToGivenRevision() throws Exception {
    fillFile(myProjectDir, new String[]{"file.txt"}, "initial contents");
    runHgOnProjectRepo("add", ".");
    runHgOnProjectRepo("commit", "-m", "initial contents");

    fillFile(myProjectDir, new String[]{"file.txt"}, "new contents");
    runHgOnProjectRepo("commit", "-m", "new contents");

    HgRevertCommand revertCommand = new HgRevertCommand(myProject);
    revertCommand.execute(myRepo.getDir(), Collections.singleton(new File(myProjectDir, "file.txt").getPath()),
                          HgRevisionNumber.getLocalInstance("0"), false);

    HgCatCommand catCommand = new HgCatCommand(myProject);
    HgCommandResult result = catCommand.execute(getHgFile("file.txt"), HgRevisionNumber.getLocalInstance("0"), Charset.defaultCharset());
    assertNotNull(result);
    assertEquals("initial contents", new String(result.getBytesOutput(), CharsetToolkit.UTF8_CHARSET));
  }
}

@@ -1222,32 +1222,117 @@
    assertEquals(
+%22Wrong cat output: %22 + result.getRawOutput() + %22with error:%22 + result.getRawError(), 
 %22initial content
@@ -1326,32 +1326,49 @@
 itial contents%22,
+%0A                
  new String(resu
@@ -2288,16 +2288,101 @@
 tEquals(
+%22Wrong cat output: %22 + result.getRawOutput() + %22with error:%22 + result.getRawError(), 
 %22initial
@@ -2392,16 +2392,33 @@
 ntents%22,
+%0A                
  new Str

add blinking test logging