github.com/elves/elvish@v0.15.0/pkg/cli/term/buffer_builder_test.go (about) 1 package term 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/elves/elvish/pkg/ui" 8 ) 9 10 var bufferBuilderWritesTests = []struct { 11 bb *BufferBuilder 12 text string 13 style string 14 want *Buffer 15 }{ 16 // Writing nothing. 17 {NewBufferBuilder(10), "", "", &Buffer{Width: 10, Lines: Lines{Line{}}}}, 18 // Writing a single rune. 19 {NewBufferBuilder(10), "a", "1", 20 &Buffer{Width: 10, Lines: Lines{Line{Cell{"a", "1"}}}}}, 21 // Writing control character. 22 {NewBufferBuilder(10), "\033", "", 23 &Buffer{Width: 10, Lines: Lines{Line{Cell{"^[", "7"}}}}}, 24 // Writing styled control character. 25 {NewBufferBuilder(10), "a\033b", "1", 26 &Buffer{Width: 10, Lines: Lines{Line{ 27 Cell{"a", "1"}, 28 Cell{"^[", "1;7"}, 29 Cell{"b", "1"}}}}}, 30 // Writing text containing a newline. 31 {NewBufferBuilder(10), "a\nb", "1", 32 &Buffer{Width: 10, Lines: Lines{ 33 Line{Cell{"a", "1"}}, Line{Cell{"b", "1"}}}}}, 34 // Writing text containing a newline when there is indent. 35 {NewBufferBuilder(10).SetIndent(2), "a\nb", "1", 36 &Buffer{Width: 10, Lines: Lines{ 37 Line{Cell{"a", "1"}}, 38 Line{Cell{" ", ""}, Cell{" ", ""}, Cell{"b", "1"}}, 39 }}}, 40 // Writing long text that triggers wrapping. 41 {NewBufferBuilder(4), "aaaab", "1", 42 &Buffer{Width: 4, Lines: Lines{ 43 Line{Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}}, 44 Line{Cell{"b", "1"}}}}}, 45 // Writing long text that triggers wrapping when there is indent. 46 {NewBufferBuilder(4).SetIndent(2), "aaaab", "1", 47 &Buffer{Width: 4, Lines: Lines{ 48 Line{Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}}, 49 Line{Cell{" ", ""}, Cell{" ", ""}, Cell{"b", "1"}}}}}, 50 // Writing long text that triggers eager wrapping. 51 {NewBufferBuilder(4).SetIndent(2).SetEagerWrap(true), "aaaa", "1", 52 &Buffer{Width: 4, Lines: Lines{ 53 Line{Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}, Cell{"a", "1"}}, 54 Line{Cell{" ", ""}, Cell{" ", ""}}}}}, 55 } 56 57 // TestBufferWrites tests BufferBuilder.Writes by calling Writes on a 58 // BufferBuilder and see if the built Buffer matches what is expected. 59 func TestBufferBuilderWrites(t *testing.T) { 60 for _, test := range bufferBuilderWritesTests { 61 bb := test.bb 62 bb.WriteStringSGR(test.text, test.style) 63 buf := bb.Buffer() 64 if !reflect.DeepEqual(buf, test.want) { 65 t.Errorf("buf.writes(%q, %q) makes it %v, want %v", 66 test.text, test.style, buf, test.want) 67 } 68 } 69 } 70 71 var styles = ui.RuneStylesheet{ 72 '-': ui.Underlined, 73 } 74 75 var bufferBuilderTests = []struct { 76 name string 77 builder *BufferBuilder 78 wantBuf *Buffer 79 }{ 80 { 81 "MarkLines", 82 NewBufferBuilder(10).MarkLines( 83 "foo ", styles, 84 "-- ", DotHere, "\n", 85 "", 86 "bar", 87 ), 88 &Buffer{Width: 10, Dot: Pos{0, 4}, Lines: Lines{ 89 Line{Cell{"f", "4"}, Cell{"o", "4"}, Cell{"o", ""}, Cell{" ", ""}}, 90 Line{Cell{"b", ""}, Cell{"a", ""}, Cell{"r", ""}}, 91 }}, 92 }, 93 } 94 95 func TestBufferBuilder(t *testing.T) { 96 for _, test := range bufferBuilderTests { 97 t.Run(test.name, func(t *testing.T) { 98 buf := test.builder.Buffer() 99 if !reflect.DeepEqual(buf, test.wantBuf) { 100 t.Errorf("Got buf %v, want %v", buf, test.wantBuf) 101 } 102 }) 103 } 104 }