line_test.go 3.2 KB

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