Skip to content

Commit ffb0bab

Browse files
Added test code for multireturn.
1 parent 1fe3270 commit ffb0bab

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

_examples/multireturn/multireturn.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package multireturn
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
/////////////// No Return //////////////
8+
func NoReturnFunc() {
9+
}
10+
11+
/////////////// Single WithoutError Return //////////////
12+
func SingleWithoutErrorFunc() int {
13+
return 100
14+
}
15+
16+
/////////////// Single WithError Return //////////////
17+
func SingleWithErrorFunc(throwError bool) error {
18+
if throwError {
19+
return fmt.Errorf("Error")
20+
} else {
21+
return nil
22+
}
23+
}
24+
25+
/////////////// Double WithoutError Return //////////////
26+
func DoubleWithoutErrorFunc() (int, int) {
27+
return 200, 300
28+
}
29+
30+
/////////////// Double WithError Return //////////////
31+
func DoubleWithErrorFunc(throwError bool) (string, error) {
32+
if throwError {
33+
return "400", fmt.Errorf("Error")
34+
} else {
35+
return "500", nil
36+
}
37+
}
38+
39+
/////////////// Triple Returns Without Error //////////////
40+
func TripleWithoutErrorFunc(vargs ...int) (int, string, int) {
41+
return 600, "700", 800
42+
}
43+
44+
/////////////// Triple Returns With Error //////////////
45+
func TripleWithErrorFunc(throwError bool) (int, int, error) {
46+
if throwError {
47+
return 900, 1000, fmt.Errorf("Error")
48+
} else {
49+
return 1100, 1200, nil
50+
}
51+
}
52+
53+
/////////////// Triple Struct Returns With Error //////////////
54+
type IntStrUct struct {
55+
P int
56+
}
57+
58+
func NewIntStrUct(n int) IntStrUct {
59+
return IntStrUct{
60+
P: n,
61+
}
62+
}
63+
64+
func TripleWithStructWithErrorFunc(throwError bool) (*IntStrUct, IntStrUct, error) {
65+
s1300 := IntStrUct{P: 1300}
66+
s1400 := IntStrUct{P: 1400}
67+
s1500 := IntStrUct{P: 1500}
68+
s1600 := IntStrUct{P: 1600}
69+
if throwError {
70+
return &s1300, s1400, fmt.Errorf("Error")
71+
} else {
72+
return &s1500, s1600, nil
73+
}
74+
}
75+
76+
/////////////// Triple Interface Returns Without Error //////////////
77+
type IntInterFace interface {
78+
Number() int
79+
}
80+
81+
func (is *IntStrUct) Number() int {
82+
return is.P
83+
}
84+
85+
func TripleWithInterfaceWithoutErrorFunc() (IntInterFace, IntStrUct, *IntStrUct) {
86+
i1700 := IntStrUct{P: 1700}
87+
s1800 := IntStrUct{P: 1800}
88+
s1900 := IntStrUct{P: 1900}
89+
90+
return &i1700, s1800, &s1900
91+
}

_examples/multireturn/test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2018 The go-python 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+
import multireturn, go
5+
6+
############### No Return ##############
7+
noResult = multireturn.NoReturnFunc()
8+
print("No Return %r" % noResult)
9+
10+
############### Single WithoutError Return ##############
11+
oneResult = multireturn.SingleWithoutErrorFunc()
12+
print("Single WithoutError Return %r" % oneResult)
13+
14+
############### Single WithError Return ##############
15+
errorFalseResult = multireturn.SingleWithErrorFunc(False)
16+
print("Single WithError(False) Return %r" % errorFalseResult)
17+
18+
errorTrueResult = multireturn.SingleWithErrorFunc(True)
19+
print("Single WithError(True) Return %r" % errorTrueResult)
20+
21+
############### Double WithoutError Return ##############
22+
twoResults = multireturn.DoubleWithoutErrorFunc()
23+
print("Double WithoutError Return %r" % twoResults)
24+
25+
############### Double WithError Return ##############
26+
(value400, errorTrueResult) = multireturn.DoubleeWithErrorFunc(True)
27+
print("Double WithError(True) Return (%r, %r)" % (value400, errorTrueResult))
28+
29+
(value500, errorFalseResult) = multireturn.DoubleWithErrorFunc(False)
30+
print("Double WithError(False) Return (%r, %r)" % (value500, errorFalseResult))
31+
32+
############### Triple Without Error Return ##############
33+
threeResults = multireturn.TripleWithoutErrorFunc()
34+
print("Triple WithoutError Return %r" % threeResult)
35+
36+
############### Triple With Error Return ##############
37+
(value900, value1000, errorTrueResult) = multireturn.TripleWithErrorFunc(True)
38+
print("Triple WithError(True) Return (%r, %r, %r)" % (value900, value1000, errorFalseResult))
39+
40+
(value1100, value1200, errorFalseResult) = multireturn.TripleWithErrorFunc(False)
41+
print("Triple WithError(False) Return (%r, %r, %r)" % (value1100, value1200, errorFalseResult))
42+
43+
############### Triple Struct Return With Error ##############
44+
(ptr1300, struct1400, errorTrueResult) = multireturn.TripleWithStructWithErrorFunc(True)
45+
print("Triple WithError(True) Return (%r, %r, %r)" % (ptr1300.P, struct1400.P, errorFalseResult))
46+
47+
(value1500, value1600, errorFalseResult) = multireturn.TripleWithStructWithErrorFunc(False)
48+
print("Triple WithError(False) Return (%r, %r, %r)" % (value1500.P, value1600.P, errorFalseResult))
49+
50+
############### Triple Interface Return Without Error ##############
51+
(interface1700, struct1800, ptr1900) = multireturn.TripleWithInterfaceWithoutErrorFunc()
52+
print("Triple WithError(True) Return (%r, %r, %r)" % (interface1700.P, struct1800.P, ptr1900))

main_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
"_examples/cstrings": []string{"py2", "py3"},
5050
"_examples/pkgconflict": []string{"py2", "py3"},
5151
"_examples/variadic": []string{"py3"},
52+
"_examples/multireturn": []string{"py3"},
5253
}
5354

5455
testEnvironment = os.Environ()
@@ -829,6 +830,31 @@ Type OK
829830
})
830831
}
831832

833+
func TestBindMultiReturn(t *testing.T) {
834+
// t.Parallel()
835+
path := "_examples/multireturn"
836+
testPkg(t, pkg{
837+
path: path,
838+
lang: features[path],
839+
cmd: "build",
840+
extras: nil,
841+
want: []byte(`No Return None
842+
Single WithoutError Return 100
843+
Single WithError(False) Return nil
844+
Single WithError(True) Return 'Error'
845+
Double WithoutError Return (200,300)
846+
Double WithError(True) Return (400, Error)
847+
Double WithError(False) Return (500, nil)
848+
Triple WithoutError Return (600, 700, 800)
849+
Triple WithError(True) Return (900, 1000, Error)
850+
Triple WithError(False) Return (1100, 1200, nil)
851+
Triple WithError(True) Return (1300, 1400, Error)
852+
Triple WithError(False) Return (1500, 1600, nil)
853+
Triple WithError(True) Return (1700, 1800, 1900)
854+
`),
855+
})
856+
}
857+
832858
// Generate / verify SUPPORT_MATRIX.md from features map.
833859
func TestCheckSupportMatrix(t *testing.T) {
834860
var buf bytes.Buffer

0 commit comments

Comments
 (0)