-
Notifications
You must be signed in to change notification settings - Fork 16
/
code_test.go
121 lines (102 loc) · 2.07 KB
/
code_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
package multicodec_test
import (
"flag"
"fmt"
"io"
"strings"
"testing"
"github.com/multiformats/go-multicodec"
)
func TestFlagValue(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in string
want multicodec.Code
wantErr string
}{
{
name: "EmptyString",
in: "",
wantErr: "unknown multicodec",
},
{
name: "Name",
in: "sha1",
want: multicodec.Sha1,
},
{
name: "NumberHex",
in: "0x11",
want: multicodec.Sha1,
},
{
name: "NumberDecimal",
in: "17",
want: multicodec.Sha1,
},
{
name: "ReservedNumber",
in: "0x300012",
want: multicodec.Code(0x300012),
},
{
name: "UnknownName",
in: "some-name",
wantErr: "unknown multicodec",
},
{
name: "UnknownNumber",
in: "0x1000",
wantErr: "unknown multicodec",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var code multicodec.Code
fs.Var(&code, "multicodec", "")
err := fs.Parse([]string{"--multicodec=" + test.in})
if test.wantErr == "" {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
} else {
gotErr := fmt.Sprint(err)
if !strings.Contains(gotErr, test.wantErr) {
t.Fatalf("want error %q, got: %v", test.wantErr, err)
}
}
})
}
}
func TestKnownCodes(t *testing.T) {
t.Parallel()
codes := multicodec.KnownCodes()
if len(codes) < 100 {
t.Fatalf("expected list to have at least 100 elements")
}
seen := map[multicodec.Code]bool{}
missing := map[multicodec.Code]bool{
multicodec.Identity: true,
multicodec.Cidv2: true,
multicodec.Path: true,
multicodec.Md5: true,
}
for _, code := range codes {
// Ipfs is deprecated in favor of Libp2p, and they share the same value.
if seen[code] && code != multicodec.Ipfs {
t.Errorf("duplicate code: %v", code)
}
seen[code] = true
if missing[code] {
delete(missing, code)
}
}
for code := range missing {
t.Errorf("missing code: %v", code)
}
}