~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/reflect/all_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
127
127
        },
128
128
        {struct {
129
129
                x struct {
130
 
                        a int8 "hi there"
 
130
                        a int8 `reflect:"hi there"`
131
131
                }
132
132
        }{},
133
 
                `struct { a int8 "hi there" }`,
 
133
                `struct { a int8 "reflect:\"hi there\"" }`,
134
134
        },
135
135
        {struct {
136
136
                x struct {
137
 
                        a int8 "hi \x00there\t\n\"\\"
 
137
                        a int8 `reflect:"hi \x00there\t\n\"\\"`
138
138
                }
139
139
        }{},
140
 
                `struct { a int8 "hi \x00there\t\n\"\\" }`,
 
140
                `struct { a int8 "reflect:\"hi \\x00there\\t\\n\\\"\\\\\"" }`,
141
141
        },
142
142
        {struct {
143
143
                x struct {
423
423
 
424
424
        // make sure tag strings are not part of element type
425
425
        typ = TypeOf(struct {
426
 
                d []uint32 "TAG"
 
426
                d []uint32 `reflect:"TAG"`
427
427
        }{}).Field(0).Type
428
428
        testType(t, 14, typ, "[]uint32")
429
429
}
1050
1050
        x, y int
1051
1051
}
1052
1052
 
 
1053
// This will be index 0.
 
1054
func (p Point) AnotherMethod(scale int) int {
 
1055
        return -1
 
1056
}
 
1057
 
 
1058
// This will be index 1.
1053
1059
func (p Point) Dist(scale int) int {
1054
1060
        //      println("Point.Dist", p.x, p.y, scale)
1055
1061
        return p.x*p.x*scale + p.y*p.y*scale
1058
1064
func TestMethod(t *testing.T) {
1059
1065
        // Non-curried method of type.
1060
1066
        p := Point{3, 4}
1061
 
        i := TypeOf(p).Method(0).Func.Call([]Value{ValueOf(p), ValueOf(10)})[0].Int()
 
1067
        i := TypeOf(p).Method(1).Func.Call([]Value{ValueOf(p), ValueOf(10)})[0].Int()
1062
1068
        if i != 250 {
1063
1069
                t.Errorf("Type Method returned %d; want 250", i)
1064
1070
        }
1065
1071
 
1066
 
        i = TypeOf(&p).Method(0).Func.Call([]Value{ValueOf(&p), ValueOf(10)})[0].Int()
 
1072
        m, ok := TypeOf(p).MethodByName("Dist")
 
1073
        if !ok {
 
1074
                t.Fatalf("method by name failed")
 
1075
        }
 
1076
        m.Func.Call([]Value{ValueOf(p), ValueOf(10)})[0].Int()
 
1077
        if i != 250 {
 
1078
                t.Errorf("Type MethodByName returned %d; want 250", i)
 
1079
        }
 
1080
 
 
1081
        i = TypeOf(&p).Method(1).Func.Call([]Value{ValueOf(&p), ValueOf(10)})[0].Int()
1067
1082
        if i != 250 {
1068
1083
                t.Errorf("Pointer Type Method returned %d; want 250", i)
1069
1084
        }
1070
1085
 
 
1086
        m, ok = TypeOf(&p).MethodByName("Dist")
 
1087
        if !ok {
 
1088
                t.Fatalf("ptr method by name failed")
 
1089
        }
 
1090
        i = m.Func.Call([]Value{ValueOf(&p), ValueOf(10)})[0].Int()
 
1091
        if i != 250 {
 
1092
                t.Errorf("Pointer Type MethodByName returned %d; want 250", i)
 
1093
        }
 
1094
 
1071
1095
        // Curried method of value.
1072
 
        i = ValueOf(p).Method(0).Call([]Value{ValueOf(10)})[0].Int()
 
1096
        i = ValueOf(p).Method(1).Call([]Value{ValueOf(10)})[0].Int()
1073
1097
        if i != 250 {
1074
1098
                t.Errorf("Value Method returned %d; want 250", i)
1075
1099
        }
 
1100
        i = ValueOf(p).MethodByName("Dist").Call([]Value{ValueOf(10)})[0].Int()
 
1101
        if i != 250 {
 
1102
                t.Errorf("Value MethodByName returned %d; want 250", i)
 
1103
        }
1076
1104
 
1077
1105
        // Curried method of pointer.
1078
 
        i = ValueOf(&p).Method(0).Call([]Value{ValueOf(10)})[0].Int()
1079
 
        if i != 250 {
1080
 
                t.Errorf("Value Method returned %d; want 250", i)
 
1106
        i = ValueOf(&p).Method(1).Call([]Value{ValueOf(10)})[0].Int()
 
1107
        if i != 250 {
 
1108
                t.Errorf("Pointer Value Method returned %d; want 250", i)
 
1109
        }
 
1110
        i = ValueOf(&p).MethodByName("Dist").Call([]Value{ValueOf(10)})[0].Int()
 
1111
        if i != 250 {
 
1112
                t.Errorf("Pointer Value MethodByName returned %d; want 250", i)
1081
1113
        }
1082
1114
 
1083
1115
        // Curried method of interface value.
1094
1126
        if i != 250 {
1095
1127
                t.Errorf("Interface Method returned %d; want 250", i)
1096
1128
        }
 
1129
        i = pv.MethodByName("Dist").Call([]Value{ValueOf(10)})[0].Int()
 
1130
        if i != 250 {
 
1131
                t.Errorf("Interface MethodByName returned %d; want 250", i)
 
1132
        }
1097
1133
}
1098
1134
 
1099
1135
func TestInterfaceSet(t *testing.T) {
1508
1544
                t.Errorf("after Fprintf CallSlice: %q != %q", b.String(), "hello 42 world")
1509
1545
        }
1510
1546
}
 
1547
 
 
1548
var tagGetTests = []struct {
 
1549
        Tag   StructTag
 
1550
        Key   string
 
1551
        Value string
 
1552
}{
 
1553
        {`protobuf:"PB(1,2)"`, `protobuf`, `PB(1,2)`},
 
1554
        {`protobuf:"PB(1,2)"`, `foo`, ``},
 
1555
        {`protobuf:"PB(1,2)"`, `rotobuf`, ``},
 
1556
        {`protobuf:"PB(1,2)" json:"name"`, `json`, `name`},
 
1557
        {`protobuf:"PB(1,2)" json:"name"`, `protobuf`, `PB(1,2)`},
 
1558
}
 
1559
 
 
1560
func TestTagGet(t *testing.T) {
 
1561
        for _, tt := range tagGetTests {
 
1562
                if v := tt.Tag.Get(tt.Key); v != tt.Value {
 
1563
                        t.Errorf("StructTag(%#q).Get(%#q) = %#q, want %#q", tt.Tag, tt.Key, v, tt.Value)
 
1564
                }
 
1565
        }
 
1566
}