-
Notifications
You must be signed in to change notification settings - Fork 3
/
bufferobserver_test.go
141 lines (126 loc) · 3.98 KB
/
bufferobserver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2014 Fredrik Ehnbom
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package text
import (
"reflect"
"testing"
)
type (
observerData struct {
buf Buffer
r Region
d []rune
}
dummyObserver struct {
badlyBehaved bool
inserted observerData
erased observerData
}
)
func (do *dummyObserver) Inserted(b Buffer, r Region, d []rune) {
if do.badlyBehaved {
if err := b.Erase(0, 1); err == nil {
panic("Expected an error here")
}
if err := b.Insert(0, "test"); err == nil {
panic("Expected an error here")
}
if err := b.InsertR(0, []rune("test")); err == nil {
panic("Expected an error here")
}
}
do.inserted = observerData{b, r, d}
}
func (do *dummyObserver) Erased(b Buffer, r Region, d []rune) {
if do.badlyBehaved {
if err := b.Erase(0, 1); err == nil {
panic("Expected an error here")
}
if err := b.Insert(0, "test"); err == nil {
panic("Expected an error here")
}
if err := b.InsertR(0, []rune("test")); err == nil {
panic("Expected an error here")
}
}
do.erased = observerData{b, r, d}
}
func TestBufferObserver(t *testing.T) {
var b = NewBuffer()
defer b.Close()
do1 := &dummyObserver{}
do2 := &dummyObserver{}
// Adding
if err := b.AddObserver(do1); err != nil {
t.Fatal(err)
}
if err := b.AddObserver(do2); err != nil {
t.Fatal(err)
}
// Re-adding
if err := b.AddObserver(do1); err == nil {
t.Fatal("Expected an error but didn't get one!")
}
if err := b.AddObserver(do2); err == nil {
t.Fatal("Expected an error but didn't get one!")
}
// Removing
if err := b.RemoveObserver(do1); err != nil {
t.Fatal(err)
}
// Re-removing
if err := b.RemoveObserver(do1); err == nil {
t.Fatal("Expected an error but didn't get one!")
}
// Ok, now that we've got that over with, lets try the actual
// callbacks
if err := b.AddObserver(do1); err != nil {
t.Fatal(err)
}
data := []rune("hello world")
exp := observerData{b, Region{0, len(data)}, data}
b.InsertR(exp.r.Begin(), exp.d)
if !reflect.DeepEqual(do1, do2) {
t.Errorf("do1's and do2's data aren't equal:\n%+v\n%+v", do1, do2)
} else if !reflect.DeepEqual(do1.inserted, exp) {
t.Errorf("do1's and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
}
data = []rune("llo w")
exp = observerData{b, Region{2, 2 + len(data)}, data}
b.Erase(exp.r.Begin(), exp.r.Size())
if !reflect.DeepEqual(do1, do2) {
t.Errorf("do1's and do2's data aren't equal:\n%+v\n%+v", do1, do2)
} else if !reflect.DeepEqual(do1.erased, exp) {
t.Errorf("do1's and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
} else if sub := b.Substr(Region{0, b.Size()}); sub != "heorld" {
t.Errorf("Unexpected buffer contents: %s", sub)
}
// Just to make sure all the badly behaved operations are all nops
do1.inserted.buf = nil
do1.badlyBehaved = true
b.Insert(exp.r.Begin(), string(exp.d))
do1.badlyBehaved = false
if !reflect.DeepEqual(do1, do2) {
t.Errorf("do1's and do2's data aren't equal:\n%+v\n%+v", do1, do2)
} else if !reflect.DeepEqual(do1.inserted, exp) {
t.Errorf("do1's inserted and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
} else if !reflect.DeepEqual(do1.erased, exp) {
t.Errorf("do1's erased and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
} else if sub := b.Substr(Region{0, b.Size()}); sub != "hello world" {
t.Errorf("Unexpected buffer contents: %s", sub)
}
do1.erased.buf = nil
do1.badlyBehaved = true
b.Erase(exp.r.Begin(), exp.r.Size())
do1.badlyBehaved = false
if !reflect.DeepEqual(do1, do2) {
t.Errorf("do1's and do2's data aren't equal:\n%+v\n%+v", do1, do2)
} else if !reflect.DeepEqual(do1.inserted, exp) {
t.Errorf("do1's inserted and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
} else if !reflect.DeepEqual(do1.erased, exp) {
t.Errorf("do1's erased and exp's data aren't equal:\n%+v\n%+v", do1.inserted, exp)
} else if sub := b.Substr(Region{0, b.Size()}); sub != "heorld" {
t.Errorf("Unexpected buffer contents: %s", sub)
}
}