diff options
| author | Yuan Fu | 2022-06-16 11:25:08 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-06-16 11:52:04 -0700 |
| commit | 33f7e10a29dad475f7872d6af87ecefaccdb55fc (patch) | |
| tree | a56a323694923e5045289aed55affe68f8a9dce5 | |
| parent | dd65d1c396da2e024468196c4d5bcb72198f524a (diff) | |
| download | emacs-33f7e10a29dad475f7872d6af87ecefaccdb55fc.tar.gz emacs-33f7e10a29dad475f7872d6af87ecefaccdb55fc.zip | |
Add treesit test for previous change
* test/src/treesit-tests.el (treesit-cross-boundary): New test.
| -rw-r--r-- | test/src/treesit-tests.el | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el index 32971fbacb4..416329d94dd 100644 --- a/test/src/treesit-tests.el +++ b/test/src/treesit-tests.el | |||
| @@ -223,6 +223,89 @@ | |||
| 223 | (treesit-parser-root-node parser)) | 223 | (treesit-parser-root-node parser)) |
| 224 | "(document (array (number)))"))))) | 224 | "(document (array (number)))"))))) |
| 225 | 225 | ||
| 226 | (ert-deftest treesit-cross-boundary () | ||
| 227 | "Tests for cross-boundary edits. | ||
| 228 | Cross-boundary means crossing visible_beg and visible_end. We | ||
| 229 | don't test if parser parses correctly, instead we just check | ||
| 230 | edits like this don't produce assertion errors. (I inserted a | ||
| 231 | bunch of assertions that checks e.g. visible_beg <= | ||
| 232 | visible_end.)" | ||
| 233 | (with-temp-buffer | ||
| 234 | (let (parser root-node pattern doc-node object-node pair-node) | ||
| 235 | (progn | ||
| 236 | (insert "xxx[1,{\"name\": \"Bob\"},2,3]xxx") | ||
| 237 | (narrow-to-region (+ (point-min) 3) (- (point-max) 3)) | ||
| 238 | (setq parser (treesit-parser-create 'json)) | ||
| 239 | ;; Now visible_beg/end = visible boundary. | ||
| 240 | (setq root-node (treesit-parser-root-node parser))) | ||
| 241 | ;; Now parser knows the content of the visible region. | ||
| 242 | (widen) | ||
| 243 | ;; Now visible_beg/end don't change, but visible region expanded. | ||
| 244 | (delete-region 1 7) | ||
| 245 | ;; (1) This change is across visible_beg. I expect | ||
| 246 | ;; ts_record_change to receive (start=1, old_end=7, new_end=1). | ||
| 247 | (treesit-parser-root-node parser) | ||
| 248 | ;; Above form forces a parse which calls | ||
| 249 | ;; `ts_ensure_position_synced'. Now visible_beg/end matches the | ||
| 250 | ;; visible region (whole buffer). We want to test that this | ||
| 251 | ;; doesn't cause assertion error. | ||
| 252 | |||
| 253 | (should (equal "{\"name\": \"Bob\"},2,3]xxx" (buffer-string))) | ||
| 254 | (narrow-to-region 1 16) | ||
| 255 | (should (equal "{\"name\": \"Bob\"}" (buffer-string))) | ||
| 256 | (treesit-parser-root-node parser) | ||
| 257 | ;; Call `ts_ensure_position_synced' again to update visible_beg/end. | ||
| 258 | (widen) | ||
| 259 | (goto-char 14) | ||
| 260 | (insert "by") | ||
| 261 | ;; (2) This change is inside [visible_beg, visible_end]. | ||
| 262 | (should (equal "{\"name\": \"Bobby\"},2,3]xxx" (buffer-string))) | ||
| 263 | (delete-region 14 23) | ||
| 264 | ;; This delete is across visible_end. | ||
| 265 | (should (equal "{\"name\": \"Bobxxx" (buffer-string))) | ||
| 266 | (treesit-parser-root-node parser) | ||
| 267 | ;; visible_beg/end synced. | ||
| 268 | |||
| 269 | (narrow-to-region 3 7) | ||
| 270 | (should (equal "name" (buffer-string))) | ||
| 271 | (treesit-parser-root-node parser) | ||
| 272 | ;; visible_beg/end synced. | ||
| 273 | (widen) | ||
| 274 | (goto-char (point-min)) | ||
| 275 | (insert "zzz") | ||
| 276 | (should (equal "zzz{\"name\": \"Bobxxx" (buffer-string))) | ||
| 277 | ;; (3) Test inserting before visible_beg. | ||
| 278 | (treesit-parser-root-node parser) | ||
| 279 | ;; visible_beg/end synced. | ||
| 280 | |||
| 281 | (narrow-to-region 4 11) | ||
| 282 | (should (equal "{\"name\"" (buffer-string))) | ||
| 283 | (treesit-parser-root-node parser) | ||
| 284 | ;; visible_beg/end synced. | ||
| 285 | (widen) | ||
| 286 | (goto-char (point-max)) | ||
| 287 | (insert "yyy") | ||
| 288 | ;; (4) This change is after visible_end. | ||
| 289 | (treesit-parser-root-node parser) | ||
| 290 | ;; Sync up visible_beg/end. | ||
| 291 | (should (equal "zzz{\"name\": \"Bobxxxyyy" (buffer-string))) | ||
| 292 | |||
| 293 | (narrow-to-region 1 17) | ||
| 294 | (should (equal "zzz{\"name\": \"Bob" (buffer-string))) | ||
| 295 | (treesit-parser-root-node parser) | ||
| 296 | ;; Sync up visible_beg/end. | ||
| 297 | (widen) | ||
| 298 | (delete-region 13 (point-max)) | ||
| 299 | (treesit-parser-root-node parser) | ||
| 300 | ;; Sync up visible_beg/end. | ||
| 301 | (should (equal "zzz{\"name\": " (buffer-string))) | ||
| 302 | ;; Ideally we want to also test the case where we delete and | ||
| 303 | ;; insert simultaneously, but the only such use is in | ||
| 304 | ;; `casify_region', all others either only inserts or only | ||
| 305 | ;; deletes. I'll leave it to someone to try to write a test | ||
| 306 | ;; that calls that. | ||
| 307 | ))) | ||
| 308 | |||
| 226 | (ert-deftest treesit-range () | 309 | (ert-deftest treesit-range () |
| 227 | "Tests if range works." | 310 | "Tests if range works." |
| 228 | (with-temp-buffer | 311 | (with-temp-buffer |