Tutorial 9 - Cleanup with finally

// tutorial9.ot
group "tutorial_9";

test("Fail, cleanup with finally")
{
  char* str = new char[10];
  op_strcmp(str, "Hello");
  verify(2 == 3);
  output("Never reached\n");
}
finally
{
  output("Fail variant, in finally\n");
  delete [] str;
}

test("Success, cleanup with finally")
{
  char* str = new char[10];
  op_strcmp(str, "Hello");
  output("Reached\n");
}
finally
{
  output("Success variant, in finally\n");
  delete [] str;
}

finally is nice to use when you need to cleanup after running a test, even if the test fails. When a test fail, execution will continue in the finally block. finally is in the same logical scope as the test body, so variables on the stack, like str in this example, can be reached. If a test succeed, it will continue directly with executing the finally block.

=========================================================================
Opera Testsuite
=========================================================================

=========================================================================
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial9.ot(2):  tutorial_9 
=========================================================================
  Fail, cleanup with finally .................................... FAILED
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial9.ot(8): '2' should be 3. The value is 2
Fail variant, in finally
  Success, cleanup with finally ................................. Reached
Passed
Success variant, in finally
=========================================================================
2 tests run, 1 test failed, 0 tests skipped, 1 test OK
Place a breakpoint in testsuite_break_here() to debug the failed test
=========================================================================