github.com/hauerwu/docker@v1.8.0-rc1/pkg/ansiescape/split_test.go (about)

     1  package ansiescape
     2  
     3  import (
     4  	"bufio"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestSplit(t *testing.T) {
    10  	lines := []string{
    11  		"test line 1",
    12  		"another test line",
    13  		"some test line",
    14  		"line with non-cursor moving sequence \x1b[1T", // Scroll Down
    15  		"line with \x1b[31;1mcolor\x1b[0m then reset",  // "color" in Bold Red
    16  		"cursor forward \x1b[1C and backward \x1b[1D",
    17  		"invalid sequence \x1babcd",
    18  		"",
    19  		"after empty",
    20  	}
    21  	splitSequences := []string{
    22  		"\x1b[1A",   // Cursor up
    23  		"\x1b[1B",   // Cursor down
    24  		"\x1b[1E",   // Cursor next line
    25  		"\x1b[1F",   // Cursor previous line
    26  		"\x1b[1;1H", // Move cursor to position
    27  		"\x1b[1;1h", // Move cursor to position
    28  		"\n",
    29  		"\r\n",
    30  		"\n\r",
    31  		"\x1b[1A\r",
    32  		"\r\x1b[1A",
    33  	}
    34  
    35  	for _, sequence := range splitSequences {
    36  		scanner := bufio.NewScanner(strings.NewReader(strings.Join(lines, sequence)))
    37  		scanner.Split(ScanANSILines)
    38  		i := 0
    39  		for scanner.Scan() {
    40  			if i >= len(lines) {
    41  				t.Fatalf("Too many scanned lines")
    42  			}
    43  			scanned := scanner.Text()
    44  			if scanned != lines[i] {
    45  				t.Fatalf("Wrong line scanned with sequence %q\n\tExpected: %q\n\tActual:   %q", sequence, lines[i], scanned)
    46  			}
    47  			i++
    48  		}
    49  		if i < len(lines) {
    50  			t.Errorf("Wrong number of lines for sequence %q: %d, expected %d", sequence, i, len(lines))
    51  		}
    52  	}
    53  }