-
Notifications
You must be signed in to change notification settings - Fork 0
/
gargs_test.go
82 lines (73 loc) · 2.23 KB
/
gargs_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
74
75
76
77
78
79
80
81
82
package gargs
import (
"testing"
)
func TestHandle(t *testing.T) {
// Define some options for testing
type options struct {
Color string `short:"c" desc:"Color of the output" defaultValue:"red"`
Delay int `short:"d" desc:"Delay in seconds" defaultValue:"5"`
}
opts := &options{}
// Test valid arguments
err, helpTriggered := Handle([]string{"binary", "-c", "blue", "-d", "10", "arg1", "arg2"}, opts, "<arg1>", "<arg2>")
if err != nil {
t.Errorf("Handle() error = %v", err)
return
}
if opts.Color != "blue" {
t.Errorf("expected Color: blue, got: %s", opts.Color)
}
if opts.Delay != 10 {
t.Errorf("expected Delay: 10, got: %d", opts.Delay)
}
if len(Params()) != 2 || GetParam("arg1") == "" || GetParam("arg2") == "" {
t.Errorf("Positional parameters not parsed correctly")
}
// Test when help is triggered
_, helpTriggered = Handle([]string{"binary", "-h"}, opts)
if !helpTriggered {
t.Errorf("expected helpTriggered: true, got: false")
}
// Test positional arguments
err, _ = Handle([]string{"binary", "value1", "value2"}, opts, "<param1>", "<param2>")
if err != nil {
t.Errorf("Handle() error = %v", err)
return
}
if GetParam("param1") != "value1" {
t.Errorf("expected param1: value1, got: %s", GetParam("param1"))
}
if GetParam("param2") != "value2" {
t.Errorf("expected param2: value2, got: %s", GetParam("param2"))
}
// Test default values
err, _ = Handle([]string{"binary"}, opts)
if err != nil {
t.Errorf("Handle() error = %v", err)
return
}
if opts.Color != "red" {
t.Errorf("expected default Color: red, got: %s", opts.Color)
}
if opts.Delay != 5 {
t.Errorf("expected default Delay: 5, got: %d", opts.Delay)
}
// Test shorthand flags
err, _ = Handle([]string{"binary", "-c", "green", "-d", "3"}, opts)
if err != nil {
t.Errorf("Handle() error = %v", err)
return
}
if opts.Color != "green" {
t.Errorf("expected Color using shorthand: green, got: %s", opts.Color)
}
if opts.Delay != 3 {
t.Errorf("expected Delay using shorthand: 3, got: %d", opts.Delay)
}
// Test missing required positional parameters
err, _ = Handle([]string{"binary", "value1"}, opts, "<param1>", "<param2>")
if err == nil {
t.Errorf("Expected an error for missing positional parameter but got none")
}
}