]> git.lizzy.rs Git - micro.git/blob - internal/buffer/buffer_test.go
Merge branch 'buffer-tests' of https://github.com/p-e-w/micro into buffer-unit-tests
[micro.git] / internal / buffer / buffer_test.go
1 package buffer
2
3 import (
4         "strings"
5         "testing"
6
7         testifyAssert "github.com/stretchr/testify/assert"
8         lua "github.com/yuin/gopher-lua"
9
10         ulua "github.com/zyedidia/micro/internal/lua"
11 )
12
13 type operation struct {
14         start Loc
15         end   Loc
16         text  []string
17 }
18
19 type asserter interface {
20         Equal(interface{}, interface{}, ...interface{}) bool
21         NotEqual(interface{}, interface{}, ...interface{}) bool
22 }
23
24 type noOpAsserter struct {
25 }
26
27 func (a *noOpAsserter) Equal(interface{}, interface{}, ...interface{}) bool {
28         return true
29 }
30
31 func (a *noOpAsserter) NotEqual(interface{}, interface{}, ...interface{}) bool {
32         return true
33 }
34
35 func init() {
36         ulua.L = lua.NewState()
37 }
38
39 func check(t *testing.T, before []string, operations []operation, after []string) {
40         var assert asserter
41         if t == nil {
42                 // Benchmark mode; don't perform assertions
43                 assert = &noOpAsserter{}
44         } else {
45                 assert = testifyAssert.New(t)
46         }
47
48         b := NewBufferFromString(strings.Join(before, "\n"), "", BTDefault)
49
50         assert.NotEqual(b.GetName(), "")
51         assert.Equal(b.ExternallyModified(), false)
52         assert.Equal(b.Modified(), false)
53         assert.Equal(b.NumCursors(), 1)
54
55         checkText := func(lines []string) {
56                 assert.Equal(b.Bytes(), []byte(strings.Join(lines, "\n")))
57                 assert.Equal(b.LinesNum(), len(lines))
58                 for i, s := range lines {
59                         assert.Equal(b.Line(i), s)
60                         assert.Equal(b.LineBytes(i), []byte(s))
61                 }
62         }
63
64         checkText(before)
65
66         var cursors []*Cursor
67
68         for _, op := range operations {
69                 cursor := NewCursor(b, op.start)
70                 cursor.SetSelectionStart(op.start)
71                 cursor.SetSelectionEnd(op.end)
72                 b.AddCursor(cursor)
73                 cursors = append(cursors, cursor)
74         }
75
76         assert.Equal(b.NumCursors(), 1+len(operations))
77
78         for i, op := range operations {
79                 cursor := cursors[i]
80                 cursor.DeleteSelection()
81                 b.Insert(cursor.Loc, strings.Join(op.text, "\n"))
82         }
83
84         checkText(after)
85
86         for _ = range operations {
87                 b.UndoOneEvent()
88                 b.UndoOneEvent()
89         }
90
91         checkText(before)
92
93         for i, op := range operations {
94                 cursor := cursors[i]
95                 assert.Equal(cursor.Loc, op.start)
96                 assert.Equal(cursor.CurSelection[0], op.start)
97                 assert.Equal(cursor.CurSelection[1], op.end)
98         }
99
100         for _ = range operations {
101                 b.RedoOneEvent()
102                 b.RedoOneEvent()
103         }
104
105         checkText(after)
106
107         b.Close()
108 }