github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/third_party/liner/line_test.go (about) 1 package liner 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "testing" 8 ) 9 10 func TestAppend(t *testing.T) { 11 var s State 12 s.AppendHistory("foo") 13 s.AppendHistory("bar") 14 15 var out bytes.Buffer 16 num, err := s.WriteHistory(&out) 17 if err != nil { 18 t.Fatal("Unexpected error writing history", err) 19 } 20 if num != 2 { 21 t.Fatalf("Expected 2 history entries, got %d", num) 22 } 23 24 s.AppendHistory("baz") 25 num, err = s.WriteHistory(&out) 26 if err != nil { 27 t.Fatal("Unexpected error writing history", err) 28 } 29 if num != 3 { 30 t.Fatalf("Expected 3 history entries, got %d", num) 31 } 32 33 s.AppendHistory("baz") 34 num, err = s.WriteHistory(&out) 35 if err != nil { 36 t.Fatal("Unexpected error writing history", err) 37 } 38 if num != 3 { 39 t.Fatalf("Expected 3 history entries after duplicate append, got %d", num) 40 } 41 42 s.AppendHistory("baz") 43 44 } 45 46 func TestHistory(t *testing.T) { 47 input := `foo 48 bar 49 baz 50 quux 51 dingle` 52 53 var s State 54 num, err := s.ReadHistory(strings.NewReader(input)) 55 if err != nil { 56 t.Fatal("Unexpected error reading history", err) 57 } 58 if num != 5 { 59 t.Fatal("Wrong number of history entries read") 60 } 61 62 var out bytes.Buffer 63 num, err = s.WriteHistory(&out) 64 if err != nil { 65 t.Fatal("Unexpected error writing history", err) 66 } 67 if num != 5 { 68 t.Fatal("Wrong number of history entries written") 69 } 70 if strings.TrimSpace(out.String()) != input { 71 t.Fatal("Round-trip failure") 72 } 73 74 // Test reading with a trailing newline present 75 var s2 State 76 num, err = s2.ReadHistory(&out) 77 if err != nil { 78 t.Fatal("Unexpected error reading history the 2nd time", err) 79 } 80 if num != 5 { 81 t.Fatal("Wrong number of history entries read the 2nd time") 82 } 83 84 num, err = s.ReadHistory(strings.NewReader(input + "\n\xff")) 85 if err == nil { 86 t.Fatal("Unexpected success reading corrupted history", err) 87 } 88 if num != 5 { 89 t.Fatal("Wrong number of history entries read the 3rd time") 90 } 91 } 92 93 func TestColumns(t *testing.T) { 94 list := []string{"foo", "food", "This entry is quite a bit longer than the typical entry"} 95 96 output := []struct { 97 width, columns, rows, maxWidth int 98 }{ 99 {80, 1, 3, len(list[2]) + 1}, 100 {120, 2, 2, len(list[2]) + 1}, 101 {800, 14, 1, 0}, 102 {8, 1, 3, 7}, 103 } 104 105 for i, o := range output { 106 col, row, max := calculateColumns(o.width, list) 107 if col != o.columns { 108 t.Fatalf("Wrong number of columns, %d != %d, in TestColumns %d\n", col, o.columns, i) 109 } 110 if row != o.rows { 111 t.Fatalf("Wrong number of rows, %d != %d, in TestColumns %d\n", row, o.rows, i) 112 } 113 if max != o.maxWidth { 114 t.Fatalf("Wrong column width, %d != %d, in TestColumns %d\n", max, o.maxWidth, i) 115 } 116 } 117 } 118 119 // This example demonstrates a way to retrieve the current 120 // history buffer without using a file. 121 func ExampleState_WriteHistory() { 122 var s State 123 s.AppendHistory("foo") 124 s.AppendHistory("bar") 125 126 buf := new(bytes.Buffer) 127 _, err := s.WriteHistory(buf) 128 if err == nil { 129 history := strings.Split(strings.TrimSpace(buf.String()), "\n") 130 for i, line := range history { 131 fmt.Println("History entry", i, ":", line) 132 } 133 } 134 // Output: 135 // History entry 0 : foo 136 // History entry 1 : bar 137 }