Skip to content

Commit

Permalink
Merge pull request #18 from bayashi/add-pointer-address-for-pointer
Browse files Browse the repository at this point in the history
Add IsPointerType() in obj.go
  • Loading branch information
bayashi authored Dec 30, 2023
2 parents 08395bd + ca0716f commit 37cabb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions obj/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (o *Object) IsDumpableRawType() bool {
return false
}

// Return boolean whether the value is a pointer
func (o *Object) IsPointerType() bool {
return o.value != nil && reflect.TypeOf(o.value).Kind() == reflect.Pointer
}

// format and truncate. For `fmt/print.go` Formatter interface
func (o *Object) Format(s fmt.State, verb rune) {
switch verb {
Expand Down
21 changes: 21 additions & 0 deletions obj/obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package obj

import (
"fmt"
"regexp"
"testing"
)

Expand Down Expand Up @@ -54,6 +55,26 @@ func TestIsDumpableRawType(t *testing.T) {
}
}

func TestIsPointerType(t *testing.T) {
i := 123
if o := NewObject(&i); !o.IsPointerType() {
t.Error("IsPointerType() was wrong")
}
if o := NewObject(7); o.IsPointerType() {
t.Error("IsPointerType() was wrong")
}
}

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

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

func TestDump(t *testing.T) {
o := NewObject(123)
if o.AsDumpString() != "(int) 123\n" {
Expand Down

0 comments on commit 37cabb3

Please sign in to comment.