-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtickscript-mode-tests.el
364 lines (323 loc) · 9.63 KB
/
tickscript-mode-tests.el
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
;;; tickscript-mode-tests.el --- Tests for tickscript-mode.el -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Marc Sherry
;; Homepage: https://github.com/msherry/tickscript-mode
;; Version: 0.1
;; Author: Marc Sherry <[email protected]>
;; Keywords: languages
;; Package-Requires: ((emacs "24.1"))
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Usage:
;; From command line:
;;
;; emacs -batch -L . -l ert -l tickscript-mode-tests.el -f ert-run-tests-batch-and-exit
;;; Commentary:
;; Contains ert tests for tickscript-mode.el. Based on
;; https://github.com/JuliaEditorSupport/julia-emacs/blob/master/julia-mode-tests.el
;;
;;; Code:
(require 'tickscript-mode)
(require 'ert)
(defmacro tickscript--should-indent (from to)
"Assert that we indent text FROM producing text TO in `tickscript-mode'."
`(with-temp-buffer
(let ((tickscript-indent-offset 4))
(tickscript-mode)
(insert ,from)
(indent-region (point-min) (point-max))
(should (equal
(buffer-substring-no-properties (point-min) (point-max))
,to)))))
(defmacro tickscript--should-font-lock (text pos-faces)
"Assert that the TEXT gets font-locked correctly at each position in POS-FACES."
`(with-temp-buffer
(tickscript-mode)
(insert ,text)
(when (fboundp #'font-lock-ensure)
(font-lock-ensure (point-min) (point-max)))
(with-no-warnings
(when (fboundp #'font-lock-flush)
(font-lock-flush)))
(font-lock-fontify-buffer)
(dolist (pair ,pos-faces)
(let ((pos (car pair))
(face (cdr pair)))
(should (eq face (get-text-property pos 'face)))))))
(defmacro tickscript--should-cleanup-dot (from to)
"Assert that we clean up the dot FROM into TO."
`(with-temp-buffer
(insert ,from)
(let ((cleaned (tickscript--cleanup-dot (tickscript--extract-dot-from-buffer))))
(should (equal cleaned ,to)))))
(defmacro tickscript--test-thing-at-point (text pos-things)
"Assert that, at each position in TEXT, items in POS-THINGS are correct."
`(with-temp-buffer
(tickscript-mode)
(insert ,text)
(dolist (pair ,pos-things)
(let ((pos (car pair))
(thing (cdr pair)))
(let ((fn (cond
((eq thing 'node) #'tickscript-node-at-point)
((eq thing 'chaining-method) #'tickscript-chaining-method-at-point)
((eq thing 'udf) #'tickscript-udf-at-point)
((eq thing 'property) #'tickscript-property-at-point)
((eq thing 'udf-param) #'tickscript-udf-param-at-point))))
(goto-char pos)
(should (funcall fn)))))))
(ert-deftest tickscript--test-indent-properties ()
"Properties should be indented under nodes."
(tickscript--should-indent
"
batch
|query(SQL)
.period(1d)
.every(1h)
.groupBy(*)
.align()
.fill('null')
.cluster('local')
"
"
batch
|query(SQL)
.period(1d)
.every(1h)
.groupBy(*)
.align()
.fill('null')
.cluster('local')
"))
(ert-deftest tickscript--test-indent-var-declaration ()
"Var declarations should be indented to 0."
(tickscript--should-indent
"
var day_median = day_batched
|median('duration')
.as('day_median')
"
"
var day_median = day_batched
|median('duration')
.as('day_median')
"))
(ert-deftest tickscript--test-indent-node-instance ()
"Previously-defined nodes should be indented to 0."
(tickscript--should-indent
"
day_batched
|median('duration')
.as('day_median')
"
"
day_batched
|median('duration')
.as('day_median')
"))
(ert-deftest tickscript--test-indent-udf ()
"User-defined functions should be indented similarly to chaining functions."
(tickscript--should-indent
"
var my_custom = other_thing
@my_udf('duration')
.as('day_udf')
"
"
var my_custom = other_thing
@my_udf('duration')
.as('day_udf')
"))
(ert-deftest tickscript--test-indent-within-parens ()
"Lines following an unclosed paren should indent to the column after the paren."
(tickscript--should-indent
"
hour_median
|join(day_median, week_median)
|eval(lambda: \"hour_median.value\" / \"day_median.value\",
lambda: \"hour_median.value\" / \"day_median.value\")
"
"
hour_median
|join(day_median, week_median)
|eval(lambda: \"hour_median.value\" / \"day_median.value\",
lambda: \"hour_median.value\" / \"day_median.value\")
"))
(ert-deftest tickscript--test-indent-comments ()
"Comments should be indented at the level of the line above
them, unless there is a blank line above, in which case they
should be indented to zero."
(tickscript--should-indent
"
// This is a definition
batch
|query(SQL)
.groupBy(*)
// .period(1h) // commented out
.window(1d)
// .fill('null')
// A new comment
"
"
// This is a definition
batch
|query(SQL)
.groupBy(*)
// .period(1h) // commented out
.window(1d)
// .fill('null')
// A new comment
"))
(ert-deftest tickscript--test-font-locking ()
"Test that font locking is applied correctly."
(tickscript--should-font-lock
"
var day_batched = batch
|query(SQL)
.period(1d)
"
'((2 . font-lock-keyword-face)
(6 . tickscript-variable)
(20 . tickscript-node)
(30 . tickscript-operator)
(51 . tickscript-property)))
(tickscript--should-font-lock
"
batch
|query('')
.groupBy(*)
|groupBy(*)
"
'((32 . tickscript-property)
(47 . tickscript-operator)
(48 . tickscript-node))
))
(ert-deftest tickscript--test-font-locking-udf ()
"Test that UDF-related font locking is correct."
(tickscript--should-font-lock
"
hour_batched
@ttest()
.field('duration')
.period(1w)
.alpha(0.001)
.prefix('week_')
"
'((19 . tickscript-udf) ;sigil
(20 . tickscript-udf) ;UDF name
(36 . nil) ;property sigil (.)
(37 . tickscript-udf-param) ;param with property name
(64 . tickscript-udf-param) ;param with property name
(84 . tickscript-udf-param) ;new param
(106 . tickscript-udf-param) ;new param
)))
(ert-deftest tickscript--test-basic-dot ()
"Test that we don't break valid DOT."
(tickscript--should-cleanup-dot
"
DOT:
digraph medians {
query3 -> median14;
query3 -> mean15;
query3 -> count16;
count16 -> join21;
}
" "digraph medians {
query3 -> median14;
query3 -> mean15;
query3 -> count16;
count16 -> join21;
}
"))
(ert-deftest tickscript--test-dot-needing-cleanup ()
"Test cleaning the broken DOT given by a running task."
(tickscript--should-cleanup-dot
"
DOT:
digraph medians {
graph [throughput=\"0.00 batches/s\"];
query3 [avg_exec_time_ns=\"0s\" batches_queried=\"0\" errors=\"0\" points_queried=\"0\" working_cardinality=\"0\" ];
query3 -> count16 [processed=\"0\"];
query3 -> mean15 [processed=\"0\"];
query3 -> median14 [processed=\"0\"];
count16 [avg_exec_time_ns=\"0s\" errors=\"0\" working_cardinality=\"0\" ];
count16 -> join21 [processed=\"0\"];
}
" "digraph medians {
graph [\"throughput=\\\"0.00 batches\\\/s\\\"\"];
query3 [\"avg_exec_time_ns=\\\"0s\\\" batches_queried=\\\"0\\\" errors=\\\"0\\\" points_queried=\\\"0\\\" working_cardinality=\\\"0\\\" \"];
query3 -> count16 [\"processed=\\\"0\\\"\"];
query3 -> mean15 [\"processed=\\\"0\\\"\"];
query3 -> median14 [\"processed=\\\"0\\\"\"];
count16 [\"avg_exec_time_ns=\\\"0s\\\" errors=\\\"0\\\" working_cardinality=\\\"0\\\" \"];
count16 -> join21 [\"processed=\\\"0\\\"\"];
}
"))
(ert-deftest tickscript--search-back-from-string ()
"Ensure that searching backwards from a string doesn't hang."
(let ((text "
var SQL = '''SELECT \"duration\"
FROM \"db\".\"retention\".\"series\"
WHERE \"result\" = 'great'
AND \"foo\" = 'bar'
'''
"))
(with-temp-buffer
(tickscript-mode)
(insert text)
(goto-char 50)
(should (eq (tickscript-last-node-pos) 'nil)))))
(ert-deftest tickscript--should-find-things-at-point ()
"Ensure that we recognize things at point correctly."
(tickscript--test-thing-at-point
"groupBy
.period
|groupBy
.groupBy
|cumulativeSum
@fff
.udfParam
"
'((1 . node)
(9 . property)
(10 . property)
(17 . node) ;sigil
(18 . node)
(26 . property) ;sigil
(27 . property)
(35 . chaining-method) ;sigil
(36 . chaining-method)
(50 . udf) ;sigil
(51 . udf)
(55 . udf-param)
(56 . udf-param)
)))
(ert-deftest tickscript--last-identifier ()
"Ensure that we recognize the parent object correctly."
(let ((text "
|groupBy
.align
@ttest
.my_param('g')
"))
(with-temp-buffer
(tickscript-mode)
(insert text)
(goto-char 8)
(should (eq (tickscript-last-node-pos t) 3))
(goto-char 9)
(should (eq (tickscript-last-node-pos t) 3))
(goto-char 24)
(should (eq (tickscript-last-udf-pos t) 19))
(goto-char 25)
(should (eq (tickscript-last-udf-pos t) 19)))))
(provide 'tickscript-mode-tests)
;;; tickscript-mode-tests.el ends here