forked from apilayer/goiban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiban_test.go
215 lines (184 loc) · 5.4 KB
/
iban_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
214
215
/*
The MIT License (MIT)
Copyright (c) 2014 Chris Grieger
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package goiban
import (
"testing"
//"fmt"
)
func TestCanInstantiateIbanFromValidString(t *testing.T) {
input := "GB29 NWBK 6016 1331 9268 19"
result := ParseToIban(input)
if result == nil {
t.Errorf("Failed to extract BBAN.")
}
}
func TestCanCheckIbanValidity(t *testing.T) {
input := "GB29 NWBK 6016 1331 9268 19"
parsedIban := ParseToIban(input)
result := parsedIban.Validate()
if result.Valid != true {
t.Errorf("Failed to validate BBAN.")
}
}
func TestCanCheckIbanTooLong(t *testing.T) {
input := "GB29 NWBK 6016 1331 9268 19 21"
parsedIban := ParseToIban(input)
if parsedIban != nil {
t.Errorf("BBAN was too long should not have succeeded.")
}
}
func TestCanCheckIbanParseable(t *testing.T) {
input := "GB29 NWBK 6016 1331 9268 19"
result := IsParseable(input)
if result.Valid != true {
t.Errorf("Failed to validate BBAN.")
}
input = "GB29"
result = IsParseable(input)
if result.Valid {
t.Errorf("Failed to validate BBAN.")
}
input = ""
result = IsParseable(input)
if result.Valid {
t.Errorf("Failed to validate BBAN.")
}
}
func TestShouldNotInstantiateIbanFromInvalidString(t *testing.T) {
input := "GB NWBK 6016 1331 9268 19"
result := ParseToIban(input)
if result != nil {
t.Errorf("Instantiated IBAN from invalid string.")
}
input = "1112 NWBK 6016 1331 9268 19"
result = ParseToIban(input)
if result != nil {
t.Errorf("Instantiated IBAN from invalid string.")
}
input = "DE12"
result = ParseToIban(input)
if result != nil {
t.Errorf("Instantiated IBAN from invalid string.")
}
input = "DE"
result = ParseToIban(input)
if result != nil {
t.Errorf("Instantiated IBAN from invalid string.")
}
input = ""
result = ParseToIban(input)
if result != nil {
t.Errorf("Instantiated IBAN from invalid string.")
}
}
func TestExtractValidBBAN(t *testing.T) {
input := "GB29 NWBK 6016 1331 9268 19"
result, _ := extractBBAN(input)
if result.Data != "NWBK60161331926819" {
t.Errorf("Failed to extract BBAN.")
}
}
func TestExtractInvalidBBAN(t *testing.T) {
input := "GB29"
var ok bool
_, ok = extractBBAN(input)
if ok {
t.Errorf("Extracted invalid BBAN.")
}
input = "GB29 NWBK 6016 1331 9268 19GB29 NWBK 6016 1331 9268 19GB29 NWBK 6016 1331 9268 19"
_, ok = extractBBAN(input)
if ok {
t.Errorf("Extracted invalid BBAN.")
}
input = "GB29 NWBK 6016 1331 9268 19 DURR"
_, ok = extractBBAN(input)
if ok {
t.Errorf("Extracted invalid BBAN. (length restriction by map)")
}
}
func TestExtractValidCountryCode(t *testing.T) {
input := "DE"
if ExtractCountryCode(input) != input {
t.Errorf("Failed to extract country code.")
}
}
func TestExtractInvalidCountryCode(t *testing.T) {
input := "D1"
if ExtractCountryCode(input) == input {
t.Errorf("Extracted invalid country code.")
}
input = ""
if ExtractCountryCode(input) != "" {
t.Errorf("Extracted invalid country code.")
}
}
func TestExtractCheckDigit(t *testing.T) {
input := "DE12"
if extractCheckDigit(input) != "12" {
t.Errorf("Failed to extract check digit.")
}
}
func TestExtractInvalidCheckDigit(t *testing.T) {
input := "D1"
if extractCheckDigit(input) == input {
t.Errorf("Extracted invalid check digit.")
}
input = ""
if extractCheckDigit(input) != "" {
t.Errorf("Extracted invalid check digit.")
}
}
func TestCountryCodeToNumericString(t *testing.T) {
input := "DE"
result := countryCodeToNumericString(input)
if result != "1314" {
t.Errorf("Can't convert country code to numeric string: " + result)
}
}
func TestInvalidCountryCodeToNumericString(t *testing.T) {
input := ""
result := countryCodeToNumericString(input)
if result != "" {
t.Errorf("Converted invalid code!")
}
input = "DEE"
result = countryCodeToNumericString(input)
if result != "" {
t.Errorf("Converted invalid code!")
}
}
func TestCanGenerateIBANFromValidData(t *testing.T) {
result := CalculateIBAN("BE", "539", "007547034")
if !result.Valid {
t.Errorf("expected result to be valid")
}
if result.Data != "BE68539007547034" {
t.Errorf("expected result iban to match, %v != %v", result.Data, "BE68539007547034")
}
}
func TestCanGenerateIBANFromValidDataEnsureCheckdigitsLeadingZeroPresent(t *testing.T) {
result := CalculateIBAN("DE", "1", "9")
if !result.Valid {
t.Errorf("expected result to be valid")
}
if result.Data != "DE22000000010000000009" {
t.Errorf("expected result iban to match, %v != %v", result.Data, "DE22000000010000000009")
}
}