-
Notifications
You must be signed in to change notification settings - Fork 13
/
hashes_test.go
181 lines (174 loc) · 3.98 KB
/
hashes_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package brutalinks
import (
"reflect"
"testing"
)
func TestHashFromString(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
val string
want Hash
}{
{
name: "random valid value",
val: "6435b2b5-26df-434c-87ca-58ddab49fcc8",
want: Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
},
{
name: "invalid value",
val: "435b2b5-26df-434c-87ca-58ddab49fcc8",
want: Hash{},
},
{
name: "empty value",
val: "",
want: Hash{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HashFromString(tt.val); !reflect.DeepEqual(got, tt.want) {
t.Errorf("HashFromString() = %v, want %v", got, tt.want)
}
})
}
}
func TestHash_MarshalText(t *testing.T) {
tests := []struct {
name string
h Hash
want []byte
wantErr bool
}{
{
name: "valid value",
h: Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
want: []byte("6435b2b5-26df-434c-87ca-58ddab49fcc8"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.h.MarshalText()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalText() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalText() got = %v, want %v", got, tt.want)
}
})
}
}
func TestHash_String(t *testing.T) {
tests := []struct {
name string
h Hash
want string
}{
{
name: "valid value",
h: Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
want: "6435b2b5-26df-434c-87ca-58ddab49fcc8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.h.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func TestHash_Valid(t *testing.T) {
tests := []struct {
name string
h Hash
want bool
}{
{
name: "valid value",
h: Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
want: true,
},
{
name: "invalid value",
h: Hash{},
want: false,
},
{
name: "valid value, but mostly nils",
h: Hash{100},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.h.IsValid(); got != tt.want {
t.Errorf("Valid(%s) = %v, want %v", tt.h, got, tt.want)
}
})
}
}
func TestHashes_Contains(t *testing.T) {
type args struct {
s Hash
}
tests := []struct {
name string
h Hashes
args args
want bool
}{
{
name: "value contained",
h: Hashes{Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200}},
args: args{Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200}},
want: true,
},
{
name: "value not contained",
h: Hashes{Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200}},
args: args{Hash{101, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200}},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.h.Contains(tt.args.s); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}
func TestHashes_String(t *testing.T) {
tests := []struct {
name string
h Hashes
want string
}{
{
name: "one value",
h: Hashes{Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200}},
want: "6435b2b5-26df-434c-87ca-58ddab49fcc8",
},
{
name: "two values",
h: Hashes{
Hash{100, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
Hash{101, 53, 178, 181, 38, 223, 67, 76, 135, 202, 88, 221, 171, 73, 252, 200},
},
want: "6435b2b5-26df-434c-87ca-58ddab49fcc8, 6535b2b5-26df-434c-87ca-58ddab49fcc8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.h.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}