Alan Donovan | 1f2812f | 2013-05-31 16:36:03 -0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | // Test of promotion of methods of an interface embedded within a |
Alan Donovan | fb0642f | 2013-07-29 14:24:09 -0400 | [diff] [blame] | 4 | // struct. In particular, this test exercises that the correct |
Alan Donovan | 1f2812f | 2013-05-31 16:36:03 -0400 | [diff] [blame] | 5 | // method is called. |
| 6 | |
| 7 | type I interface { |
| 8 | one() int |
| 9 | two() string |
| 10 | } |
| 11 | |
| 12 | type S struct { |
| 13 | I |
| 14 | } |
| 15 | |
| 16 | type impl struct{} |
| 17 | |
| 18 | func (impl) one() int { |
| 19 | return 1 |
| 20 | } |
| 21 | |
| 22 | func (impl) two() string { |
| 23 | return "two" |
| 24 | } |
| 25 | |
| 26 | func main() { |
| 27 | var s S |
| 28 | s.I = impl{} |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 29 | if one := s.I.one(); one != 1 { |
| 30 | panic(one) |
| 31 | } |
Alan Donovan | 1f2812f | 2013-05-31 16:36:03 -0400 | [diff] [blame] | 32 | if one := s.one(); one != 1 { |
| 33 | panic(one) |
| 34 | } |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 35 | closOne := s.I.one |
| 36 | if one := closOne(); one != 1 { |
| 37 | panic(one) |
| 38 | } |
| 39 | closOne = s.one |
| 40 | if one := closOne(); one != 1 { |
| 41 | panic(one) |
| 42 | } |
| 43 | |
| 44 | if two := s.I.two(); two != "two" { |
| 45 | panic(two) |
| 46 | } |
Alan Donovan | 1f2812f | 2013-05-31 16:36:03 -0400 | [diff] [blame] | 47 | if two := s.two(); two != "two" { |
| 48 | panic(two) |
| 49 | } |
Alan Donovan | 4da31df | 2013-07-26 11:22:34 -0400 | [diff] [blame] | 50 | closTwo := s.I.two |
| 51 | if two := closTwo(); two != "two" { |
| 52 | panic(two) |
| 53 | } |
| 54 | closTwo = s.two |
| 55 | if two := closTwo(); two != "two" { |
| 56 | panic(two) |
| 57 | } |
Alan Donovan | 1f2812f | 2013-05-31 16:36:03 -0400 | [diff] [blame] | 58 | } |