|
1 | 1 | (ns rewrite-clj.paredit-test
|
2 | 2 | (:require [clojure.test :refer [deftest is testing]]
|
| 3 | + [rewrite-clj.node :as n] |
3 | 4 | [rewrite-clj.paredit :as pe]
|
4 | 5 | [rewrite-clj.zip :as z]
|
5 | 6 | [rewrite-clj.zip.test-helper :as th]))
|
|
391 | 392 | (is (= s (th/root-locmarked-string zloc)) "(sanity) string before")
|
392 | 393 | (is (= expected (-> zloc (pe/wrap-fully-forward-slurp t) th/root-locmarked-string)) "string after")))))))
|
393 | 394 |
|
| 395 | +;; TODO what about comments? |
394 | 396 | (deftest splice-killing-backward-test
|
395 | 397 | (doseq [opts zipper-opts]
|
396 |
| - (testing (zipper-opts-desc opts) |
397 |
| - (let [res (-> (th/of-locmarked-string "(foo (let ((x 5)) ⊚(sqrt n)) bar)" opts) |
398 |
| - pe/splice-killing-backward)] |
399 |
| - (is (= "(foo ⊚(sqrt n) bar)" (th/root-locmarked-string res))))))) |
| 398 | + (testing (str "zipper opts" opts) |
| 399 | + (doseq [[s expected] |
| 400 | + [["(foo (let ((x 5)) ⊚(sqrt n)) bar)" "(foo ⊚(sqrt n) bar)"] |
| 401 | + ["( a ( b c ⊚d e f) g)" "( a ⊚d e f g)"] |
| 402 | + ["( [a] ( [b] [c] ⊚[d] [e] [f]) [g])" "( [a] ⊚[d] [e] [f] [g])"] |
| 403 | + ["( [a] ( [b] [c] [d] [e] ⊚[f]) [g])" "( [a] ⊚[f] [g])"] |
| 404 | + ["( (⊚ ) [g])" "( ⊚[g])"] |
| 405 | + ["( [a] (⊚ ))" "( ⊚[a])"] |
| 406 | + ["( (⊚ ))" "⊚()"] |
| 407 | + ["[⊚1]" "⊚1"] |
| 408 | + ["[⊚1 2]" "⊚1 2"] |
| 409 | + ["[1 2 ⊚3 4 5]" "⊚3 4 5"] |
| 410 | + ["[1 2⊚ 3 4 5]" "⊚3 4 5"] |
| 411 | + ["[1 2 3 4 5⊚ ]" "◬"]]] |
| 412 | + (testing s |
| 413 | + (let [zloc (th/of-locmarked-string s opts) |
| 414 | + res (pe/splice-killing-backward zloc)] |
| 415 | + (is (= s (th/root-locmarked-string zloc)) "(sanity) s before change") |
| 416 | + (is (= expected (th/root-locmarked-string res)) "root-string after"))))))) |
400 | 417 |
|
| 418 | +;; TODO what about comments? |
401 | 419 | (deftest splice-killing-forward-test
|
402 | 420 | (doseq [opts zipper-opts]
|
403 |
| - (testing (zipper-opts-desc opts) |
404 |
| - (doseq [[s expected] |
405 |
| - [["(a (b c ⊚d e) f)" "(a b ⊚c f)"] |
406 |
| - ["(a (⊚b c d e) f)" "(⊚a f)"]]] |
407 |
| - (let [zloc (th/of-locmarked-string s opts)] |
408 |
| - (is (= s (th/root-locmarked-string zloc)) "(sanity) string before") |
409 |
| - (is (= expected (-> zloc pe/splice-killing-forward th/root-locmarked-string)) "string after")))))) |
| 421 | + (testing (str "zipper opts" opts) |
| 422 | + (doseq [[s expected] |
| 423 | + [["(a (b c ⊚d e f) g)" "(a b ⊚c g)"] |
| 424 | + ["(a (⊚b c d e) f)" "(⊚a f)"] |
| 425 | + ["( a ( b c ⊚d e f) g)" "( a b ⊚c g)"] |
| 426 | + ["( [a] ( [b] [c] ⊚[d] [e] [f]) [g])" "( [a] [b] ⊚[c] [g])"] |
| 427 | + ["( [a] ( ⊚[b] [c] [d] [e] [f]) [g])" "( ⊚[a] [g])"] |
| 428 | + ["( ( ⊚[b] [c] [d] [e] [f]) [g])" "( ⊚[g])"] |
| 429 | + ["( [a] ( ⊚[b] [c] [d] [e] [f]))" "( ⊚[a])"] |
| 430 | + ["( ( ⊚[b] [c] [d] [e] [f]))" "⊚()"] |
| 431 | + ["( (⊚ ) [g])" "( ⊚[g])"] |
| 432 | + ["( [a] (⊚ ))" "( ⊚[a])"] |
| 433 | + ["( (⊚ ))" "⊚()"] |
| 434 | + ["[⊚1]" "◬"] |
| 435 | + ["[⊚1 2]" "◬"] |
| 436 | + ["[1 2 ⊚3 4 5]" "1 ⊚2"] |
| 437 | + ["[1 2⊚ 3 4 5]" "1 ⊚2"] |
| 438 | + ["[ ⊚1 2 3 4 5 ]" "◬"]]] |
| 439 | + (testing s |
| 440 | + (let [zloc (th/of-locmarked-string s opts) |
| 441 | + res (pe/splice-killing-forward zloc)] |
| 442 | + (is (= s (th/root-locmarked-string zloc)) "(sanity) s before change") |
| 443 | + (is (= expected (th/root-locmarked-string res)) "root-string after"))))))) |
410 | 444 |
|
411 | 445 | (deftest split-test
|
412 | 446 | (doseq [opts zipper-opts]
|
|
436 | 470 | [["(\"Hello ⊚World\" 42)" "(⊚\"Hello \" \"World\" 42)"]
|
437 | 471 | ["(\"⊚Hello World\" 101)" "(⊚\"\" \"Hello World\" 101)"]
|
438 | 472 | ["(\"H⊚ello World\" 101)" "(⊚\"H\" \"ello World\" 101)"]
|
| 473 | + ["(\"Hello World⊚\" 101)" "(⊚\"Hello World\") (101)"] |
| 474 | + ["bingo bango (\"Hello\n Wor⊚ld\" 101)" "bingo bango (⊚\"Hello\n Wor\" \"ld\" 101)"] |
439 | 475 | ["(⊚\"Hello World\" 101)" "(⊚\"Hello World\") (101)"]]]
|
440 | 476 | (let [{:keys [pos s]} (th/pos-and-s s)
|
441 | 477 | zloc (z/of-string* s {:track-position? true})]
|
|
485 | 521 | (let [zloc (th/of-locmarked-string s opts)]
|
486 | 522 | (is (= s (th/root-locmarked-string zloc)) "(sanity) string before")
|
487 | 523 | (is (= expected (-> zloc pe/move-to-prev th/root-locmarked-string)) "string after"))))))
|
| 524 | + |
| 525 | +(deftest ops-on-changed-zipper-test |
| 526 | + (doseq [opts zipper-opts] |
| 527 | + (testing (str "zipper opts " opts) |
| 528 | + ;; create our zipper dynamically to avoid any reader metadata |
| 529 | + ;; we used to rely on this metadata and it was a problem |
| 530 | + ;; see https://github.com/clj-commons/rewrite-clj/issues/256 |
| 531 | + (let [zloc (-> (z/of-node (n/forms-node |
| 532 | + [(n/token-node 'foo) (n/spaces 1) |
| 533 | + (n/list-node |
| 534 | + [(n/token-node 'bar) (n/spaces 1) |
| 535 | + (n/token-node 'baz) (n/spaces 1) |
| 536 | + (n/vector-node |
| 537 | + [(n/token-node 1) (n/spaces 1) |
| 538 | + (n/token-node 2)]) |
| 539 | + (n/spaces 1) |
| 540 | + (n/vector-node |
| 541 | + [(n/token-node 3) (n/spaces 1) |
| 542 | + (n/token-node 4)]) |
| 543 | + (n/spaces 1) |
| 544 | + (n/keyword-node :bip) (n/spaces 1) |
| 545 | + (n/keyword-node :bop)]) |
| 546 | + (n/spaces 1) |
| 547 | + (n/token-node :bap)]) |
| 548 | + opts) |
| 549 | + z/right z/down z/right z/right z/down)] |
| 550 | + ;; 1 2 3 4 |
| 551 | + ;; 12345678901234567890123456789012345678901 |
| 552 | + (is (= "foo (bar baz [⊚1 2] [3 4] :bip :bop) :bap" (th/root-locmarked-string zloc)) "(sanity) before") |
| 553 | + (is (= "foo (bar baz ⊚1 [2] [3 4] :bip :bop) :bap" (-> zloc pe/barf-backward th/root-locmarked-string))) |
| 554 | + (is (= "foo (bar baz [⊚1] 2 [3 4] :bip :bop) :bap" (-> zloc pe/barf-forward th/root-locmarked-string))) |
| 555 | + (is (= "foo (bar baz [1 2 ⊚3 4] :bip :bop) :bap" (-> zloc z/up z/right pe/join th/root-locmarked-string))) |
| 556 | + (is (= "foo (bar baz ⊚[] [3 4] :bip :bop) :bap" (-> zloc pe/kill th/root-locmarked-string))) |
| 557 | + (when (:track-position? opts) |
| 558 | + (is (= "foo (bar baz [1 2] [3 4]⊚ ) :bap" (-> zloc (pe/kill-at-pos {:row 1 :col 28}) th/root-locmarked-string)))) |
| 559 | + (is (= "foo (bar baz ⊚1 [2] [3 4] :bip :bop) :bap" (-> zloc pe/move-to-prev th/root-locmarked-string))) |
| 560 | + (is (= "foo (bar baz ⊚1 [3 4] :bip :bop) :bap" (-> zloc pe/raise th/root-locmarked-string))) |
| 561 | + (is (= "foo (bar [baz ⊚1 2] [3 4] :bip :bop) :bap" (-> zloc pe/slurp-backward th/root-locmarked-string))) |
| 562 | + (is (= "foo ([bar baz ⊚1 2] [3 4] :bip :bop) :bap" (-> zloc pe/slurp-backward-fully th/root-locmarked-string))) |
| 563 | + (is (= "foo (bar baz [⊚1 2 [3 4]] :bip :bop) :bap" (-> zloc pe/slurp-forward th/root-locmarked-string))) |
| 564 | + (is (= "foo (bar baz [1 2] [⊚3 4 :bip :bop]) :bap" (-> zloc z/up z/right z/down pe/slurp-forward-fully th/root-locmarked-string))) |
| 565 | + (is (= "foo (bar baz ⊚1 2 [3 4] :bip :bop) :bap" (-> zloc z/up pe/splice th/root-locmarked-string))) |
| 566 | + (is (= "foo (bar baz ⊚2 [3 4] :bip :bop) :bap" (-> zloc z/right pe/splice-killing-backward th/root-locmarked-string))) |
| 567 | + (is (= "foo (bar baz ⊚2 [3 4] :bip :bop) :bap" (-> zloc z/right pe/splice-killing-backward th/root-locmarked-string))) |
| 568 | + (is (= "foo (bar baz [⊚1] [2] [3 4] :bip :bop) :bap" (-> zloc pe/split th/root-locmarked-string))) |
| 569 | + (when (:track-position? opts) |
| 570 | + (is (= "foo (bar baz [1 2] [⊚3] [4] :bip :bop) :bap" (-> zloc (pe/split-at-pos {:row 1 :col 22}) th/root-locmarked-string)))) |
| 571 | + (is (= "foo (bar baz [#{⊚1} 2] [3 4] :bip :bop) :bap" (-> zloc (pe/wrap-around :set) th/root-locmarked-string))) |
| 572 | + (is (= "foo (bar baz [{⊚1 2}] [3 4] :bip :bop) :bap" (-> zloc (pe/wrap-fully-forward-slurp :map) th/root-locmarked-string))))))) |
0 commit comments