Class: TestBlockTry
- Defined in:
- lib/dolzenko/try_block.rb
Instance Method Summary collapse
- #setup ⇒ Object
- #test_asserts_passed_block ⇒ Object
- #test_core_ext ⇒ Object
- #test_doesnt_eat_general_exception ⇒ Object
- #test_doesnt_eat_general_exception_originated_from_somewhere_else ⇒ Object
- #test_doesnt_eat_nil_object_exception_originated_from_somewhere_else ⇒ Object
- #test_eval ⇒ Object
- #test_hash_digging ⇒ Object
- #test_meta_method ⇒ Object
- #test_name_error_in_block ⇒ Object
- #test_properly_eats_exception_on_nil_object_from_call_site ⇒ Object
Instance Method Details
#setup ⇒ Object
63 64 65 |
# File 'lib/dolzenko/try_block.rb', line 63 def setup TryBlock.install! end |
#test_asserts_passed_block ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/dolzenko/try_block.rb', line 158 def test_asserts_passed_block assert_raises ArgumentError do TryBlock.try_block end assert_raises ArgumentError do "qwerty".try_block end end |
#test_core_ext ⇒ Object
168 169 170 171 |
# File 'lib/dolzenko/try_block.rb', line 168 def test_core_ext assert_nil nil.try_block { 42 } assert_equal 6, "qwerty".try_block { length } end |
#test_doesnt_eat_general_exception ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/dolzenko/try_block.rb', line 89 def test_doesnt_eat_general_exception assert_raises RuntimeError do TryBlock { raise "FAIL" } end assert_raises RuntimeError do "qwerty".try_block { raise "FAIL" } end end |
#test_doesnt_eat_general_exception_originated_from_somewhere_else ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dolzenko/try_block.rb', line 99 def test_doesnt_eat_general_exception_originated_from_somewhere_else proc_raiser = proc { raise "FAIL" } obj_raiser = "qwerty".tap { |obj| def obj.raiser; raise "FAIL" end } assert_raises RuntimeError do TryBlock { proc_raiser[] } end assert_raises RuntimeError do obj_raiser.try_block { raiser } end assert_raises RuntimeError do [1].try_block { detect { |e| raise "FAIL" } } end end |
#test_doesnt_eat_nil_object_exception_originated_from_somewhere_else ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dolzenko/try_block.rb', line 116 def test_doesnt_eat_nil_object_exception_originated_from_somewhere_else some_nil = nil no_method_raiser = proc { some_nil.doesnt_exist_on_nil } obj_no_method_raiser = "qwerty".tap { |obj| def obj.raiser; doesnt_exist_on_string() end } if RUBY_VERSION < '1.9' # ["try_block.rb:97", # "try_block.rb:118:in `[]'", # "try_block.rb:118:in `test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", # "try_block.rb:37:in `BlockTry'", # "try_block.rb:118:in `test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", <= call site assert_raises(NoMethodError) do TryBlock do no_method_raiser[] end end else # ["try_block.rb:97:in `block in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", # "try_block.rb:112:in `[]'", # "try_block.rb:112:in `block (2 levels) in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", # "try_block.rb:37:in `BlockTry'", # "try_block.rb:112:in `block in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", <= call site # ... assert_raises(NoMethodError) { TryBlock { no_method_raiser[] } } # this will only work in 1.9 end assert_raises NoMethodError do obj_no_method_raiser.try_block { raiser } end end |
#test_eval ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/dolzenko/try_block.rb', line 180 def test_eval assert_raises(NoMethodError) { eval("TryBlock { 'qwerty'.doesnt_exist_on_string }") } assert_nil(eval("TryBlock { nil.doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/ assert_raises(NoMethodError) { eval("'qwerty'.try_block { doesnt_exist_on_string() }") } assert_nil(eval("'qwerty'.try_block { nil.doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/ assert_equal 6, eval("'qwerty'.try_block { length }") assert_nil(eval("[false].try_block { detect { |e| e }.doesnt_exist_on_nil.also_doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/ end |
#test_hash_digging ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/dolzenko/try_block.rb', line 148 def test_hash_digging h = { :asd => 123, :rty => 456} assert_nil TryBlock { h[:qwe][:asd][:zxc] } assert_equal 123, TryBlock { h[:asd] } assert_nil h.try_block { self[:qwe][:asd][:zxc] } assert_nil h.try_block { |ha| ha[:qwe][:asd][:zxc] } assert_equal 456, h.try_block { |ha| ha[:rty] } end |
#test_meta_method ⇒ Object
173 174 175 176 177 178 |
# File 'lib/dolzenko/try_block.rb', line 173 def obj_raiser = Class.new.tap { |c| c.module_eval("def raiser; raise 'FAIL' end ") } assert_raises(RuntimeError) { TryBlock { obj_raiser.new.raiser } } assert_raises(RuntimeError) { obj_raiser.new.try_block { raiser } } end |
#test_name_error_in_block ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/dolzenko/try_block.rb', line 79 def test_name_error_in_block assert_raises NameError do TryBlock { no_such_name } end assert_raises NameError do "qwerty".try_block { no_such_name } end end |
#test_properly_eats_exception_on_nil_object_from_call_site ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dolzenko/try_block.rb', line 67 def test_properly_eats_exception_on_nil_object_from_call_site some_nil = nil assert_nil TryBlock { some_nil.something_else } assert_nil "qwerty".try_block { some_nil.something_else } assert_nil some_nil.try_block { something_else } assert_nil [false].try_block { detect { |e| e }.something } end |