Skip to content

Commit

Permalink
no len or cap comment for struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed May 24, 2022
1 parent 9f5d802 commit 254dc9b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func TestAssertionErr(t *testing.T) {

as.Eq(data{1, "a"}, data{1, "b"})
m.check(`
got_test.data/* len=2 */{
got_test.data{
A: 1,
S: "a",
}
⦗not ==⦘
got_test.data/* len=2 */{
got_test.data{
A: 1,
S: "b",
}`)
Expand Down
6 changes: 4 additions & 2 deletions lib/gop/fixtures/expected.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[]interface {}/* len=37 cap=37 */{
[]interface {}/* len=38 cap=38 */{
nil,
[]int{
},
[]interface {}/* len=4 cap=4 */{
true,
false,
Expand Down Expand Up @@ -27,7 +29,7 @@
"a": 1,
},
unsafe.Pointer(uintptr({{.ptr}})),
struct { Int int; str string; M map[int]int }/* len=3 */{
struct { Int int; str string; M map[int]int }{
Int: 10,
str: "ok",
M: map[int]int{
Expand Down
6 changes: 4 additions & 2 deletions lib/gop/fixtures/expected_with_color.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<36>[]interface {}<39><37>/* len=37 cap=37 */<39>{
<36>[]interface {}<39><37>/* len=38 cap=38 */<39>{
<31>nil<39>,
<36>[]int<39>{
},
<36>[]interface {}<39><37>/* len=4 cap=4 */<39>{
<34>true<39>,
<34>false<39>,
Expand Down Expand Up @@ -27,7 +29,7 @@
<33>"a"<39>: <32>1<39>,
},
<36>unsafe.Pointer<39>(<36>uintptr<39>(<32>{{.ptr}}<39>)),
<36>struct { Int int; str string; M map[int]int }<39><37>/* len=3 */<39>{
<36>struct { Int int; str string; M map[int]int }<39>{
Int: <32>10<39>,
str: <33>"ok"<39>,
M: <36>map[int]int<39>{
Expand Down
26 changes: 20 additions & 6 deletions lib/gop/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"unsafe"

"github.com/ysmood/got"
"github.com/ysmood/got/lib/diff"
"github.com/ysmood/got/lib/gop"
)

Expand All @@ -37,6 +38,7 @@ func TestTokenize(t *testing.T) {

v := []interface{}{
nil,
[]int{},
[]interface{}{true, false, uintptr(0x17), float32(100.121111133)},
true, 10, int8(2), int32(100),
float64(100.121111133),
Expand Down Expand Up @@ -93,7 +95,10 @@ func TestTokenize(t *testing.T) {
"ptr": fmt.Sprintf("%v", &ref),
}))

g.Eq(out, expected.String())
if out != expected.String() {
g.Fail()
g.Log(diff.Diff(out, expected.String()))
}
}

out := gop.StripANSI(gop.F(v))
Expand Down Expand Up @@ -132,11 +137,11 @@ func TestCircularRef(t *testing.T) {
a.B = &b

g.Eq(gop.StripANSI(gop.F(a)), ""+
"gop_test.A/* len=2 */{\n"+
"gop_test.A{\n"+
" Int: 10,\n"+
" B: &gop_test.B/* len=2 */{\n"+
" B: &gop_test.B{\n"+
" s: \"test\",\n"+
" a: &gop_test.A/* len=2 */{\n"+
" a: &gop_test.A{\n"+
" Int: 10,\n"+
" B: gop.Circular(\"B\").(*gop_test.B),\n"+
" },\n"+
Expand All @@ -148,11 +153,11 @@ func TestCircularNilRef(t *testing.T) {
arr := []A{{}, {}}

got.T(t).Eq(gop.StripANSI(gop.F(arr)), `[]gop_test.A/* len=2 cap=2 */{
gop_test.A/* len=2 */{
gop_test.A{
Int: 0,
B: (*gop_test.B)(nil),
},
gop_test.A/* len=2 */{
gop_test.A{
Int: 0,
B: (*gop_test.B)(nil),
},
Expand Down Expand Up @@ -244,6 +249,15 @@ func TestTypeName(t *testing.T) {
g.Eq(gop.Plain(b('a')), "b(97)")
}

func TestSliceCapNotEqual(t *testing.T) {
g := got.T(t)

x := gop.Plain(make([]int, 3, 10))
y := gop.Plain(make([]int, 3))

g.Desc("we should show the diff of cap").Neq(x, y)
}

func TestFixNestedStyle(t *testing.T) {
g := got.T(t)

Expand Down
5 changes: 1 addition & 4 deletions lib/gop/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func tokenizeCollection(sn seen, p path, v reflect.Value) []*Token {
} else {
ts = append(ts, typeName(v.Type().String()))
}
if v.Kind() == reflect.Slice {
if v.Kind() == reflect.Slice && v.Cap() > 0 {
ts = append(ts, &Token{Comment, formatLenCap(v.Len(), v.Cap())})
}
ts = append(ts, &Token{SliceOpen, "{"})
Expand Down Expand Up @@ -305,9 +305,6 @@ func tokenizeCollection(sn seen, p path, v reflect.Value) []*Token {
t := v.Type()

ts = append(ts, typeName(t.String()))
if v.NumField() > 1 {
ts = append(ts, &Token{Comment, formatLenCap(v.NumField(), -1)})
}
ts = append(ts, &Token{StructOpen, "{"})
for i := 0; i < v.NumField(); i++ {
name := t.Field(i).Name
Expand Down

0 comments on commit 254dc9b

Please sign in to comment.