1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-11-08 16:00:10 +00:00
rconfig/general_test.go

92 lines
1.9 KiB
Go
Raw Normal View History

2015-09-08 20:07:40 +00:00
package rconfig
import (
"os"
"reflect"
"testing"
2015-09-08 20:07:40 +00:00
)
func TestGeneralExecution(t *testing.T) {
type test struct {
2015-09-08 20:07:40 +00:00
Test string `default:"foo" env:"shell" flag:"shell" description:"Test"`
Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"`
DefaultFlag string `default:"goo"`
SadFlag string
}
var (
args []string
cfg test
2015-09-08 20:07:40 +00:00
)
exec := func(desc string, tests [][2]interface{}) {
if err := parse(&cfg, args); err != nil {
t.Fatalf("Parsing options caused error: %s", err)
}
2015-09-08 20:07:40 +00:00
for _, test := range tests {
if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
}
}
}
2015-09-08 20:07:40 +00:00
cfg = test{}
args = []string{
"--shell=test23",
"-t", "bla",
}
exec("defined arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
2015-09-08 20:07:40 +00:00
})
cfg = test{}
args = []string{}
exec("no arguments", [][2]interface{}{
{&cfg.Test, "foo"},
2015-09-08 20:07:40 +00:00
})
cfg = test{}
args = []string{}
os.Setenv("shell", "test546")
exec("no arguments and set env", [][2]interface{}{
{&cfg.Test, "test546"},
2015-09-08 20:07:40 +00:00
})
os.Unsetenv("shell")
2015-09-08 20:07:40 +00:00
cfg = test{}
args = []string{
"--shell=test23",
"-t", "bla",
"positional1", "positional2",
}
exec("additional arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
})
if !reflect.DeepEqual(Args(), []string{"positional1", "positional2"}) {
t.Errorf("expected positional arguments to match")
}
}
func TestValidationIntegration(t *testing.T) {
type tValidated struct {
Test string `flag:"test" default:"" validate:"nonzero"`
}
var (
cfgValidated = tValidated{}
args = []string{}
)
if err := parseAndValidate(&cfgValidated, args); err == nil {
t.Errorf("Expected error, got none")
}
}