forked from voxelbrain/goptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetag_test.go
67 lines (61 loc) · 1.52 KB
/
parsetag_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
package goptions
import (
"reflect"
"testing"
)
func TestParseTag_Minimal(t *testing.T) {
var tag string
tag = `--name, -n, description='Some name'`
f, e := parseStructField(reflect.ValueOf(string("")), tag)
if e != nil {
t.Fatalf("Tag parsing failed: %s", e)
}
expected := &Flag{
Long: "name",
Short: "n",
Description: "Some name",
}
if !flagequal(f, expected) {
t.Fatalf("Expected %#v, got %#v", expected, f)
}
}
func TestParseTag_More(t *testing.T) {
var tag string
tag = `--name, -n, description='Some name', mutexgroup='selector', obligatory`
f, e := parseStructField(reflect.ValueOf(string("")), tag)
if e != nil {
t.Fatalf("Tag parsing failed: %s", e)
}
expected := &Flag{
Long: "name",
Short: "n",
Description: "Some name",
MutexGroups: []string{"selector"},
Obligatory: true,
}
if !flagequal(f, expected) {
t.Fatalf("Expected %#v, got %#v", expected, f)
}
}
func TestParseTag_MultipleFlags(t *testing.T) {
var tag string
var e error
tag = `--name1, --name2`
_, e = parseStructField(reflect.ValueOf(string("")), tag)
if e == nil {
t.Fatalf("Parsing should have failed")
}
tag = `-n, -v`
_, e = parseStructField(reflect.ValueOf(string("")), tag)
if e == nil {
t.Fatalf("Parsing should have failed")
}
}
func flagequal(f1, f2 *Flag) bool {
return f1.Short == f2.Short &&
f1.Long == f2.Long &&
reflect.DeepEqual(f1.MutexGroups, f2.MutexGroups) &&
f1.Description == f2.Description &&
f1.Obligatory == f2.Obligatory &&
f1.WasSpecified == f2.WasSpecified
}