Source file test/fixedbugs/bug424.dir/main.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Tests that method calls through an interface always
     6  // call the locally defined method localT.m independent
     7  // at which embedding level it is and in which order
     8  // embedding is done.
     9  
    10  package main
    11  
    12  import "./lib"
    13  import "reflect"
    14  import "fmt"
    15  
    16  type localI interface {
    17  	m() string
    18  }
    19  
    20  type localT struct{}
    21  
    22  func (t *localT) m() string {
    23  	return "main.localT.m"
    24  }
    25  
    26  type myT1 struct {
    27  	localT
    28  }
    29  
    30  type myT2 struct {
    31  	localT
    32  	lib.T
    33  }
    34  
    35  type myT3 struct {
    36  	lib.T
    37  	localT
    38  }
    39  
    40  func main() {
    41  	var i localI
    42  
    43  	i = new(localT)
    44  	if i.m() != "main.localT.m" {
    45  		println("BUG: localT:", i.m(), "called")
    46  	}
    47  
    48  	i = new(myT1)
    49  	if i.m() != "main.localT.m" {
    50  		println("BUG: myT1:", i.m(), "called")
    51  	}
    52  
    53  	i = new(myT2)
    54  	if i.m() != "main.localT.m" {
    55  		println("BUG: myT2:", i.m(), "called")
    56  	}
    57  
    58  	t3 := new(myT3)
    59  	if t3.m() != "main.localT.m" {
    60  		println("BUG: t3:", t3.m(), "called")
    61  	}
    62  
    63  	i = new(myT3)
    64  	if i.m() != "main.localT.m" {
    65  		t := reflect.TypeOf(i)
    66  		n := t.NumMethod()
    67  		for j := 0; j < n; j++ {
    68  			m := t.Method(j)
    69  			fmt.Printf("#%d: %s.%s %s\n", j, m.PkgPath, m.Name, m.Type)
    70  		}
    71  		println("BUG: myT3:", i.m(), "called")
    72  	}
    73  
    74  	var t4 struct {
    75  		localT
    76  		lib.T
    77  	}
    78  	if t4.m() != "main.localT.m" {
    79  		println("BUG: t4:", t4.m(), "called")
    80  	}
    81  	i = &t4
    82  	if i.m() != "main.localT.m" {
    83  		println("BUG: myT4:", i.m(), "called")
    84  	}
    85  
    86  	var t5 struct {
    87  		lib.T
    88  		localT
    89  	}
    90  	if t5.m() != "main.localT.m" {
    91  		println("BUG: t5:", t5.m(), "called")
    92  	}
    93  	i = &t5
    94  	if i.m() != "main.localT.m" {
    95  		println("BUG: myT5:", i.m(), "called")
    96  	}
    97  }
    98  

View as plain text