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
|
;;; go-font-lock-test.el
;; Copyright 2019 The go-mode Authors. All rights reserved. Use of
;; this source code is governed by a BSD-style license that can be
;; found in the LICENSE file.
(require 'ert)
(require 'go-mode)
(require 'cl-lib)
(ert-deftest go--fontify-signature ()
(go--should-fontify "KfuncK FfooF() { }")
(go--should-fontify "KfuncK FfooF(TaT) { }")
(go--should-fontify "KfuncK FfooF(TaT, TbT) { }")
(go--should-fontify "KfuncK FfooF(TaT) TaT { }")
(go--should-fontify "KfuncK FfooF(VaV TbT) (VaV TbT) { }")
(go--should-fontify "KfuncK FfooF(VaV, VbV TcT) (VaV TbT, VcV TdT) { }")
(go--should-fontify "KfuncK (TbT) FfooF(VaV, VbV TcT) TdT { }")
(go--should-fontify "KfuncK (VaV TbT) FfooF(VaV TbT) (TdT) { }")
(go--should-fontify "VfooV := KfuncK(VaV TbT) TcT { }")
(go--should-fontify "KfuncK(...TintT) { }")
(go--should-fontify "KfuncK(VaV ...TintT) { }")
(go--should-fontify "KfuncK(VaV ...KinterfaceK{}) { }")
(go--should-fontify "KfuncK(KinterfaceK { FfooF() }, TstringT) KinterfaceK{}")
(go--should-fontify "KfuncK(VaV TbT, VcV KfuncK(VdV *TeT) TdT) TfT")
(go--should-fontify "KfuncK(VaV KfuncK() TbT, VcV TdT)")
(go--should-fontify "
KfuncK FfooF(
VaV TcatT, VbV KinterfaceK { FbarkF() },
VcV TbananaT,
) (
VwhyV TdothisT,
VjustV TstopT,
) { }")
(go--should-fontify "
D// DQ
QD// DQ(
QKfuncK (VfV TintT) {}
"))
(ert-deftest go--fontify-struct ()
(go--should-fontify "KstructK { i TintT }")
(go--should-fontify "KstructK { a, b TintT }")
(go--should-fontify "
KstructK {
a TboolT
c KstructK { f *Tfoo.ZebraT }
}"))
(ert-deftest go--fontify-interface ()
(go--should-fontify "
KinterfaceK {
FfooF(VaV, VbV TcT) *TstringT
}")
(go--should-fontify "
KinterfaceK {
FfooF(KinterfaceK { FaF() TintT }) (VcV TdT)
}")
(go--should-fontify "
KmapK[TstringT]KinterfaceK{}{
S`foo`S: foo.FbarF(baz),
}"))
(ert-deftest go--fontify-type-switch ()
(go--should-fontify "
KswitchK foo.(KtypeK) {
KcaseK TstringT, *Tfoo.ZebraT, [2]TbyteT:
KcaseK CnilC:
KcaseK TfooT, TbarT, D// DQhi
Q
D// DQthere
Q TbazT, TquxT:
KdefaultK:
}")
(go--should-fontify "
KswitchK foo.(KtypeK) {
KcaseK KinterfaceK { FfooF(TintT, TstringT) }, KstructK { i, j TintT }, TstringT:
}")
(go--should-fontify "
KswitchK 123 {
KcaseK string:
}"))
(ert-deftest go--fontify-composite-literal ()
(go--should-fontify "TfooT{")
(go--should-fontify "[]TfooT{")
(go--should-fontify "Tfoo.ZarT{")
(go--should-fontify "[]Tfoo.ZarT{")
(go--should-fontify "TfooT{CbarC:baz, CquxC: 123}")
(go--should-fontify "TfooT{
CbarC: baz,
}")
(go--should-fontify "[]TfooT{{
CbarC: baz,
}, {
CbarC: baz,
}}")
(go--should-fontify "TsomeMapT{
foo.Zar: baz,
a + b: 3,
a-b: 4,
}"))
(ert-deftest go--fontify-slices-arrays-maps ()
(go--should-fontify "[]TfooT")
(go--should-fontify "[]Tfoo.ZarT")
(go--should-fontify "[]*Tfoo.ZarT")
(go--should-fontify "[123]TfooT")
(go--should-fontify "[...]TfooT")
(go--should-fontify "[foo.Zar]TfooT")
(go--should-fontify "D/*DQhi*/Q[1]*TfooT")
(go--should-fontify "KmapK[*Tfoo.ZarT]*Tbar.ZarT")
(go--should-fontify "[]KmapK[TfooT]TbarT")
(go--should-fontify "KmapK[[1][2][three]*Tfoo.ZarT][four][]*Tbar.ZarT")
(go--should-fontify "KmapK[TstringT]KmapK[TstringT]Tfloat64T")
(go--should-fontify "KmapK[[2][c]*TintT]TboolT"))
(ert-deftest go--fontify-negation ()
;; Fontify unary "!".
(go--should-fontify "N!Nfoo")
;; Alternate fontification with multiple "!".
(go--should-fontify "N!N!foo")
(go--should-fontify "N!N!N!Nfoo")
;; Don't fontify "!=" operator.
(go--should-fontify "foo != bar"))
(ert-deftest go--fontify-type-decl ()
(go--should-fontify "KtypeK TfooT TbarT")
(go--should-fontify "KtypeK TfooT Tbar.ZarT")
(go--should-fontify "KtypeK TfooT KstructK { }")
(go--should-fontify "KtypeK TfooT = Tbar.ZarT")
(go--should-fontify "KtypeK TfooT = KmapK[TstringT]TstringT")
(go--should-fontify "
KtypeK (
TfooT TbarT
TfooT KstructK {}
TfooT = *Tbar.ZarT
)")
(go--should-fontify "typeName = abc"))
(ert-deftest go--fontify-var-decl ()
(go--should-fontify "KvarK VfooV = bar")
(go--should-fontify "KvarK VfooV, VbarV = bar, baz")
(go--should-fontify "KvarK VfooV TbarT D// DQcoolQ")
(go--should-fontify "KvarK VfooV TbarT = baz")
(go--should-fontify "KvarK VfooV KstructK { i TintT } = baz")
(go--should-fontify "KvarK VfooV []*Tfoo.ZarT D// DQcoolQ")
(go--should-fontify "
KvarK (
VfooV TbarT
VfooV KfuncK(ViV TintT)
VfooV = bar
VfooV TbarT = baz
VfooV, VbarV = baz, qux
VfooV, VbarV TbazT = qux, zorb
)"))
(ert-deftest go--fontify-const-decl ()
(go--should-fontify "KconstK CfooC, CbarC = 123, 456 D// D")
(go--should-fontify "KconstK CfooC, CbarC TbazT = 123, 456")
(go--should-fontify "
KconstK (
CaC = 1
CaC TintT = 1
CaC, CbC TintT = 1, 2
)"))
(ert-deftest go--fontify-labels ()
(go--should-fontify "
CfooC:
KforK {
KcontinueK CfooC
KbreakK CfooC
KgotoK CfooC
}
"))
(ert-deftest go--fontify-assign ()
(go--should-fontify "VfooV := bar")
(go--should-fontify "foo = bar D// DQ:=Q")
(go--should-fontify "VfooV, VbarV := baz, qux")
(go--should-fontify "foo, bar = baz, qux")
(go--should-fontify "KfuncK FfooF(ViV TintT) { VbarV := baz }"))
(ert-deftest go--fontify-index-multiply ()
(go--should-fontify "foo[1]*10 + 1")
(go--should-fontify "foo[1]*foo[2] + 1"))
(ert-deftest go--fontify-go-dot-mod ()
(go--should-fontify "
KmoduleK foo
KgoK 1.13
KrequireK (
Nexample.com/require/go/bananaN Sv12.34.56SV-1234-456abcV D// DQindirect
Q Nnoslash.devN Sv1.2.3S
)
KreplaceK (
Nfoo.example.com/barN Sv1.2.3S => Nfoo.example.com/barN Sv1.2.3S
Nexample.com/foo/barN => Nexample.com/baz/barN Sv0.0.0SV-20201112005413-933910cbaea0V
)
" 'go-dot-mod-mode))
(defun go--should-match-face (want-face)
(let ((got-face (get-text-property (point) 'face)))
(if (not (eq got-face want-face))
(progn
(message "char '%s' (%s): wanted %s, got %s" (char-to-string (char-after)) (point) want-face got-face)
nil)
t)))
(defun go--should-fontify (contents &optional mode)
"Verify fontification.
CONTENTS is a template that uses single capital letters to
represent expected font lock face names. For example:
BmakeB([]TintT, 0)
expects \"make\" to be a (B)uiltin and \"int\" to be a (T)type."
(with-temp-buffer
(setq mode (or mode 'go-mode))
(funcall mode)
(insert contents)
(goto-char (point-min))
;; First pass through buffer looks for the face tags. We delete
;; the tags and record the expected face ranges in `faces'.
(let ((case-fold-search nil) faces start start-pos)
(while (re-search-forward "[TBKCFSNVDQ]" nil t)
(let ((found-char (char-before)))
(backward-delete-char 1)
(if start
(progn
(should (= found-char start))
(let ((face (cl-case found-char
(?T 'font-lock-type-face)
(?B 'font-lock-builtin-face)
(?K 'font-lock-keyword-face)
(?C 'font-lock-constant-face)
(?F 'font-lock-function-name-face)
(?S (if (eq mode 'go-mode) 'font-lock-string-face 'go-dot-mod-module-semver))
(?N (if (eq mode 'go-mode) 'font-lock-negation-char-face 'go-dot-mod-module-name))
(?V (if (eq mode 'go-mode) 'font-lock-variable-name-face 'go-dot-mod-module-version))
(?D 'font-lock-comment-delimiter-face)
(?Q 'font-lock-comment-face))))
(setq faces (append faces `((,face ,start-pos ,(point))))))
(setq start nil))
(setq start found-char)
(setq start-pos (point)))))
;; Fontify buffer now that we have removed the tags.
(font-lock-fontify-buffer)
(goto-char (point-min))
;; Go through buffer one character at a time making sure the
;; character's face is correct.
(let ((face (pop faces)))
(while (not (eobp))
(while (and face (>= (point) (nth 2 face)))
(setq face (pop faces)))
(if (and face (>= (point) (nth 1 face)))
(should (go--should-match-face (nth 0 face)))
(should (go--should-match-face nil)))
(forward-char))))))
|