summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/execution/process/AnsiEscapeDecoderTest.java
blob: df0acc766dc74823e662f7999f14c02db1e03a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.intellij.execution.process;

import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.testFramework.PlatformTestCase;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;

import java.util.Arrays;
import java.util.List;

public class AnsiEscapeDecoderTest extends PlatformTestCase {

  public void testTextWithoutColors() throws Exception {
    AnsiEscapeDecoder decoder = new AnsiEscapeDecoder();
    decoder.escapeText("", ProcessOutputTypes.STDOUT, createExpectedAcceptor(
      Pair.create("", ProcessOutputTypes.STDOUT)
    ));
    decoder.escapeText("simple text", ProcessOutputTypes.STDOUT, createExpectedAcceptor(
      Pair.create("simple text", ProcessOutputTypes.STDOUT)
    ));
  }

  public void testSingleColoredChunk() throws Exception {
    AnsiEscapeDecoder decoder = new AnsiEscapeDecoder();
    decoder.escapeText("Chrome 35.0.1916 (Linux): Executed 0 of 1\u001B[32m SUCCESS\u001B[39m (0 secs / 0 secs)\n", ProcessOutputTypes.STDOUT, createExpectedAcceptor(
      Pair.create("Chrome 35.0.1916 (Linux): Executed 0 of 1", ProcessOutputTypes.STDOUT),
      Pair.create(" SUCCESS", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[32m")),
      Pair.create(" (0 secs / 0 secs)\n", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[39m"))
    ));
  }

  public void testCompoundEscSeq() throws Exception {
    AnsiEscapeDecoder decoder = new AnsiEscapeDecoder();
    decoder.escapeText("E\u001B[41m\u001B[37mE\u001B[0mE", ProcessOutputTypes.STDOUT, createExpectedAcceptor(
      Pair.create("E", ProcessOutputTypes.STDOUT),
      Pair.create("E", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[41;37m")),
      Pair.create("E", ProcessOutputTypes.STDOUT)
    ));
  }

  public void testOtherEscSeq() throws Exception {
    AnsiEscapeDecoder decoder = new AnsiEscapeDecoder();
    decoder.escapeText("Plain\u001B[32mGreen\u001B[39mNormal\u001B[1A\u001B[2K\u001B[31mRed\u001B[39m",
                       ProcessOutputTypes.STDOUT,
                       createExpectedAcceptor(
                         Pair.create("Plain", ProcessOutputTypes.STDOUT),
                         Pair.create("Green", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[32m")),
                         Pair.create("Normal", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[39m")),
                         Pair.create("Red", ColoredOutputTypeRegistry.getInstance().getOutputKey("\u001B[31m"))
                       )
    );
  }

  private static AnsiEscapeDecoder.ColoredChunksAcceptor createExpectedAcceptor(@NotNull final Pair<String, Key>... expected) {
    return new AnsiEscapeDecoder.ColoredChunksAcceptor() {
      @Override
      public void coloredChunksAvailable(List<Pair<String, Key>> chunks) {
        Assert.assertEquals(Arrays.asList(expected), chunks);
      }

      @Override
      public void coloredTextAvailable(String text, Key attributes) {
        throw new RuntimeException(); // shouldn't be called
      }
    };
  }
}