diff --git a/obj/obj.go b/obj/obj.go index 70d2ed9..d7e205e 100644 --- a/obj/obj.go +++ b/obj/obj.go @@ -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 "" } @@ -59,15 +59,20 @@ func (o *Object) AsString() string { return "" 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) } diff --git a/obj/obj_test.go b/obj/obj_test.go index fa99a3f..2a90bad 100644 --- a/obj/obj_test.go +++ b/obj/obj_test.go @@ -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()) } @@ -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()) } diff --git a/witness_test.go b/witness_test.go index 8482089..4513f16 100644 --- a/witness_test.go +++ b/witness_test.go @@ -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) }