-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_test.go
213 lines (199 loc) Β· 5.77 KB
/
parse_test.go
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
package cloningprimer
import (
"errors"
"testing"
)
type testCaseEnzyme struct {
in string
want map[string]RestrictEnzyme
err error
}
type testCaseSequence struct {
in string
want string
err error
}
func TestParseEnzymesFromFile(t *testing.T) {
cases := []testCaseEnzyme{
// test invalid file extension
{
in: "tests/parse1.seq",
want: nil,
err: errors.New("invalid input: tests/parse1.seq is not a *.re file (see ./doc.go for more information)"),
},
// test non-existing file
{
in: "tests/doesnotexist.re",
want: nil,
err: errors.New("error opening file: open tests/doesnotexist.re: no such file or directory"),
},
// test correct parsing of enzymes from a file without comments: `parse1.re'
{
in: "tests/parse1.re",
want: map[string]RestrictEnzyme{
"AclI": {
Name: "AclI",
RecognitionSite: "AACGTT",
NoPalinCleav: "no",
},
},
err: nil,
},
// test correct parsing of enzymes from a file with comments: `parse2.re'
{
in: "tests/parse2.re",
want: map[string]RestrictEnzyme{
"AclI": {
Name: "AclI",
RecognitionSite: "AACGTT",
NoPalinCleav: "no",
ID: "A1A1",
Isoschizomeres: []string{"AclI"},
},
},
err: nil,
},
// test correct parsing of enzymes from a file with comments but no column labels: `parse2.re'
// also, two enzymes are passed instead of just one
{
in: "tests/parse3.re",
want: map[string]RestrictEnzyme{
"AclI": {
Name: "AclI",
RecognitionSite: "AACGTT",
NoPalinCleav: "no",
ID: "A1A1",
Isoschizomeres: []string{"AclI"},
},
"AclII": {
Name: "AclII",
RecognitionSite: "ACCGGT",
NoPalinCleav: "no",
ID: "A2A2",
Isoschizomeres: []string{"AclII", "AclIII"},
},
},
err: nil,
},
}
// loop over test cases
for _, c := range cases {
got, err := ParseEnzymesFromFile(c.in)
// test similarity of expected and received value
if !isSimilarMap(got, c.want) {
t.Errorf("ParseEnzymesFromFile(%v) == %v, want %v\n", c.in, got, c.want)
}
// if no error is returned, test if none is expected
if err == nil && c.err != nil {
t.Errorf("ParseEnzymesFromFile(%v) == %v, want %v\n", c.in, err, c.err)
}
// if error is returned, test if an error is expected
if err != nil {
// if c.err is nil, print wanted and received errors
// else if an error is wanted and received but error messages are not the same
// print wanted and received error
if c.err == nil {
t.Errorf("ParseEnzymesFromFile(%v) == %v, want %v\n", c.in, err, c.err)
} else if err.Error() != c.err.Error() {
t.Errorf("ParseEnzymesFromFile(%v) == %v, want %v\n", c.in, err, c.err)
}
}
}
}
func TestParseSequenceFromFile(t *testing.T) {
cases := []testCaseSequence{
// test invalid file extension
{
in: "tests/parse1.re",
want: "",
err: errors.New("invalid input: tests/parse1.re is not a *.seq file (see ./doc.go for more information)"),
},
// test non-existing file
{
in: "tests/doesnotexist.seq",
want: "",
err: errors.New("error opening file: open tests/doesnotexist.seq: no such file or directory"),
},
// test correct parsing of sequence from a file without comments: `parse1.seq'
{
in: "tests/parse1.seq",
want: "ATGGCCGCGT",
err: nil,
},
// test correct parsing of enzymes from a file with comments: `parse2.seq'
{
in: "tests/parse2.seq",
want: "ATGGCCGCGT",
err: nil,
},
// test correct parsing of sequence from a file with additional white spaces and returns: `parse3.seq'
{
in: "tests/parse3.seq",
want: "ATGGCCGCGTTGACGAGTGAGCATAGGCA",
err: nil,
},
// test parsing of sequence with non-nucleotide letters from a file: `parse4.seq'
{
in: "tests/parse4.seq",
want: "ATTATGA",
err: errors.New("invalid letter in nucleotide sequence: Q at position 7"),
},
}
// loop over test cases
for _, c := range cases {
got, err := ParseSequenceFromFile(c.in)
// test similarity of expected and received value
if got != c.want {
t.Errorf("ParseSequenceFromFile(%v) == %v, want %v\n", c.in, got, c.want)
}
// if no error is returned, test if none is expected
if err == nil && c.err != nil {
t.Errorf("ParseSequenceFromFile(%v) == %v, want %v\n", c.in, err, c.err)
}
// if error is returned, test if an error is expected
if err != nil {
// if c.err is nil, print wanted and received errors
// else if an error is wanted and received but error messages are not the same
// print wanted and received error
if c.err == nil {
t.Errorf("ParseSequenceFromFile(%v) == %v, want %v\n", c.in, err, c.err)
} else if err.Error() != c.err.Error() {
t.Errorf("ParseSequenceFromFile(%v) == %v, want %v\n", c.in, err, c.err)
}
}
}
}
// isSimilarMap only tests if all keys in `m1' exist in `m2' and if all mapped `RestrictEnzyme'
// structs have matching values for their fields
// this function is not robust if invalid input is provided and should only be used for testing purposes!
func isSimilarMap(m1, m2 map[string]RestrictEnzyme) bool {
if (m1 == nil) && (m2 == nil) {
return true
}
if (m1 == nil) || (m2 == nil) {
return false
}
for k, v := range m1 {
// test fields of type string
if val, ok := m2[k]; !ok {
return false
} else if val.Name != v.Name {
return false
} else if val.RecognitionSite != v.RecognitionSite {
return false
} else if val.NoPalinCleav != v.NoPalinCleav {
return false
} else if val.ID != v.ID {
return false
} else if len(val.Isoschizomeres) != len(v.Isoschizomeres) {
return false
} else {
for i := 0; i < len(val.Isoschizomeres); i++ {
if val.Isoschizomeres[i] != v.Isoschizomeres[i] {
return false
}
}
}
}
return true
}