-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_test.go
73 lines (65 loc) · 1.55 KB
/
count_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
package human
import (
"encoding/json"
"fmt"
"testing"
yaml "gopkg.in/yaml.v3"
)
func TestCountParse(t *testing.T) {
for _, test := range []struct {
in string
out Count
}{
{in: "0", out: 0},
{in: "1234", out: 1234},
{in: "10.2K", out: 10200},
} {
t.Run(test.in, func(t *testing.T) {
c, err := ParseCount(test.in)
if err != nil {
t.Fatal(err)
}
if c != test.out {
t.Error("parsed count mismatch:", c, "!=", test.out)
}
})
}
}
func TestCountFormat(t *testing.T) {
for _, test := range []struct {
in Count
fmt string
out string
}{
{in: 0, fmt: "%v", out: "0"},
{in: 1234, fmt: "%v", out: "1234"},
{in: 10234, fmt: "%v", out: "10.2K"},
{in: 123456789, fmt: "%d", out: "123456789"},
{in: 123456789, fmt: "%s", out: "123M"},
{in: 123456789, fmt: "%#v", out: "human.Count(1.23456789e+08)"},
} {
t.Run(test.out, func(t *testing.T) {
if s := fmt.Sprintf(test.fmt, test.in); s != test.out {
t.Error("formatted count mismatch:", s, "!=", test.out)
}
})
}
}
func TestCountJSON(t *testing.T) {
testCountEncoding(t, Count(1), json.Marshal, json.Unmarshal)
}
func TestCountYAML(t *testing.T) {
testCountEncoding(t, Count(1), yaml.Marshal, yaml.Unmarshal)
}
func testCountEncoding(t *testing.T, x Count, marshal func(any) ([]byte, error), unmarshal func([]byte, any) error) {
b, err := marshal(x)
if err != nil {
t.Fatal("marshal error:", err)
}
v := Count(0)
if err := unmarshal(b, &v); err != nil {
t.Error("unmarshal error:", err)
} else if v != x {
t.Error("value mismatch:", v, "!=", x)
}
}