-
Notifications
You must be signed in to change notification settings - Fork 5
/
elisp_test.go
207 lines (184 loc) · 5.17 KB
/
elisp_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package emacsclient
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestAsChar(t *testing.T) {
assert.Equal(t, "?a", AsChar("a"))
assert.Equal(t, "?α", AsChar("α"))
assert.Equal(t, "?\\ ", AsChar(" "))
assert.Equal(t, "?\\\"", AsChar("\""))
assert.Equal(t, "?\\[", AsChar("["))
assert.Equal(t, "?\\]", AsChar("]"))
assert.Equal(t, "?\\)", AsChar(")"))
assert.Equal(t, "?\\(", AsChar("("))
assert.Equal(t, "?\\n", AsChar("\n"))
assert.Equal(t, "?\\x03", AsChar("\003"))
assert.Equal(t, "?\\x00", AsChar(""))
}
func TestAsBool(t *testing.T) {
assert.Equal(t, "t", AsBool(true))
assert.Equal(t, "nil", AsBool(false))
}
func TestAsString(t *testing.T) {
assert.Equal(t, `"string"`, AsString("string"))
assert.Equal(t, `"αλπηα"`, AsString("αλπηα"))
assert.Equal(t, `"\"hello\", he said"`, AsString("\"hello\", he said"))
assert.Equal(t, `"the end.\n"`, AsString("the end.\n"))
}
func TestAsStringList(t *testing.T) {
assert.Equal(t, `'("hello" "world")`, AsStringList([]string{"hello", "world"}))
assert.Equal(t, `'("hello")`, AsStringList([]string{"hello"}))
assert.Equal(t, `'()`, AsStringList([]string{}))
}
func TestReadBool(t *testing.T) {
responses := make(chan Response, 100)
responses <- Response{
Type: SuccessResponse,
Text: "nil"}
result, err := ReadBool(responses)
assert.Nil(t, err)
assert.Equal(t, false, result)
responses <- Response{
Type: SuccessResponse,
Text: "t"}
result, err = ReadBool(responses)
assert.Nil(t, err)
assert.Equal(t, true, result)
responses <- Response{
Type: SuccessResponse,
Text: "'()"}
result, err = ReadBool(responses)
assert.Nil(t, err)
assert.Equal(t, true, result)
responses <- Response{
Type: SuccessResponse,
Text: `"text"`}
result, err = ReadBool(responses)
assert.Nil(t, err)
assert.Equal(t, true, result)
}
func TestReadBoolWithError(t *testing.T) {
responses := make(chan Response, 100)
responses <- Response{
Type: ErrorResponse,
Text: "Something's wrong"}
_, err := ReadBool(responses)
assert.Error(t, err)
}
func TestReadBoolWithContinue(t *testing.T) {
responses := make(chan Response, 100)
responses <- Response{
Type: ContinueResponse,
Text: `more text"`}
_, err := ReadBool(responses)
assert.Error(t, err)
}
func TestReadSingleString(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"hello, world"`}
assert.Nil(t, ReadString(responses, out))
assert.Equal(t, "hello, world", out.String())
}
func TestReadStringWithEscapedCharacters(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"\"\a\b\t\n\v\f\r\e\s\d hello αλπηα"`}
assert.Nil(t, ReadString(responses, out))
assert.Equal(t, "\"\a\b\t\n\v\f\r\x1b \x7f hello αλπηα", out.String())
}
func TestReadContinuedString(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"hello, `}
responses <- Response{
Type: ContinueResponse,
Text: `world"`}
assert.Nil(t, ReadString(responses, out))
assert.Equal(t, "hello, world", out.String())
}
func TestReadContinuedStringWithinEscapeSequence(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"hello\`}
responses <- Response{
Type: ContinueResponse,
Text: `n"`}
assert.Nil(t, ReadString(responses, out))
assert.Equal(t, "hello\n", out.String())
}
func TestReadStringWithError(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: ErrorResponse,
Text: "something went wrong"}
assert.Error(t, ReadString(responses, out))
}
func TestReadStringContinuedWithError(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"hello\`}
responses <- Response{
Type: ErrorResponse,
Text: "something went wrong"}
assert.Error(t, ReadString(responses, out))
}
func TestReadStringNotFinished(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: `"hello\`}
close(responses)
assert.Error(t, ReadString(responses, out))
}
func TestReadStringNotAString(t *testing.T) {
responses := make(chan Response, 100)
out := &strings.Builder{}
responses <- Response{
Type: SuccessResponse,
Text: "123"}
close(responses)
assert.Error(t, ReadString(responses, out))
}
func TestExecuteTemplate(t *testing.T) {
type argType struct {
AString string
AStringList []string
AnEmptyList []string
ABool bool
AChar string
}
text, err := ExecuteTemplate(
&argType{
AString: "foo bar",
AStringList: []string{"foo", "bar"},
AnEmptyList: []string{},
ABool: true,
AChar: "c",
},
`(astring {{str .AString}})
(astringlist {{strList .AStringList}})
(anemptylist {{strList .AnEmptyList}})
(abool {{bool .ABool}})
(achar {{char .AChar}})`)
assert.Nil(t, err)
assert.Equal(t, `(astring "foo bar")
(astringlist '("foo" "bar"))
(anemptylist '())
(abool t)
(achar ?c)`, text)
}