File: marshaler_test.go

package info (click to toggle)
golang-github-voxelbrain-goptions 2.5.11-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 168 kB
  • sloc: makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (3)
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)
	}
}