github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/drivers/cga/ansi_test.go (about)

     1  package cga
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestAnsi(t *testing.T) {
     9  	var cases = []struct {
    10  		str    string
    11  		action byte
    12  		params []string
    13  		err    error
    14  	}{
    15  		{
    16  			str:    "\x1b[12;24G",
    17  			action: 'G',
    18  			params: []string{"12", "24"},
    19  		},
    20  		{
    21  			str:    "\x1b[12;;24G",
    22  			action: 'G',
    23  			params: []string{"12", "", "24"},
    24  		},
    25  		{
    26  			str:    "\x1b[G",
    27  			action: 'G',
    28  			params: []string{},
    29  		},
    30  		{
    31  			str: "X\x1b[G",
    32  			err: errNormalChar,
    33  		},
    34  		{
    35  			str: "\x1bX[G",
    36  			err: errInvalidChar,
    37  		},
    38  		{
    39  			str: "\x1b[\x00G",
    40  			err: errInvalidChar,
    41  		},
    42  		{
    43  			str: "\x1b[12\x00",
    44  			err: errInvalidChar,
    45  		},
    46  	}
    47  
    48  	p := ansiParser{}
    49  	for _, test := range cases {
    50  		p.Reset()
    51  	runcase:
    52  		for i := range test.str {
    53  			err := p.step(test.str[i])
    54  			if err == nil {
    55  				continue
    56  			}
    57  			if err != errCSIDone {
    58  				if err == test.err {
    59  					break runcase
    60  				} else {
    61  					t.Fatalf("%q[%d] expect %v got %v", test.str, i, test.err, err)
    62  				}
    63  			}
    64  			if test.action != p.Action() {
    65  				t.Fatalf("%q[%d] expect %v got %v", test.str, i, test.action, p.Action())
    66  			}
    67  			if !reflect.DeepEqual(test.params, p.Params()) {
    68  				t.Fatalf("%q[%d] expect %q got %q", test.str, i, test.params, p.Params())
    69  			}
    70  		}
    71  	}
    72  }