PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/go/director_exception_runme.go

#
Go | 95 lines | 77 code | 11 blank | 7 comment | 10 complexity | f2d23844f5e5d1f81d7acccd5a415dbe MD5 | raw file
 1package main
 2
 3import . "./director_exception"
 4
 5type Exception struct {
 6	msg string
 7}
 8
 9func NewException(a, b string) *Exception {
10	return &Exception{a + b}
11}
12
13type MyFoo struct{} // From Foo
14func (p *MyFoo) Ping() string {
15	panic("MyFoo::ping() EXCEPTION")
16}
17
18type MyFoo2 struct{} // From Foo
19func (p *MyFoo2) Ping() bool {
20	return true // should return a string
21}
22
23type MyFoo3 struct{} // From Foo
24func (p *MyFoo3) Ping() string {
25	panic(NewException("foo", "bar"))
26}
27
28func main() {
29	// Check that the NotImplementedError raised by MyFoo.ping()
30	// is returned by MyFoo.pong().
31	ok := false
32	a := NewDirectorFoo(&MyFoo{})
33	b := Launder(a)
34	func() {
35		defer func() {
36			e := recover()
37			if e.(string) == "MyFoo::ping() EXCEPTION" {
38				ok = true
39			} else {
40				panic("Unexpected error message: " + e.(string))
41			}
42		}()
43		b.Pong()
44	}()
45	if !ok {
46		panic(0)
47	}
48
49	// Check that if the method has the wrong return type it is
50	// not called.
51	ok = false
52	a = NewDirectorFoo(&MyFoo2{})
53	b = Launder(a)
54	e := b.Pong()
55	if e != "Foo::pong();"+"Foo::ping()" {
56		panic(e)
57	}
58
59	// Check that the director can return an exception which
60	// requires two arguments to the constructor, without mangling
61	// it.
62	ok = false
63	a = NewDirectorFoo(&MyFoo3{})
64	b = Launder(a)
65	func() {
66		defer func() {
67			e := recover()
68			if e.(*Exception).msg == "foobar" {
69				ok = true
70			} else {
71				panic("Unexpected error message: " + e.(string))
72			}
73		}()
74		b.Pong()
75	}()
76	if !ok {
77		panic(0)
78	}
79
80	func() {
81		defer func() {
82			e := recover()
83			_ = e.(Exception2)
84		}()
85		panic(NewException2())
86	}()
87
88	func() {
89		defer func() {
90			e := recover()
91			_ = e.(Exception1)
92		}()
93		panic(NewException1())
94	}()
95}