Fix left-most diff cases.
[lightning.git] / hgvs / diff_test.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package hgvs
6
7 import (
8         "strings"
9         "testing"
10
11         "gopkg.in/check.v1"
12 )
13
14 func Test(t *testing.T) { check.TestingT(t) }
15
16 type diffSuite struct{}
17
18 var _ = check.Suite(&diffSuite{})
19
20 func (s *diffSuite) TestDiff(c *check.C) {
21         for _, trial := range []struct {
22                 a      string
23                 b      string
24                 expect []string
25         }{
26                 {
27                         a:      "aaaaaaaaaa",
28                         b:      "aaaaCaaaaa",
29                         expect: []string{"5A>C"},
30                 },
31                 {
32                         a:      "aaaacGcaaa",
33                         b:      "aaaaccaaa",
34                         expect: []string{"6del"},
35                 },
36                 {
37                         a:      "aaaacGGcaaa",
38                         b:      "aaaaccaaa",
39                         expect: []string{"6_7del"},
40                 },
41                 {
42                         a:      "aaaac",
43                         b:      "aaaa",
44                         expect: []string{"5del"},
45                 },
46                 {
47                         a:      "aaaa",
48                         b:      "aaCaa",
49                         expect: []string{"2_3insC"},
50                 },
51                 {
52                         a:      "aaGGGtt",
53                         b:      "aaCCCtt",
54                         expect: []string{"3_5delinsCCC"},
55                 },
56                 {
57                         a:      "aa",
58                         b:      "aaCCC",
59                         expect: []string{"2_3insCCC"},
60                 },
61                 {
62                         a:      "aaGGttAAtttt",
63                         b:      "aaCCttttttC",
64                         expect: []string{"3G>C", "4G>C", "7_8del", "12_13insC"},
65                 },
66                 {
67                         // without cleanup, diffmatchpatch solves this as {"3del", "=A", "4_5insA"}
68                         a:      "aggaggggg",
69                         b:      "agAaggggg",
70                         expect: []string{"3G>A"},
71                 },
72                 {
73                         // without cleanup, diffmatchpatch solves this as {"3_4del", "=A", "5_6insAA"}
74                         a:      "agggaggggg",
75                         b:      "agAAaggggg",
76                         expect: []string{"3G>A", "4G>A"},
77                 },
78                 {
79                         // without cleanup, diffmatchpatch solves this as {"3_4del", "=A", "5_6insCA"}
80                         a:      "agggaggggg",
81                         b:      "agACaggggg",
82                         expect: []string{"3G>A", "4G>C"},
83                 },
84                 {
85                         // without cleanup, diffmatchpatch solves this as {"3_7del", "=A", "8_9insAAACA"}
86                         a:      "aggggggaggggg",
87                         b:      "agAAAACaggggg",
88                         expect: []string{"3_7delinsAAAAC"},
89                 },
90                 {
91                         // without cleanup, diffmatchpatch solves this as {"3_7del", "=AAAA", "11_12insCAAAA"}
92                         a:      "aggggggaaaaggggg",
93                         b:      "agAAAACaaaaggggg",
94                         expect: []string{"3_7delinsAAAAC"},
95                 },
96                 {
97                         a:      "agggaggggg",
98                         b:      "agCAaggggg",
99                         expect: []string{"3G>C", "4G>A"},
100                 },
101                 {
102                         a:      "agggg",
103                         b:      "agAAg",
104                         expect: []string{"3G>A", "4G>A"},
105                 },
106                 {
107                         a:      "aggggg",
108                         b:      "agAAAg",
109                         expect: []string{"3_5delinsAAA"},
110                 },
111                 {
112                         a:      "acgtgaa",
113                         b:      "acTtgaa",
114                         expect: []string{"3G>T"},
115                 },
116                 {
117                         a:      "tcagaagac",
118                         b:      "tcaAaagac",
119                         expect: []string{"4G>A"},
120                 },
121                 {
122                         a:      "tcagatggac",
123                         b:      "tcaAaCggac",
124                         expect: []string{"4G>A", "6T>C"},
125                 },
126                 {
127                         a:      "tcagatggac",
128                         b:      "tcaAaCggTc",
129                         expect: []string{"4G>A", "6T>C", "9A>T"},
130                 },
131                 {
132                         a:      "tcagatggac",
133                         b:      "tcaAaCCggTc",
134                         expect: []string{"4G>A", "6delinsCC", "9A>T"},
135                 },
136                 {
137                         a:      "tcatagagac",
138                         b:      "tcacaagac",
139                         expect: []string{"4T>C", "6del"},
140                 },
141                 {
142                         a:      "tcatcgagac",
143                         b:      "tcGCcgagac",
144                         expect: []string{"3A>G", "4T>C"},
145                 },
146                 {
147                         a:      "tcatcgagac",
148                         b:      "tcGCcggac",
149                         expect: []string{"3A>G", "4T>C", "7del"},
150                 },
151                 {
152                         // should delete leftmost
153                         a:      "acgacaTTtttacac",
154                         b:      "acgacatttacac",
155                         expect: []string{"7_8del"},
156                 },
157                 {
158                         // should insert leftmost
159                         a:      "acgacatttacac",
160                         b:      "acgacaTTtttacac",
161                         expect: []string{"6_7insTT"},
162                 },
163                 {
164                         a:      "ccccaGATAtat",
165                         b:      "ccccatat",
166                         expect: []string{"6_9del"},
167                 },
168                 {
169                         a:      "aGATAtat",
170                         b:      "atat",
171                         expect: []string{"2_5del"},
172                 },
173         } {
174                 c.Log(trial)
175                 var vars []string
176                 diffs, _ := Diff(strings.ToUpper(trial.a), strings.ToUpper(trial.b), 0)
177                 for _, v := range diffs {
178                         vars = append(vars, v.String())
179                 }
180                 c.Check(vars, check.DeepEquals, trial.expect)
181         }
182 }