colorable_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package colorable
  2. import (
  3. "bytes"
  4. "os"
  5. "runtime"
  6. "testing"
  7. )
  8. // checkEncoding checks that colorable is output encoding agnostic as long as
  9. // the encoding is a superset of ASCII. This implies that one byte not part of
  10. // an ANSI sequence must give exactly one byte in output
  11. func checkEncoding(t *testing.T, data []byte) {
  12. // Send non-UTF8 data to colorable
  13. b := bytes.NewBuffer(make([]byte, 0, 10))
  14. if b.Len() != 0 {
  15. t.FailNow()
  16. }
  17. // TODO move colorable wrapping outside the test
  18. c := NewNonColorable(b)
  19. c.Write(data)
  20. if b.Len() != len(data) {
  21. t.Fatalf("%d bytes expected, got %d", len(data), b.Len())
  22. }
  23. }
  24. func TestEncoding(t *testing.T) {
  25. checkEncoding(t, []byte{}) // Empty
  26. checkEncoding(t, []byte(`abc`)) // "abc"
  27. checkEncoding(t, []byte(`é`)) // "é" in UTF-8
  28. checkEncoding(t, []byte{233}) // 'é' in Latin-1
  29. }
  30. func TestColorable(t *testing.T) {
  31. if runtime.GOOS == "windows" {
  32. t.Skipf("skip this test on windows")
  33. }
  34. _, ok := NewColorableStdout().(*os.File)
  35. if !ok {
  36. t.Fatalf("should os.Stdout on UNIX")
  37. }
  38. _, ok = NewColorableStderr().(*os.File)
  39. if !ok {
  40. t.Fatalf("should os.Stdout on UNIX")
  41. }
  42. _, ok = NewColorable(os.Stdout).(*os.File)
  43. if !ok {
  44. t.Fatalf("should os.Stdout on UNIX")
  45. }
  46. }