-
Notifications
You must be signed in to change notification settings - Fork 28
/
rg_test.go
140 lines (134 loc) · 5.24 KB
/
rg_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
package brdoc
import (
"testing"
)
func TestIsRG(t *testing.T) {
for i, tc := range []struct {
name string
expectedIsValid bool
expectedErr error
v string
uf FederativeUnit
}{
{"InvalidData_ShouldReturnFalse", false, nil, "3467875434578764345789654", SP},
{"InvalidData_ShouldReturnFalse", false, nil, "", SP},
{"InvalidData_ShouldReturnFalse", false, nil, "AAAAAAA", SP},
{"InvalidData_ShouldReturnFalse", false, nil, "34.112.513-A", SP},
{"InvalidData_ShouldReturnFalse", false, nil, "34.112.513-X", SP},
{"InvalidData_ShouldReturnFalse", false, nil, "34.112.513-x", SP},
{"InvalidCheckDigit_ShouldReturnFalse", false, nil, "34.112.513-9", SP},
{"ValidFormat_ShouldReturnTrue", true, nil, "34.112.513-1", SP},
{"ValidFormat_ShouldReturnTrue", true, nil, "341125131", SP},
{"ValidFormat_ShouldReturnTrue", true, nil, "25.540.324-0", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "255403240", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "39.406.714-9", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "24.454.119-X", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "24454119X", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "24.454.119-x", RJ},
{"ValidFormat_ShouldReturnTrue", true, nil, "24454119x", RJ},
{"InvalidData_ShouldReturnFalse", false, nil, "39.406.714-X", RJ},
{"InvalidData_ShouldReturnFalse", false, nil, "39.406.714-0", RJ},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "39.406.714-0", PR},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "25.540.324-0", ES},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "255403240", BA},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "39.406.714-9", MT},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "24.454.119-X", RS},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "24.454.119-X", RS},
{"NotImplementedUF_ShouldReturnError", false, errNotImplementedUF, "24454119X", DF},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
isValid, err := IsRG(tc.v, tc.uf)
assertEqual(t, tc.expectedErr, err)
if err == nil {
assertEqual(t, tc.expectedIsValid, isValid)
}
})
}
}
func TestCalculateRGCheckDigit(t *testing.T) {
for i, tc := range []struct {
name string
expected int
v string
}{
{"CalculateRGCheckDigit_ShouldReturn_1", 1, "34.112.513-1"},
{"CalculateRGCheckDigit_ShouldReturn_1", 1, "341125131"},
{"CalculateRGCheckDigit_ShouldReturn_9", 9, "39.406.714-9"},
{"CalculateRGCheckDigit_ShouldReturn_9", 9, "394067149"},
{"CalculateRGCheckDigit_ShouldReturn_11", 11, "25.540.324-0"},
{"CalculateRGCheckDigit_ShouldReturn_11", 11, "255403240"},
{"CalculateRGCheckDigit_ShouldReturn_10", 10, "24.454.119-X"},
{"CalculateRGCheckDigit_ShouldReturn_10", 10, "24454119X"},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
assertEqual(t, tc.expected, calculateRGCheckDigit(tc.v))
})
}
}
func TestGetRGCheckDigit(t *testing.T) {
for i, tc := range []struct {
name string
expected int
v string
}{
{"GetRGCheckDigit_ShouldReturn_1", 1, "34.112.513-1"},
{"GetRGCheckDigit_ShouldReturn_1", 1, "341125131"},
{"GetRGCheckDigit_ShouldReturn_9", 9, "39.406.714-9"},
{"GetRGCheckDigit_ShouldReturn_9", 9, "394067149"},
{"GetRGCheckDigit_ShouldReturn_11", 11, "25.540.324-0"},
{"GetRGCheckDigit_ShouldReturn_11", 11, "255403240"},
{"GetRGCheckDigit_ShouldReturn_10", 10, "24.454.119-X"},
{"GetRGCheckDigit_ShouldReturn_10", 10, "24454119X"},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
assertEqual(t, tc.expected, getRGCheckDigit(tc.v))
})
}
}
func TestCleanNonRGDigits(t *testing.T) {
for i, tc := range []struct {
name string
expected string
v string
}{
{"CleanNonRGDigits", "341125131", "34.112.513-1"},
{"CleanNonRGDigits", "341125131", "341125131"},
{"CleanNonRGDigits", "394067149", "39.406.714-9"},
{"CleanNonRGDigits", "394067149", "394067149"},
{"CleanNonRGDigits", "24454119X", "24.454.119-X"},
{"CleanNonRGDigits", "24454119X", "24454119X"},
{"CleanNonRGDigits", "24454119x", "24.454.119-x"},
{"CleanNonRGDigits", "24454119x", "24454119x"},
{"CleanNonRGDigits", "24454119x", "24//45///41--1.9x"},
{"CleanNonRGDigits", "", "AAaaaAAaaa"},
{"CleanNonRGDigits", "XxXxXx", "XxXxXx"},
{"CleanNonRGDigits", "XxXxXx", "XxXyxXx"},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
assertEqual(t, tc.expected, cleanNonRGDigits(tc.v))
})
}
}
func TestIsRGValidDigit(t *testing.T) {
for i, tc := range []struct {
name string
expected bool
v rune
}{
{"ValidDigit_ShouldReturnTrue", true, 'x'},
{"ValidDigit_ShouldReturnTrue", true, 'X'},
{"ValidDigit_ShouldReturnTrue", true, '1'},
{"ValidDigit_ShouldReturnTrue", true, '2'},
{"ValidDigit_ShouldReturnTrue", true, '3'},
{"ValidDigit_ShouldReturnTrue", true, '9'},
{"ValidDigit_ShouldReturnTrue", true, '0'},
{"InvalidDigit_ShouldReturnFalse", false, 'Y'},
{"InvalidDigit_ShouldReturnFalse", false, 'A'},
{"InvalidDigit_ShouldReturnFalse", false, '-'},
{"InvalidDigit_ShouldReturnFalse", false, '.'},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
assertEqual(t, tc.expected, isRGValidDigit(tc.v))
})
}
}