-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshort_test.go
65 lines (59 loc) · 1.61 KB
/
short_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
// Copyright 2021 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Originally written by Changkun Ou <changkun.de> at
// changkun.de/s/redir, adopted by Mai Yang <maiyang.me>.
package main
import (
"context"
"testing"
)
func TestOpValid(t *testing.T) {
tests := []struct {
o string
want op
valid bool
}{
{o: "", want: op(""), valid: false},
{o: "create", want: opCreate, valid: true},
{o: "delete", want: opDelete, valid: true},
{o: "update", want: opUpdate, valid: true},
{o: "fetch", want: opFetch, valid: true},
}
for _, tt := range tests {
o := op(tt.o)
if o != tt.want || o.valid() != tt.valid {
t.Fatalf("want %v got %v, valid %v but %v", tt.want, o, tt.valid, o.valid())
}
}
}
func TestShortCmd(t *testing.T) {
k, v := "alias", "link"
ctx := context.Background()
tests := []struct {
o op
k, v string
wantNil bool
}{
{o: opCreate, k: "alias", v: "link", wantNil: true},
{o: opUpdate, k: "alias", v: "link", wantNil: true},
{o: opFetch, k: "alias", v: "link", wantNil: true},
{o: opDelete, k: "alias", v: "link", wantNil: true},
{o: opFetch, k: "alias2", v: "link", wantNil: false},
{o: opFetch, k: "alias2", v: "link", wantNil: false},
{o: opDelete, k: "alias2", v: "link", wantNil: true},
}
for _, tt := range tests {
err := shortCmd(ctx, tt.o, tt.k, tt.v)
if tt.wantNil {
if err != nil {
t.Fatalf("shortCmd with err: %v", err)
}
} else {
if err == nil {
t.Fatalf("shortCmd without error: %v, %v, %v", tt.o, k, v)
}
}
}
}