-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffparser_test.go
136 lines (108 loc) · 4.28 KB
/
ffparser_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
package ffparser
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type TestPojo1 struct {
Name string `record:"start=1,end=10"`
Address string `record:"start=11,end=30"`
PhoneNumber string `record:"start=31,end=40"`
Other string
}
type TestPojo2 struct {
Name string `record:"start=1,end=10,padchar=*"`
Address string `record:"start=11,end=30,padalign=left"`
PhoneNumber string `record:"start=31,end=50,padchar=0,padalign=left"`
Other string
}
type TestDecorator1 struct {
NumInt int `record:"start=1,end=5,decorator=IntDecorator"`
NumInt64 int64 `record:"start=6,end=10,decorator=Int64Decorator"`
NumFloat64 float64 `record:"start=11,end=25,decorator=BrazilMoneyDecorator"`
NumFloat64_2 float64 `record:"start=26,end=40,decorator=BrazilMoneyDecorator"`
DataHora time.Time `record:"start=41,end=54,decorator=DateTimeDecorator"`
DataHoraBR time.Time `record:"start=55,end=62,decorator=BrazilDateDecorator"`
}
func TestShouldParseTextToStructWithAutoDetectFieldType(t *testing.T) {
parser := NewSimpleParser()
type AutoDetectStruct struct {
Value1 time.Time `record:"start=1,end=21"`
Value2 int `record:"start=22,end=32"`
Value3 int64 `record:"start=33,end=43"`
Value4 float64 `record:"start=44,end=54"`
Value5 string `record:"start=55,end=65"`
}
result, _ := parser.ParseToText(&AutoDetectStruct{
Value1: time.Date(2017, 5, 10, 0, 0, 0, 0, time.UTC),
Value2: 12402,
Value3: 45678,
Value4: 4567833.22,
Value5: "be happy",
})
assert.Equal(t, "10052017000000 000000124020000004567804567833.22be happy ", result)
}
func TestShouldCreateFromTextToStructWithAutoDetectFieldType(t *testing.T) {
textoParaConversao := "be happy "
parser := NewSimpleParser()
type AutoDetectStruct struct {
Value string `record:"start=1,end=10"`
}
var objeto AutoDetectStruct
err := parser.CreateFromText(&objeto, textoParaConversao)
assert.Nil(t, err)
assert.Equal(t, "be happy", objeto.Value)
}
func TestShouldParseTextToStructWithAutoDetectFieldTypeAndDecorators(t *testing.T) {
parser := NewSimpleParser()
result, _ := parser.ParseToText(&TestDecorator1{
NumInt: 1,
NumInt64: int64(402),
NumFloat64: 178.5,
NumFloat64_2: 167.0,
DataHora: time.Date(2020, 3, 31, 18, 55, 2, 0, time.UTC),
})
assert.Equal(t, "00001004020000000000178500000000000167003103202018550200000000", result)
}
func TestShouldCreateFromTextAutoDetectFieldTypeAndDecorators(t *testing.T) {
texto := "00001124020000000000178500000000000167003103202018550200000000"
var estrutura TestDecorator1
parser := NewSimpleParser()
parser.CreateFromText(&estrutura, texto)
dataHoraEsperada := time.Date(2020, 3, 31, 18, 55, 2, 0, time.UTC)
var dataBrEsperada time.Time
assert.Equal(t, 1, estrutura.NumInt)
assert.Equal(t, int64(12402), estrutura.NumInt64)
assert.Equal(t, 178.5, estrutura.NumFloat64)
assert.Equal(t, 167.0, estrutura.NumFloat64_2)
assert.Equal(t, dataHoraEsperada.String(), estrutura.DataHora.String())
assert.Equal(t, dataBrEsperada, estrutura.DataHoraBR)
}
func TestShouldCreateFromTextAutoDetectFieldTypeAndDecoratorsBlankStrings(t *testing.T) {
texto := "0000112402 "
var estrutura TestDecorator1
parser := NewSimpleParser()
parser.CreateFromText(&estrutura, texto)
var dataHoraEsperada time.Time
var dataBrEsperada time.Time
assert.Equal(t, 1, estrutura.NumInt)
assert.Equal(t, int64(12402), estrutura.NumInt64)
assert.Equal(t, 0.0, estrutura.NumFloat64)
assert.Equal(t, 0.0, estrutura.NumFloat64_2)
assert.Equal(t, dataHoraEsperada, estrutura.DataHora)
assert.Equal(t, dataBrEsperada, estrutura.DataHoraBR)
}
func TestShouldCreateFromTextAutoDetectFieldTypeAndDecoratorsZeros(t *testing.T) {
texto := "00000000000000000000000000000000000000000000000000000000000000"
var estrutura TestDecorator1
parser := NewSimpleParser()
parser.CreateFromText(&estrutura, texto)
var dataHoraEsperada time.Time
var dataBrEsperada time.Time
assert.Equal(t, 0, estrutura.NumInt)
assert.Equal(t, int64(0), estrutura.NumInt64)
assert.Equal(t, float64(0.0), estrutura.NumFloat64)
assert.Equal(t, float64(0.0), estrutura.NumFloat64_2)
assert.Equal(t, dataHoraEsperada, estrutura.DataHora)
assert.Equal(t, dataBrEsperada, estrutura.DataHoraBR)
}