Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type Sample struct {
StructWithNoTag Struct
DeepSliceOfStructWithNoTag [][][]Struct

NonInitialBool bool `default:"true"`
NonInitialString string `default:"foo"`
NonInitialSlice []int `default:"[123]"`
NonInitialStruct Struct `default:"{}"`
Expand Down Expand Up @@ -189,6 +190,7 @@ func TestMustSet(t *testing.T) {
}
}()
sample := &Sample{
NonInitialBool: false,
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
Expand All @@ -215,6 +217,7 @@ func TestMustSet(t *testing.T) {
}
}()
sample := Sample{
NonInitialBool: false,
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
Expand All @@ -226,8 +229,36 @@ func TestMustSet(t *testing.T) {

}

func TestOverrideDefaultTrueBoolToFalse(t *testing.T) {
type example struct {
BoolTrue bool `default:"true"`
BoolPointerTrue *bool `default:"true"`
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the workaround to have a field that defaults to true that a user can still set to false and not have defaults.Set() change it back to true.

}

e := &example{
BoolTrue: false,
BoolPointerTrue: func() *bool { b := false; return &b }(), // lolsob
}

err := Set(e)
if err != nil {
t.Errorf("Set() should not return an error: %v", err)
}
t.Run("bool", func(t *testing.T) {
if e.BoolTrue == true {
t.Errorf("it should not override user-set false with the default of true")
}
})
t.Run("bool pointer", func(t *testing.T) {
if *e.BoolPointerTrue == true {
t.Errorf("it should not override user-set false with the default of true")
}
})
}

func TestInit(t *testing.T) {
sample := &Sample{
NonInitialBool: false,
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
Expand Down Expand Up @@ -577,6 +608,9 @@ func TestInit(t *testing.T) {
})

t.Run("non-initial value", func(t *testing.T) {
if sample.NonInitialBool != false { // true is default; false is the non-initial value
t.Errorf("it should not override non-initial bool value")
}
if sample.NonInitialString != "string" {
t.Errorf("it should not override non-initial value")
}
Expand Down