util_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. package terminal
  15. import (
  16. "testing"
  17. )
  18. func TestStripAnsiEscapes(t *testing.T) {
  19. testcases := []struct {
  20. input string
  21. output string
  22. }{
  23. {
  24. "",
  25. "",
  26. },
  27. {
  28. "This is a test",
  29. "This is a test",
  30. },
  31. {
  32. "interrupted: \x1b[12",
  33. "interrupted: ",
  34. },
  35. {
  36. "other \x1bescape \x1b",
  37. "other \x1bescape \x1b",
  38. },
  39. { // from pretty-error macro
  40. "\x1b[1mart/Android.mk: \x1b[31merror:\x1b[0m\x1b[1m art: test error \x1b[0m",
  41. "art/Android.mk: error: art: test error ",
  42. },
  43. { // from envsetup.sh make wrapper
  44. "\x1b[0;31m#### make failed to build some targets (2 seconds) ####\x1b[00m",
  45. "#### make failed to build some targets (2 seconds) ####",
  46. },
  47. { // from clang (via ninja testcase)
  48. "\x1b[1maffixmgr.cxx:286:15: \x1b[0m\x1b[0;1;35mwarning: \x1b[0m\x1b[1musing the result... [-Wparentheses]\x1b[0m",
  49. "affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]",
  50. },
  51. }
  52. for _, tc := range testcases {
  53. got := string(stripAnsiEscapes([]byte(tc.input)))
  54. if got != tc.output {
  55. t.Errorf("output strings didn't match\n"+
  56. "input: %#v\n"+
  57. " want: %#v\n"+
  58. " got: %#v", tc.input, tc.output, got)
  59. }
  60. }
  61. }