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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
;; -*- flycheck-disabled-checkers: (emacs-lisp-checkdoc) ; lexical-binding:t -*-
(load (concat (file-name-directory (or load-file-name (buffer-file-name)
default-directory))
"utils.el") nil 'nomessage 'nosuffix)
(describe "Test indent-new-comment-line"
(it "works with -- ..."
(expect (lua-buffer-strs
(lua-insert-goto-<> '("-- foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("-- foobar"
"-- "))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("xyzzy -- foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("xyzzy -- foobar"
"-- "))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("xyz<> xyzzy -- foobar"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("xyz"
"xyzzy -- foobar")))
(it "works with ---- ...."
(expect (lua-buffer-strs
(lua-insert-goto-<> '("---- foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("---- foobar"
"---- "))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("xyzzy ---- foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("xyzzy ---- foobar"
"---- ")))
(it "doesn't recognize \"--\" inside strings and comments"
(expect (lua-buffer-strs
(lua-insert-goto-<> '("\"-- \" .. foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("\"-- \" .. foobar"
""))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("'-- ' .. foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("'-- ' .. foobar"
""))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("[[-- ]] .. foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("[[-- ]] .. foobar"
""))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("--[[-- ]] .. foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("--[[-- ]] .. foobar"
""))
(expect (lua-buffer-strs
(lua-insert-goto-<> '("---[[-- ]] .. foobar <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("---[[-- ]] .. foobar"
"---")))
(it "works when the comment is empty"
(expect (lua-buffer-strs
(lua-insert-goto-<> '("-- <>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("--"
"--"))
;; Let's make sure that whitespace is optional.
(expect (lua-buffer-strs
(lua-insert-goto-<> '("--<>"))
(execute-kbd-macro (kbd "M-j")))
:to-equal
'("--"
"--"))))
(describe "lua-comment-start-pos"
;; Single-line comments
(it "returns beginning of single-line comment if inside"
(with-lua-buffer
(lua-insert-goto-<> '("-- <>"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of single-line comment if between delimiters"
(with-lua-buffer
(lua-insert-goto-<> '("-<>- "))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns nil if before delimiters"
(with-lua-buffer
(lua-insert-goto-<> '("<>-- "))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if before single-line comment"
(with-lua-buffer
(lua-insert-goto-<> '("<>"
"-- "))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if after single-line comment"
(with-lua-buffer
(lua-insert-goto-<> '("--"
"<>"))
(expect (lua-comment-start-pos)
:to-equal nil)))
;; Single-line comments + strings
(it "returns nil if inside single-line string"
(with-lua-buffer
(lua-insert-goto-<> '("'--<>'"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if inside multi-line string"
(with-lua-buffer
(lua-insert-goto-<> '("[[--<>]]"))
(expect (lua-comment-start-pos)
:to-equal nil)))
;; Multi-line comments
(it "returns beginning of multi-line comment if inside 1"
(with-lua-buffer
(lua-insert-goto-<> '("--[[<> ]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if inside 2"
(with-lua-buffer
(lua-insert-goto-<> '("--[[<>"
"]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if inside 3"
(with-lua-buffer
(lua-insert-goto-<> '("--[["
"<>]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if between delimiters 1"
(with-lua-buffer
(lua-insert-goto-<> '("-<>-[[ ]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of single-line comment if between delimiters 2"
(with-lua-buffer
(lua-insert-goto-<> '("--<>[[ ]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if between delimiters 3"
(with-lua-buffer
(lua-insert-goto-<> '("--[<>[ ]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if between delimiters 4"
(with-lua-buffer
(lua-insert-goto-<> '("--[=<>[ ]]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns beginning of multi-line comment if between delimiters 5"
(with-lua-buffer
(lua-insert-goto-<> '("--[[ ]<>]"))
(expect (lua-comment-start-pos)
:to-equal 1)))
(it "returns nil if before multi-line opener"
(with-lua-buffer
(lua-insert-goto-<> '("<>--[[ ]]"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if after multi-line closer"
(with-lua-buffer
(lua-insert-goto-<> '("--[[ ]]<>"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns beginning of multi-line comment if after multi-line closer with different opener"
(with-lua-buffer
(lua-insert-goto-<> '("--[==[ ]]<>"))
(expect (lua-comment-start-pos)
:to-equal 1)))
;; Multi-line comments with strings
(it "returns nil if multi-line opener is inside string 1"
(with-lua-buffer
(lua-insert-goto-<> '("'--[[' <>]]"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if multi-line opener is inside string 2"
(with-lua-buffer
(lua-insert-goto-<> '("'--[[' ]]<>"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if multi-line opener is inside multi-line string 1"
(with-lua-buffer
(lua-insert-goto-<> '("[[--[[]] <>]]"))
(expect (lua-comment-start-pos)
:to-equal nil)))
(it "returns nil if multi-line opener is inside multi-line string 2"
(with-lua-buffer
(lua-insert-goto-<> '("[[--[[]] ]]<>"))
(expect (lua-comment-start-pos)
:to-equal nil))))
(describe "lua-comment-or-string-start-p/-pos"
(lua--parametrize-tests
(strings expected-result)
(;; single-line strings: single-quote
(("<>'foo'") nil)
(("'<>foo'") 1)
(("'foo<>'") 1)
(("'foo'<>") nil)
;; single-line strings: double-quote
(("<>\"foo\"") nil)
(("\"<>foo\"") 1)
(("\"foo<>\"") 1)
(("\"foo\"<>") nil)
;; multi-line strings
(("<>[[foo]]") nil)
(("[[<>foo]]") 1)
(("[<>[foo]]") 1)
(("[=<>[foo]=]") 1)
(("[<>=[foo]=]") 1)
(("[[foo<>]]") 1)
(("[[foo]<>]") 1)
(("[[foo]<>=]") 1)
(("[[foo]=<>]") 1)
(("[[foo]]<>") nil)
;; single-line comments
(("foo <>-- bar") nil)
(("foo -<>- bar") 5)
(("foo --<> bar") 5)
(("foo -- <>bar") 5)
(("foo -- bar<>") 5)
;; multi-line comments
(("foo <>--[[ bar ]]") nil)
(("foo -<>-[[ bar ]]") 5)
(("foo --<>[[ bar ]]") 5)
(("foo --[<>[ bar ]]") 5)
(("foo --[[<> bar ]]") 5)
(("foo --[[ bar <>]]") 5)
(("foo --[[ bar ]<>]") 5)
(("foo --[[ bar ]]<>") nil)
(("foo --[==[ bar ]]<>") 5)
;; single-line comment containing multi-line comment
(("foo <>---[[ bar ]]") nil)
(("foo --<>-[[ bar ]]") 5)
(("foo ---<>[[ bar ]]") 5)
(("foo ---[<>[ bar ]]") 5)
(("foo ---[[<> bar ]]") 5)
(("foo ---[[ bar ]]<>") 5)
;; multi-line comment containing single-line comment
(("foo --[[ -- bar ]]<>") nil)
;; string containing multi-line comment opener
(("foo '--[[' <> bar ]]") nil)
(("foo [[--[[]] <> bar ]]") nil)
(("foo [[--[==[]] <> bar ]==]") nil)
;; single dash: not a comment
(("foo = bar -<> baz") nil)
(("foo = bar <>- baz") nil))
:it (format "returns %s for %S"
(if expected-result (format "truthy/%S" expected-result) "nil") strings)
(with-lua-buffer
(lua-insert-goto-<> strings)
(expect (lua-comment-or-string-start-pos)
:to-equal expected-result)
(if expected-result
(expect (lua-comment-or-string-p) :to-be-truthy)
(expect (lua-comment-or-string-p) :not :to-be-truthy)))))
(describe "lua-skip-ws-and-comments-backward"
(describe "doesn't move point"
(lua--parametrize-tests
(navigation-spec test-name)
(("<>" "empty buffer")
("<> --[[]]foo" "at beginning of non-empty buffer")
(" f<>oo" "in the middle of variable")
(" foo<>" "at the end of variable")
(" foo<>--" "between variable and comment")
(" foo 'bar'<>" "at the end of single-quote string literal")
(" foo [[bar]]<>" "at the end of multi-line string literal")
(" foo '<>bar'" "inside string literal")
(" foo (<>bar)" "inside function call literal")
(" foo '-- <> bar'" "within whitespace inside single-line string literal")
(" foo [[-- \n<> bar]]" "within whitespace inside multi-line string literal")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name navigation-spec))
(expect navigation-spec
:with-point-at "<"
:after-executing (lua-skip-ws-and-comments-backward)
:to-end-up-at ">")))
(describe "moves point"
(lua--parametrize-tests
(navigation-spec test-name)
(("<2> <1>" "skip whitespace at the beginning of buffer")
("foo<2> <1>" "skip ws after variable")
("foo()<2> <1>" "skip ws after function call")
("foo<2> \n\t\n<1>" "skip newlines/tabs/spaces after variable")
;; single-line comments
("foo<2> -- <1>" "escape single-line comment and skip ws")
("foo<2> -<1>-" "escape single-line comment delimiter")
("foo<2> -- '<1>'" "escape commented out string and skip ws")
("foo<2> -- [[<1>]]" "escape commented out string and skip ws")
("foo<2> -- \n<1>" "skip single-line comment and ws")
("foo<2> -- \n--\n--\n<1>" "skip several single-line comments and ws")
;; multi-line
("foo<2> --[[ <1> ]]" "escape multi-line comment and skip ws")
("foo<2> -<1>-[[ ]]" "escape multi-line comment delimiter and skip ws 1")
("foo<2> --<1>[[ ]]" "escape multi-line comment delimiter and skip ws 2")
("foo<2> --[<1>[ ]]" "escape multi-line comment delimiter and skip ws 3")
("foo<2> --[[ ]<1>]" "escape multi-line comment delimiter and skip ws 4")
("foo<2> --[[ \n\n ]]\n\n--[[ ]]<1>" "skip multi-line comments and ws")
;; luadoc keywords
("foo<2> --[[ @see foo <1>]]" "escape multi-line comment with luadoc keyword 1")
("foo<2> --[[ @s<1>ee foo ]]" "escape multi-line comment with luadoc keyword 2")
("foo<2> --[[ <1>@see foo ]]" "escape multi-line comment with luadoc keyword 3")
("foo<2> -- @see foo <1>" "escape single-line comment with luadoc keyword 1")
("foo<2> -- @s<1>ee foo " "escape single-line comment with luadoc keyword 2")
("foo<2> -- <1>@see foo " "escape single-line comment with luadoc keyword 3")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name navigation-spec))
(expect navigation-spec
:with-point-at "<1>"
:after-executing (lua-skip-ws-and-comments-backward)
:to-end-up-at "<2>")))
(describe "respects limit"
(lua--parametrize-tests
(limit navigation-spec test-name)
((3 " <2> <1>" "respect limit in whitespace")
(100 " <2><1> " "don't move if limit is beyond point")
(5 "-- <2> <1>" "respect limit when escaping single-line comment")
(5 "--[[<2> <1>]]" "respect limit when escaping multi-line comment")
(5 " <2>-- <1>" "respect limit when escaping multi-line comment")
(5 " <2>--[[ <1>]]" "respect limit when escaping multi-line comment")
(5 "-- <2>@see x <1>" "respect limit when escaping single-line luadoc comment")
(5 "--[[<2>@see x <1>]]" "respect limit when escaping multi-line luadoc comment")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: limit=%S %S" test-name limit navigation-spec))
(expect navigation-spec
:with-point-at "<1>"
:after-executing (lua-skip-ws-and-comments-backward limit)
:to-end-up-at "<2>"))))
(describe "lua-skip-ws-and-comments-forward"
(describe "doesn't move point"
(lua--parametrize-tests
(navigation-spec test-name)
(("<>" "empty buffer")
(" --[[]]<>" "at end of non-empty buffer")
(" f<>oo " "in the middle of variable")
(" <>foo " "at the beginning of variable")
(" --[[]]<>foo " "between variable and comment")
(" foo <>'bar'" "at the end of single-quote string literal")
(" foo <>[[bar]]" "at the end of multi-line string literal")
(" foo 'bar<>'" "inside string literal")
(" foo (bar<>)" "inside function call literal")
(" foo '-- <> bar'" "within whitespace inside single-line string literal")
(" foo [[-- \n<>\n bar]]" "within whitespace inside multi-line string literal")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name navigation-spec))
(expect navigation-spec
:with-point-at "<"
:after-executing (lua-skip-ws-and-comments-forward)
:to-end-up-at ">")))
(describe "moves point"
(lua--parametrize-tests
(navigation-spec test-name)
(("<1> <2>" "skip whitespace at the end of buffer")
("<1> <2>bar" "skip ws before variable")
("foo<1> <2>()" "skip ws before function call")
("<1> \n\t\n<2>foo" "skip newlines/tabs/spaces before variable")
;; single-line comments
("foo -- <1>\n <2>bar" "escape single-line comment and skip ws")
("foo -<1>- \n <2>bar" "escape single-line comment delimiter")
("foo -- '<1>' \n <2>bar" "escape commented out string and skip ws")
("foo -- [[<1>]] \n <2>bar" "escape commented out string and skip ws")
("foo <1>-- \n \n <2>bar" "skip single-line comment and ws")
("foo <1>-- \n--\n--\n \n <2>bar" "skip several single-line comments and ws")
;; multi-line
("foo --[[ <1> ]] <2>bar" "escape multi-line comment and skip ws")
("foo -<1>-[[ ]] <2>bar" "escape multi-line comment delimiter and skip ws 1")
("foo --<1>[[ ]] <2>bar" "escape multi-line comment delimiter and skip ws 2")
("foo --[<1>[ ]] <2>bar" "escape multi-line comment delimiter and skip ws 3")
("foo --[[ ]<1>] <2>bar" "escape multi-line comment delimiter and skip ws 4")
("foo <1>--[[ \n\n ]]\n\n--[[ ]] <2>bar" "skip multi-line comments and ws")
;; luadoc keywords
("foo --[[ @see foo <1>]] <2>bar" "escape multi-line comment with luadoc keyword 1")
("foo --[[ @s<1>ee foo ]] <2>bar" "escape multi-line comment with luadoc keyword 2")
("foo --[[ <1>@see foo ]] <2>bar" "escape multi-line comment with luadoc keyword 3")
("foo -- @see foo<1> \n <2>bar" "escape single-line comment with luadoc keyword 1")
("foo -- @s<1>ee foo \n <2>bar" "escape single-line comment with luadoc keyword 2")
("foo -- <1>@see foo \n <2>bar" "escape single-line comment with luadoc keyword 3")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: %S" test-name navigation-spec))
(expect navigation-spec
:with-point-at "<1>"
:after-executing (lua-skip-ws-and-comments-forward)
:to-end-up-at "<2>")))
(describe "respects limit"
(lua--parametrize-tests
(limit navigation-spec test-name)
((6 " <1> <2> " "in whitespace")
(1 " <2><1> " "don't move if limit is before point")
(8 "-- <1> <2> \n" "when escaping single-line comment 1")
(8 "-- <1> \n<2> " "when escaping single-line comment 2")
(8 "-- <1> <2>\n " "when escaping single-line comment 3")
(8 "--[[<1> <2> ]] \n" "when escaping multi-line comment 1")
(8 "--[[<1> ]<2>] \n" "when escaping multi-line comment 1")
(8 "--[[<1> <2> ]] \n" "when escaping multi-line comment 1")
(7 "-- <1>@s<2>ee x " "when escaping single-line luadoc comment")
(8 "-- <1>@se<2>e x " "when escaping single-line luadoc comment")
(9 "-- <1>@see<2> x " "when escaping single-line luadoc comment")
(7 "--[[<1>@s<2>ee x]] " "when escaping multi-line luadoc comment")
(8 "--[[<1>@se<2>e x]] " "when escaping multi-line luadoc comment")
(9 "--[[<1>@see<2> x]] " "when escaping multi-line luadoc comment")
)
:it (replace-regexp-in-string "\n" "\\\\n" (format "%s: limit=%S %S" test-name limit navigation-spec))
(expect navigation-spec
:with-point-at "<1>"
:after-executing (lua-skip-ws-and-comments-forward limit)
:to-end-up-at "<2>"))))
(describe "lua-find-regexp"
(it "does not match open-bracket that is part of multiline string opener: forward"
(with-lua-buffer
(lua-insert-goto-<> '("<>foo = [[ bar ]]"))
(expect (lua-find-regexp 'forward "\\[") :not :to-be-truthy)))
(it "does not match open-bracket that is part of multiline string opener: backward"
(with-lua-buffer
(lua-insert-goto-<> '("foo = [[ bar ]]<>"))
(expect (lua-find-regexp 'backward "\\[") :not :to-be-truthy)))
(it "does not match close-bracket that is part of multiline string closer: forward"
(with-lua-buffer
(lua-insert-goto-<> '("<>foo = [[ bar ]]"))
(expect (lua-find-regexp 'forward "]") :not :to-be-truthy)))
(it "does not match close-bracket that is part of multiline string closer: backward"
(with-lua-buffer
(lua-insert-goto-<> '("<>foo = [[ bar ]]"))
(expect (lua-find-regexp 'backward "]") :not :to-be-truthy)))
(it "does not match minus that is part of comment starter: forward"
(with-lua-buffer
(lua-insert-goto-<> '("<>foo = [[ bar ]] -- baz"))
(expect (lua-find-regexp 'forward "-") :not :to-be-truthy)))
(it "does not match minus that is part of comment starter: backward"
(with-lua-buffer
(lua-insert-goto-<> '("<>foo = [[ bar ]] -- baz"))
(expect (lua-find-regexp 'backward "-") :not :to-be-truthy))))
|