-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbml-mode-tests.el
288 lines (270 loc) · 12.5 KB
/
dbml-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
;;; dbml-mode-tests.el -- tests for dbml-mode
;;; Code:
(require 'ert)
(require 'dbml-mode)
(require 'font-lock)
(setq ert-quiet t)
(defsubst dbml-mode-in-ert ()
"Because something is turned off in ERT."
(dbml-mode)
(font-lock-mode 1)
(if (< emacs-major-version 25)
(font-lock-fontify-buffer)
(font-lock-ensure)))
(defsubst dbml-mode-test-file (path)
"Check for font properties based on marks in file located in PATH."
(let (to-highlight highlighted work-buff properties inside (last-pos 0))
(with-temp-buffer
(setq work-buff (current-buffer))
(with-temp-buffer
(let ((coding-system-for-read 'utf-8))
(insert-file-contents (format "test-files/%s" path)))
(should (eq (point) (point-min)))
(let (collect track copy line start end)
(while (not (eobp))
(let ((char (char-to-string (char-after))))
(delete-char 1)
(cond ((and (not line) (string= char ">")) (setq collect t))
(collect
(cond
((string= char "\\") (push "\n" line))
((string= char "\n")
(setq collect nil)
(with-current-buffer work-buff
(insert (string-join (reverse line) "")))
(setq line nil))
(t (push char line))))
((and (not collect) (string= char "#")) (setq track t))
(track
(cond ((string= char "^")
(setq start last-pos)
(setq inside t)
(push start properties)
(setq last-pos (1+ last-pos)))
((string= char "-")
;; inside
(setq last-pos (1+ last-pos)))
((string= char "~")
;; outside, whitespace or noise between highlights
(setq last-pos (1+ last-pos)))
((string= char "$")
(setq end last-pos)
(setq inside nil)
(push end properties))
((string= char "|")
(setq track nil)
(setq copy t))
((string= char "\n")
(setq track nil))))
(copy
(if (not (string= char "\n"))
(push char line)
(setq copy nil)
(push (intern (string-join (reverse line) ""))
properties)
(setq line nil)))
(t (setq last-pos (1+ last-pos))))))))
(setq to-highlight
(buffer-substring-no-properties (point-min) (point-max)))
(goto-char (point-min))
(dbml-mode-in-ert)
(setq highlighted (format "%S" (buffer-string))))
(let ((real (if properties (substring highlighted 1) highlighted))
(expected
(if properties
(format
"%s" `(,(format "%S" to-highlight) ,@(reverse properties)))
(format "%S" to-highlight))))
(should (string= expected real)))))
(ert-deftest dbml-mode-run-test-files ()
"Run `dbml-mode' against test-files and verify its highlighting."
(let ((files (directory-files "test-files" nil ".txt$"))
(ran 0) failed)
(dolist (name files)
(let* ((stats ert--current-run-stats)
(test (make-ert-test
:name (string-trim-right name ".txt")
:body (lambda () (dbml-mode-test-file name))))
(result (ert-run-test test)))
(setq ran (1+ ran))
(if (ert-test-passed-p result)
(progn
(if (ert-test-result-expected-p test result)
(setf (ert--stats-passed-expected stats)
(1+ (ert--stats-passed-expected stats)))
(setf (ert--stats-passed-unexpected stats)
(1+ (ert--stats-passed-unexpected stats)))))
(if (ert-test-result-expected-p test result)
(setf (ert--stats-failed-expected stats)
(1+ (ert--stats-failed-expected stats)))
;; failed and unexpected
(setf (ert--stats-failed-unexpected stats)
(1+ (ert--stats-failed-unexpected stats)))
(let ((should-forms (ert-test-failed-should-forms result)))
(push `(,(ert-test-name test)
,(with-temp-buffer (insert (format "%s" should-forms))
(pp-buffer) (buffer-string)))
failed))))))
(advice-add 'ert-stats-total
:filter-return `(lambda (result) (+ result ,ran)))
(when failed
(ert-fail failed))))
(ert-deftest dbml-mode-column-name-rehighlight-in-anchored ()
"Re-highlight columns (anchored block pattern matching region)."
(let* ((noninteractive nil)
(lines '("table name {"
"one type"
"two type"
"}"))
(expected '("table name {"
"one type"
"two type2"
"three type"
"}")))
(with-temp-buffer
(dolist (char (string-to-list (string-join lines "\n")))
(when char
(execute-kbd-macro (kbd (cond ((= char 10) "RET")
((= char 32) "SPC")
(t (char-to-string char)))))))
(should-not (text-properties-at (point-min)))
(dbml-mode-in-ert)
(should
(string= (format "%S" (buffer-string))
(replace-regexp-in-string
"placeholderxxxxxxxxxxxxxxxxxxxxx"
(string-join lines "\n")
(format
"%S" #("placeholderxxxxxxxxxxxxxxxxxxxxx"
0 5 (face font-lock-keyword-face fontified t)
5 6 (fontified t)
6 10 (face font-lock-type-face fontified t)
10 13 (fontified t)
13 16 (face font-lock-variable-name-face fontified t)
16 17 (fontified t)
17 21 (face font-lock-type-face fontified t)
21 22 (fontified t)
22 25 (face font-lock-variable-name-face fontified t)
25 26 (fontified t)
26 30 (face font-lock-type-face fontified t)
30 32 (fontified t))))))
;; Go to last col's type, type something to trigger re-highlighting
(goto-char (- (point-max) 2))
(execute-kbd-macro (kbd "2"))
(dolist (char (string-to-list "\nthree type"))
(when char (execute-kbd-macro (kbd (cond ((= char 10) "RET")
((= char 32) "SPC")
(t (char-to-string char)))))))
;; TODO: fontification-functions are NOT called in ERT. Why?
(jit-lock-fontify-now)
(should
(string= (format "%S" (buffer-string))
(replace-regexp-in-string
"placeholderxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
(string-join expected "\n")
(format
;; DO NOT TOUCH THESE!!!!
"%S" #("placeholderxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
0 5 (face font-lock-keyword-face fontified t)
5 6 (fontified t)
6 10 (face font-lock-type-face fontified t)
10 13 (fontified t)
13 16 (face font-lock-variable-name-face fontified t)
16 17 (fontified t)
17 21 (face font-lock-type-face fontified t)
21 22 (fontified t)
22 25 (face font-lock-variable-name-face fontified t)
25 26 (fontified t)
26 31 (face font-lock-type-face fontified t)
31 32 (fontified t)
32 37 (face font-lock-variable-name-face fontified t)
37 38 (fontified t)
38 42 (face font-lock-type-face fontified t)
42 44 (fontified t)))))))))
(ert-deftest dbml-mode-table-name-duplicate ()
"Highlight table name if duplicated before occurrence."
(let ((lines '("table name{}"
"table name{}")))
(with-temp-buffer
(insert (string-join lines "\n"))
(should-not (text-properties-at (point-min)))
(dbml-mode-in-ert)
(should (string= (format "%S" (buffer-string))
(replace-regexp-in-string
"placeholderxxxxxxxxxxxx"
(string-join lines "\n")
(format
"%S" #("placeholderxxxxxxxxxxxx"
0 5 (face font-lock-keyword-face)
6 10 (face font-lock-type-face)
13 18 (face font-lock-keyword-face)
19 23 (face (underline error))))))))))
(ert-deftest dbml-mode-keyword-multi-occurrence ()
"Highlight keywords even if duplicated."
(let ((lines '("table table" "table")))
(with-temp-buffer
(insert (string-join lines "\n"))
(should-not (text-properties-at (point-min)))
(dbml-mode-in-ert)
(should (string= (format "%S" (buffer-string))
(replace-regexp-in-string
"placeholderxxxxxx"
(string-join lines "\n")
(format
"%S" #("placeholderxxxxxx"
0 5 (face font-lock-keyword-face)
6 11 (face font-lock-type-face)
12 17 (face font-lock-keyword-face)))))))))
(ert-deftest dbml-mode-not-a-column ()
"Columns are anchored. No random highlighting out of braces."
(let ((lines '("Table name {}"
" don't highlight this line with column font!")))
(with-temp-buffer
(insert (string-join lines "\n"))
(should-not (text-properties-at (point-min)))
(dbml-mode-in-ert)
(should (string= (format "%S" (buffer-string))
(replace-regexp-in-string
"placeholder"
(string-join lines "\n")
(format
"%S" #("placeholder"
0 5 (face font-lock-keyword-face)
6 10 (face font-lock-type-face)))))))))
(ert-deftest dbml-mode-column-settings ()
"Columns settings are anchored to brackets and belong to a known list."
;; TODO: pull to defcustom var
(dolist (word-prop '("pk" "primary key" "null" "not null" "unique"
"increment" ("note" font-lock-keyword-face) "default"))
(let* ((word (if (stringp word-prop) word-prop (car word-prop)))
(prop (if (stringp word-prop) '(font-lock-builtin-face)
(backquote (font-lock-builtin-face ,@(cdr word-prop)))))
(blank ", not, ")
(text (format "table test{col type [%s%s%s]}" word blank word)))
(with-temp-buffer
(insert text)
(should-not (text-properties-at (point-min)))
(dbml-mode-in-ert)
(should
(string=
(replace-regexp-in-string "#" "" (format "%S" (buffer-string)))
(replace-regexp-in-string
"placeholderxxxxxxxxxxxxxxxxxxxxxx" text
(format
"%S" (list
"placeholderxxxxxxxxxxxxxxxxxxxxxx"
0 5 '(face font-lock-keyword-face)
6 10 '(face font-lock-type-face)
11 14 '(face font-lock-variable-name-face)
15 19 '(face font-lock-type-face)
20 21 '(face bold)
;; append/prepend -> ()
21 (+ 21 (length word)) '(face (font-lock-builtin-face))
(+ 21 (length word) (length blank))
(+ 21 (length word) (length blank) (length word))
(backquote (face ,prop))
(+ 21 (length word) (length blank) (length word))
(+ 21 (length word) (length blank) (length word) 1)
'(face bold))))))))))
(provide 'dbml-mode-tests)
;;; dbml-mode-tests.el ends here