-
Notifications
You must be signed in to change notification settings - Fork 44
/
parser_test.go
50 lines (44 loc) · 994 Bytes
/
parser_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
package cli
import (
"net/url"
"testing"
"github.com/labstack/gommon/color"
)
func TestJSONParser(t *testing.T) {
type T struct {
A string
B int
}
type argT struct {
Value T `cli:"t" parser:"json"`
}
v := new(argT)
clr := color.Color{}
flagSet := parseArgv([]string{`-t`, `{"a": "string", "b": 2}`}, v, clr)
if flagSet.err != nil {
t.Errorf("error: %v", flagSet.err)
return
}
want := T{A: "string", B: 2}
if v.Value != want {
t.Errorf("want %v, got %v", want, v.Value)
}
}
func TestURLParser(t *testing.T) {
type argT struct {
Addr url.URL `cli:"u,url" parser:"url"`
}
v := new(argT)
clr := color.Color{}
flagSet := parseArgv([]string{`-u`, `https://www.google.com`}, v, clr)
if flagSet.err != nil {
t.Errorf("error: %v", flagSet.err)
return
}
if v.Addr.Scheme != "https" {
t.Errorf("schema want %v, got %v", "https", v.Addr.Scheme)
}
if v.Addr.Host != "www.google.com" {
t.Errorf("host want %v, got %v", "www.google.com", v.Addr.Host)
}
}