aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/go/director_exception_runme.go
blob: 167d751938aa7f80569885a22d8d882ed1442e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

import . "swigtests/director_exception"

type Exception struct {
	msg string
}

func NewException(a, b string) *Exception {
	return &Exception{a + b}
}

type MyFoo struct{} // From Foo
func (p *MyFoo) Ping() string {
	panic("MyFoo::ping() EXCEPTION")
}

type MyFoo2 struct{} // From Foo
func (p *MyFoo2) Ping() bool {
	return true // should return a string
}

type MyFoo3 struct{} // From Foo
func (p *MyFoo3) Ping() string {
	panic(NewException("foo", "bar"))
}

func main() {
	// Check that the NotImplementedError raised by MyFoo.ping()
	// is returned by MyFoo.pong().
	ok := false
	a := NewDirectorFoo(&MyFoo{})
	b := Launder(a)
	func() {
		defer func() {
			e := recover()
			if e.(string) == "MyFoo::ping() EXCEPTION" {
				ok = true
			} else {
				panic("Unexpected error message: " + e.(string))
			}
		}()
		b.Pong()
	}()
	if !ok {
		panic(0)
	}

	// Check that if the method has the wrong return type it is
	// not called.
	ok = false
	a = NewDirectorFoo(&MyFoo2{})
	b = Launder(a)
	e := b.Pong()
	if e != "Foo::pong();"+"Foo::ping()" {
		panic(e)
	}

	// Check that the director can return an exception which
	// requires two arguments to the constructor, without mangling
	// it.
	ok = false
	a = NewDirectorFoo(&MyFoo3{})
	b = Launder(a)
	func() {
		defer func() {
			e := recover()
			if e.(*Exception).msg == "foobar" {
				ok = true
			} else {
				panic("Unexpected error message: " + e.(string))
			}
		}()
		b.Pong()
	}()
	if !ok {
		panic(0)
	}

	func() {
		defer func() {
			e := recover()
			_ = e.(Exception2)
		}()
		panic(NewException2())
	}()

	func() {
		defer func() {
			e := recover()
			_ = e.(Exception1)
		}()
		panic(NewException1())
	}()
}