github.com/goplus/gossa@v0.3.25/testdata/mrvchain.go (about)

     1  // Tests of call chaining f(g()) when g has multiple return values (MRVs).
     2  // See https://code.google.com/p/go/issues/detail?id=4573.
     3  
     4  package main
     5  
     6  func assert(actual, expected int) {
     7  	if actual != expected {
     8  		panic(actual)
     9  	}
    10  }
    11  
    12  func g() (int, int) {
    13  	return 5, 7
    14  }
    15  
    16  func g2() (float64, float64) {
    17  	return 5, 7
    18  }
    19  
    20  func f1v(x int, v ...int) {
    21  	assert(x, 5)
    22  	assert(v[0], 7)
    23  }
    24  
    25  func f2(x, y int) {
    26  	assert(x, 5)
    27  	assert(y, 7)
    28  }
    29  
    30  func f2v(x, y int, v ...int) {
    31  	assert(x, 5)
    32  	assert(y, 7)
    33  	assert(len(v), 0)
    34  }
    35  
    36  func complexArgs() (float64, float64) {
    37  	return 5, 7
    38  }
    39  
    40  func appendArgs() ([]string, string) {
    41  	return []string{"foo"}, "bar"
    42  }
    43  
    44  func h() (i interface{}, ok bool) {
    45  	m := map[int]string{1: "hi"}
    46  	i, ok = m[1] // string->interface{} conversion within multi-valued expression
    47  	return
    48  }
    49  
    50  func h2() (i interface{}, ok bool) {
    51  	ch := make(chan string, 1)
    52  	ch <- "hi"
    53  	i, ok = <-ch // string->interface{} conversion within multi-valued expression
    54  	return
    55  }
    56  
    57  func main() {
    58  	f1v(g())
    59  	f2(g())
    60  	f2v(g())
    61  	if c := complex(complexArgs()); c != 5+7i {
    62  		panic(c)
    63  	}
    64  	if s := append(appendArgs()); len(s) != 2 || s[0] != "foo" || s[1] != "bar" {
    65  		panic(s)
    66  	}
    67  	i, ok := h()
    68  	if !ok || i.(string) != "hi" {
    69  		panic(i)
    70  	}
    71  	i, ok = h2()
    72  	if !ok || i.(string) != "hi" {
    73  		panic(i)
    74  	}
    75  }