| 8 | ) |
| 9 | |
| 10 | func TestLineBuffer(t *testing.T) { |
| 11 | t.Run("appends lines to the buffer", func(t *testing.T) { |
| 12 | var lb LineBuffer |
| 13 | |
| 14 | lb.Append("hello") |
| 15 | |
| 16 | var buf bytes.Buffer |
| 17 | |
| 18 | n, err := lb.WriteTo(&buf) |
| 19 | assert.NoError(t, err) |
| 20 | |
| 21 | assert.Equal(t, int64(5), n) |
| 22 | }) |
| 23 | |
| 24 | t.Run("wraps around automatically", func(t *testing.T) { |
| 25 | var lb LineBuffer |
| 26 | |
| 27 | lb.Size = 3 |
| 28 | |
| 29 | lb.Append("hello1") |
| 30 | lb.Append("hello2") |
| 31 | lb.Append("hello3") |
| 32 | lb.Append("hello4") |
| 33 | |
| 34 | var lines []string |
| 35 | |
| 36 | lb.Do(func(x string) error { |
| 37 | lines = append(lines, x) |
| 38 | return nil |
| 39 | }) |
| 40 | |
| 41 | assert.Equal(t, 3, len(lb.lines)) |
| 42 | |
| 43 | assert.Equal(t, "hello2", lines[0]) |
| 44 | assert.Equal(t, "hello3", lines[1]) |
| 45 | assert.Equal(t, "hello4", lines[2]) |
| 46 | }) |
| 47 | |
| 48 | t.Run("wraps around automatically multiple times", func(t *testing.T) { |
| 49 | var lb LineBuffer |
| 50 | |
| 51 | lb.Size = 3 |
| 52 | |
| 53 | lb.Append("hello1") |
| 54 | lb.Append("hello2") |
| 55 | lb.Append("hello3") |
| 56 | lb.Append("hello4") |
| 57 | lb.Append("hello5") |
| 58 | lb.Append("hello6") |
| 59 | lb.Append("hello7") |
| 60 | |
| 61 | var lines []string |
| 62 | |
| 63 | lb.Do(func(x string) error { |
| 64 | lines = append(lines, x) |
| 65 | return nil |
| 66 | }) |
| 67 | |