home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / LIBRARY / TEST.SA < prev    next >
Text File  |  1994-10-25  |  4KB  |  100 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- test.sa: Class for testing classes.
  9. -------------------------------------------------------------------
  10. class TEST is
  11.    -- To be included by a testing class. The test routine should
  12.    -- start with the call `class_name("CLASS_FOO");'. It should then
  13.    -- have calls to `test' or `unchecked_test' which actually perform
  14.    -- the test. It should end with `finish' to print out the results.
  15.    -- The results are sent to both `stderr' and `stdout' so you can
  16.    -- redirect output to a file and still see whether all tests were
  17.    -- passed. The routine being tested must return a string. The
  18.    -- basic classes all have routines named `str' to produce string
  19.    -- representations of themselves. A typical test might look something
  20.    -- like: `test("sum", (1+1).str, "2");'. The tests are numbered and
  21.    -- the failures are summarized at the end. 
  22.  
  23.    shared class_name_str:STR;    -- The name of the tested class.
  24.    shared failures:FLIST{INT};    -- The tests which failed.
  25.    shared failure_docs:FLIST{STR}; -- The documentation of the failures.
  26.    shared test_number:INT;    -- Which test.
  27.    
  28.    class_name(nm:STR) is
  29.       -- Specify the name of the class being tested. Must be called first.
  30.       class_name_str:=nm;
  31.       test_number:=1;
  32.       #OUT + "Test of class " + nm + ":\n\n" end;
  33.  
  34.    test(doc,does,should:STR) is
  35.       -- Perform the test with the description `doc', return value `does',
  36.       -- and desired return value `should'. Keep track of failures.
  37.       if (void(class_name_str)) then
  38.      #ERR + "Error in TEST class: \"class_name\" not called\n";
  39.      return;
  40.      end;
  41.       #OUT + "   " + class_name_str + " Test ";
  42.       #OUT + test_number.str + " (" + doc + ")  ";
  43.       if does.is_eq(should).not then
  44.      failures:=failures.push(test_number);
  45.      failure_docs:=failure_docs.push(doc);
  46.      #OUT + " Found problem!\n";
  47.       else #OUT + " succeeded\n"; end;
  48.       #OUT + "   Should = " + should + "\n";
  49.       #OUT + "   Does   = " + does + "\n\n";
  50.       test_number:=test_number+1 end;
  51.    
  52.    unchecked_test(doc,does,should:STR) is
  53.       -- Perform the test with the description `doc', return value `does',
  54.       -- and desired return value `should'. Don't keep track of failures.
  55.       #OUT + "   " + class_name_str + " Test " + test_number.str +
  56.          " (" + doc + ")\n";
  57.       #OUT + "   Should = " + should + "\n";
  58.       #OUT + "   Does   = " + does +"\n\n";
  59.       test_number:=test_number+1 end;
  60.  
  61.    finish is
  62.       -- Complete the testing on the current class.
  63.       if failures.is_empty then
  64.      -- Put on both ERR and OUT so one can avoid looking
  65.      -- if one wants.
  66.      #ERR + "Class " + class_name_str +
  67.         " passed all checkable tests.\n";
  68.      #OUT + "OUT: Class " + class_name_str +
  69.         " passed all checkable tests.\n"
  70.       else
  71.      loop
  72.         f1::=failures.elt!.str;
  73.         f2::=failure_docs.elt!;
  74.         msg::="Class "+class_name_str+" failed test "+f1+" ("+f2+")\n";
  75.         #ERR + msg;
  76.         #OUT + "OUT: " + msg;
  77.      end;
  78.       end end;
  79.  
  80. end; -- class TEST
  81.  
  82. -------------------------------------------------------------------
  83. class TEST_TEST is
  84.    -- Test of `TEST'.
  85.    
  86.    include TEST;
  87.    
  88.    main is
  89.       -- Test of `TEST'.
  90.       class_name("TEST");
  91.       test("A good test", "good", "good");
  92.       test("A bad test", "good", "bad");
  93.       unchecked_test("Unchecked test", "anything", "anything");
  94.       finish;
  95.    end; -- main
  96.    
  97. end; -- class TEST_TEST
  98.  
  99. -------------------------------------------------------------------
  100.