-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_test.go
159 lines (138 loc) · 3.03 KB
/
field_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
package dsv
import (
"errors"
"testing"
"github.com/tj/assert"
)
func TestSortFields(t *testing.T) {
fieldsAssert := []field{
field{
index: -1,
}, field{
index: 0,
}, field{
index: 0,
}, field{
index: 1,
}, field{
index: 2,
}, field{
index: 3,
}, field{
index: 4,
}, field{
index: 5,
}, field{
index: 7,
}, field{
index: 7,
}, field{
index: 12,
},
}
fields := []field{
field{
index: 7,
}, field{
index: 2,
}, field{
index: 0,
}, field{
index: 0,
}, field{
index: 12,
}, field{
index: 5,
}, field{
index: 7,
}, field{
index: 3,
}, field{
index: 4,
}, field{
index: 1,
}, field{
index: -1,
},
}
sortedFields, err := sortFields(fields)
if err != nil {
t.Fatal("Error should be nil")
}
assert.EqualValues(t, fieldsAssert, sortedFields, "Arrays should be equals")
}
func TestPaddingValueWithFiftyEmpty(t *testing.T) {
assertFieldValue := " 123456789"
fieldTest := field{
index: 1,
length: 50,
paddingChar: " ",
paddingRight: false,
value: "123456789",
}
fieldWithPad, err := padValue(fieldTest)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, assertFieldValue, fieldWithPad, "Value should be with padding of 41 empty spaces")
}
func TestPaddingValueWithFourZeros(t *testing.T) {
assertFieldValue := "0000123456789"
fieldTest := field{
index: 1,
length: 13,
paddingChar: "0",
paddingRight: false,
value: "123456789",
}
fieldWithPad, err := padValue(fieldTest)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, assertFieldValue, fieldWithPad, "Value should be with padding of 4 zeros")
}
func TestPaddingValueWithRightLength(t *testing.T) {
assertFieldValue := "123456789"
fieldTest := field{
index: 1,
length: 9,
paddingChar: "0",
paddingRight: false,
value: "123456789",
}
fieldWithPad, err := padValue(fieldTest)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, assertFieldValue, fieldWithPad, "Value should keep unchanged")
}
func TestPaddingValueShouldNotAcceptPaddingCharWithMoreThanOneCharacter(t *testing.T) {
paddingCharErrorAssert := errors.New("The padding char should be one char length")
fieldTest := field{
index: 1,
length: 10,
paddingChar: "00",
paddingRight: false,
value: "123456789",
}
_, err := padValue(fieldTest)
if err == nil {
t.Fatalf("padValue should return an error about padding char length")
}
assert.EqualValues(t, paddingCharErrorAssert, err, "Error should be about padding char length")
}
func TestPaddingValueRight(t *testing.T) {
assertFieldValue := "1234567890000"
fieldTest := field{
index: 1,
length: 13,
paddingChar: "0",
paddingRight: true,
value: "123456789",
}
fieldWithPad, err := padValue(fieldTest)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, assertFieldValue, fieldWithPad, "Value should be with padding of 4 zeros in the right side of the string")
}