BufferedReader checkFile = new BufferedReader(new InputStreamReader(
inputStream));
String line;
String cleanedOutput = tokenStreamToString(lexerResult).replace("definedEx(",
"defined(");
FeatureExpr errorCondition = LexerFrontend.getErrorCondition(lexerResult);
while ((line = checkFile.readLine()) != null) {
//expecting NOT to find a token
if (line.startsWith("!")) {
String substring = line.substring(2);
if (cleanedOutput.contains(substring)) {
System.err.println(cleanedOutput);
Assert.fail(substring
+ " found but not expected in output\n"
+ cleanedOutput);
}
}
//expecting to find a token a specific number of times
if (line.startsWith("+")) {
int expected = Integer.parseInt(line.substring(1, 2));
int found = 0;
String substring = line.substring(3);
String content = cleanedOutput;
int idx = content.indexOf(substring);
while (idx >= 0) {
found++;
content = content.substring(idx + substring.length());
idx = content.indexOf(substring);
}
if (expected != found) {
failOutput(cleanedOutput);
Assert.fail(substring + " found " + found
+ " times, but expected " + expected + " times\n"
+ content);
}
}
//expect to find a token an arbitrary number of times
if (line.startsWith("*")) {
String substring = line.substring(2);
int idx = cleanedOutput.indexOf(substring);
if (idx < 0) {
failOutput(cleanedOutput);
Assert.fail(substring + " not found but expected\n"
+ cleanedOutput);
}
}
//expect to find a token with a specific presence condition
if (line.startsWith("T")) {
// checks presence condition for token
// Syntax: T <tokenText> with <presenceCondition>
String expectedName = line.substring(2);
String expectedFeature = expectedName.substring(expectedName
.indexOf(" with ") + 6);
expectedName = expectedName.substring(0, expectedName
.indexOf(" with "));
FeatureExpr expectedExpr = FeatureExprLib.featureExprParser().parse(expectedFeature);
boolean foundToken = false;
for (LexerToken t : LexerFrontend.conditionalResultToList(lexerResult, FeatureExprFactory.True())) {
if (t.getText().equals(expectedName)) {
foundToken = true;
// expect equivalent presence conditions
Assert.assertTrue("found token " + expectedName
+ " with " + t.getFeature()
+ " instead of expected " + expectedExpr,
t.getFeature().equivalentTo(expectedExpr));
}
}
Assert.assertTrue("token " + expectedName + " not found.",
foundToken);
}
//expects a lexer error (always or under a specific condition)
if (line.startsWith("error")) {
//error <- fails under all conditions
//error CONFIG_FOO | CONFIG_BAR <- fails under specific conditions
String condition = line.substring(5).trim();
if (condition.length() == 0)
condition = "1";
FeatureExpr expectedErrorCondition = FeatureExprLib.featureExprParser().parse(condition);
containsErrorCheck = true;
Assert.assertTrue(
"Expected error IF " + expectedErrorCondition + ", but preprocessing succeeded unless " + errorCondition,
errorCondition.equivalentTo(expectedErrorCondition));