PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Tests/Runtime/Exception/test_rescue_sequence.rb

#
Ruby | 130 lines | 95 code | 12 blank | 23 comment | 0 complexity | 9e39d9d9d6c221229d9d3df7fd1f61d2 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. # ****************************************************************************
  2. #
  3. # Copyright (c) Microsoft Corporation.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. # ****************************************************************************
  15. require '../../util/assert.rb'
  16. # first match in the sequence
  17. def test_match_the_first
  18. $g = 1
  19. begin
  20. divide_by_zero
  21. rescue ZeroDivisionError
  22. $g += 10
  23. rescue NameError
  24. $g += 100
  25. rescue TypeError
  26. $g += 1000
  27. end
  28. assert_equal($g, 11)
  29. end
  30. # second match in the sequence
  31. def test_match_the_middle
  32. $g = 1
  33. begin
  34. divide_by_zero
  35. rescue NameError
  36. $g += 10
  37. rescue ZeroDivisionError
  38. $g += 100
  39. rescue TypeError
  40. $g += 1000
  41. end
  42. assert_equal($g, 101)
  43. end
  44. # last match in the sequence
  45. def test_match_the_last
  46. $g = 1
  47. begin
  48. divide_by_zero
  49. rescue NameError
  50. $g += 10
  51. rescue TypeError
  52. $g += 100
  53. rescue ZeroDivisionError
  54. $g += 1000
  55. end
  56. assert_equal($g, 1001)
  57. end
  58. # parent/child relation
  59. # child before parent in the sequence
  60. def test_match_exact_before_parent
  61. $g = 1
  62. begin
  63. divide_by_zero
  64. rescue ZeroDivisionError
  65. $g += 10
  66. rescue TypeError
  67. $g += 100
  68. rescue StandardError
  69. $g += 1000
  70. end
  71. assert_equal($g, 11)
  72. end
  73. # child after parent in the sequence
  74. def test_match_parent_before_exact
  75. $g = 1
  76. begin
  77. divide_by_zero
  78. rescue StandardError
  79. $g += 10
  80. rescue TypeError
  81. $g += 100
  82. rescue ZeroDivisionError
  83. $g += 1000
  84. end
  85. assert_equal($g, 11)
  86. end
  87. # unusual cases
  88. # rescue twice by the exact exception
  89. def test_exact_match_twice
  90. $g = 1
  91. begin
  92. divide_by_zero
  93. rescue ZeroDivisionError
  94. $g += 10
  95. rescue ZeroDivisionError
  96. $g += 100
  97. end
  98. assert_equal($g, 11)
  99. end
  100. # rescue twice by the parent exception
  101. def test_parent_match_twice
  102. $g = 1
  103. begin
  104. divide_by_zero
  105. rescue StandardError
  106. $g += 10
  107. rescue StandardError
  108. $g += 100
  109. end
  110. assert_equal($g, 11)
  111. end
  112. test_match_the_first
  113. test_match_the_middle
  114. test_match_the_last
  115. test_match_exact_before_parent
  116. test_match_parent_before_exact
  117. test_exact_match_twice
  118. test_parent_match_twice