PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms 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
  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
 16require '../../util/assert.rb'
 17
 18# first match in the sequence
 19def test_match_the_first
 20    $g = 1 
 21    begin 
 22        divide_by_zero
 23    rescue ZeroDivisionError
 24        $g += 10
 25    rescue NameError
 26        $g += 100
 27    rescue TypeError
 28        $g += 1000    
 29    end 
 30    assert_equal($g, 11)    
 31end 
 32
 33# second match in the sequence
 34def test_match_the_middle
 35    $g = 1 
 36    begin 
 37        divide_by_zero
 38    rescue NameError
 39        $g += 10
 40    rescue ZeroDivisionError
 41        $g += 100
 42    rescue TypeError
 43        $g += 1000    
 44    end 
 45    assert_equal($g, 101)
 46end 
 47
 48# last match in the sequence
 49def test_match_the_last
 50    $g = 1 
 51    begin 
 52        divide_by_zero
 53    rescue NameError
 54        $g += 10
 55    rescue TypeError
 56        $g += 100
 57    rescue ZeroDivisionError
 58        $g += 1000    
 59    end 
 60    assert_equal($g, 1001)
 61end 
 62
 63# parent/child relation
 64
 65# child before parent in the sequence
 66def test_match_exact_before_parent
 67    $g = 1 
 68    begin 
 69        divide_by_zero
 70    rescue ZeroDivisionError
 71        $g += 10
 72    rescue TypeError
 73        $g += 100
 74    rescue StandardError
 75        $g += 1000    
 76    end 
 77    assert_equal($g, 11)
 78end 
 79
 80# child after parent in the sequence
 81def test_match_parent_before_exact
 82    $g = 1 
 83    begin 
 84        divide_by_zero
 85    rescue StandardError
 86        $g += 10
 87    rescue TypeError
 88        $g += 100
 89    rescue ZeroDivisionError
 90        $g += 1000    
 91    end 
 92    assert_equal($g, 11)
 93end 
 94
 95# unusual cases
 96
 97# rescue twice by the exact exception
 98def test_exact_match_twice
 99    $g = 1
100    begin 
101        divide_by_zero
102    rescue ZeroDivisionError
103        $g += 10
104    rescue ZeroDivisionError
105        $g += 100
106    end    
107    assert_equal($g, 11)
108end 
109
110# rescue twice by the parent exception
111def test_parent_match_twice
112    $g = 1
113    begin 
114        divide_by_zero
115    rescue StandardError
116        $g += 10
117    rescue StandardError
118        $g += 100
119    end    
120    assert_equal($g, 11)
121end 
122
123test_match_the_first
124test_match_the_middle
125test_match_the_last
126test_match_exact_before_parent
127test_match_parent_before_exact
128test_exact_match_twice
129test_parent_match_twice
130