Tutorial 11 - Foreach magic

// tutorial11.ot
group "tutorial_11";

table Table1(int, int, char*)
{
  { 3, 3, "Hello" },
  { 4, 54, "Hi" },
  { 5, 65, "Hej" },
  { 1, 100, "Hejsan" }
}

foreach (a, b, c) from Table1
{
  test("The test " c)
  {
    verify(a == 3 || a == 4 || a == 5 || a == 1);
    verify(b == 3 || b == 54 || b == 65 || b == 100);
    verify(op_strcmp(c, "Hello") == 0 ||
	   op_strcmp(c, "Hi") == 0 ||
	   op_strcmp(c, "Hej") == 0 ||
	   op_strcmp(c, "Hejsan") == 0);
  }

  test("Failing test 3 only " c)
  {
    verify(a == 3 || a == 4 || a == 5 || a == 1);
    verify(b == 3 || b == 54 || b == 65 || b == 100);
    verify(op_strcmp(c, "Hello") == 0 ||
	   op_strcmp(c, "Hi") == 0 ||
	   op_strcmp(c, "Hejsan") == 0);
  }

}

foreach is a very magical thing. For each table row, it will duplicate the content inside the foreach body. If there are tests inside the foreach body, they will be duplicated and independent of each other. So, in this test, the "Hej" version called "Failing test 3 only Hej", will fail, but all other tests will run.

In the example we also introduce an advanced way to work with strings in Selftest. See the line test("The test" c). The 2 strings will be concatenated into one. That will hopefully simplify string handling a lot for you, which is good when writing tests.

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

=========================================================================
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial11.ot(2):  tutorial_11 
=========================================================================
  The test Hello ................................................ Passed
  Failing test 3 only Hello ..................................... Passed
  The test Hi ................................................... Passed
  Failing test 3 only Hi ........................................ Passed
  The test Hej .................................................. Passed
  Failing test 3 only Hej ....................................... FAILED
E:/src/operaclean/opera-windows7/modules/selftest/documentation/tutorial/tutorial11.ot(30): strcmp("Hej", "Hello") == 0 ||
	       strcmp("Hej", "Hi") == 0 ||
		   strcmp("Hej", "Hejsan") == 0) should be true
  The test Hejsan ............................................... Passed
  Failing test 3 only Hejsan .................................... Passed
=========================================================================
8 tests run, 1 test failed, 0 tests skipped, 7 tests OK
Place a breakpoint in testsuite_break_here() to debug the failed test
=========================================================================