github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/virtualterm/write_test.go (about)

     1  package virtualterm_test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/ansi/codes"
    10  	"github.com/lmorg/murex/utils/virtualterm"
    11  )
    12  
    13  func TestWriteBasic(t *testing.T) {
    14  	count.Tests(t, 1)
    15  
    16  	term := virtualterm.NewTerminal(5, 3)
    17  	test := "Hello world!"
    18  	exp := "Hello\n worl\nd!   \n"
    19  
    20  	term.Write([]rune(test))
    21  	act := term.Export()
    22  
    23  	if exp != act {
    24  		t.Error("Expected output does not match actual output")
    25  		t.Logf("  Expected: '%s'", exp)
    26  		t.Logf("  Actual:   '%s'", act)
    27  		t.Logf("  exp bytes: %v", []byte(exp))
    28  		t.Logf("  act bytes: %v", []byte(act))
    29  	}
    30  }
    31  
    32  func TestWriteNewLineCrLf1(t *testing.T) {
    33  	count.Tests(t, 1)
    34  
    35  	term := virtualterm.NewTerminal(5, 3)
    36  	term.MakeRaw()
    37  	test := "1\n2\n3\n4\n5"
    38  	exp := "   4 \n    5\n     \n"
    39  
    40  	term.Write([]rune(test))
    41  	act := term.Export()
    42  
    43  	if exp != act {
    44  		t.Error("Expected output does not match actual output")
    45  		t.Logf("  Expected: '%s'", exp)
    46  		t.Logf("  Actual:   '%s'", act)
    47  		t.Logf("  exp bytes: %v", []byte(exp))
    48  		t.Logf("  act bytes: %v", []byte(act))
    49  	}
    50  }
    51  
    52  func TestWriteNewLineCrLf2(t *testing.T) {
    53  	count.Tests(t, 1)
    54  
    55  	term := virtualterm.NewTerminal(5, 3)
    56  	test := "1\r\n2\r\n3\r\n4\r\n5"
    57  	exp := "3    \n4    \n5    \n"
    58  
    59  	term.Write([]rune(test))
    60  	act := term.Export()
    61  
    62  	if exp != act {
    63  		t.Error("Expected output does not match actual output")
    64  		t.Logf("  Expected: '%s'", exp)
    65  		t.Logf("  Actual:   '%s'", act)
    66  		t.Logf("  exp bytes: %v", []byte(exp))
    67  		t.Logf("  act bytes: %v", []byte(act))
    68  	}
    69  }
    70  
    71  func TestWriteNewLineLf(t *testing.T) {
    72  	count.Tests(t, 1)
    73  
    74  	term := virtualterm.NewTerminal(5, 3)
    75  	test := "1\n2\n3\n4\n5"
    76  	exp := "3    \n4    \n5    \n"
    77  
    78  	term.Write([]rune(test))
    79  	act := term.Export()
    80  
    81  	if exp != act {
    82  		t.Error("Expected output does not match actual output")
    83  		t.Logf("  Expected: '%s'", exp)
    84  		t.Logf("  Actual:   '%s'", act)
    85  		t.Logf("  exp bytes: %v", []byte(exp))
    86  		t.Logf("  act bytes: %v", []byte(act))
    87  	}
    88  }
    89  
    90  func TestWriteTwice(t *testing.T) {
    91  	count.Tests(t, 1)
    92  
    93  	term := virtualterm.NewTerminal(5, 3)
    94  	w1 := "foo\n"
    95  	w2 := "bar\n"
    96  
    97  	term.Write([]rune(w1))
    98  	term.Write([]rune(w2))
    99  
   100  	exp := "foo  \nbar  \n     \n"
   101  	act := term.Export()
   102  
   103  	if exp != act {
   104  		t.Error("Expected output does not match actual output")
   105  		t.Logf("  Expected: '%s'", exp)
   106  		t.Logf("  Actual:   '%s'", act)
   107  		t.Logf("  exp bytes: %v", []byte(exp))
   108  		t.Logf("  act bytes: %v", []byte(act))
   109  	}
   110  }
   111  
   112  func TestWriteSgrBasicExportHtml(t *testing.T) {
   113  	count.Tests(t, 1)
   114  
   115  	term := virtualterm.NewTerminal(120, 1)
   116  	test := fmt.Sprintf("Normal%sBold%sUnderscore%sReset", codes.Bold, codes.Underscore, codes.Reset)
   117  	exp1 := `<span class="">Normal</span><span class="sgr-bold">Bold</span><span class="sgr-bold sgr-underscore">Underscore</span><span class="">Reset</span><span class="">                                                                                               
   118  </span>`
   119  	exp2 := `<span class="">Normal</span><span class="sgr-bold">Bold</span><span class="sgr-underscore sgr-bold">Underscore</span><span class="">Reset</span><span class="">                                                                                               
   120  </span>`
   121  
   122  	term.Write([]rune(test))
   123  	act := strings.TrimSpace(term.ExportHtml())
   124  
   125  	if exp1 != act && exp2 != act {
   126  		t.Error("Expected output does not match actual output")
   127  		t.Logf("  Expected: '%s'", exp1)
   128  		t.Logf("  Actual:   '%s'", act)
   129  		//t.Logf("  exp bytes: %v", []byte(exp))
   130  		//t.Logf("  act bytes: %v", []byte(act))
   131  	}
   132  }