forked from voxelbrain/goptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaler_test.go
73 lines (68 loc) · 1.36 KB
/
marshaler_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
package goptions
import (
"fmt"
"reflect"
"strings"
"testing"
)
type Name struct {
FirstName string
LastName string
}
func (n *Name) MarshalGoption(val string) error {
f := strings.SplitN(val, " ", 2)
if len(f) != 2 {
return fmt.Errorf("Incomplete name")
}
n.FirstName = f[0]
n.LastName = f[1]
return nil
}
func TestMarshaler(t *testing.T) {
var args []string
var err error
var fs *FlagSet
var options struct {
Name *Name `goptions:"--name"`
}
args = []string{"--name", "Alexander Surma"}
fs = NewFlagSet("goptions", &options)
err = fs.Parse(args)
if err != nil {
t.Fatalf("Parsing failed: %s", err)
}
expected := &Name{
FirstName: "Alexander",
LastName: "Surma",
}
if !reflect.DeepEqual(options.Name, expected) {
t.Fatalf("Unexpected value: %#v", options)
}
}
func TestArrayOfMarshaler(t *testing.T) {
var args []string
var err error
var fs *FlagSet
var options struct {
Names []*Name `goptions:"--name"`
}
args = []string{"--name", "Alexander Surma", "--name", "Yo Mama"}
fs = NewFlagSet("goptions", &options)
err = fs.Parse(args)
if err != nil {
t.Fatalf("Parsing failed: %s", err)
}
expected := []*Name{
&Name{
FirstName: "Alexander",
LastName: "Surma",
},
&Name{
FirstName: "Yo",
LastName: "Mama",
},
}
if !reflect.DeepEqual(options.Names, expected) {
t.Fatalf("Unexpected value: %#v", options)
}
}