001: /**************************************************************************/
002: /* NICE Testsuite */
003: /* A testsuite for the Nice programming language */
004: /* (c) Alex Greif 2002 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.testsuite;
012:
013: import java.util.Iterator;
014:
015: /**
016: * TestCase class for test cases that should fail.
017: *
018: * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
019: * @version $Id: FailTestCase.java,v 1.14 2005/03/21 21:25:57 bonniot Exp $
020: */
021: public class FailTestCase extends TestCase {
022:
023: /**
024: * Constructor.
025: *
026: * @param suite The testsuite this testcase belongs to
027: */
028: public FailTestCase(TestSuite suite) {
029: super (suite);
030: }
031:
032: /**
033: * Performs the test for this testcase.
034: * Compilation should fail, otherwise throw exception.
035: *
036: */
037: public void performTest() {
038: super .performTest();
039: try {
040: compilePackages();
041: } catch (TestSuiteException e) {
042: if (!noLocation && getFailPositions().isEmpty()) {
043: TestNice.getOutput().log("warning",
044: "Failure position not checked");
045: warning();
046: return;
047: }
048: checkFailPositions();
049: if (getFailPositions().isEmpty())
050: pass();
051: else
052: warning();
053: return;
054: } catch (CompilerBugException e) {
055: fail();
056: return;
057: }
058:
059: TestNice.getOutput().log(
060: "Compilation was expected to fail, but it succeeded.");
061: fail();
062: }
063:
064: /**
065: * Parses the compiler messages for line, column, filename and compares them with the
066: * expected failure positions that the user defined. If the user defined more expected
067: * failure positions than the compiler recognized then a warning is shown.
068: * A warning is also shown when the user expected a failure at another position than the
069: * compiler recognized.
070: *
071: */
072: private void checkFailPositions() {
073: String compilerMessages = getCompilerMessages();
074: //System.out.println(">" + compilerMessages + "<");
075:
076: for (Iterator iter = getFailPositions().iterator(); iter
077: .hasNext();) {
078: FailPosition failPosition = (FailPosition) iter.next();
079: String s = failPosition.getFileName() + ": line "
080: + failPosition.getLine() + ", column "
081: + failPosition.getColumn() + ":";
082: //System.out.println(">" + s + "<");
083: if (compilerMessages.indexOf(s) == -1)
084: TestNice.getOutput().log(
085: "warning",
086: "Failure not at expected position ("
087: + failPosition.getFileName()
088: + ": line " + failPosition.getLine()
089: + ", column "
090: + failPosition.getColumn() + ")");
091: else
092: iter.remove();
093:
094: }
095:
096: if (!getFailPositions().isEmpty())
097: TestNice.getOutput().log("warning",
098: "more expected failures than compiler failures");
099:
100: /*
101: int failPositionCounter = 0;
102: int pos = 0;
103:
104:
105: while(true) {
106: int linePos = compilerMessages.indexOf(": line ", pos);
107:
108: if (linePos == -1)
109: break;
110:
111: pos = linePos + 7;
112: int columnPos = compilerMessages.indexOf(", column ");
113:
114: int line = Integer.parseInt(compilerMessages.substring(pos, columnPos).trim());
115:
116: pos = columnPos + 9;
117: int colonPos = compilerMessages.indexOf(":", pos);
118:
119: int column = Integer.parseInt(compilerMessages.substring(pos, colonPos).trim());
120:
121: FailPosition failPosition = (FailPosition)getFailPositions().get(failPositionCounter++);
122:
123: System.out.println("comp-line: " + line + " comp-column: " + column);
124: System.out.println("exp-line: " + failPosition.getLine() + " exp-column: " + failPosition.getColumn());
125:
126:
127: if (line != failPosition.getLine() || column != failPosition.getColumn())
128: TestNice.getOutput().log("warning", "Failure not at expected position (" +
129: failPosition.getLine() + "," + failPosition.getColumn() +
130: ") but at (" + line + "," + column + ")");
131: }
132: */
133: }
134:
135: }
136:
137: // Local Variables:
138: // tab-width: 2
139: // End:
|