::xoexception::test
Class TestTry

Heritage:
::xotcl::Object
  |
  +--::xox::Node
        |
        +--::xounit::Test
              |
              +--::xounit::Assert
                    |
                    +--::xounit::TestCase
Class TestTry
superclass ::xounit::TestCase,

Variables
NameDefault ValueClassComment
currentTestMethod    ::xounit::TestCase
 name of the currently running test method.
nodeName    ::xox::Node
 
result    ::xounit::TestCase
 the result for the current run.
 
Methods
NameComment
testCatch {}  
testCatchAssert {}  
testCatchException {}  
testCatchMultiException {}  
testFinally {}  
testNestedTry {}  
testNestedTry2 {}  
testRethrowException {}  
   
Methods from ::xotcl::Object
#, ., ?, ?code, ?methods, ?object, abstract, copy, coverageFilter, defaultmethod, extractConfigureArg, filterappend, garbageCollect, get#, getClean#, hasclass, init, methodTag, mixinappend, move, profileFilter, self, setParameterDefaults, shell, tclcmd, traceFilter,
 
Instproc Detail

testCatch

Description:
 
Code:
  ::xoexception::test::TestTry instproc testCatch {}  {
   

    set value [ ::xoexception::try {

        error foobar

    } catch { error someError } { 

        my assertEquals $someError foobar
    }]

    my assertEquals $someError foobar
    unset someError
 
    ::xoexception::try {

        set value 1

    } catch { error someError } { 

        set value 2
    }

    my assertFalse [ info exists someError ]
    my assertEquals $value 1

}

testCatchAssert

Description:
 
Code:
  ::xoexception::test::TestTry instproc testCatchAssert {}  {
   

    ::xoexception::try {

        my fail "assert failure"

    } catch { ::xounit::AssertionError assert } {

        my assertTrue [ info exists assert ]
        my assertTrue [ Object isobject $assert ]
        my assertEquals [ $assert message ] "assert failure"
        set ran 1
    } 

    my assertTrue [ info exists assert ]
    my assertTrue [ Object isobject $assert ]
    my assertEquals [ $assert message ] "assert failure"
    my assertEquals $ran 1

}

testCatchException

Description:
 
Code:
  ::xoexception::test::TestTry instproc testCatchException {}  {
   

    catch {

        ::xoexception::try {

            error foobar

        } catch { ::xoexception::Exception anError } { 

            my fail "should not run"
        }

        puts "testCatchException failed did not throw uncaught error"

        exit 1
    }


    set value [ ::xoexception::try {

        error [ ::xoexception::Exception new ]

    } catch { error anError } { 

        my assertTrue [ Object isobject $anError ]
    }]

    my assertTrue [ Object isobject $anError ]

    set value [ ::xoexception::try {

        error [ ::xoexception::Error new ]

    } catch { error anError } { 

        my assertTrue [ Object isobject $anError ]
    }]

    my assertTrue [ info exists anError ]
    my assertTrue [ Object isobject $anError ]


    unset anError

    catch {

    set value [ ::xoexception::try {

        error [ ::xoexception::Exception new ]

    } catch { ::xoexception::Error anError } { 

    }]

    }

    my assertFalse [ info exists anError ]

    set value [ ::xoexception::try {

        error [ ::xoexception::Error new ]

    } catch { ::xoexception::Throwable anError } { 

    }]

    my assertTrue [ info exists anError ]
    my assertTrue [ Object isobject $anError ]

}

testCatchMultiException

Description:
 
Code:
  ::xoexception::test::TestTry instproc testCatchMultiException {}  {
   

    set value [ ::xoexception::try {

        error [ ::xoexception::Error new ]

    } catch { ::xoexception::Error anError } { 

    } catch { ::xoexception::Exception anExeception } {
        
    } catch { error tclError } {
        
    }]

    my assertTrue [ info exists anError ]
    my assertFalse [ info exists anException ]
    my assertFalse [ info exists tclError ]

    unset anError

    set value [ ::xoexception::try {

        error [ ::xoexception::Exception new ]

    } catch { ::xoexception::Error anError } { 

    } catch { ::xoexception::Exception anException } {
        
    } catch { error tclError } {
        
    }]

    my assertFalse [ info exists anError ]
    my assertTrue [ info exists anException ]
    my assertFalse [ info exists tclError ]

    unset anException

    set value [ ::xoexception::try {

        error foobar

    } catch { ::xoexception::Error anError } { 

    } catch { ::xoexception::Exception anExeception } {
        
    } catch { error tclError } {
        
    }]

    my assertFalse [ info exists anError ]
    my assertFalse [ info exists anException ]
    my assertTrue [ info exists tclError ]

}

testFinally

Description:
 
Code:
  ::xoexception::test::TestTry instproc testFinally {}  {
   

    ::xoexception::try {

        set value 1

    } finally { 

        set a 5
    }

    my assertEquals $value 1 
    my assertEquals $a 5 

    unset value
    unset a

    catch {

        ::xoexception::try {

            set value 1
            error foobar
            set value 2

        } finally { 

            set a 5
        }
    }

    my assertEquals $value 1 
    my assertEquals $a 5 

}

testNestedTry

Description:
 
Code:
  ::xoexception::test::TestTry instproc testNestedTry {}  {
   

    ::xoexception::try {

        ::xoexception::try {
            
            error [ ::xoexception::Exception new ]

        } catch { ::xoexception::Exception e } {

            my assertEquals [ $e info class ] ::xoexception::Exception 
        }

        my assert [ info exists e ]

    } catch { ::xoexception::Exception e } {

        my assertEquals [ $e info class ] ::xoexception::Exception 
    }

    my assert [ info exists e ]

}

testNestedTry2

Description:
 
Code:
  ::xoexception::test::TestTry instproc testNestedTry2 {}  {
   

    ::xoexception::try {

        ::xoexception::try {
            
            error [ ::xoexception::Error new ]

            my fail "Should have thrown error"

        } catch { ::xoexception::Exception e } {

           my fail "Should not catch exception"
        }

    } catch { ::xoexception::Error e } {

        my assertEquals [ $e info class ] ::xoexception::Error 
    }

    my assert [ info exists e ]

}

testRethrowException

Description:
 
Code:
  ::xoexception::test::TestTry instproc testRethrowException {}  {
   

    catch {

        ::xoexception::try {
            
            error [ ::xoexception::Error new ]
            my fail "Should throw error"

        } catch { ::xoexception::Exception e } {

            my fail "Should not be caught"
        }

    } e

   my assert [ info exists e ] 1
   my assert [ Object isobject $e ] 2
   my assertEquals [ $e info class ] ::xoexception::Error 3

}