...

Source file src/github.com/mattn/go-colorable/colorable_test.go

Documentation: github.com/mattn/go-colorable

     1  package colorable
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  // checkEncoding checks that colorable is output encoding agnostic as long as
    11  // the encoding is a superset of ASCII. This implies that one byte not part of
    12  // an ANSI sequence must give exactly one byte in output
    13  func checkEncoding(t *testing.T, data []byte) {
    14  	// Send non-UTF8 data to colorable
    15  	b := bytes.NewBuffer(make([]byte, 0, 10))
    16  	if b.Len() != 0 {
    17  		t.FailNow()
    18  	}
    19  	// TODO move colorable wrapping outside the test
    20  	NewNonColorable(b).Write(data)
    21  	if b.Len() != len(data) {
    22  		t.Fatalf("%d bytes expected, got %d", len(data), b.Len())
    23  	}
    24  }
    25  
    26  func TestEncoding(t *testing.T) {
    27  	checkEncoding(t, []byte{})      // Empty
    28  	checkEncoding(t, []byte(`abc`)) // "abc"
    29  	checkEncoding(t, []byte(`é`))   // "é" in UTF-8
    30  	checkEncoding(t, []byte{233})   // 'é' in Latin-1
    31  }
    32  
    33  func TestNonColorable(t *testing.T) {
    34  	var buf bytes.Buffer
    35  	want := "hello"
    36  	NewNonColorable(&buf).Write([]byte("\x1b[0m" + want + "\x1b[2J"))
    37  	got := buf.String()
    38  	if got != "hello" {
    39  		t.Fatalf("want %q but %q", want, got)
    40  	}
    41  
    42  	buf.Reset()
    43  	NewNonColorable(&buf).Write([]byte("\x1b["))
    44  	got = buf.String()
    45  	if got != "" {
    46  		t.Fatalf("want %q but %q", "", got)
    47  	}
    48  }
    49  
    50  func TestNonColorableNil(t *testing.T) {
    51  	paniced := false
    52  	func() {
    53  		defer func() {
    54  			recover()
    55  			paniced = true
    56  		}()
    57  		NewNonColorable(nil)
    58  		NewColorable(nil)
    59  	}()
    60  
    61  	if !paniced {
    62  		t.Fatalf("should panic")
    63  	}
    64  }
    65  
    66  func TestNonColorableESC(t *testing.T) {
    67  	var b bytes.Buffer
    68  	NewNonColorable(&b).Write([]byte{0x1b})
    69  	if b.Len() > 0 {
    70  		t.Fatalf("0 bytes expected, got %d", b.Len())
    71  	}
    72  }
    73  
    74  func TestNonColorableBadESC(t *testing.T) {
    75  	var b bytes.Buffer
    76  	NewNonColorable(&b).Write([]byte{0x1b, 0x1b})
    77  	if b.Len() > 0 {
    78  		t.Fatalf("0 bytes expected, got %d", b.Len())
    79  	}
    80  }
    81  
    82  func TestColorable(t *testing.T) {
    83  	if runtime.GOOS == "windows" {
    84  		t.Skipf("skip this test on windows")
    85  	}
    86  	_, ok := NewColorableStdout().(*os.File)
    87  	if !ok {
    88  		t.Fatalf("should os.Stdout on UNIX")
    89  	}
    90  	_, ok = NewColorableStderr().(*os.File)
    91  	if !ok {
    92  		t.Fatalf("should os.Stdout on UNIX")
    93  	}
    94  	_, ok = NewColorable(os.Stdout).(*os.File)
    95  	if !ok {
    96  		t.Fatalf("should os.Stdout on UNIX")
    97  	}
    98  }
    99  

View as plain text