File: getopt_test.go

package info (click to toggle)
golang-github-pborman-getopt 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
// Copyright 2020 Google Inc.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package getopt

import (
	"testing"
)

func TestMandatory(t *testing.T) {
	for _, tt := range []struct {
		name string
		in   []string
		err  string
	}{
		{
			name: "required option present",
			in:   []string{"test", "-r"},
		},
		{
			name: "required option not present",
			in:   []string{"test", "-o"},
			err:  "test: option -r is mandatory",
		},
		{
			name: "no options",
			in:   []string{"test"},
			err:  "test: option -r is mandatory",
		},
	} {
		reset()
		var val bool
		Flag(&val, 'o')
		Flag(&val, 'r').Mandatory()
		parse(tt.in)
		if s := checkError(tt.err); s != "" {
			t.Errorf("%s: %s", tt.name, s)
		}
	}
}

func TestGroup(t *testing.T) {
	for _, tt := range []struct {
		name string
		in   []string
		err  string
	}{
		{
			name: "no args",
			in:   []string{"test"},
			err:  "test: exactly one of the following options must be specified: -A, -B",
		},
		{
			name: "one of each",
			in:   []string{"test", "-A", "-C"},
		},
		{
			name: "Two in group One",
			in:   []string{"test", "-A", "-B"},
			err:  "test: options -A and -B are mutually exclusive",
		},
		{
			name: "Two in group Two",
			in:   []string{"test", "-A", "-D", "-C"},
			err:  "test: options -C and -D are mutually exclusive",
		},
	} {
		reset()
		var val bool
		Flag(&val, 'o')
		Flag(&val, 'A').SetGroup("One")
		Flag(&val, 'B').SetGroup("One")
		Flag(&val, 'C').SetGroup("Two")
		Flag(&val, 'D').SetGroup("Two")
		RequiredGroup("One")
		parse(tt.in)
		if s := checkError(tt.err); s != "" {
			t.Errorf("%s: %s", tt.name, s)
		}
	}
}