...

Source file src/github.com/Azure/go-ansiterm/parser_test.go

Documentation: github.com/Azure/go-ansiterm

     1  package ansiterm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestStateTransitions(t *testing.T) {
     9  	stateTransitionHelper(t, "CsiEntry", "Ground", alphabetics)
    10  	stateTransitionHelper(t, "CsiEntry", "CsiParam", csiCollectables)
    11  	stateTransitionHelper(t, "Escape", "CsiEntry", []byte{ANSI_ESCAPE_SECONDARY})
    12  	stateTransitionHelper(t, "Escape", "OscString", []byte{0x5D})
    13  	stateTransitionHelper(t, "Escape", "Ground", escapeToGroundBytes)
    14  	stateTransitionHelper(t, "Escape", "EscapeIntermediate", intermeds)
    15  	stateTransitionHelper(t, "EscapeIntermediate", "EscapeIntermediate", intermeds)
    16  	stateTransitionHelper(t, "EscapeIntermediate", "EscapeIntermediate", executors)
    17  	stateTransitionHelper(t, "EscapeIntermediate", "Ground", escapeIntermediateToGroundBytes)
    18  	stateTransitionHelper(t, "OscString", "Ground", []byte{ANSI_BEL})
    19  	stateTransitionHelper(t, "OscString", "Ground", []byte{0x5C})
    20  	stateTransitionHelper(t, "Ground", "Ground", executors)
    21  }
    22  
    23  func TestAnyToX(t *testing.T) {
    24  	anyToXHelper(t, []byte{ANSI_ESCAPE_PRIMARY}, "Escape")
    25  	anyToXHelper(t, []byte{DCS_ENTRY}, "DcsEntry")
    26  	anyToXHelper(t, []byte{OSC_STRING}, "OscString")
    27  	anyToXHelper(t, []byte{CSI_ENTRY}, "CsiEntry")
    28  	anyToXHelper(t, toGroundBytes, "Ground")
    29  }
    30  
    31  func TestCollectCsiParams(t *testing.T) {
    32  	parser, _ := createTestParser("CsiEntry")
    33  	parser.Parse(csiCollectables)
    34  
    35  	buffer := parser.context.paramBuffer
    36  	bufferCount := len(buffer)
    37  
    38  	if bufferCount != len(csiCollectables) {
    39  		t.Errorf("Buffer:    %v", buffer)
    40  		t.Errorf("CsiParams: %v", csiCollectables)
    41  		t.Errorf("Buffer count failure: %d != %d", bufferCount, len(csiParams))
    42  		return
    43  	}
    44  
    45  	for i, v := range csiCollectables {
    46  		if v != buffer[i] {
    47  			t.Errorf("Buffer:    %v", buffer)
    48  			t.Errorf("CsiParams: %v", csiParams)
    49  			t.Errorf("Mismatch at buffer[%d] = %d", i, buffer[i])
    50  		}
    51  	}
    52  }
    53  
    54  func TestParseParams(t *testing.T) {
    55  	parseParamsHelper(t, []byte{}, []string{})
    56  	parseParamsHelper(t, []byte{';'}, []string{})
    57  	parseParamsHelper(t, []byte{';', ';'}, []string{})
    58  	parseParamsHelper(t, []byte{'7'}, []string{"7"})
    59  	parseParamsHelper(t, []byte{'7', ';'}, []string{"7"})
    60  	parseParamsHelper(t, []byte{'7', ';', ';'}, []string{"7"})
    61  	parseParamsHelper(t, []byte{'7', ';', ';', '8'}, []string{"7", "8"})
    62  	parseParamsHelper(t, []byte{'7', ';', '8', ';'}, []string{"7", "8"})
    63  	parseParamsHelper(t, []byte{'7', ';', ';', '8', ';', ';'}, []string{"7", "8"})
    64  	parseParamsHelper(t, []byte{'7', '8'}, []string{"78"})
    65  	parseParamsHelper(t, []byte{'7', '8', ';'}, []string{"78"})
    66  	parseParamsHelper(t, []byte{'7', '8', ';', '9', '0'}, []string{"78", "90"})
    67  	parseParamsHelper(t, []byte{'7', '8', ';', ';', '9', '0'}, []string{"78", "90"})
    68  	parseParamsHelper(t, []byte{'7', '8', ';', '9', '0', ';'}, []string{"78", "90"})
    69  	parseParamsHelper(t, []byte{'7', '8', ';', '9', '0', ';', ';'}, []string{"78", "90"})
    70  }
    71  
    72  func TestCursor(t *testing.T) {
    73  	cursorSingleParamHelper(t, 'A', "CUU")
    74  	cursorSingleParamHelper(t, 'B', "CUD")
    75  	cursorSingleParamHelper(t, 'C', "CUF")
    76  	cursorSingleParamHelper(t, 'D', "CUB")
    77  	cursorSingleParamHelper(t, 'E', "CNL")
    78  	cursorSingleParamHelper(t, 'F', "CPL")
    79  	cursorSingleParamHelper(t, 'G', "CHA")
    80  	cursorTwoParamHelper(t, 'H', "CUP")
    81  	cursorTwoParamHelper(t, 'f', "HVP")
    82  	funcCallParamHelper(t, []byte{'?', '2', '5', 'h'}, "CsiEntry", "Ground", []string{"DECTCEM([true])"})
    83  	funcCallParamHelper(t, []byte{'?', '2', '5', 'l'}, "CsiEntry", "Ground", []string{"DECTCEM([false])"})
    84  }
    85  
    86  func TestErase(t *testing.T) {
    87  	// Erase in Display
    88  	eraseHelper(t, 'J', "ED")
    89  
    90  	// Erase in Line
    91  	eraseHelper(t, 'K', "EL")
    92  }
    93  
    94  func TestSelectGraphicRendition(t *testing.T) {
    95  	funcCallParamHelper(t, []byte{'m'}, "CsiEntry", "Ground", []string{"SGR([0])"})
    96  	funcCallParamHelper(t, []byte{'0', 'm'}, "CsiEntry", "Ground", []string{"SGR([0])"})
    97  	funcCallParamHelper(t, []byte{'0', ';', '1', 'm'}, "CsiEntry", "Ground", []string{"SGR([0 1])"})
    98  	funcCallParamHelper(t, []byte{'0', ';', '1', ';', '2', 'm'}, "CsiEntry", "Ground", []string{"SGR([0 1 2])"})
    99  }
   100  
   101  func TestScroll(t *testing.T) {
   102  	scrollHelper(t, 'S', "SU")
   103  	scrollHelper(t, 'T', "SD")
   104  }
   105  
   106  func TestPrint(t *testing.T) {
   107  	parser, evtHandler := createTestParser("Ground")
   108  	parser.Parse(printables)
   109  	validateState(t, parser.currState, "Ground")
   110  
   111  	for i, v := range printables {
   112  		expectedCall := fmt.Sprintf("Print([%s])", string(v))
   113  		actualCall := evtHandler.FunctionCalls[i]
   114  		if actualCall != expectedCall {
   115  			t.Errorf("Actual != Expected: %v != %v at %d", actualCall, expectedCall, i)
   116  		}
   117  	}
   118  }
   119  
   120  func TestClear(t *testing.T) {
   121  	p, _ := createTestParser("Ground")
   122  	fillContext(p.context)
   123  	p.clear()
   124  	validateEmptyContext(t, p.context)
   125  }
   126  
   127  func TestClearOnStateChange(t *testing.T) {
   128  	clearOnStateChangeHelper(t, "Ground", "Escape", []byte{ANSI_ESCAPE_PRIMARY})
   129  	clearOnStateChangeHelper(t, "Ground", "CsiEntry", []byte{CSI_ENTRY})
   130  }
   131  
   132  func TestC0(t *testing.T) {
   133  	expectedCall := "Execute([" + string(rune(ANSI_LINE_FEED)) + "])"
   134  	c0Helper(t, []byte{ANSI_LINE_FEED}, "Ground", []string{expectedCall})
   135  	expectedCall = "Execute([" + string(rune(ANSI_CARRIAGE_RETURN)) + "])"
   136  	c0Helper(t, []byte{ANSI_CARRIAGE_RETURN}, "Ground", []string{expectedCall})
   137  }
   138  
   139  func TestEscDispatch(t *testing.T) {
   140  	funcCallParamHelper(t, []byte{'M'}, "Escape", "Ground", []string{"RI([])"})
   141  }
   142  

View as plain text