PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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