-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_test.go
116 lines (111 loc) · 2.31 KB
/
strings_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
// Copyright (c) 2023–2024 The convert developers. All rights reserved.
// Project site: https://github.com/gotmc/convert
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package convert
import (
"testing"
)
func TestStringFloats(t *testing.T) {
testCases := []struct {
given string
sep string
expected []float64
}{
{
given: "0.001,0.002,0.003",
sep: ",",
expected: []float64{0.001, 0.002, 0.003},
},
{
given: "10.0,20.0,30.0,40.0",
sep: ",",
expected: []float64{10.0, 20.0, 30.0, 40.0},
},
}
for _, tc := range testCases {
calcs, err := StringToFloats(tc.given, tc.sep)
if err != nil {
t.Errorf("error parsing slice of strings into floats: %s", err)
}
for i, calc := range calcs {
if calc != tc.expected[i] {
t.Errorf(
"given %s / index %d expected = %f / calculated = %f",
tc.given,
i,
tc.expected[i],
calc,
)
}
}
}
}
func TestStringNFloats(t *testing.T) {
testCases := []struct {
given string
sep string
numExpected int
expected []float64
}{
{
given: "0.001,0.002,0.003",
sep: ",",
numExpected: 3,
expected: []float64{0.001, 0.002, 0.003},
},
{
given: "10.0,20.0,30.0,40.0",
sep: ",",
numExpected: 4,
expected: []float64{10.0, 20.0, 30.0, 40.0},
},
}
for _, tc := range testCases {
calcs, err := StringToNFloats(tc.given, tc.sep, tc.numExpected)
if err != nil {
t.Errorf("error parsing slice of strings into floats: %s", err)
}
for i, calc := range calcs {
if calc != tc.expected[i] {
t.Errorf(
"given %s / index %d expected = %f / calculated = %f",
tc.given,
i,
tc.expected[i],
calc,
)
}
}
}
}
func TestStripDoubleQuotes(t *testing.T) {
testCases := []struct {
given string
expected string
}{
{
given: "\"0.001,0.002\"",
expected: "0.001,0.002",
},
{
given: "\"0.001,0.002\"\n",
expected: "0.001,0.002",
},
{
given: "\"foo,bash\"\n",
expected: "foo,bash",
},
}
for _, tc := range testCases {
got := StripDoubleQuotes(tc.given)
if got != tc.expected {
t.Errorf(
"given %s / expected = %s / got = %s",
tc.given,
tc.expected,
got,
)
}
}
}