field: add tests for fields, WIP

This commit is contained in:
Fatih Arslan 2014-08-10 16:05:37 +03:00
parent 53f565ab3e
commit d5c544c4f6
2 changed files with 56 additions and 1 deletions

View File

@ -15,7 +15,8 @@ func (f *Field) Tag(key string) string {
return f.field.Tag.Get(key)
}
// Value returns the underlying value of of the field.
// Value returns the underlying value of of the field. It panics if the field
// is not exported.
func (f *Field) Value() interface{} {
return f.value.Interface()
}
@ -24,3 +25,8 @@ func (f *Field) Value() interface{} {
func (f *Field) IsEmbedded() bool {
return f.field.Anonymous
}
// IsExported returns true if the given field is exported.
func (f *Field) IsExported() bool {
return f.field.PkgPath == ""
}

49
field_test.go Normal file
View File

@ -0,0 +1,49 @@
package structure
import "testing"
// A test struct that defines all cases
type Foo struct {
A string
B int `structure:"y"`
C bool `json:"c"`
d string // not exported
*Bar // embedded
}
type Bar struct {
E string
F int
g []string
}
func newStruct() *Struct {
b := &Bar{
E: "example",
F: 2,
g: []string{"zeynep", "fatih"},
}
f := &Foo{
A: "gopher",
B: 1,
C: true,
d: "small",
}
f.Bar = b
return New(f)
}
func TestField(t *testing.T) {
s := newStruct()
defer func() {
err := recover()
if err == nil {
t.Error("Retrieveing a non existing field from the struct should panic")
}
}()
_ = s.Field("no-field")
}