|
| 1 | +;;; rust-mode-tests.el --- ERT tests for rust-mode.el |
| 2 | + |
| 3 | +(require 'rust-mode) |
| 4 | +(require 'ert) |
| 5 | +(require 'cl) |
| 6 | + |
| 7 | +(setq rust-test-fill-column 32) |
| 8 | + |
| 9 | +(defun rust-compare-code-after-manip (original point-pos manip-func expected got) |
| 10 | + (equal expected got)) |
| 11 | + |
| 12 | +(defun rust-test-explain-bad-manip (original point-pos manip-func expected got) |
| 13 | + (if (equal expected got) |
| 14 | + nil |
| 15 | + (list |
| 16 | + ;; The (goto-char) and (insert) business here is just for |
| 17 | + ;; convenience--after an error, you can copy-paste that into emacs eval to |
| 18 | + ;; insert the bare strings into a buffer |
| 19 | + "Rust code was manipulated wrong after:" |
| 20 | + `(insert ,original) |
| 21 | + `(goto-char ,point-pos) |
| 22 | + 'expected `(insert ,expected) |
| 23 | + 'got `(insert ,got) |
| 24 | + (loop for i from 0 to (max (length original) (length expected)) |
| 25 | + for oi = (if (< i (length got)) (elt got i)) |
| 26 | + for ei = (if (< i (length expected)) (elt expected i)) |
| 27 | + while (equal oi ei) |
| 28 | + finally return `(first-difference-at |
| 29 | + (goto-char ,(+ 1 i)) |
| 30 | + expected ,(char-to-string ei) |
| 31 | + got ,(char-to-string oi)))))) |
| 32 | +(put 'rust-compare-code-after-manip 'ert-explainer |
| 33 | + 'rust-test-explain-bad-manip) |
| 34 | + |
| 35 | +(defun rust-test-manip-code (original point-pos manip-func expected) |
| 36 | + (with-temp-buffer |
| 37 | + (rust-mode) |
| 38 | + (insert original) |
| 39 | + (goto-char point-pos) |
| 40 | + (funcall manip-func) |
| 41 | + (should (rust-compare-code-after-manip |
| 42 | + original point-pos manip-func expected (buffer-string))))) |
| 43 | + |
| 44 | +(defun test-fill-paragraph (unfilled expected &optional start-pos end-pos) |
| 45 | + "We're going to run through many scenarios here--the point should be able to be anywhere from the start-pos (defaults to 1) through end-pos (defaults to the length of what was passed in) and (fill-paragraph) should return the same result. |
| 46 | +
|
| 47 | +Also, the result should be the same regardless of whether the code is at the beginning or end of the file. (If you're not careful, that can make a difference.) So we test each position given above with the passed code at the beginning, the end, neither and both. So we do this a total of (end-pos - start-pos)*4 times. Oy." |
| 48 | + (let* ((start-pos (or start-pos 1)) |
| 49 | + (end-pos (or end-pos (length unfilled))) |
| 50 | + (padding "\n \n") |
| 51 | + (padding-len (length padding))) |
| 52 | + (loop |
| 53 | + for pad-at-beginning from 0 to 1 |
| 54 | + for pad-at-end from 0 to 1 |
| 55 | + with padding-beginning = (if (= 0 pad-at-beginning) "" padding) |
| 56 | + with padding-end = (if (= 0 pad-at-end) "" padding) |
| 57 | + with padding-adjust = (* padding-len pad-at-beginning) |
| 58 | + with padding-beginning = (if (= 0 pad-at-beginning) "" padding) |
| 59 | + with padding-end = (if (= 0 pad-at-end) "" padding) |
| 60 | + for pos from (if (= 1 start-pos) 1 (+ padding-adjust start-pos)) to (+ end-pos padding-adjust) |
| 61 | + do (rust-test-manip-code |
| 62 | + (concat padding-beginning unfilled padding-end) |
| 63 | + pos |
| 64 | + (lambda () |
| 65 | + (let ((fill-column rust-test-fill-column)) |
| 66 | + (fill-paragraph))) |
| 67 | + (concat padding-beginning expected padding-end))))) |
| 68 | + |
| 69 | +(ert-deftest fill-paragraph-top-level-multi-line-style-doc-comment-second-line () |
| 70 | + (test-fill-paragraph |
| 71 | + "/** |
| 72 | + * This is a very very very very very very very long string |
| 73 | + */" |
| 74 | + "/** |
| 75 | + * This is a very very very very |
| 76 | + * very very very long string |
| 77 | + */")) |
| 78 | + |
| 79 | + |
| 80 | +(ert-deftest fill-paragraph-top-level-multi-line-style-doc-comment-first-line () |
| 81 | + (test-fill-paragraph |
| 82 | + "/** This is a very very very very very very very long string |
| 83 | + */" |
| 84 | + "/** This is a very very very |
| 85 | + * very very very very long |
| 86 | + * string |
| 87 | + */")) |
| 88 | + |
| 89 | +(ert-deftest fill-paragraph-multi-paragraph-multi-line-style-doc-comment () |
| 90 | + (let |
| 91 | + ((multi-paragraph-unfilled |
| 92 | + "/** |
| 93 | + * This is the first really really really really really really really long paragraph |
| 94 | + * |
| 95 | + * This is the second really really really really really really long paragraph |
| 96 | + */")) |
| 97 | + (test-fill-paragraph |
| 98 | + multi-paragraph-unfilled |
| 99 | + "/** |
| 100 | + * This is the first really |
| 101 | + * really really really really |
| 102 | + * really really long paragraph |
| 103 | + * |
| 104 | + * This is the second really really really really really really long paragraph |
| 105 | + */" |
| 106 | + 1 89) |
| 107 | + (test-fill-paragraph |
| 108 | + multi-paragraph-unfilled |
| 109 | + "/** |
| 110 | + * This is the first really really really really really really really long paragraph |
| 111 | + * |
| 112 | + * This is the second really |
| 113 | + * really really really really |
| 114 | + * really long paragraph |
| 115 | + */" |
| 116 | + 90))) |
| 117 | + |
| 118 | +(ert-deftest fill-paragraph-multi-paragraph-single-line-style-doc-comment () |
| 119 | + (let |
| 120 | + ((multi-paragraph-unfilled |
| 121 | + "/// This is the first really really really really really really really long paragraph |
| 122 | +/// |
| 123 | +/// This is the second really really really really really really long paragraph")) |
| 124 | + (test-fill-paragraph |
| 125 | + multi-paragraph-unfilled |
| 126 | + "/// This is the first really |
| 127 | +/// really really really really |
| 128 | +/// really really long paragraph |
| 129 | +/// |
| 130 | +/// This is the second really really really really really really long paragraph" |
| 131 | + 1 86) |
| 132 | + (test-fill-paragraph |
| 133 | + multi-paragraph-unfilled |
| 134 | + "/// This is the first really really really really really really really long paragraph |
| 135 | +/// |
| 136 | +/// This is the second really |
| 137 | +/// really really really really |
| 138 | +/// really long paragraph" |
| 139 | + 87))) |
| 140 | + |
| 141 | +(ert-deftest fill-paragraph-multi-paragraph-single-line-style-indented () |
| 142 | + (test-fill-paragraph |
| 143 | + " // This is the first really really really really really really really long paragraph |
| 144 | + // |
| 145 | + // This is the second really really really really really really long paragraph" |
| 146 | + " // This is the first really |
| 147 | + // really really really |
| 148 | + // really really really |
| 149 | + // long paragraph |
| 150 | + // |
| 151 | + // This is the second really really really really really really long paragraph" 1 89)) |
| 152 | + |
| 153 | +(ert-deftest fill-paragraph-multi-line-style-inner-doc-comment () |
| 154 | + (test-fill-paragraph |
| 155 | + "/*! This is a very very very very very very very long string |
| 156 | + */" |
| 157 | + "/*! This is a very very very |
| 158 | + * very very very very long |
| 159 | + * string |
| 160 | + */")) |
| 161 | + |
| 162 | +(ert-deftest fill-paragraph-single-line-style-inner-doc-comment () |
| 163 | + (test-fill-paragraph |
| 164 | + "//! This is a very very very very very very very long string" |
| 165 | + "//! This is a very very very |
| 166 | +//! very very very very long |
| 167 | +//! string")) |
| 168 | + |
| 169 | +(ert-deftest fill-paragraph-prefixless-multi-line-doc-comment () |
| 170 | + (test-fill-paragraph |
| 171 | + "/** |
| 172 | +This is my summary. Blah blah blah blah blah. Dilly dally dilly dally dilly dally doo. |
| 173 | +
|
| 174 | +This is some more text. Fee fie fo fum. Humpty dumpty sat on a wall. |
| 175 | +*/" |
| 176 | + "/** |
| 177 | +This is my summary. Blah blah |
| 178 | +blah blah blah. Dilly dally |
| 179 | +dilly dally dilly dally doo. |
| 180 | +
|
| 181 | +This is some more text. Fee fie fo fum. Humpty dumpty sat on a wall. |
| 182 | +*/" 4 90)) |
| 183 | + |
| 184 | +(ert-deftest fill-paragraph-with-no-space-after-star-prefix () |
| 185 | + (test-fill-paragraph |
| 186 | + "/** |
| 187 | + *This is a very very very very very very very long string |
| 188 | + */" |
| 189 | + "/** |
| 190 | + *This is a very very very very |
| 191 | + *very very very long string |
| 192 | + */")) |
| 193 | + |
| 194 | +(defun test-auto-fill (initial position inserted expected) |
| 195 | + (rust-test-manip-code |
| 196 | + initial |
| 197 | + position |
| 198 | + (lambda () |
| 199 | + (unwind-protect |
| 200 | + (progn |
| 201 | + (let ((fill-column rust-test-fill-column)) |
| 202 | + (auto-fill-mode) |
| 203 | + (goto-char position) |
| 204 | + (insert inserted) |
| 205 | + (syntax-ppss-flush-cache 1) |
| 206 | + (funcall auto-fill-function))) |
| 207 | + (auto-fill-mode t))) |
| 208 | + expected)) |
| 209 | + |
| 210 | +(ert-deftest auto-fill-multi-line-doc-comment () |
| 211 | + (test-auto-fill |
| 212 | + "/** |
| 213 | + * |
| 214 | + */" |
| 215 | + 8 |
| 216 | + "This is a very very very very very very very long string" |
| 217 | + "/** |
| 218 | + * This is a very very very very |
| 219 | + * very very very long string |
| 220 | + */")) |
| 221 | + |
| 222 | +(ert-deftest auto-fill-single-line-doc-comment () |
| 223 | + (test-auto-fill |
| 224 | + "/// This is the first really |
| 225 | +/// really really really really |
| 226 | +/// really really long paragraph |
| 227 | +/// |
| 228 | +/// " |
| 229 | + 103 |
| 230 | + "This is the second really really really really really really long paragraph" |
| 231 | + "/// This is the first really |
| 232 | +/// really really really really |
| 233 | +/// really really long paragraph |
| 234 | +/// |
| 235 | +/// This is the second really |
| 236 | +/// really really really really |
| 237 | +/// really long paragraph" |
| 238 | + )) |
| 239 | + |
| 240 | +(ert-deftest auto-fill-multi-line-prefixless () |
| 241 | + (test-auto-fill |
| 242 | + "/* |
| 243 | +
|
| 244 | + */" |
| 245 | + 4 |
| 246 | + "This is a very very very very very very very long string" |
| 247 | + "/* |
| 248 | +This is a very very very very |
| 249 | +very very very long string |
| 250 | + */" |
| 251 | + )) |
| 252 | + |
| 253 | +(defun test-indent (indented) |
| 254 | + (let ((deindented (replace-regexp-in-string "^[[:blank:]]*" " " indented))) |
| 255 | + (rust-test-manip-code |
| 256 | + deindented |
| 257 | + 1 |
| 258 | + (lambda () (indent-region 1 (buffer-size))) |
| 259 | + indented))) |
| 260 | + |
| 261 | + |
| 262 | +(ert-deftest indent-struct-fields-aligned () |
| 263 | + (test-indent |
| 264 | +" |
| 265 | +struct Foo { bar: int, |
| 266 | + baz: int } |
| 267 | +
|
| 268 | +struct Blah {x:int, |
| 269 | + y:int, |
| 270 | + z:~str}")) |
| 271 | + |
| 272 | +(ert-deftest indent-doc-comments () |
| 273 | + (test-indent |
| 274 | +" |
| 275 | +/** |
| 276 | + * This is a doc comment |
| 277 | + * |
| 278 | + */ |
| 279 | +
|
| 280 | +/// So is this |
| 281 | +
|
| 282 | +fn foo() { |
| 283 | + /*! |
| 284 | + * this is a nested doc comment |
| 285 | + */ |
| 286 | + |
| 287 | + //! And so is this |
| 288 | +}")) |
| 289 | + |
| 290 | +(ert-deftest indent-inside-braces () |
| 291 | + (test-indent |
| 292 | + " |
| 293 | +// struct fields out one level: |
| 294 | +struct foo { |
| 295 | + a:int, |
| 296 | + // comments too |
| 297 | + b:char |
| 298 | +} |
| 299 | +
|
| 300 | +fn bar(x:~int) { // comment here should not affect the next indent |
| 301 | + bla(); |
| 302 | + bla(); |
| 303 | +}")) |
| 304 | + |
| 305 | +(ert-deftest indent-top-level () |
| 306 | + (test-indent |
| 307 | + " |
| 308 | +// Everything here is at the top level and should not be indented |
| 309 | +#[attrib] |
| 310 | +mod foo; |
| 311 | +
|
| 312 | +pub static bar = Quux{a: b()} |
| 313 | +
|
| 314 | +use foo::bar::baz; |
| 315 | +
|
| 316 | +fn foo() { } |
| 317 | +")) |
| 318 | + |
| 319 | +(ert-deftest indent-params-no-align () |
| 320 | + (test-indent |
| 321 | + " |
| 322 | +// Indent out one level because no params appear on the first line |
| 323 | +fn xyzzy( |
| 324 | + a:int, |
| 325 | + b:char) { } |
| 326 | +
|
| 327 | +fn abcdef( |
| 328 | + a:int, |
| 329 | + b:char) |
| 330 | + -> char |
| 331 | +{ }")) |
| 332 | + |
| 333 | +(ert-deftest indent-params-align () |
| 334 | + (test-indent |
| 335 | + " |
| 336 | +// Align the second line of params to the first |
| 337 | +fn foo(a:int, |
| 338 | + b:char) { } |
| 339 | +
|
| 340 | +fn bar( a:int, |
| 341 | + b:char) |
| 342 | + -> int |
| 343 | +{ } |
| 344 | +
|
| 345 | +fn baz( a:int, // shoudl work with a comment here |
| 346 | + b:char) |
| 347 | + -> int |
| 348 | +{ } |
| 349 | +")) |
| 350 | + |
| 351 | +(ert-deftest indent-square-bracket-alignment () |
| 352 | + (test-indent |
| 353 | + " |
| 354 | +fn args_on_the_next_line( // with a comment |
| 355 | + a:int, |
| 356 | + b:~str) { |
| 357 | + let aaaaaa = [ |
| 358 | + 1, |
| 359 | + 2, |
| 360 | + 3]; |
| 361 | + let bbbbbbb = [1, 2, 3, |
| 362 | + 4, 5, 6]; |
| 363 | + let ccc = [ 10, 9, 8, |
| 364 | + 7, 6, 5]; |
| 365 | +} |
| 366 | +")) |
| 367 | + |
| 368 | +(ert-deftest indent-nested-fns () |
| 369 | + (test-indent |
| 370 | + " |
| 371 | +fn nexted_fns(a: fn(b:int, |
| 372 | + c:char) |
| 373 | + -> int, |
| 374 | + d: int) |
| 375 | + -> uint |
| 376 | +{ |
| 377 | + 0 |
| 378 | +} |
| 379 | +" |
| 380 | +)) |
0 commit comments