]> git.lizzy.rs Git - micro.git/blob - internal/buffer/buffer_test.go
Support csharp-script syntax. (#1425)
[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(false, b.ExternallyModified())
52         assert.Equal(false, b.Modified())
53         assert.Equal(1, b.NumCursors())
54
55         checkText := func(lines []string) {
56                 assert.Equal([]byte(strings.Join(lines, "\n")), b.Bytes())
57                 assert.Equal(len(lines), b.LinesNum())
58                 for i, s := range lines {
59                         assert.Equal(s, b.Line(i))
60                         assert.Equal([]byte(s), b.LineBytes(i))
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(1+len(operations), b.NumCursors())
77
78         for i, op := range operations {
79                 cursor := cursors[i]
80                 b.SetCurCursor(cursor.Num)
81                 cursor.DeleteSelection()
82                 b.Insert(cursor.Loc, strings.Join(op.text, "\n"))
83         }
84
85         checkText(after)
86
87         // must have exactly two events per operation (delete and insert)
88         for range operations {
89                 b.UndoOneEvent()
90                 b.UndoOneEvent()
91         }
92
93         checkText(before)
94
95         for i, op := range operations {
96                 cursor := cursors[i]
97                 if op.start == op.end {
98                         assert.Equal(op.start, cursor.Loc)
99                 } else {
100                         assert.Equal(op.start, cursor.CurSelection[0])
101                         assert.Equal(op.end, cursor.CurSelection[1])
102                 }
103         }
104
105         for range operations {
106                 b.RedoOneEvent()
107                 b.RedoOneEvent()
108         }
109
110         checkText(after)
111
112         b.Close()
113 }