github.com/Psiphon-Labs/goarista@v0.0.0-20160825065156-d002785f4c67/areflect/force_test.go (about)

     1  // Copyright (C) 2016  Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package areflect
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  type somestruct struct {
    13  	a uint32
    14  }
    15  
    16  func TestForcePublic(t *testing.T) {
    17  	c := somestruct{a: 42}
    18  	v := reflect.ValueOf(c)
    19  	// Without the call to forceExport(), the following line would crash with
    20  	// "panic: reflect.Value.Interface: cannot return value obtained from
    21  	// unexported field or method".
    22  	a := ForceExport(v.FieldByName("a")).Interface()
    23  	if i, ok := a.(uint32); !ok {
    24  		t.Fatalf("Should have gotten a uint32 but got a %T", a)
    25  	} else if i != 42 {
    26  		t.Fatalf("Should have gotten 42 but got a %d", i)
    27  	}
    28  }