-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lisp
2131 lines (1871 loc) · 82.8 KB
/
test.lisp
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; test.lisp --- Tests for software difference display and resolution
(defpackage :resolve/test
(:use :gt/full
#+gt :testbot
:software-evolution-library
:software-evolution-library/utility/debug
:stefil+
:software-evolution-library/software/parseable
:software-evolution-library/software/cl
:software-evolution-library/software/c
:software-evolution-library/software/javascript
:software-evolution-library/software/typescript
:software-evolution-library/software/project
:software-evolution-library/software/javascript-project
:software-evolution-library/software/simple
:software-evolution-library/software/tree-sitter
:software-evolution-library/components/formatting
:software-evolution-library/components/test-suite
:resolve/core
:resolve/ast-diff
:resolve/alist
:resolve/auto-merge
:resolve/software/project
:resolve/software/auto-mergeable
:resolve/software/parseable
:resolve/software/lisp)
#+gt (:shadowing-import-from :testbot :batch-test)
(:import-from :cmd)
(:local-nicknames (:diff :resolve/ast-diff))
(:shadow :function-body)
(:import-from :uiop/stream :read-file-forms)
(:import-from :resolve/ast-diff :ast-diff* :ast-patch*
:simplify-diff-for-printing
:has-conflict-node-before-slot-specifiers
:standardized-children
:unstandardize-children
:copy-with-standardized-children
:slot-specifier)
(:import-from :software-evolution-library/command-line
:create-software
:create-test-suite)
(:export :test :batch-test
:pack-intertext-recursively))
(in-package :resolve/test)
(in-readtable :curry-compose-reader-macros)
(defvar *this-file*
#.(or *compile-file-truename*
*load-truename*
*default-pathname-defaults*))
(define-constant +etc-dir+
(append (pathname-directory
#.(or *compile-file-truename*
*load-truename*
*default-pathname-defaults*))
(list "test" "etc"))
:test #'equalp
:documentation "Path to directory holding testing artifacts.")
(define-constant +javascript-dir+ (append +etc-dir+ (list "javascript"))
:test #'equalp
:documentation "Path to directory holding Javascript test programs.")
(define-constant +gcd-dir+ (append +etc-dir+ (list "gcd"))
:test #'equalp
:documentation "Path to directory holding GCD test programs.")
(defvar *binary-search* nil "Holds the binary_search software object.")
(defvar *forms* nil "Forms used in tests.")
(defvar *old* nil "Software used in diff/merge tests.")
(defvar *my* nil "Software used in diff/merge tests.")
(defvar *your* nil "Software used in diff/merge tests.")
(defvar *variants* nil "List of software variants.")
(defvar *cnf* nil)
(defvar *new* nil)
(defvar *tests* nil)
;;; Fixtures
(defixture resolve-asd-file-forms
(:setup (setf *forms* (read-file-forms
(make-pathname
:name "resolve"
:type "asd"
:directory
(pathname-directory *this-file*)))))
(:teardown (setf *forms* nil)))
(defixture binary-search-c
(:setup
(setf *binary-search*
(from-file
(make-instance 'c
:flags (list
"-I"
(namestring (make-pathname :directory +etc-dir+))))
(make-pathname
:name "binary_search"
:type "c"
:directory +etc-dir+))))
(:teardown
(setf *binary-search* nil)))
(defixture json-conflict-yargs
(:setup
(destructuring-bind (base left right)
(mapcar
(lambda (name)
(from-file
(make-instance 'json)
(make-pathname :directory (append +javascript-dir+ '("package-json"))
:type "json"
:name name)))
'("base-d5da5eb" "left-ea14630" "right-6655688"))
(setf *old* base *my* left *your* right)))
(:teardown
(setf *old* nil *my* nil *your* nil)))
(defixture gcd-conflict-c
(:setup
(destructuring-bind (my old your)
(mapcar
(lambda (name)
(from-file
(make-instance 'c)
(make-pathname :directory +gcd-dir+
:type "c"
:name name)))
'("gcd-wo-curlies-fix" "gcd-wo-curlies" "gcd-wo-curlies-prose"))
(setf *my* my *old* old *your* your)))
(:teardown
(setf *old* nil *my* nil *your* nil)))
(defixture gcd-conflict-javascript
(:setup
(destructuring-bind (my old your)
(mapcar
(lambda (name)
(from-file
(make-instance 'javascript)
(make-pathname :directory +gcd-dir+
:type "js"
:name name)))
'("gcd-fix" "gcd" "gcd-prose"))
(setf *my* my *old* old *your* your)))
(:teardown
(setf *old* nil *my* nil *your* nil)))
(defixture gcd-conflict-typescript
(:setup
(destructuring-bind (my old your)
(mapcar
(lambda (name)
(from-file
(make-instance 'typescript)
(make-pathname :directory +gcd-dir+
:type "js"
:name name)))
'("gcd-fix" "gcd" "gcd-prose"))
(setf *my* my *old* old *your* your)))
(:teardown
(setf *old* nil *my* nil *your* nil)))
(defun populate-js-abacus-variants (&key (as 'javascript))
(mapcar
(lambda (name)
(cons (make-keyword (string-upcase name))
(from-file
(make-instance as)
(make-pathname :directory (append +javascript-dir+ '("abacus"))
:type "js"
:name (concatenate 'string "abacus-" name)))))
'("orig" "calc-line" "borders" "space" "bead" "animate" "min-lines")))
(defixture javascript-abacus-variants
(:setup
(setf *variants* (populate-js-abacus-variants)))
(:teardown
(setf *variants* nil)))
(defixture typescript-abacus-variants
(:setup
(setf *variants* (populate-js-abacus-variants :as 'typescript)))
(:teardown
(setf *variants* nil)))
(defixture javascript-converge-conflict
(:setup (setf *variants* (populate-js-abacus-variants)
*cnf* (converge (aget :borders *variants*)
(aget :orig *variants*)
(aget :min-lines *variants*) :conflict t
:base-cost 5)))
(:teardown (setf *variants* nil *cnf* nil)))
(defixture typescript-converge-conflict
(:setup (setf *variants* (populate-js-abacus-variants :as 'typescript)
*cnf* (converge (aget :borders *variants*)
(aget :orig *variants*)
(aget :min-lines *variants*) :conflict t
:base-cost 5)))
(:teardown (setf *variants* nil *cnf* nil)))
(defroot test)
;;;; AST Diff tests
(defsuite ast-diff-tests "AST-level diffs of Sexprs.")
(deftest sexp-diff-empty ()
(is (equalp (multiple-value-list (ast-diff nil nil))
'(((:same . :nil)) 0))
"Simplest case -- empty list to itself."))
(deftest sexp-diff-empty-to-nonempty ()
(is (equalp (unastify-lisp-diff (multiple-value-list (ast-diff nil '(1))))
'(((:insert . 1) (:same . :nil)) 1))
"Empty list vs. list of one atom"))
(deftest sexp-diff-nonempty-to-empty ()
(is (equalp (unastify-lisp-diff (multiple-value-list (ast-diff '(1) nil)))
'(((:delete . 1) (:same . :nil)) 1))
"List of one atom vs. empty list"))
(deftest sexp-diff-numbers ()
(is (equalp (multiple-value-list (ast-diff 1 2 :base-cost 0))
'(; ((:insert . 2) (:delete . 1))
((:replace 1 2))
2))
"Diff of two numbers"))
(deftest sexp-diff-numbers-same ()
(is (equalp (multiple-value-list (ast-diff 1 1))
'(((:same . 1))
0))
"Diff of two equal numbers"))
(deftest sexp-diff-equal-zero-cost ()
(is (zerop (nth-value 1 (ast-diff '(1 2 3 4) '(1 2 3 4))))
"Equal should have 0 cost."))
(deftest sexp-diff-string.1 ()
(is (eql (nth-value 1 (ast-diff "a" "ab")) 1)
"Adding a character has cost 1"))
(deftest sexp-diff-string.2 ()
(is (eql (nth-value 1 (ast-diff "ab" "a")) 1)
"Deleting a character has cost 1"))
(deftest sexp-diff-string.3 ()
(is (eql (nth-value 1 (ast-diff "a" " a ")) 2)
"Add whitespace has cost 2"))
(deftest sexp-diff-string.4 ()
(is (eql (nth-value 1 (ast-diff "a" " a " :ignore-whitespace t)) 0)
"Adding whitespace costs nothing"))
(deftest sexp-diff-string.5 ()
(is (eql (nth-value 1 (ast-diff " a " "a")) 2)
"Adding whitespace has cost 2"))
(deftest sexp-diff-string.6 ()
(is (eql (nth-value 1 (ast-diff " a " "a" :ignore-whitespace t)) 0)
"Adding whitespace costs nothing"))
(deftest sexp-diff-string.7 ()
(is (equal (multiple-value-list (ast-diff "a" "b" :strings nil))
'(((:insert-sequence . "b") (:delete-sequence . "a"))
6))))
(deftest sexp-diff-string.8 ()
(is (equal (ast-diff '("a") '("b") :strings nil)
'(; (:insert . "b") (:delete . "a")
(:replace "a" "b")
(:same . :nil)))))
(deftest sexp-diff-string.9 ()
(is (equal (ast-diff "" "b" :strings nil)
'((:insert-sequence . "b")))))
(deftest sexp-diff-string.10 ()
(is (equal (ast-diff "a" "" :strings nil)
'((:delete-sequence . "a")))))
(deftest sexp-diff-string.11 ()
(is (equal (multiple-value-list
(ast-diff " " "" :strings nil :ignore-whitespace t))
'(((:delete-sequence . " ")) 2))))
(deftest sexp-diff-string.12 ()
(is (equal (multiple-value-list
(ast-diff "" " " :strings nil :ignore-whitespace t))
'(((:insert-sequence . " ")) 2))))
(deftest sexp-diff-string.13 ()
(is (equal (multiple-value-list
(ast-diff " " "a" :strings nil :ignore-whitespace t))
'(((:insert-sequence . "a") (:delete-sequence . " ")) 5))))
(deftest sexp-diff-string.14 ()
(is (equal (multiple-value-list
(ast-diff "a" " " :strings nil :ignore-whitespace t))
'(((:insert-sequence . " ") (:delete-sequence . "a")) 5))))
(deftest sexp-diff-string.15 ()
(is (equal (multiple-value-list
(ast-diff "abc" "abc" :strings nil))
'(((:same-sequence . "abc")) 0))))
(deftest sexp-diff-string.16 ()
(is (equal (multiple-value-list
(ast-diff " abc " " abc " :strings nil :ignore-whitespace t))
'(((:insert-sequence . " abc ") (:delete-sequence . " abc ")) 4))))
(deftest sexp-diff-string.17 ()
(let ((s1 "abc")
(s2 (make-array '(3) :element-type 'character
:initial-contents '(#\a #\b #\c)
:adjustable t)))
(is (string= s1 s2))
(is (equal (multiple-value-list (ast-diff s1 s2 :strings t))
'(((:same-sequence . "abc")) 0)))))
(deftest sexp-diff-vector.1 ()
(is (equalp (multiple-value-list
(ast-diff '(#(1 2)) '(#(1 2))))
'(((:same . #(1 2)) (:same . :nil)) 0))))
(deftest sexp-diff-non-equal-first-element ()
(is (not (zerop (nth-value 1 (ast-diff '(1 2 3 4) '(0 2 3 4)))))
"Difference in first car is caught."))
(deftest sexp-shuffled-elements1 ()
(is (equalp (ast-patch '(1 2 3 4)
(ast-diff '(1 2 3 4) '(1 4 3 2)))
'(1 4 3 2))
"Shuffled elements create a correct diff"))
(deftest sexp-shuffled-elements2 ()
(is (equalp (ast-patch '(1 2 3 4)
(ast-diff '(1 2 3 4) '(1 4 3 5 2)))
'(1 4 3 5 2))
"Shuffled elements create a correct diff"))
(deftest sexp-shuffled-elements3 ()
(is (equalp (ast-patch '(1 2 3 5 4)
(ast-diff '(1 2 3 5 4) '(1 4 3 2)))
'(1 4 3 2))
"Shuffled elements create a correct diff"))
(deftest sexp-insert-then-delete ()
(is (equalp (ast-patch '1 '((:insert . 2) (:delete . 1))) 2)
"Insert followed by delete in patch"))
(deftest sexp-replace ()
(is (equalp (ast-patch '(1) '((:replace 1 2) (:same . :nil))) '(2))
"Replace leaf value by another value"))
(deftest sexp-two-differences ()
(is (equalp (ast-patch '(1 2 3 4 5) (ast-diff '(1 2 3 4 5) '(1 6 3 7 5)))
'(1 6 3 7 5))
"Two lists that differ in two non-adjacent places"))
(deftest sexp-wrap.1 ()
(is (equalp (ast-patch '(1 (2) 3)
`((:same . 1) (:recurse :wrap ((:same . 2) (:same . :nil)) (0) (nil) ((:nil)) (:list) ,(astify '((2)))) (:same . 3) (:same . :nil)))
'(1 ((2)) 3))))
(deftest sexp-wrap.2 ()
(is (equalp (ast-patch '(1 (2) 3)
`((:same . 1) (:recurse :wrap ((:same . 2) (:same . :nil)) (0 0) (nil nil) ((:nil) (:nil)) (:list :list) ,(astify '((2)))) (:same . 3) (:same . :nil)))
'(1 (((2))) 3))))
(deftest sexp-wrap-sequence.1 ()
(is (equalp (ast-patch '(1 2 3 4)
`((:same . 1) (:wrap-sequence 2
((:same . 2) (:same . 3) (:insert . :nil))
(0)
(nil) (nil)
(:list)
,(astify '(1 2 3 4)))
(:same . 4) (:same . :nil)))
'(1 (2 3) 4))))
(deftest sexp-unwrap-sequence.1 ()
(is (equalp
(ast-patch '(1 (:a (2 3 4 5) :b) 5)
'((:same . 1)
(:unwrap-sequence
((:same . 3) (:same . 4))
(1)
((:a) (2))
((:b :nil) (5 :nil)))
(:same . 5)
(:same . :nil)))
'(1 3 4 5))))
(deftest sexp-unwrap-sequence.2 ()
(is (equalp
(ast-patch '(1 (2 3 4 5) 5)
'((:same . 1)
(:unwrap-sequence
((:same . 3) (:same . 4))
()
((2))
((5 :nil)))
(:same . 5)
(:same . :nil)))
'(1 3 4 5))))
(deftest sexp-diff-wrap-sequence.1 ()
(is (equalp
(unastify-lisp-diff (ast-diff '(1 2 (3) 4 5) '(1 (2 (3) 4) 5)
:wrap-sequences t))
'((:same . 1)
(:wrap-sequence 3 ((:same . 2) (:same 3) (:same . 4) (:insert . :nil))
nil nil nil nil (2 (3) 4))
(:same . 5) (:same . :nil)))))
(deftest sexp-diff-wrap-sequence.2 ()
(is (equalp
(unastify-lisp-diff (ast-diff '(1 2 (3) 4 5) '(1 (2 (3) . 4) 5)
:wrap-sequences t))
'((:same . 1)
(:wrap-sequence 3 ((:same . 2) (:same 3) (:same . 4)) nil
nil nil nil (2 (3) . 4))
(:same . 5) (:same . :nil)))))
;; Nested sequence wrapping
(deftest sexp-diff-wrap-sequence.3 ()
(is (equalp
(let ((l1 '(1 2 (3 :a (4) :b 5) 6 7))
(l2 '(1 (2 (3 (:a (4) :b) 5) 6) 7)))
(unastify-lisp-diff (ast-diff l1 l2 :wrap-sequences t)))
'((:same . 1)
(:wrap-sequence 3
((:same . 2)
(:recurse (:same . 3)
(:wrap-sequence 3 ((:same . :a) (:same 4)
(:same . :b) (:insert . :nil))
nil nil nil nil (:a (4) :b))
(:same . 5) (:same . :nil))
(:same . 6) (:insert . :nil))
nil nil nil nil (2 (3 (:a (4) :b) 5) 6))
(:same . 7) (:same . :nil)))))
(deftest sexp-diff-simple-sublist-test ()
(multiple-value-bind (script cost)
(ast-diff '(1 '(1 2 3 4) 3 4) '(1 '(1 2 3 4) 3 5))
(declare (ignorable cost))
(multiple-value-bind (script-sublist cost-sublist)
(ast-diff '(1 2 3 4) '(1 2 3 5))
(declare (ignorable cost-sublist))
(is (= (length script) (length script-sublist))
"Sublists have no effect on script when same.")
(is (= (length script) (length script-sublist))
"Sublists have no effect on cost when same."))))
(deftest ast-diff-one-added-at-the-end ()
(multiple-value-bind (diff cost) (ast-diff '(1 2) '(1 2 3))
(is (= 1 cost) "Cost of a single addition at the end is one.")
(is (= 4 (length diff)))))
(deftest ast-diff-recurses-into-subtree.1 ()
(multiple-value-bind (diff cost)
(ast-diff '(1 (1 2 3 4 5) 2) '(1 (1 2 4 5) 3) :base-cost 0)
(is (= 3 cost) "Cost of a sub-tree diff performed recursion.")
(is (equalp '(:same :recurse :replace :same) (mapcar #'car diff)))))
(deftest ast-diff-recurses-into-subtree.2 ()
(multiple-value-bind (diff cost)
(ast-diff '(1 (1 2 3 4 5) 2) '(1 (1 2 4 5) 3) :base-cost 2)
(is (= 7 cost) "Cost of a sub-tree diff performed recursion (expected 7, is ~a)." cost)
(is (equalp '(:same :recurse :replace :same) (mapcar #'car diff)))))
(deftest ast-diff-nested-recursion-into-subtree ()
(multiple-value-bind (diff cost)
(ast-diff '(((1 nil 3)) 3) '(((1 2 3)) 3) :base-cost 0)
(is (= 3 cost) "Cost of a nested sub-tree diff performed recursion.")
(is (equalp '(:recurse :same :same) (mapcar #'car diff)))))
(defun ast-diff-and-patch-equal-p (orig new &rest args &key &allow-other-keys)
(equal? new (ast-patch orig (apply #'ast-diff orig new args))))
(deftest ast-diff-and-patch-is-equal-simple ()
(is (ast-diff-and-patch-equal-p '(1 2 3 4) '(1 2 z 4))))
(deftest ast-diff-and-patch-is-equal-recurse ()
(is (ast-diff-and-patch-equal-p '(1 2 (1 2 3 4) 4) '(1 2 (1 2 z 4) 4))))
(deftest ast-diff-and-patch-is-equal-tail ()
(is (ast-diff-and-patch-equal-p '(1 2 3 . 4) '(1 5 3 . 4))))
(deftest ast-diff-and-patch-unequal-tail ()
(is (ast-diff-and-patch-equal-p '(1 2 3 . 4) '(1 2 3 . 5))))
(deftest ast-diff-with-string ()
(is (ast-diff-and-patch-equal-p '((1 "foo" 2)) '((3 "foo" 2)))))
(deftest ast-diff-patch-wrap.1 ()
(is (ast-diff-and-patch-equal-p '((1)) '(2 ((1)) 3) :wrap t)))
(deftest ast-diff-patch-wrap.2 ()
(is (ast-diff-and-patch-equal-p '(:a :b :c :d ((1)) :e :f :g)
'(:a :b :c :d (2 ((1)) 3) :e :f :g) :wrap t)))
(deftest ast-diff-patch-wrap.3 ()
(is (ast-diff-and-patch-equal-p '((:a :b :c :d :e :f))
'((:a :b (:c :d) :e :f)) :wrap t)))
(deftest ast-diff-patch-wrap.4 ()
(is (ast-diff-and-patch-equal-p '((:a :b :c :d :e :f))
'((:a :b (:c :d) :e :f)) :wrap t :wrap-sequence t)))
(deftest ast-diff-patch-wrap.5 ()
(is (ast-diff-and-patch-equal-p '(1 (2) 3) '(1 ((2)) 3) :wrap t)))
(deftest ast-diff-patch-unwrap.1 ()
(is (ast-diff-and-patch-equal-p '(2 ((1)) 3) '((1)) :wrap t)))
(deftest ast-diff-patch-unwrap.2 ()
(is (ast-diff-and-patch-equal-p '(:a (2 ((1)) 3) :b) '(:a ((1)) :b) :wrap t)))
(deftest ast-diff-simple-dotted-list ()
(is (= 2 (nth-value 1 (ast-diff '(1 . 1) '(1 . 2) :base-cost 0))))
(is (= 4 (nth-value 1 (ast-diff '(1 . 1) '(1 . 2) :base-cost 2)))))
(deftest ast-diff-nested-dotted-list ()
(is (= 2 (nth-value 1 (ast-diff '((1 . 2)) '((1 . 1)) :base-cost 0))))
(is (= 4 (nth-value 1 (ast-diff '((1 . 2)) '((1 . 1)) :base-cost 2)))))
(deftest ast-diff-mixed-proper-improper-list ()
(is (= 2 (nth-value 1 (ast-diff (cons 1 nil) (cons 1 1) :base-cost 0))))
(is (= 4 (nth-value 1 (ast-diff (cons 1 nil) (cons 1 1) :base-cost 2)))))
(deftest ast-diff-double-insert ()
(is (= 2 (nth-value 1 (ast-diff '(1 2 3 4) '(1 2 3 4 5 6))))))
(defun ast-diff-alist-test (al1 al2)
(flet ((%f (alist) (make-instance 'resolve/alist:alist-for-diff
:alist alist)))
(let* ((al-obj1 (%f al1))
(al-obj2 (%f al2))
(diff (ast-diff al-obj1 al-obj2))
(al-obj3 (ast-patch al-obj1 diff)))
(sort (copy-list (resolve/alist:alist-of-alist-for-diff al-obj3))
#'string< :key #'car))))
(deftest ast-diff-alist-diff-1 ()
(is (equal (ast-diff-alist-test nil nil) nil)))
(deftest ast-diff-alist-diff-2 ()
(is (equal (ast-diff-alist-test nil '((a . 1))) '((a . 1)))))
(deftest ast-diff-alist-diff-3 ()
(is (equal (ast-diff-alist-test '((a . 1)) nil) nil)))
(deftest ast-diff-alist-diff-4 ()
(is (equal (ast-diff-alist-test '((a . 1)) '((a . 2))) '((a . 2)))))
(deftest ast-diff-alist-diff-5 ()
(is (equal (ast-diff-alist-test '((a . 1) (b . 3)) '((a . 2) (b . 3)))
'((a . 2) (b . 3)))))
(deftest ast-diff-alist-diff-6 ()
(is (equal (ast-diff-alist-test '((a . 1) (b . 2)) '((b . 2) (a . 1)))
'((a . 1) (b . 2)))))
(deftest ast-diff-alist-diff-7 ()
(is (equal (ast-diff-alist-test '((a . 1)) '((b . 2) (a . 3)))
'((a . 3) (b . 2)))))
(defun apply-printed-diff (diff)
(let* ((diff (regex-replace-all "((?s)\\[-.*?-\\])" diff ""))
(diff (string-replace-all "{+" diff ""))
(diff (string-replace-all "+}" diff "")))
diff))
(deftest print-lisp-diff.1 ()
(let ((v1 (astify '()))
(v2 (astify '())))
(is (equalp (with-output-to-string (s)
(print-diff (ast-diff v1 v2)
v1 v2
:no-color t :stream s))
"")
"Print diff of empty lists")))
(deftest print-lisp-diff.2 ()
(let ((v1 (astify '(())))
(v2 (astify '(()))))
(is (equalp (with-output-to-string (s)
(print-diff (ast-diff v1 v2)
v1 v2
:no-color t :stream s))
"()")
"Print diff of list of empty list")))
(deftest print-lisp-diff.3 ()
(let ((v1 (astify '()))
(v2 (astify '(()))))
(is (equalp (with-output-to-string (s)
(print-diff (ast-diff v1 v2)
v1 v2
:no-color t :stream s))
"{+()+}")
"Print diff of insertion of empty list")))
(deftest print-lisp-diff.4 ()
(let ((v1 (astify '(())))
(v2 (astify '())))
(is (equalp (with-output-to-string (s)
(print-diff (ast-diff v1 v2)
v1 v2
:no-color t :stream s))
"[-()-]")
"Print diff of deletion of empty list")))
(deftest sexp-diff-on-ast-file ()
(with-fixture resolve-asd-file-forms
(is (zerop (nth-value 1 (ast-diff *forms* *forms*)))
"Handles forms from a lisp source file.")))
(deftest has-conflict-node-before-slot-specifiers.test ()
(let ((c (make-instance 'sel/sw/parseable:conflict-ast))
(s (make-instance 'slot-specifier)))
(is (has-conflict-node-before-slot-specifiers (list c)))
(is (not (has-conflict-node-before-slot-specifiers ())))
(is (not (has-conflict-node-before-slot-specifiers (list 'a))))
(is (has-conflict-node-before-slot-specifiers (list 'a c s 'b)))
(is (not (has-conflict-node-before-slot-specifiers (list s c))))))
(deftest standarized-children.test ()
(let* ((g (genome (from-string (make 'javascript) "[1,2,3]")))
(g2 (copy-with-standardized-children
g (standardized-children g))))
(is (equal? g g2))))
;;;; C AST Diff tests
(defsuite c-ast-diff-tests "AST-level diffs of c objects.")
;;; TODO: FIXME
#+broken
(deftest (diff-gets-back-on-track :long-running) ()
(let ((obj1 (from-string (make-instance 'c)
"int a; int b; int c; int d;"))
(obj2 (from-string (make-instance 'c)
"int a; int z; int b; int c; int d;")))
(is (= 8 (nth-value 1 (ast-diff obj1 obj2 :base-cost 0))))
(is (= 12 (nth-value 1 (ast-diff obj1 obj2 :base-cost 2))))
(is (= 6 (nth-value 1 (ast-diff obj1 obj2 :ignore-whitespace t
:base-cost 0))))
(is (= 10 (nth-value 1 (ast-diff obj1 obj2 :ignore-whitespace t
:base-cost 2))))))
(deftest (diff-insert :long-running) ()
(let ((orig (from-string (make-instance 'c)
"int x; int y; int z;"))
(a (from-string (make-instance 'c)
"int a; int x; int y; int z;"))
(b (from-string (make-instance 'c)
"int x; int b; int y; int z;"))
(c (from-string (make-instance 'c)
"int x; int y; int z; int c;")))
(let ((diff-a (ast-diff orig a)))
(is diff-a)
(is (equal? (genome (ast-patch (copy orig) diff-a))
(genome a)))
(is (equal? (mapcar #'car diff-a)
'(:same :same :same :same :same :insert
:recurse :same :same :same :same :same))))
(let ((diff-b (ast-diff orig b)))
(is diff-b)
(is (equal?
(genome (ast-patch (copy orig) diff-b))
(genome b)))
(is (equal? (mapcar #'car diff-b)
'(:same :same :same :same :same :same
:insert :same :same :same :same :same))))
(let ((diff-c (ast-diff orig c)))
(is diff-c)
(is (equal?
(genome (ast-patch (copy orig) diff-c))
(genome c)))
(is (equal? (mapcar #'car diff-c)
'(:same :same :same :same :same :same :same
:same :insert :same :same :same))))))
(deftest (diff-delete :long-running) ()
(let ((orig (from-string (make-instance 'c)
"int x; int y; int z;"))
(a (from-string (make-instance 'c)
"int y; int z;"))
(b (from-string (make-instance 'c)
"int x; int z;"))
(c (from-string (make-instance 'c)
"int x; int y;")))
(let ((diff-a (ast-diff orig a)))
(is diff-a)
(is (equal?
(genome (ast-patch (copy orig) diff-a))
(genome a)))
(is (equal? (mapcar #'car diff-a)
'(:same :same :same :same :same :delete
:recurse :same :same :same :same))))
(let ((diff-b (ast-diff orig b)))
(is diff-b)
(is (equal?
(genome (ast-patch (copy orig) diff-b))
(genome b)))
(is (equal? (mapcar #'car diff-b)
'(:same :same :same :same :same :same
:delete :same :same :same :same))))
(let ((diff-c (ast-diff orig c)))
(is diff-c)
(is (equal?
(genome (ast-patch (copy orig) diff-c))
(genome c)))
(is (equal? (mapcar #'car diff-c)
'(:same :same :same :same :same :same :same
:delete :same :same :same))))))
(deftest (diff-recursive :long-running) ()
(let* ((orig (from-string (make-instance 'c)
"int x = 1; int y = 2; int z = 3;"))
(new (from-string (make-instance 'c)
"int x = 1; int y = 5; int z = 3;"))
(diff (ast-diff orig new)))
(is diff)
(is (equal? (genome (ast-patch (copy orig) diff))
(genome new)))
(is (equal? (mapcar #'car diff)
'(:same :same :same :same :same :same
:recurse :same :same :same :same)))))
(deftest (diff-text-changes :long-running) ()
(let ((orig (from-string (make-instance 'c)
"/* 1 */ int x; /* 2 */ int y; int z; /* 3 */"))
(a (from-string (make-instance 'c)
"/* X */ int x; /* 2 */ int y; int z; /* 3 */"))
(b (from-string (make-instance 'c)
"/* 1 */ int x; /* X */ int y; int z; /* 3 */"))
(c (from-string (make-instance 'c)
"/* 1 */ int x; /* 2 */ int y; int z; /* X */")))
(let ((diff-a (ast-diff orig a)))
(is diff-a)
(is (equal?
(genome (ast-patch (copy orig) diff-a))
(genome a)))
(is (equal? (mapcar #'car diff-a)
'(:same :same :same :same :same
:recurse :same :same :same :same
:same))))
(let ((diff-b (ast-diff orig b)))
(is diff-b)
(is (equal?
(genome (ast-patch (copy orig) diff-b))
(genome b)))
(is (equal? (mapcar #'car diff-b)
'(:same :same :same :same :same :same
:recurse :same :same :same :same))))
(let ((diff-c (ast-diff orig c)))
(is diff-c)
(is (equal?
(genome (ast-patch (copy orig) diff-c))
(genome c)))
(is (equal? (mapcar #'car diff-c)
'(:same :same :same :same :same :same
:same :recurse :same :same :same))))))
(deftest diff-elide-same-test ()
(with-each-fixture (javascript-abacus-variants typescript-abacus-variants)
(let ((orig (aget :orig *variants*))
(bead (aget :bead *variants*)))
(is (every
[{member _ '(:delete :insert :replace :delete-sequence :insert-sequence)}
#'second]
(ast-diff-elide-same (ast-diff orig bead)))))))
(defun keys-of-diff (d)
;; Use tree-sitter-class-name to promote the names of choice
;; subclasses to their class in the tree-sitter grammar so the tests
;; aren't exposed to internal details of the tree-sitter
;; implementation.
(mapcar #'tree-sitter-class-name
(sort (remove-if-not #'symbolp
(copy-list (remove-duplicates (flatten d))))
#'string< :key #'symbol-name)))
(deftest diff-wrap/unwrap.1 ()
(flet ((keys (d) (keys-of-diff d)))
(let* ((s1 "int f() { return 1; }")
(s2 "int f() { return 1+2; }")
(obj1 (from-string (make-instance 'c) s1))
(obj2 (from-string (make-instance 'c) s2)))
(multiple-value-bind (diff cost)
(ast-diff obj1 obj2 :wrap t :max-wrap-diff 1000 :base-cost 0)
(is (equal (keys diff) '(c-binary-expression :delete :recurse :same :wrap)))
(is (= cost 6))
#+debug-print-diff (print-diff diff :no-color t)
(is (source-text= s2 (ast-patch obj1 diff)))
;; TODO
#+debug-print-diff
(is (equal (with-output-to-string (*standard-output*)
(print-diff diff obj1 obj2 :no-color t))
"int f() { return 1{++2+}; }")))
(multiple-value-bind (diff cost)
(ast-diff obj2 obj1 :wrap t :max-wrap-diff 1000 :base-cost 0)
(is (equal (keys diff) '(:insert :recurse :same :unwrap)))
(is (= cost 6))
(is (source-text= s1 (ast-patch obj2 diff)))
#+debug-print-diff
(is (equal (with-output-to-string (*standard-output*)
(print-diff diff :no-color t))
"int f() { return 1[-+2-]; }")))
(multiple-value-bind (diff cost)
(ast-diff obj1 obj2 :wrap t :max-wrap-diff -100 :base-cost 0)
(is (equal (keys diff) '(:recurse :replace :same)))
(is (= cost 10))
(is (equal s2 (source-text (ast-patch obj1 diff)))))
(multiple-value-bind (diff cost)
(ast-diff obj2 obj1 :wrap t :max-wrap-diff -100 :base-cost 0)
(is (equal (keys diff) '(:recurse :replace :same)))
(is (= cost 10))
(is (source-text= s1 (ast-patch obj2 diff)))))))
(deftest diff-sequence-wrap/unwrap.1 ()
(let* ((s1 "int f(int x, int y) { int c = 1; int z = x+y; return z+c; }")
(s2 "int f(int x, int y, int p) { int c = 1; if (p == 0) { int z = x+y; return z+c; } return 0; }")
(obj1 (from-string* (make-instance 'c) s1))
(obj2 (from-string* (make-instance 'c) s2)))
(multiple-value-bind (diff cost)
(ast-diff obj1 obj2 :wrap t :max-wrap-diff 1000
:wrap-sequences t)
(let ((k (keys-of-diff diff)))
(is (equal k '(c-if-statement :insert :recurse :same :wrap-sequence))))
(is (= cost 35))
;; TODO
(is (source-text= s2 (ast-patch obj1 diff)))
#+debug-print-diff
(let ((s (with-output-to-string (*standard-output*)
(print-diff diff :no-color t))))
(is (equal s "int f(int x, int y{+, int p+}) { int c = 1; {+if (p == 0) { +}int z = x+y; return z+c;{+ } return 0;+} }"))))
(multiple-value-bind (diff cost)
(ast-diff obj2 obj1 :wrap t :max-wrap-diff 1000
:wrap-sequences t)
(let ((k (keys-of-diff diff)))
(is (equal k '(:delete :recurse :same :unwrap-sequence))))
(is (= cost 35))
(is (source-text= s1 (ast-patch obj2 diff)))
#+debug-print-diff
(let ((s (with-output-to-string (*standard-output*) (print-diff diff :no-color t))))
(is (equal s "int f(int x, int y[-, int p-]) { int c = 1; [-if (p == 0) { -]int z = x+y; return z+c;[- } return 0;-] }")))
)))
(deftest diff-wrap-patch.1 ()
(let* ((s1 "int f() { return 1; }")
(s2 "int f() { return 1+2; }")
(obj1 (from-string (make-instance 'c) s1))
(obj2 (from-string (make-instance 'c) s2))
(diff (ast-diff obj1 obj2 :wrap t))
(ast1 (genome obj1))
(ast2 (genome obj2))
(ast3 (ast-patch ast1 diff)))
#+debug
(progn
(format t "AST2:~%~a~%----------------~%~s~%"
(source-text ast2) (ast-to-list-form ast2))
(format t "AST3:~%~a~%----------------~%~s~%"
(source-text ast3) (ast-to-list-form ast3)))
(is (equal? ast2 ast3))))
(defun from-string* (class string)
"Like `from-string', but assert there are no parse errors or source
text fragments."
(lret ((result (genome (from-string class string))))
(assert (notany (of-type '(or parse-error-ast
source-text-fragment))
result))))
(defun test-print-diff (v1 v2 &key result base-cost (lang 'c)
ignore-whitespace)
(declare (optimize debug))
(let* ((v1 (if (stringp v1) (from-string lang v1) v1))
(v2 (if (stringp v2) (from-string lang v2) v2))
(diff (multiple-value-call #'ast-diff
v1 v2
(if base-cost
(values :base-cost base-cost)
(values))))
(pdiff (print-diff diff
v1 v2
:no-color t
:stream nil)))
(is (not (source-text= v1 v2)))
(is (not (source-text= pdiff v1)))
(is (not (source-text= pdiff v2)))
;; We actually parsed v1 and v2.
(dolist (v (list v1 v2))
(is (null (collect-if
(of-type '(or parse-error-ast source-text-fragment))
(genome v)))))
(when result
(is (equal pdiff result)))
;; The diff applies.
(mvlet* ((v2-text (source-text v2))
(applied-diff (apply-printed-diff pdiff))
(v2-text applied-diff
(if ignore-whitespace
(values (collapse-whitespace v2-text)
(collapse-whitespace applied-diff))
(values v2-text applied-diff)))
(mismatch (mismatch v2-text applied-diff)))
(is (null mismatch)
"Mismatch: ~s~2%~s~2%~s"
(take 100 (subseq v2-text mismatch))
(take 100 (subseq applied-diff mismatch))
pdiff))))
(deftest print-diff.1 ()
(test-print-diff "int a; int c;"
"int a; int b; int c;"
:result "int a;{+ int b;+} int c;"))
(deftest print-diff.1a ()
"Like print-diff.1, but a deletion instead of an insertion."
(test-print-diff "int a; int b; int c;"
"int a; int c;"
:result "int a;[- int b;-] int c;"))
(deftest print-diff.2 ()
"Print diff of a deletion"
(test-print-diff "int a; int b; int c;"
"int a; int c;"
:result "int a;[- int b;-] int c;"))
(deftest print-diff.3 ()
"Print diff of a replacement"
(test-print-diff "int a; int b; int c;"
"int a; int d; int c;"
:result "int a; int {+d+}[-b-]; int c;"
:base-cost 0))
;; Increasing the base cost makes larger scale replacements
;; more prefered, vs. fine scaled replacement inside strings
(deftest print-diff.3a ()
"Print diff of a replacement"
(test-print-diff "int a; int b; int c;"
"int a; int d; int c;"
:result "int a; int {+d+}[-b-]; int c;"
:base-cost 1))
(deftest print-diff.4 ()
"Print diff of deletion of a character in a string"
(test-print-diff "char *s = \"abcd\";"
"char *s = \"acd\";"
:result "char *s = \"a[-b-]cd\";"
:base-cost 2))
(deftest print-diff.4a ()
"Print diff of deletion of a character in a string"
(test-print-diff "char *s = \"abcd\";"
"char *s = \"acd\";"
:result "char *s = \"a[-b-]cd\";"
:base-cost 3))
(deftest print-diff.5 ()
"Print diff of deletion of substring in a string"
(test-print-diff "char *s = \"abcd\";"
"char *s = \"ad\";"
:result "char *s = \"a[-bc-]d\";"
:base-cost 1))
(deftest print-diff.6 ()
"Print diff of insertion of a substring in a string"
(test-print-diff "char *s = \"ad\";"
"char *s = \"abcd\";"
:result "char *s = \"a{+bc+}d\";"
:base-cost 1))
(deftest print-diff.7 ()
"Print diff of insertion of a character in a string"
(test-print-diff "char *s = \"ad\";"
"char *s = \"abd\";"
:result "char *s = \"a{+b+}d\";"
:base-cost 1))
;; See <https://difftastic.wilfred.me.uk/tricky_cases.html> for
;; tricky-print-diff.
;;; Using Javascript for these since its parser is so robust.
(deftest tricky-print-diff.1 ()
"Adding delimiters."
(test-print-diff "x;"
"(x);"
#+(or) "{+(+}x{+)+}"))
(deftest tricky-print-diff.2 ()