Sha256: b9adeb30de7780416bfac3237bf975cc763461cdd49029e5acdb9fc744cc81a0

Contents?: true

Size: 1.72 KB

Versions: 28

Compression:

Stored size: 1.72 KB

Contents

require 'test_helper'
require 'tins/xt'

module Tins
  class TryTest < Test::Unit::TestCase

    def test_attempt_block_condition
      assert attempt(:attempts => 1, :exception_class => nil) { |c| c == 1 }
      assert attempt(:attempts => 3, :exception_class => nil) { |c| c == 1 }
      assert_equal false, attempt(:attempts => 3, :exception_class => nil) { |c| c == 4 }
      assert_nil attempt(:attempts => 0, :exception_class => nil) { |c| c == 4 }
      assert_raise(Exception) { attempt(:attempts => 3, :exception_class => nil) { raise Exception } }
    end

    class MyError < StandardError; end
    class MyException < Exception; end

    def test_attempt_default_exception
      assert attempt(1) { |c| c != 1 and raise MyError }
      assert attempt(3) { |c| c != 1 and raise MyError }
      assert_equal false, attempt(3) { |c| c != 4 and raise MyError }
      assert_nil attempt(0) { |c| c != 4 and raise MyError }
      assert_raise(Exception) { attempt(3) { raise Exception } }
    end

    def test_attempt_exception
      assert attempt(:attempts => 1, :exception_class => MyException) { |c| c != 1 and raise MyException }
      assert attempt(:attempts => 3, :exception_class => MyException) { |c| c != 1 and raise MyException }
      assert_nil attempt(:attempts => 0, :exception_class => MyException) { |c| c != 4 and raise MyException }
      assert_raise(Exception) { attempt(:attempts => 3, :exception_class => MyException) { raise Exception } }
    end

    def test_reraise_exception
      tries = 0
      assert_raise(MyException) do
        attempt(:attempts => 3, :exception_class => MyException, :reraise => true) do |c|
          tries = c; raise MyException
        end
      end
      assert_equal 3, tries
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
tins-0.8.3 tests/try_test.rb
tins-0.8.2 tests/try_test.rb
tins-0.8.0 tests/try_test.rb
tins-0.7.4 tests/try_test.rb
tins-0.7.3 tests/try_test.rb
tins-0.7.2 tests/try_test.rb
tins-0.7.1 tests/try_test.rb
tins-0.7.0 tests/try_test.rb
tins-0.6.0 tests/try_test.rb
tins-0.5.6 tests/try_test.rb
tins-0.5.5 tests/try_test.rb
tins-0.5.4 tests/try_test.rb
tins-0.5.3 tests/try_test.rb
tins-0.5.2 tests/try_test.rb
tins-0.5.1 tests/try_test.rb
tins-0.5.0 tests/try_test.rb
tins-0.4.3 tests/try_test.rb
tins-0.4.2 tests/try_test.rb
tins-0.4.1 tests/try_test.rb
tins-0.4.0 tests/try_test.rb