forked from periph/conn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_test.go
107 lines (101 loc) · 1.97 KB
/
volume_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
package unit
import "testing"
func TestVolume_String(t *testing.T) {
if s := Volume(3785 * MilliLitre).String(); s != "3.785L" {
t.Fatalf("%#v", s)
}
}
func TestVolume_Set(t *testing.T) {
succeeds := []struct {
in string
expected Volume
}{
{"1nL", NanoLitre},
{"1uL", MicroLitre},
{"1µL", MicroLitre},
{"1mL", MilliLitre},
{"1L", Litre},
{"1kL", KiloLitre},
{"1ML", MegaLitre},
{"1GL", GigaLitre},
// Maximum and minimum values that are allowed.
{"9.223372036854775807GL", 9223372036854775807},
{"-9.223372036854775807GL", -9223372036854775807},
}
fails := []struct {
in string
err string
}{
{
"10EL",
"unknown unit prefix; valid prefixes for \"L\" are n,u,µ,m,k,M, or G",
},
{
"10",
"no unit provided; need L",
},
{
"9.224GL",
"maximum value is 9.223GL",
},
{
"-9.224GL",
"minimum value is -9.223GL",
},
{
"9223372036854775808nL",
"maximum value is 9.223GL",
},
{
"-9223372036854775808nL",
"minimum value is -9.223GL",
},
{
"1random",
"unknown unit provided; need L",
},
{
"L",
"not a number",
},
{
"RPM",
"does not contain number or unit L",
},
{
"++1L",
"contains multiple plus symbols",
},
{
"--1L",
"contains multiple minus symbols",
},
{
"+-1L",
"contains both plus and minus symbols",
},
{
"1.1.1.1L",
"contains multiple decimal points",
},
{
string([]byte{0x33, 0x01}),
"unexpected end of string",
},
}
for i, tt := range succeeds {
var got Volume
if err := got.Set(tt.in); err != nil {
t.Errorf("#%d: Volume.Set(%s) unexpected error: %v", i, tt.in, err)
}
if got != tt.expected {
t.Errorf("#%d: Volume.Set(%s) wanted: %v(%d) but got: %v(%d)", i, tt.in, tt.expected, tt.expected, got, got)
}
}
for i, tt := range fails {
var got Volume
if err := got.Set(tt.in); err == nil || err.Error() != tt.err {
t.Errorf("#%d: Volume.Set(%s) \nexpected: %s\ngot: %s", i, tt.in, tt.err, err)
}
}
}