Skip to content

Commit

Permalink
Merge pull request #21 from bayashi/always-show-pointer-address
Browse files Browse the repository at this point in the history
Improve `AsString` method
  • Loading branch information
bayashi committed Jan 7, 2024
2 parents 2687f94 + aa2fe67 commit a47abe8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
13 changes: 9 additions & 4 deletions obj/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (o *Object) AsType() string {

// Return the value as string that was converted by fmt.Sprintf for each type
func (o *Object) AsString() string {
if o == nil || !o.touch {
if !o.touch {
return ""
}

Expand All @@ -59,15 +59,20 @@ func (o *Object) AsString() string {
return "<nil>"
case bool:
return fmt.Sprintf("<%t>", o.value)
case *bool:
return o.AsDumpString()
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", o.value)
case float32, float64, complex64, complex128:
return fmt.Sprintf("%g", o.value)
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64,
*float32, *float64, *complex64, *complex128:
return o.AsDumpString()
case error:
return fmt.Sprintf("%+v", o.value)
return o.AsDumpString()
default:
if o.IsStructType() {
return fmt.Sprintf("%p, %#v", &o.value, o.value)
if o.IsPointerType() {
return fmt.Sprintf("%p, %#v", o.value, o.value)
} else {
return fmt.Sprintf("%#v", o.value)
}
Expand Down
46 changes: 43 additions & 3 deletions obj/obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,41 @@ func TestIsPointerType(t *testing.T) {
}
}

func TestPointerValue(t *testing.T) {
func TestBooleanPointer(t *testing.T) {
f := false
o := NewObject(&f)

expectRe := regexp.MustCompile(`\(\*bool\)\([0-9a-fx]+\)\(false\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
}

func TestError(t *testing.T) {
e := fmt.Errorf("foo error")
o := NewObject(e)

expectRe := regexp.MustCompile(`\(\*errors\.errorString\)\([0-9a-fx]+\)\(foo error\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
}

func TestPointerValueInt(t *testing.T) {
i := 123
o := NewObject(&i)

expectRe := regexp.MustCompile(`\(\*int\)\([0-9a-fx]+\)`)
expectRe := regexp.MustCompile(`\(\*int\)\([0-9a-fx]+\)\(123\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
}

func TestPointerValueFloat(t *testing.T) {
var i float64 = 0.123
o := NewObject(&i)

expectRe := regexp.MustCompile(`\(*float64\)\([0-9a-fx]+\)\(0\.123\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
Expand All @@ -79,7 +109,17 @@ func TestStructValue(t *testing.T) {
i := struct{ ID int }{ ID: 123 }
o := NewObject(i)

expectRe := regexp.MustCompile(`[0-9a-fx]+, struct { ID int }{ID:123}`)
expectRe := regexp.MustCompile(`struct { ID int }{ID:123}`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
}

func TestStructPointerValue(t *testing.T) {
i := struct{ ID int }{ ID: 123 }
o := NewObject(&i)

expectRe := regexp.MustCompile(`[0-9a-fx]+, &struct { ID int }{ID:123}`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
}
Expand Down
2 changes: 1 addition & 1 deletion witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestError(t *testing.T) {
t.Errorf("Not matched the regexp `%s` for %q", gotTypeRe.String(), res)
}

gotRe := regexp.MustCompile(`Actually got:\s*\terror example 123`)
gotRe := regexp.MustCompile(`Actually got:\s*\t\(\*errors\.errorString\)\([0-9a-fx]+\)\(error example 123\)`)
if gotRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res)
}
Expand Down

0 comments on commit a47abe8

Please sign in to comment.