Skip to content

Commit d0ff16c

Browse files
committed
Add testing of using a new type that implements getopt.Value.
1 parent a6aa402 commit d0ff16c

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

value_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2019 Paul Borman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
14+
package options_test
15+
16+
// Demonstrate that user defined values can be used.
17+
18+
import (
19+
"errors"
20+
"fmt"
21+
"strconv"
22+
"strings"
23+
"testing"
24+
25+
"github.com/pborman/options"
26+
"github.com/pborman/getopt/v2"
27+
)
28+
29+
type myType struct {
30+
File string
31+
Line int
32+
}
33+
34+
var (
35+
errFormat = errors.New("format error, need FILE:LINE")
36+
errLine = errors.New("line numbers must be positive integers")
37+
)
38+
39+
func (m *myType) Set(s string, _ getopt.Option) error {
40+
x := strings.Index(s, ":")
41+
if x < 0 {
42+
return errFormat
43+
}
44+
n, err := strconv.Atoi(s[x+1:])
45+
if n < 1 || err != nil {
46+
return errLine
47+
}
48+
m.File = s[:x]
49+
m.Line = n
50+
return nil
51+
}
52+
53+
func (m *myType) String() string {
54+
if *m == (myType{}) {
55+
return ""
56+
}
57+
return fmt.Sprintf("%s:%d", m.File, m.Line)
58+
}
59+
60+
type myOpts struct {
61+
Location myType `getopt:"--loc=FILE:LINE a location"`
62+
}
63+
64+
func (m *myOpts) String() string {
65+
if m == nil {
66+
return "nil"
67+
}
68+
if *m == (myOpts{}) {
69+
return "empty"
70+
}
71+
return m.Location.String()
72+
}
73+
74+
func TestUserType(t *testing.T) {
75+
76+
for _, tt := range []struct {
77+
name string
78+
in string
79+
inOpts *myOpts
80+
outOpts *myOpts
81+
err error
82+
}{
83+
{
84+
name: "no arguments",
85+
},
86+
{
87+
name: "bad format",
88+
in: "test --loc=here",
89+
err: errFormat,
90+
},
91+
{
92+
name: "bad line",
93+
in: "test --loc=here:",
94+
err: errLine,
95+
},
96+
{
97+
name: "negative line",
98+
in: "test --loc=here:-1",
99+
err: errLine,
100+
},
101+
{
102+
name: "pass",
103+
in: "test --loc=this_file.go:42",
104+
outOpts: &myOpts{
105+
Location: myType{File: "this_file.go", Line: 42},
106+
},
107+
},
108+
{
109+
name: "default",
110+
inOpts: &myOpts{
111+
Location: myType{File: "this_file.go", Line: 42},
112+
},
113+
outOpts: &myOpts{
114+
Location: myType{File: "this_file.go", Line: 42},
115+
},
116+
},
117+
{
118+
name: "override",
119+
in: "test --loc=this_file.go:42",
120+
inOpts: &myOpts{
121+
Location: myType{File: "that_file.go", Line: 17},
122+
},
123+
outOpts: &myOpts{
124+
Location: myType{File: "this_file.go", Line: 42},
125+
},
126+
},
127+
} {
128+
t.Run(tt.name, func(t *testing.T) {
129+
if tt.inOpts == nil {
130+
tt.inOpts = &myOpts{}
131+
}
132+
if tt.outOpts == nil {
133+
tt.outOpts = &myOpts{}
134+
}
135+
set := getopt.New()
136+
if err := options.RegisterSet("", tt.inOpts, set); err != nil {
137+
t.Fatalf("RegisteSet: %v", err)
138+
}
139+
err := set.Getopt(strings.Fields(tt.in), nil)
140+
// Unwrap the error
141+
if ge, ok := err.(*getopt.Error); ok {
142+
err = ge.Err
143+
}
144+
if err != tt.err {
145+
t.Fatalf("got error '%v', want '%v'", err, tt.err)
146+
}
147+
if *tt.outOpts != *tt.inOpts {
148+
t.Fatalf("got %v, want %v", tt.inOpts, tt.outOpts)
149+
}
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)