Skip to content

Commit

Permalink
test: squash testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Apr 18, 2022
1 parent 70c5638 commit 6db1ebe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
42 changes: 0 additions & 42 deletions tests/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,48 +242,6 @@ func TestMapMerge(t *testing.T) {
})
}

type confTestNestedSquash struct {
Nested1 `xconf:",squash"`
Nested2 `xconf:",squash"`
}
type confTestNestedSquashOff struct {
Nested1 `xconf:"nested1"`
Nested2 `xconf:"nested2"`
}

func TestSquash(t *testing.T) {
Convey("TestSquash Enable", t, func(c C) {
cc := &confTestNestedSquash{}
cc.Nested1.Deadline = time.Now()
cc.TimeoutMap = map[string]time.Duration{"read": time.Second}
x := xconf.New(
xconf.WithFlagSet(flag.NewFlagSet("suqash_anable", flag.ContinueOnError)),
xconf.WithFlagArgs(),
xconf.WithDebug(true),
xconf.WithMapMerge(true),
)
So(x.Parse(cc), ShouldBeNil)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeFalse)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeFalse)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeFalse)
})
Convey("TestSquash Disable", t, func(c C) {
cc := &confTestNestedSquashOff{}
cc.Nested1.Deadline = time.Now()
cc.Nested2.TimeoutMap = map[string]time.Duration{"read": time.Second}
x := xconf.New(
xconf.WithFlagSet(flag.NewFlagSet("suqash_disable", flag.ContinueOnError)),
xconf.WithFlagArgs(),
xconf.WithDebug(true),
xconf.WithMapMerge(true),
)
So(x.Parse(cc), ShouldBeNil)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeTrue)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeTrue)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeTrue)
})
}

type TestConf1 struct {
HTTPAddress string `xconf:"http_address" default:"0.0.0.0:0000"`
Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
Expand Down
51 changes: 51 additions & 0 deletions tests/bugs/bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package bugs

import (
"bytes"
"flag"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -62,3 +64,52 @@ func TestBug_7(t *testing.T) {
So(xconf.SaveVarToWriterAsYAML(ss, os.Stderr), ShouldBeNil)
})
}

type Nested1 struct {
Deadline time.Time `xconf:"deadline"`
DeadlineAsSecond int
}
type Nested2 struct {
TimeoutMap map[string]time.Duration `xconf:"timeout_map"`
}
type confTestNestedSquash struct {
Nested1 `xconf:",squash"`
Nested2 `xconf:",squash"`
}
type confTestNestedSquashOff struct {
Nested1 `xconf:"nested1"`
Nested2 `xconf:"nested2"`
}

func TestBug_8(t *testing.T) {
Convey("TestSquash Enable", t, func(c C) {
cc := &confTestNestedSquash{}
cc.Nested1.Deadline = time.Now()
cc.TimeoutMap = map[string]time.Duration{"read": time.Second}
x := xconf.New(
xconf.WithFlagSet(flag.NewFlagSet("suqash_anable", flag.ContinueOnError)),
xconf.WithFlagArgs(),
xconf.WithDebug(true),
xconf.WithMapMerge(true),
)
So(x.Parse(cc), ShouldBeNil)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeFalse)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeFalse)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeFalse)
})
Convey("TestSquash Disable", t, func(c C) {
cc := &confTestNestedSquashOff{}
cc.Nested1.Deadline = time.Now()
cc.Nested2.TimeoutMap = map[string]time.Duration{"read": time.Second}
x := xconf.New(
xconf.WithFlagSet(flag.NewFlagSet("suqash_disable", flag.ContinueOnError)),
xconf.WithFlagArgs(),
xconf.WithDebug(true),
xconf.WithMapMerge(true),
)
So(x.Parse(cc), ShouldBeNil)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeYAML)), "nested"), ShouldBeTrue)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeJSON)), "nested"), ShouldBeTrue)
So(strings.Contains(string(x.MustSaveToBytes(xconf.ConfigTypeTOML)), "nested"), ShouldBeTrue)
})
}
14 changes: 0 additions & 14 deletions tests/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ type RedisTimeout = redis.Timeout

var optionUsage = `在这里描述一些应用级别的配置规则`

type Nested1 struct {
Deadline time.Time `xconf:"deadline"`
DeadlineAsSecond int
}
type Nested2 struct {
TimeoutMap map[string]time.Duration `xconf:"timeout_map"`
}

var defaultNested1 = Nested1{Deadline: time.Now()}

// ConfigOptionDeclareWithDefault go-lint
//go:generate optiongen --option_with_struct_name=false --new_func=NewTestConfig --xconf=true --empty_composite_nil=true --usage_tag_name=usage
func ConfigOptionDeclareWithDefault() interface{} {
Expand Down Expand Up @@ -71,9 +61,5 @@ func ConfigOptionDeclareWithDefault() interface{} {
"RedisAsPointer": (*Redis)(&redis.Conf{}),
"Redis": (Redis)(redis.Conf{}),
"RedisTimeout": (*RedisTimeout)(&redis.Timeout{}),
// annotation@Nested1(xconf=",squash",inline="true")
"Nested1": (Nested1)(defaultNested1),
// annotation@Nested2(inline="true")
"Nested2": (*Nested2)(nil),
}
}
28 changes: 0 additions & 28 deletions tests/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6db1ebe

Please sign in to comment.