1// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package terminal
16
17import (
18	"testing"
19)
20
21func TestStripAnsiEscapes(t *testing.T) {
22	testcases := []struct {
23		input  string
24		output string
25	}{
26		{
27			"",
28			"",
29		},
30		{
31			"This is a test",
32			"This is a test",
33		},
34		{
35			"interrupted: \x1b[12",
36			"interrupted: ",
37		},
38		{
39			"other \x1bescape \x1b",
40			"other \x1bescape \x1b",
41		},
42		{ // from pretty-error macro
43			"\x1b[1mart/Android.mk: \x1b[31merror:\x1b[0m\x1b[1m art: test error \x1b[0m",
44			"art/Android.mk: error: art: test error ",
45		},
46		{ // from envsetup.sh make wrapper
47			"\x1b[0;31m#### make failed to build some targets (2 seconds) ####\x1b[00m",
48			"#### make failed to build some targets (2 seconds) ####",
49		},
50		{ // from clang (via ninja testcase)
51			"\x1b[1maffixmgr.cxx:286:15: \x1b[0m\x1b[0;1;35mwarning: \x1b[0m\x1b[1musing the result... [-Wparentheses]\x1b[0m",
52			"affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]",
53		},
54	}
55	for _, tc := range testcases {
56		got := string(stripAnsiEscapes([]byte(tc.input)))
57		if got != tc.output {
58			t.Errorf("output strings didn't match\n"+
59				"input: %#v\n"+
60				" want: %#v\n"+
61				"  got: %#v", tc.input, tc.output, got)
62		}
63	}
64}
65