Skip to content

Commit

Permalink
feat(Struct,SStruct): String() aligns values vertically
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Jul 30, 2023
1 parent 7e45206 commit db15062
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
11 changes: 9 additions & 2 deletions td/td_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,16 @@ func (s *tdStruct) String() string {
} else {
buf.WriteString("{\n")

maxLen := 0
for _, fieldInfo := range s.expectedFields {
fmt.Fprintf(buf, " %s: %s\n", //nolint: errcheck
fieldInfo.name, util.ToString(fieldInfo.expected))
if len(fieldInfo.name) > maxLen {
maxLen = len(fieldInfo.name)
}
}
maxLen++
for _, fieldInfo := range s.expectedFields {
fmt.Fprintf(buf, " %-*s %s\n", //nolint: errcheck
maxLen, fieldInfo.name+":", util.ToString(fieldInfo.expected))
}

buf.WriteString("})")
Expand Down
38 changes: 25 additions & 13 deletions td/td_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ func TestStruct(t *testing.T) {
}).String(),
`Struct(td_test.MyStruct{
ValBool: false
ValInt: 123
ValStr: "foobar"
ValInt: 123
ValStr: "foobar"
})`)

test.EqualStr(t,
Expand All @@ -397,8 +397,20 @@ func TestStruct(t *testing.T) {
}).String(),
`Struct(*td_test.MyStruct{
ValBool: false
ValInt: 123
ValStr: "foobar"
ValInt: 123
ValStr: "foobar"
})`)

test.EqualStr(t,
td.Struct(&MyStruct{},
td.StructFields{
"ValBool": false,
"= Val*": td.NotZero(),
}).String(),
`Struct(*td_test.MyStruct{
ValBool: false
ValInt: NotZero()
ValStr: NotZero()
})`)

test.EqualStr(t,
Expand Down Expand Up @@ -1057,10 +1069,10 @@ func TestSStruct(t *testing.T) {
"ValBool": false,
}).String(),
`SStruct(td_test.MyStruct{
Ptr: (*int)(<nil>)
Ptr: (*int)(<nil>)
ValBool: false
ValInt: 123
ValStr: "foobar"
ValInt: 123
ValStr: "foobar"
})`)

test.EqualStr(t,
Expand All @@ -1074,19 +1086,19 @@ func TestSStruct(t *testing.T) {
"ValBool": false,
}).String(),
`SStruct(*td_test.MyStruct{
Ptr: (*int)(<nil>)
Ptr: (*int)(<nil>)
ValBool: false
ValInt: 123
ValStr: "foobar"
ValInt: 123
ValStr: "foobar"
})`)

test.EqualStr(t,
td.SStruct(&MyStruct{}, td.StructFields{}).String(),
`SStruct(*td_test.MyStruct{
Ptr: (*int)(<nil>)
Ptr: (*int)(<nil>)
ValBool: false
ValInt: 0
ValStr: ""
ValInt: 0
ValStr: ""
})`)

// Erroneous op
Expand Down

0 comments on commit db15062

Please sign in to comment.