aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/all_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/reflect/all_test.go')
-rw-r--r--src/reflect/all_test.go214
1 files changed, 190 insertions, 24 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index abdfe41908..be15362aae 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -2405,8 +2405,14 @@ func TestVariadicMethodValue(t *testing.T) {
points := []Point{{20, 21}, {22, 23}, {24, 25}}
want := int64(p.TotalDist(points[0], points[1], points[2]))
+ // Variadic method of type.
+ tfunc := TypeOf((func(Point, ...Point) int)(nil))
+ if tt := TypeOf(p).Method(4).Type; tt != tfunc {
+ t.Errorf("Variadic Method Type from TypeOf is %s; want %s", tt, tfunc)
+ }
+
// Curried method of value.
- tfunc := TypeOf((func(...Point) int)(nil))
+ tfunc = TypeOf((func(...Point) int)(nil))
v := ValueOf(p).Method(4)
if tt := v.Type(); tt != tfunc {
t.Errorf("Variadic Method Type is %s; want %s", tt, tfunc)
@@ -2989,6 +2995,14 @@ func TestUnexportedMethods(t *testing.T) {
if got := typ.NumMethod(); got != 0 {
t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
}
+
+ var i unexpI
+ if got := TypeOf(&i).Elem().NumMethod(); got != 0 {
+ t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
+ }
+ if got := ValueOf(&i).Elem().NumMethod(); got != 0 {
+ t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
+ }
}
type InnerInt struct {
@@ -3642,21 +3656,21 @@ func TestCallPanic(t *testing.T) {
v := ValueOf(T{i, i, i, i, T2{i, i}, i, i, T2{i, i}})
badCall(func() { call(v.Field(0).Method(0)) }) // .t0.W
badCall(func() { call(v.Field(0).Elem().Method(0)) }) // .t0.W
- badCall(func() { call(v.Field(0).Method(1)) }) // .t0.w
+ badMethod(func() { call(v.Field(0).Method(1)) }) // .t0.w
badMethod(func() { call(v.Field(0).Elem().Method(2)) }) // .t0.w
ok(func() { call(v.Field(1).Method(0)) }) // .T1.Y
ok(func() { call(v.Field(1).Elem().Method(0)) }) // .T1.Y
- badCall(func() { call(v.Field(1).Method(1)) }) // .T1.y
+ badMethod(func() { call(v.Field(1).Method(1)) }) // .T1.y
badMethod(func() { call(v.Field(1).Elem().Method(2)) }) // .T1.y
ok(func() { call(v.Field(2).Method(0)) }) // .NamedT0.W
ok(func() { call(v.Field(2).Elem().Method(0)) }) // .NamedT0.W
- badCall(func() { call(v.Field(2).Method(1)) }) // .NamedT0.w
+ badMethod(func() { call(v.Field(2).Method(1)) }) // .NamedT0.w
badMethod(func() { call(v.Field(2).Elem().Method(2)) }) // .NamedT0.w
ok(func() { call(v.Field(3).Method(0)) }) // .NamedT1.Y
ok(func() { call(v.Field(3).Elem().Method(0)) }) // .NamedT1.Y
- badCall(func() { call(v.Field(3).Method(1)) }) // .NamedT1.y
+ badMethod(func() { call(v.Field(3).Method(1)) }) // .NamedT1.y
badMethod(func() { call(v.Field(3).Elem().Method(3)) }) // .NamedT1.y
ok(func() { call(v.Field(4).Field(0).Method(0)) }) // .NamedT2.T1.Y
@@ -3666,7 +3680,7 @@ func TestCallPanic(t *testing.T) {
badCall(func() { call(v.Field(5).Method(0)) }) // .namedT0.W
badCall(func() { call(v.Field(5).Elem().Method(0)) }) // .namedT0.W
- badCall(func() { call(v.Field(5).Method(1)) }) // .namedT0.w
+ badMethod(func() { call(v.Field(5).Method(1)) }) // .namedT0.w
badMethod(func() { call(v.Field(5).Elem().Method(2)) }) // .namedT0.w
badCall(func() { call(v.Field(6).Method(0)) }) // .namedT1.Y
@@ -4259,24 +4273,6 @@ var gFloat32 float32
func TestConvertNaNs(t *testing.T) {
const snan uint32 = 0x7f800001
-
- // Test to see if a store followed by a load of a signaling NaN
- // maintains the signaling bit. The only platform known to fail
- // this test is 386,GO386=387. The real test below will always fail
- // if the platform can't even store+load a float without mucking
- // with the bits.
- gFloat32 = math.Float32frombits(snan)
- runtime.Gosched() // make sure we don't optimize the store/load away
- r := math.Float32bits(gFloat32)
- if r != snan {
- // This should only happen on 386,GO386=387. We have no way to
- // test for 387, so we just make sure we're at least on 386.
- if runtime.GOARCH != "386" {
- t.Errorf("store/load of sNaN not faithful")
- }
- t.Skip("skipping test, float store+load not faithful")
- }
-
type myFloat32 float32
x := V(myFloat32(math.Float32frombits(snan)))
y := x.Convert(TypeOf(float32(0)))
@@ -7177,6 +7173,176 @@ func TestMapIterDelete1(t *testing.T) {
}
}
+func TestStructTagLookup(t *testing.T) {
+ var tests = []struct {
+ tag StructTag
+ key string
+ expectedValue string
+ expectedOK bool
+ }{
+ {
+ tag: `json:"json_value_1"`,
+ key: "json",
+ expectedValue: "json_value_1",
+ expectedOK: true,
+ },
+ {
+ tag: `json:"json_value_2" xml:"xml_value_2"`,
+ key: "json",
+ expectedValue: "json_value_2",
+ expectedOK: true,
+ },
+ {
+ tag: `json:"json_value_3" xml:"xml_value_3"`,
+ key: "xml",
+ expectedValue: "xml_value_3",
+ expectedOK: true,
+ },
+ {
+ tag: `bson json:"shared_value_4"`,
+ key: "json",
+ expectedValue: "shared_value_4",
+ expectedOK: true,
+ },
+ {
+ tag: `bson json:"shared_value_5"`,
+ key: "bson",
+ expectedValue: "shared_value_5",
+ expectedOK: true,
+ },
+ {
+ tag: `json bson xml form:"field_1,omitempty" other:"value_1"`,
+ key: "xml",
+ expectedValue: "field_1,omitempty",
+ expectedOK: true,
+ },
+ {
+ tag: `json bson xml form:"field_2,omitempty" other:"value_2"`,
+ key: "form",
+ expectedValue: "field_2,omitempty",
+ expectedOK: true,
+ },
+ {
+ tag: `json bson xml form:"field_3,omitempty" other:"value_3"`,
+ key: "other",
+ expectedValue: "value_3",
+ expectedOK: true,
+ },
+ {
+ tag: `json bson xml form:"field_4" other:"value_4"`,
+ key: "json",
+ expectedValue: "field_4",
+ expectedOK: true,
+ },
+ {
+ tag: `json bson xml form:"field_5" other:"value_5"`,
+ key: "non_existing",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `json "json_6"`,
+ key: "json",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `json:"json_7" bson "bson_7"`,
+ key: "json",
+ expectedValue: "json_7",
+ expectedOK: true,
+ },
+ {
+ tag: `json:"json_8" xml "xml_8"`,
+ key: "xml",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `json bson xml form "form_9" other:"value_9"`,
+ key: "bson",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `json bson xml form "form_10" other:"value_10"`,
+ key: "other",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `json bson xml form:"form_11" other "value_11"`,
+ key: "json",
+ expectedValue: "form_11",
+ expectedOK: true,
+ },
+ {
+ tag: `tag1`,
+ key: "tag1",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `tag2 :"hello_2"`,
+ key: "tag2",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: `tag3: "hello_3"`,
+ key: "tag3",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "json\x7fbson: \"hello_4\"",
+ key: "json",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "json\x7fbson: \"hello_5\"",
+ key: "bson",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "json bson:\x7f\"hello_6\"",
+ key: "json",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "json bson:\x7f\"hello_7\"",
+ key: "bson",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "json\x09bson:\"hello_8\"",
+ key: "json",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ {
+ tag: "a\x7fb json:\"val\"",
+ key: "json",
+ expectedValue: "",
+ expectedOK: false,
+ },
+ }
+
+ for _, test := range tests {
+ v, ok := test.tag.Lookup(test.key)
+ if v != test.expectedValue {
+ t.Errorf("struct tag lookup failed, got %s, want %s", v, test.expectedValue)
+ }
+ if ok != test.expectedOK {
+ t.Errorf("struct tag lookup failed, got %t, want %t", ok, test.expectedOK)
+ }
+ }
+}
+
// iterateToString returns the set of elements
// returned by an iterator in readable form.
func iterateToString(it *MapIter) string {