Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert.Equal panics on unexported fields #8

Open
sparks opened this issue Mar 11, 2013 · 1 comment
Open

assert.Equal panics on unexported fields #8

sparks opened this issue Mar 11, 2013 · 1 comment

Comments

@sparks
Copy link

sparks commented Mar 11, 2013

Given two unequal structs from another package with unexported fields assert.Equal will panic.

Example with time package

package asserttest

import (
    "github.com/bmizerany/assert"
    "testing"
    "time"
)

func TestBreakAssert(t *testing.T) {
    assert.Equal(t, time.Now(), time.Unix(0, 0))
}

Example with an invented struct

asserttest/assert_test.go

package asserttest

import (
    "other"
    "github.com/bmizerany/assert"
    "testing"
)

func TestBreakAssert2(t *testing.T) {
    a := new(other.Other)
    a.Outer = 2
    b := new(other.Other)
    b.Outer = 4
    assert.Equal(t, a, b)
}

other/other.go

package other

type Other struct {
    Outer int
    inner int
}

Both these snippets panic like so

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
panic: reflect.Value.Interface: cannot return value obtained from unexported field or method
etc ...
@justincampbell
Copy link

I avoided this by using the Equal method on a time instead:

assert.T(t, time.Now().Equal(time.Unix(0, 0)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants