-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathalias_test.go
More file actions
115 lines (104 loc) · 2.57 KB
/
Copy pathalias_test.go
File metadata and controls
115 lines (104 loc) · 2.57 KB
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
package claircore
import (
"encoding/json"
"fmt"
"testing"
"unique"
)
func alias(space, name string) Alias {
return Alias{
Space: unique.Make(space),
Name: name,
}
}
func ExampleAlias_uri_slash() {
fmt.Println(alias("https://example.com/", "CVE-2014-0160"))
// Output:
// https://example.com/CVE-2014-0160
}
func ExampleAlias_uri_anchor() {
fmt.Println(alias("https://example.com/cve", "CVE-2014-0160"))
// Output:
// https://example.com/cve#CVE-2014-0160
}
func ExampleAlias_cve() {
fmt.Println(alias("CVE", "2014-0160"))
// Output:
// CVE-2014-0160
}
func ExampleAlias_Equal() {
a, b := alias("CVE", "2014-0160"), alias("CVE", "2014-0160")
fmt.Println("equal:", a.Equal(b))
// Output:
// equal: true
}
func TestAlias(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
t.Run("OK", func(t *testing.T) {
a := Alias{Space: unique.Make("TEST"), Name: "1"}
if got, want := a.Valid(), true; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Zero", func(t *testing.T) {
a := Alias{}
if got, want := a.Valid(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("MissingSpace", func(t *testing.T) {
a := Alias{Name: "1"}
if got, want := a.Valid(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("EmptySpace", func(t *testing.T) {
a := Alias{Space: unique.Make(""), Name: "1"}
if got, want := a.Valid(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("MissingName", func(t *testing.T) {
a := Alias{Space: unique.Make("TEST")}
if got, want := a.Valid(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
})
t.Run("JSON", func(t *testing.T) {
a := Alias{Space: unique.Make("CVE"), Name: "2024-24786"}
data, err := json.Marshal(a)
if err != nil {
t.Fatal(err)
}
want := `{"space":"CVE","name":"2024-24786"}`
if got := string(data); got != want {
t.Fatalf("marshal: got %s, want %s", got, want)
}
var b Alias
if err := json.Unmarshal(data, &b); err != nil {
t.Fatal(err)
}
if !a.Equal(b) {
t.Errorf("unmarshal: got %v, want %v", b, a)
}
})
t.Run("JSONZero", func(t *testing.T) {
a := Alias{}
data, err := json.Marshal(a)
if err != nil {
t.Fatal(err)
}
want := `{"space":"","name":""}`
if got := string(data); got != want {
t.Errorf("marshal: got %s, want %s", got, want)
}
var b Alias
if err := json.Unmarshal(data, &b); err != nil {
t.Fatal(err)
}
if b.Valid() {
t.Errorf("unmarshal zero: expected invalid alias, got valid: %v", b)
}
})
}