2021-06-18 16:46:56 +03:00
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
2021-06-19 16:00:47 +03:00
|
|
|
"context"
|
2021-06-18 16:46:56 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
2021-06-19 16:00:47 +03:00
|
|
|
"time"
|
2021-06-18 16:46:56 +03:00
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2021-06-18 16:46:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoad(t *testing.T) {
|
|
|
|
os.Args = append(os.Args, "-broker", "5566:33")
|
2021-06-19 16:00:47 +03:00
|
|
|
os.Args = append(os.Args, "-verbose")
|
|
|
|
os.Args = append(os.Args, "-wait", "5s")
|
|
|
|
os.Args = append(os.Args, "-addr", "33,44")
|
|
|
|
os.Args = append(os.Args, "-time", time.RFC822)
|
|
|
|
type Config struct {
|
|
|
|
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
|
|
|
|
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
|
|
|
|
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
|
|
|
|
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
|
|
|
|
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &Config{}
|
2021-06-18 16:46:56 +03:00
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
c := NewConfig(config.Struct(cfg), TimeFormat(time.RFC822))
|
|
|
|
if err := c.Init(); err != nil {
|
2021-06-18 16:46:56 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-19 16:00:47 +03:00
|
|
|
if err := c.Load(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Broker != "5566:33" {
|
|
|
|
t.Fatalf("failed to parse flags broker value invalid: %#+v", cfg)
|
|
|
|
}
|
2021-06-19 16:00:47 +03:00
|
|
|
if tf := cfg.Time.Format(time.RFC822); tf != "02 Jan 06 14:32 MSK" {
|
|
|
|
t.Fatalf("parse time error: %v", cfg.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("cfg %#+v", cfg)
|
2021-06-18 16:46:56 +03:00
|
|
|
}
|