package tool.model.grammar;
import static org.junit.Assert.*;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.junit.BeforeClass;
import org.junit.Test;
public class ForteParserTest {
ForteParser parser = new ForteParser(null);
private TokenStream getStream(String source){
CharStream stream =
new NoCaseStringStream(source);
ForteLexer lexer = new ForteLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return tokenStream;
}
private void failOnSyntaxError(String string){
int errors = parser.getNumberOfSyntaxErrors();
System.out.println(string + " errors: " + errors);
if (errors > 0){
fail("Syntax errors: " + errors);
}
}
@Test
public void testForstatementCollection() throws RecognitionException {
parser.setTokenStream(getStream(" for p in self.PaintingData do\n" +
" if p.Name = deletedName then\n" +
" -- This is the one. Delete it.\n" +
" self.PaintingData.DeleteRow(p);\n" +
" exit;\n" +
" end if;\n" +
" end for;\n" +
""));
parser.forStatement();
failOnSyntaxError("forstatementCollection");
}
@Test
public void testForstatementCountControlled() throws RecognitionException {
parser.setTokenStream(getStream(" for p in 1 to 23 do\n" +
" if p.Name = deletedName then\n" +
" -- This is the one. Delete it.\n" +
" self.PaintingData.DeleteRow(p);\n" +
" exit;\n" +
" end if;\n" +
" end for;\n" +
""));
parser.forStatement();
failOnSyntaxError("forstatementCollectionCountControlled");
}
@Test
public void testForstatementCountControlledBy() throws RecognitionException {
parser.setTokenStream(getStream(" for p in 100 to 1 by -1 do\n" +
" if p.Name = deletedName then\n" +
" -- This is the one. Delete it.\n" +
" self.PaintingData.DeleteRow(p);\n" +
" exit;\n" +
" end if;\n" +
" end for;\n" +
""));
parser.forStatement();
failOnSyntaxError("forstatementCollectionCountControlledBy");
}
@Test
public void testExitStatment() throws RecognitionException {
parser.setTokenStream(getStream("exit;"));
parser.exitStatment();
failOnSyntaxError("exitStatment");
}
@Test
public void testExitStatmentWithLabel() throws RecognitionException {
parser.setTokenStream(getStream("exit bob;"));
parser.exitStatment();
failOnSyntaxError("exitStatmentWithLabel");
}
@Test
public void testContinueStatment() throws RecognitionException {
parser.setTokenStream(getStream("continue;"));
parser.continueStatment();
failOnSyntaxError("continueStatment");
}
@Test
public void testContinueStatmentWithLabel() throws RecognitionException {
parser.setTokenStream(getStream("continue bob;"));
parser.continueStatment();
failOnSyntaxError("continueStatmentWithLabel");
}
@Test
public void testReturnStatementMethodCall() throws RecognitionException {
parser.setTokenStream(getStream("return self.GetBidForPainting (name = name, \n" +
" paintingForBid = p);"));
parser.returnStatement();
failOnSyntaxError("returnStatementMethodCall");
}
@Test
public void testReturnStatementNil() throws RecognitionException {
parser.setTokenStream(getStream("return;"));
parser.returnStatement();
failOnSyntaxError("returnStatementNil");
}
@Test
public void testReturnStatementVariable() throws RecognitionException {
parser.setTokenStream(getStream("return abcde;"));
parser.returnStatement();
failOnSyntaxError("returnStatementVariable");
}
@Test
public void testReturnStatementExpression() throws RecognitionException {
parser.setTokenStream(getStream("return 3*4;"));
parser.returnStatement();
failOnSyntaxError("returnStatementExpression");
}
@Test
public void testCaseStatement() throws RecognitionException {
parser.setTokenStream(getStream("" +
" case AuctionService.RequestAuctioneerStatus\n" +
" (password = self.AuctioneerPassword, \n" +
" name = self.UserName) is\n" +
" when 0 do\n" +
" -- Got it. Turn on the right UI panel.\n" +
" <AuctioneerOnly>.State = FS_UPDATE;\n" +
" self.IsAuctioneer = TRUE;\n" +
" when 1 do\n" +
" -- Bad password.\n" +
" self.Window.MessageDialog(\n" +
" 'Bad password. Enter new password and try again.',\n" +
" messageType = MT_ERROR);\n" +
" when 2 do\n" +
" -- Already has an auctioneer.\n" +
" self.Window.MessageDialog(\n" +
" 'Someone else is the auctioneer. Try again later.',\n" +
" messageType = MT_ERROR);\n" +
" end case;\n" +
""));
parser.caseStatement();
failOnSyntaxError("caseStatement");
}
// @Test
// public void testDoWhileStatement() throws RecognitionException {
// parser.setTokenStream(getStream(""));
// parser.doWhileStatement();
// failOnSyntaxError("doWhileStatement");
// }
@Test
public void testWhileDoStatement() throws RecognitionException {
parser.setTokenStream(getStream(" while_loop : while not isConnected do\n" +
" -- First assume that Excel is running and open \n" +
" -- the connection.\n" +
" begin\n" +
" -- Don't autoLaunch if it is not running, \n" +
" -- as Excel will not open the correct \n" +
" -- worksheet.\n" +
" dde_cnv.InitiateConnection (\n" +
" application='Excel',\n" +
" topic = worksheet_name, \n" +
" autoLaunch = FALSE);\n" +
" status_line = 'Successfully connected...';\n" +
" isConnected = TRUE;\n" +
" -- Now set the window usage to allow \n" +
" -- operations.\n" +
" self.Window.Usage = WU_UPDATE;\n" +
" exception -- Worksheet not running.\n" +
" when e : SystemResourceException do\n" +
" status_line = 'Need to start EXCEL...';\n" +
" end;\n" +
"\n" +
" -- If it got started, get out of while loop.\n" +
" if isConnected then\n" +
" exit;\n" +
" -- If this isn't the first pass, then Excel is\n" +
" -- running, but our connection failed. It could just\n" +
" -- be slow to start on slow machines, so we'll give it\n" +
" -- 5 tries, with a 3 second pause after each.\n" +
" elseif (counter > 1) and (counter <= 5) then\n" +
" -- Call method that pauses 3 seconds.\n" +
" self.OurPause();\n" +
" elseif counter > 5 then\n" +
" status_line = \n" +
" 'Excel is running but Forte connection failed.';\n" +
" exit; \n" +
" end if;\n" +
"\n" +
" if not already_started then\n" +
" -- The worksheet is not started. The easiest \n" +
" -- thing to do is to start it up, using the \n" +
" -- RunCommand. Use an asynchronous call to \n" +
" -- RunCommand, because you don't want to wait \n" +
" -- for Excel to exit before continuing.\n" +
" cmd : TextData = new;\n" +
" -- Note: Edit this path if your EXCEL executable is \n" +
" -- located elsewhere.\n" +
" cmd.SetValue('C:\\\\EXCEL\\\\EXCEL.EXE ');\n" +
" cmd.Concat(worksheet_name);\n" +
" begin\n" +
" task.Part.OperatingSystem.RunCommand\n" +
" (command = cmd, isSynchronous = FALSE);\n" +
" \n" +
" already_started = TRUE;\n" +
" exception -- Worksheet not running.\n" +
" when e : SystemException do\n" +
" status_line = \n" +
" 'Could not open worksheet or Excel.';\n" +
" exit while_loop; -- Exit the While loop.\n" +
" \n" +
" end;\n" +
" end if;\n" +
" -- If it gets here, it should be running. The \n" +
" -- code now loops back to retry to \n" +
" -- InitiateConnection to the freshly started \n" +
" -- spreadsheet. We bump the counter so we can check if\n" +
" -- we' re trying to start excel more than once.\n" +
" counter = counter + 1;\n" +
" end while;\n" +
""));
parser.whileDoStatement();
failOnSyntaxError("whileDoStatement");
}
@Test
public void testIfStatement() throws RecognitionException {
parser.setTokenStream(getStream("if b = NIL then\n" +
" -- None found. Print out message and return.\n" +
" t.ReplaceParameters();\n" +
"end if;\n" +
""));
parser.ifStatement();
failOnSyntaxError("ifStatement");
}
@Test
public void testIfStatementWithElse() throws RecognitionException {
parser.setTokenStream(getStream("if b = NIL then\n" +
" -- None found. Print out message and return.\n" +
" t.ReplaceParameters();\n" +
"else\n" +
" -- Painting found. Delete it and the bid.\n" +
" foundit = TRUE;\n" +
" t.ReplaceParameters();\n" +
" -- Now delete the entry in the array.\n" +
" self.PaintingsUnderBid.DeleteRow(b);\n" +
"\n" +
" -- Send out an event.\n" +
" post self.PaintingDeleted (paintingName = p.name);\n" +
"end if;\n" +
""));
parser.ifStatement();
failOnSyntaxError("ifStatementWithElse");
}
@Test
public void testIfStatementWithElseIf() throws RecognitionException {
parser.setTokenStream(getStream("if b = NIL then\n" +
" -- None found. Print out message and return.\n" +
" t.ReplaceParameters();\n" +
"elseif thing.isGreaterThan(bob) then\n" +
" x.value = 36;\n" +
"else\n" +
" -- Painting found. Delete it and the bid.\n" +
" foundit = TRUE;\n" +
" t.ReplaceParameters();\n" +
" -- Now delete the entry in the array.\n" +
" self.PaintingsUnderBid.DeleteRow(b);\n" +
"\n" +
" -- Send out an event.\n" +
" post self.PaintingDeleted (paintingName = p.name);\n" +
"end if;\n" +
""));
parser.ifStatement();
failOnSyntaxError("ifStatementWithElseIf");
}
@Test
public void testRaiseStatement() throws RecognitionException {
parser.setTokenStream(getStream("raise e;"));
parser.raiseStatement();
failOnSyntaxError("raiseStatement with exception");
}
@Test
public void testRaiseStatementWithout() throws RecognitionException {
parser.setTokenStream(getStream("raise;"));
parser.raiseStatement();
failOnSyntaxError("raiseStatement");
}
@Test
public void testPostStatement() throws RecognitionException {
parser.setTokenStream(getStream("post ObjectsAssigned(anAssignObjectList);"));
parser.postStatement();
failOnSyntaxError("postStatement");
}
@Test
public void testEventLoopStatement() throws RecognitionException {
parser.setTokenStream(getStream(" event loop\n" +
" when self.theBid.StartBid_return (\n" +
" nvalue = bidValue, ntime = lastBidTime,\n" +
" nprogress = bidInProgress, nname = return) do\n" +
" -- The lock has been granted. \n" +
" -- Now let the OK button\n" +
" -- and updates on the bid amount be allowed.\n" +
" theMessage = 'Bid lock granted to you.';\n" +
" \n" +
" <IncreaseButton>.State = FS_UPDATE;\n" +
" <OKButton>.State = FS_UPDATE;\n" +
"\n" +
" -- Set the data again (just to be sure).\n" +
" self.theOriginalBidValue = nvalue;\n" +
" self.theBidValue = nvalue;\n" +
" self.theLastBidTime.SetValue(ntime);\n" +
" self.theBidInProgress = nprogress;\n" +
" self.hasBidLock = TRUE;\n" +
"\n" +
" when self.theBid.StartBid_exception (e = exception) \n" +
" do\n" +
" -- Add the exception to the errors for this task, \n" +
" -- and then re-raise the exception to get out.\n" +
" task.ErrorMgr.AddError(GenericException(e));\n" +
" raise e;\n" +
"\n" +
" -- Process the button clicks that might occur\n" +
" when <OKButton>.Click do\n" +
" -- This completes the bid, commits the \n" +
" -- transaction and gets out.\n" +
" self.theBid.CompleteBid (bid = theBidValue);\n" +
" exit;\n" +
"\n" +
" when task.Shutdown do\n" +
" if self.hasBidLock then\n" +
" post self.theBid.BidCancelled;\n" +
" self.theBidValue = self.theOriginalBidValue;\n" +
" end if;\n" +
" transaction.Abort(TRUE);\n" +
" exit;\n" +
"\n" +
" when <CancelButton>.Click do\n" +
" -- This cancels the bid by aborting the \n" +
" -- transaction. This will drop out of the event \n" +
" -- loop and into the exception section of the \n" +
" -- transaction.\n" +
" if self.hasBidLock then\n" +
" post self.theBid.BidCancelled;\n" +
" self.theBidValue = self.theOriginalBidValue;\n" +
" end if;\n" +
" transaction.Abort(TRUE);\n" +
" exit;\n" +
"\n" +
" when <IncreaseButton>.Click do\n" +
" -- Increase by the minimum.\n" +
" self.theBidValue = self.theBidValue + 50000;\n" +
"\n" +
" -- Process the update events on the data.\n" +
" when self.theBid.BidCompleted (nBid = newBid, \n" +
" tBid = timeOfBid, wBid = whoBid) do\n" +
" self.theOriginalBidValue = nBid;\n" +
" self.theBidValue = nBid;\n" +
" self.theLastBidTime.SetValue(tBid);\n" +
" self.theLastBidder = wBid;\n" +
" -- If this is the last concurrent bidder, the \n" +
" -- GotLock event will arrive right after this.\n" +
" end event;\n" +
""));
parser.eventLoopStatement();
failOnSyntaxError("eventLoopStatement");
}
@Test
public void testRegisterStatement() throws RecognitionException {
parser.setTokenStream(getStream("register self.ArtObjectHandler();"));
parser.registerStatement();
failOnSyntaxError("registerStatement");
}
@Test
public void testWhenClause() throws RecognitionException {
parser.setTokenStream(getStream("when <IncreaseButton>.Click do\n" +
" -- Increase by the minimum.\n" +
" self.theBidValue = self.theBidValue + 50000;"));
parser.whenEvent();
failOnSyntaxError("whenClause");
}
@Test
public void testExceptionBlock() throws RecognitionException {
parser.setTokenStream(getStream("exception -- For the transaction\n" +
" when e : GenericException do\n" +
" -- Transaction is aborted. Get out.\n" +
" task.ErrMgr.ShowErrors(TRUE);\n" +
"\n" +
" when e : AbortException do\n" +
" task.ErrMgr.Clear(); -- Do not show errors."));
parser.exceptionBlock();
failOnSyntaxError("exceptionBlock");
}
// @Test
// public void testEvent() throws RecognitionException {
// parser.setTokenStream(getStream(""));
// parser.event();
// failOnSyntaxError("event");
// }
// @Test
// public void testBlock() throws RecognitionException {
// parser.setTokenStream(getStream(""));
// parser.block();
// failOnSyntaxError("block");
// }
@Test
public void testBlock() throws RecognitionException {
parser.setTokenStream(getStream("begin transaction do\n" +
" -- Specify some output parameters, even though \n" +
" -- they will not be filled in here, but will be \n" +
" -- filled in on the StartBid_return event.\n" +
" ptg : Painting;\n" +
" bidding_task : TaskDesc;\n" +
" theMessage = 'Waiting for bid to become available.';\n" +
"\n" +
"\n" +
"exception -- For the transaction\n" +
" when e : GenericException do\n" +
" -- Transaction is aborted. Get out.\n" +
" task.ErrMgr.ShowErrors(TRUE);\n" +
"\n" +
" when e : AbortException do\n" +
" task.ErrMgr.Clear(); -- Do not show errors.\n" +
"end transaction; -- Commits transaction.\n" +
""));
parser.block();
failOnSyntaxError("block");
}
@Test
public void testArguments() throws RecognitionException {
parser.setTokenStream(getStream("(statementHandle = dynStatement, // The Statement\n" +
" inputDataSet = inputDescriptor, // Not used\n" +
" resultDataSet = outputDescriptor)"));
parser.arguments();
failOnSyntaxError("arguments");
}
@Test
public void testArgumentList() throws RecognitionException {
parser.setTokenStream(getStream("statementHandle = dynStatement, // The Statement\n" +
" inputDataSet = inputDescriptor, // Not used\n" +
" outputDescriptor"));
parser.argumentList();
failOnSyntaxError("argumentList");
}
@Test
public void testArgument() throws RecognitionException {
parser.setTokenStream(getStream("statementHandle = dynStatement"));
parser.argument();
failOnSyntaxError("argument");
}
}