Package tool.model.grammar

Source Code of tool.model.grammar.TestClass

package tool.model.grammar;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.junit.Test;

import tool.model.ToolClass;



public class TestClass extends ANTLRTest{

  @Test
  public void stringLiteralTest() throws RecognitionException {
    ForteParser parser = getParser("'\'\''");
    printTree(parser.literal().getTree());
    syntaxErrors(parser);
  }
 
  @Test
  public void stringLiteralQuoteQuote() throws RecognitionException {
    CommonTokenStream tokenStream = getStream("'bob''s bike'");
    ForteParser parser = new ForteParser(tokenStream);
    printTree(parser.literal().getTree());
    syntaxErrors(parser);
  }

  @Test
  public void stringLiteralQuote() throws RecognitionException {
    CommonTokenStream tokenStream = getStream("'bob\\'s bike'");
    ForteParser parser = new ForteParser(tokenStream);
    printTree(parser.literal().getTree());
    syntaxErrors(parser);
  }
 
  @Test
  public void stringLiteralTestBackSlash() throws RecognitionException {
    ForteParser parser = getParser("'E:\\runtime-EclipseApplication\\Demo35\\Projects\\Auction\\BidWindow.fsw'");
    printTree(parser.literal().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void constantShortTest() throws RecognitionException {
    ForteParser parser = getParser("constant ATO_BLANK_FIELD = 'bill\\'s bike';");
    printTree(parser.constantDeclaration().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void constantLongTest() throws RecognitionException {
    ForteParser parser = getParser("constant ATO_BLANK = '\'\'' HAS PROPERTY id = 0 ;");
    printTree(parser.constantDeclaration().getTree());
    syntaxErrors(parser);
  }

  @Test
  public void classTypeTest() throws RecognitionException {
    ForteParser parser = getParser("Framework.string");
    printTree(parser.type().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void primativeTypeTest() throws RecognitionException {
    ForteParser parser = getParser("integer");
    printTree(parser.type().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void arrayTypeTest() throws RecognitionException {
    ForteParser parser = getParser("Framework.Array of AuctionServerProject.Painting");
    printTree(parser.type().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void declarationTerminatorTest() throws RecognitionException {
    ForteParser parser = getParser("HAS PROPERTY id = 3  , SerialNumber = 3 ;");
    printTree(parser.declatrationTerminator().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void attributeTest() throws RecognitionException {
    ForteParser parser = getParser("public attribute PaintingData: AuctionServerProject.Painting HAS PROPERTY id = 3  , SerialNumber = 3 ;");
    printTree(parser.attributeDeclaration().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void arrayAttributeTest() throws RecognitionException {
    CommonTokenStream tokenStream = getStream("private attribute PaintingData: Framework.Array of AuctionServerProject.Painting HAS PROPERTY id = 3  , SerialNumber = 3 ;");
    ForteParser parser = new ForteParser(tokenStream);
    printTree(parser.attributeDeclaration().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void methodDeclarationTest() throws RecognitionException {
    ForteParser parser = getParser("public method GetValues(output paintingForBid: AuctionServerProject.Painting,\r\n" +
            "  output bidValue: Framework.double,\r\n" +
            "  output lastBidTime: Framework.DateTimeData,\r\n" +
            "  output lastBidder: Framework.string,\r\n" +
            "  output bidInProgress: Framework.boolean) HAS PROPERTY id = 3 ;\r\n" +
            "");
    printTree(parser.methodDeclaration().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void methodDeclarationWithEventTest() throws RecognitionException {
    ForteParser parser = getParser("public method StartBid(input bidderName: Framework.string,\r\n" +
            "  output bidValue: Framework.double,\r\n" +
            "  output lastBidTime: Framework.DateTimeData,\r\n" +
            "  output bidInProgress: Framework.boolean): Framework.TextData where completion = (return = StartBid_return(4), exception = StartBid_exception(5)) HAS PROPERTY id = 5 ;");
    printTree(parser.methodDeclaration().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void whenEventTest() throws RecognitionException {
    ForteParser parser = getParser("when task.Shutdown do\r\n" +
        "    exit;");
    printTree(parser.whenEvent().getTree());
    syntaxErrors(parser);
  }
  //@Test
  public void methodImplementationTest() throws RecognitionException {
    ForteParser parser = getParser("method MainWindow.Display\r\n" +
        "begin\r\n" +
        "-- Initialize a local DisplayNode variable, which we \r\n" +
        "-- will later assign to the mapped attribute \r\n" +
        "-- for the TreeView field. It is more \r\n" +
        "-- efficient to build up a local variable and \r\n" +
        "-- assign it to the mapped attribute, than to directly \r\n" +
        "-- build the nodes in the mapped attribute. Always \r\n" +
        "-- set the IsFolder, IsFilled, and IsOpened attributes \r\n" +
        "-- to TRUE in the root node. Always set them in this order. \r\n" +
        "-- This will become the root node of the TreeView.\r\n" +
        "ourBirdTreeView : DisplayNode = new;\r\n" +
        "ourBirdTreeView.IsFolder = TRUE;\r\n" +
        "ourBirdTreeView.IsFilled = TRUE;\r\n" +
        "ourBirdTreeView.IsOpened = TRUE;\r\n" +
        "ourBirdTreeView.DVNodeText = 'ROOT NAME';\r\n" +
        "\r\n" +
        "-- We add three levels to the ourBirdTreeView\r\n" +
        "-- DisplayNode. We load them with data from\r\n" +
        "-- the OrderList array, which is populated in\r\n" +
        "-- the Init method of this class. This is an\r\n" +
        "-- appropriate technique when you have small \r\n" +
        "-- amounts of data. An alternative technique, \r\n" +
        "-- if you have a lot of data to display, would\r\n" +
        "-- be to create the first level of the hierarchy \r\n" +
        "-- only, connect each node at this level to the data\r\n" +
        "-- in the next level down with DisplayNode's Related\r\n" +
        "-- attribute, and build out the lower levels at\r\n" +
        "-- runtime. Note that when you use the alternate\r\n" +
        "-- technique, you should always set the IsFolder\r\n" +
        "-- attribute to TRUE, regardless of whether the node\r\n" +
        "-- has a child node. Doing so will allow the user to\r\n" +
        "-- click the control, generating an event which you can\r\n" +
        "-- handle to build the child node.\r\n" +
        "\r\n" +
        "Node : DisplayNode;\r\n" +
        "ChildNode : DisplayNode;\r\n" +
        "GrandChildNode : DisplayNode;\r\n" +
        "\r\n" +
        "for order in OrderList do\r\n" +
        "  Node = new;\r\n" +
        "  Node.DVNodeText = new;\r\n" +
        "  Node.DVNodeText.SetValue(order.Name);\r\n" +
        "  Node.IsFolder = FALSE;\r\n" +
        "  Node.IsFilled = TRUE;\r\n" +
        "  Node.IsOpened = TRUE;\r\n" +
        "  -- Parent 'Order' level node to the root node.\r\n" +
        "   Node.Parent = ourBirdTreeView;\r\n" +
        "\r\n" +
        "  if order.FamilyList <> nil then\r\n" +
        "      Node.IsFolder = TRUE;\r\n" +
        "    for family in (order.FamilyList) do\r\n" +
        "      ChildNode = new;\r\n" +
        "      ChildNode.DVNodeText = new;\r\n" +
        "      ChildNode.DVNodeText.SetValue(family.Name);\r\n" +
        "      ChildNode.IsFolder = FALSE;\r\n" +
        "      ChildNode.IsFilled = TRUE;\r\n" +
        "      ChildNode.IsOpened = FALSE;\r\n" +
        "      -- Parent the 'Family' level node to the 'Order' level node.\r\n" +
        "      ChildNode.Parent = Node;\r\n" +
        "      \r\n" +
        "      if family.GenusList <> nil then  \r\n" +
        "          ChildNode.IsFolder = TRUE;\r\n" +
        "        for genus in family.GenusList do\r\n" +
        "          GrandChildNode = new;\r\n" +
        "          GrandChildNode.DVNodeText = genus.Name;\r\n" +
        "          GrandChildNode.IsFolder = FALSE;\r\n" +
        "          GrandChildNode.IsFilled = TRUE;\r\n" +
        "          GrandChildNode.IsOpened = FALSE;\r\n" +
        "          -- Parent the 'Genus' level node to the 'Family' level node.\r\n" +
        "          GrandChildNode.Parent = ChildNode;\r\n" +
        "          \r\n" +
        "          -- If a genus has species data associated with it, we will\r\n" +
        "          -- display the species information in the ListView field,\r\n" +
        "          -- when a user clicks on that genus. For now, we use the\r\n" +
        "          -- Related attribute to connect this node with its species\r\n" +
        "          -- list. We will build the species display node when it's\r\n" +
        "          -- requested.\r\n" +
        "          if genus.SpeciesList <> nil then\r\n" +
        "            ourGenusInfo : GenusInfo = new;\r\n" +
        "            ourGenusInfo.SpeciesList = genus.SpeciesList;\r\n" +
        "            GrandChildNode.Related = ourGenusInfo;\r\n" +
        "          end if;\r\n" +
        "        end for;\r\n" +
        "      end if;    \r\n" +
        "      \r\n" +
        "    end for;\r\n" +
        "  end if;\r\n" +
        "    \r\n" +
        "end for;\r\n" +
        "\r\n" +
        "-- Assign the local variable to the mapped attribute, now that the\r\n" +
        "-- structure is complete.\r\n" +
        "BirdTreeView = ourBirdTreeView;\r\n" +
        "\r\n" +
        "self.Open();\r\n" +
        "\r\n" +
        "\r\n" +
        "event loop\r\n" +
        "  when task.Shutdown do\r\n" +
        "    exit;\r\n" +
        "    \r\n" +
        "  when <BirdTreeView>.AfterCurrentNodeChange(ourNode = newNode) do\r\n" +
        "    \r\n" +
        "    -- Only genus level nodes have their Related attribute set.\r\n" +
        "    if ourNode.Related <> nil then\r\n" +
        "      ourGenus : GenusInfo = new;\r\n" +
        "      \r\n" +
        "      -- If it's a genus, use the Related attribute to\r\n" +
        "      -- build the array of SpeciesDisplayNode with the\r\n" +
        "      -- data for this genus.\r\n" +
        "      ourGenus = (GenusInfo)(ourNode.Related);\r\n" +
        "      if ourGenus.SpeciesList <> nil then\r\n" +
        "        i : integer = 1;\r\n" +
        "        ourSpeciesNodeList : array of SpeciesDisplayNode = new;\r\n" +
        "        while i <= ourGenus.SpeciesList.Items do\r\n" +
        "          ourSpeciesNodeList[i] = new;\r\n" +
        "          ourSpeciesNodeList[i].DVNodeText = new;\r\n" +
        "          ourSpeciesNodeList[i].DVNodeText.SetValue(ourGenus.SpeciesList[i].Name);\r\n" +
        "          ourSpeciesNodeList[i].Length = ourGenus.SpeciesList[i].Length;\r\n" +
        "          ourSpeciesNodeList[i].Weight = ourGenus.SpeciesList[i].Weight;\r\n" +
        "          ourSpeciesNodeList[i].Status = ourGenus.SpeciesList[i].Status;\r\n" +
        "          i = i + 1;\r\n" +
        "        end while;\r\n" +
        "        -- Set the view nodes for the ListView field. This automatically\r\n" +
        "        -- populates the ListView field with data in the array of DisplayNodes.\r\n" +
        "        <BirdListView>.SetViewNodes(nodes = ourSpeciesNodeList);\r\n" +
        "        \r\n" +
        "        \r\n" +
        "      end if;    \r\n" +
        "    \r\n" +
        "    else\r\n" +
        "      -- If the user has moved to a node that is not a genus,\r\n" +
        "      -- we must blank out the ListView field.\r\n" +
        "      <BirdListView>.SetViewNodes((array of DisplayNode)(nil));  \r\n" +
        "    end if;  \r\n" +
        "      \r\n" +
        "end event;\r\n" +
        "self.Close();\r\n" +
        "\r\n" +
        "\r\n" +
        "end method;");
    printTree(parser.methodImplementation().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void localVariableTest() throws RecognitionException {
    ForteParser parser = getParser("pa : Painting = new;");
    printTree(parser.statement().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void localArrayVariableTest() throws RecognitionException {
    ForteParser parser = getParser("pa : array of Painting = new;");
    printTree(parser.statement().getTree());
    syntaxErrors(parser);
  }

  @Test
  public void statementTest() throws RecognitionException {
    ForteParser parser = getParser("__PutProperty(17,OLE.VariantI2(Value=Action))\r\n");
    printTree(parser.expression().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void castExpressionTest() throws RecognitionException {
    ForteParser parser = getParser("(NavAbsGetDatesContent)(FrameContent)");
    printTree(parser.expression().getTree());
    syntaxErrors(parser);
  }
 
  @Test
  public void castExpressionTest_2() throws RecognitionException {
    ForteParser parser = getParser("(InvAccCalculations)(BusinessObj).thePortfoHldg.theInvtProd");
    printTree(parser.expression().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void castExpressionTest_3() throws RecognitionException {
    ForteParser parser = getParser("(Discretionary)(theContent.theSubject) = theDiscretionary");
    printTree(parser.expression().getTree());
    syntaxErrors(parser);
  }
  @Test
  public void virtualAttributeTest() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute Action: Framework.i2 = (set = __PutProperty(17,OLE.VariantI2(Value=Action))\r\n" +
            "        , get = OLE.VariantI2(__GetProperty(17)).Value\r\n" +
            "        ) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
        syntaxErrors(parser);
  }
  @Test
  public void virtualAttributeTestGetOnly() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute theEffDateContent: NavAbsWindows.NavAbsGetDatesContent = (get = FrameContent) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
    syntaxErrors(parser);
  }

  @Test
  public void virtualAttributeTestWithCast() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute theEffDateContent: NavAbsWindows.NavAbsGetDatesContent = (get =(InvAccCalculations)(BusinessObj).thePortfoHldg.theInvtProd) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
        syntaxErrors(parser);
  }

  @Test
  public void virtualAttributeTestWithCast_2() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute theDiscretionary: NavCoreClasses.Discretionary = (set =(Discretionary)(theContent.theSubject) = theDiscretionary, get =(Discretionary)(theContent.theSubject)) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
        syntaxErrors(parser);
  }
  @Test
  public void windowAttribute() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute theDiscretionary: NavCoreClasses.Discretionary = (set =(Discretionary)(theContent.theSubject) = theDiscretionary, get =(Discretionary)(theContent.theSubject)) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
        syntaxErrors(parser);
  }
  @Test
  public void attribute() throws RecognitionException {
    ForteParser parser = getParser("public virtual attribute theDiscretionary: NavCoreClasses.Discretionary = (set =(Discretionary)(theContent.theSubject) = theDiscretionary, get =(Discretionary)(theContent.theSubject)) HAS PROPERTY id = 1  , SerialNumber = 1 ;");
    CommonTree tree = (CommonTree) parser.virtualAttributeDeclaration().getTree();
    printTree("AST", tree);
    parseForteASTComponent(tree, parser.getTokenStream());
        syntaxErrors(parser);
  }
  @Test
  public void testCDF() throws RecognitionException, IOException {
      this.testClassDefinitionFile(new File("TestSmall//TreeList//MainWindow.cdf"), true);
   
  }
  //@Test
  public void testCEX() throws RecognitionException, IOException {
    this.testClassImplementationFile(new File("TestSmall//TreeList//MainWindow.cex"), true);
 
  }
  public int testClassDefinitionFile(File cdf, boolean failOnError) throws IOException, RecognitionException {
    CharStream stream =
        new NoCaseFileStream(cdf.getAbsolutePath());
    ForteLexer lexer = new ForteLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    ForteParser parser = new ForteParser(tokenStream);
    CommonTree tree = (CommonTree) parser.cdfFile().getTree();
    printTree(tree);
    int errors = parser.getNumberOfSyntaxErrors();
    int treeerrorsCDF = 0;
    int treeerrorsAST = 0;
    System.out.println("\t" + cdf.getName() + " parse errors: " + errors );
    //treeerrorsCDF = parseUsingCDFTree(tree, tokenStream, cdf.getName());
    //System.out.println("\tCDF:" + cdf.getName() + "  tree errors: " + treeerrorsCDF);
    treeerrorsAST = parseForteASTComponent(tree, tokenStream);
    System.out.println("\tAST:" + cdf.getName() + "  tree errors: " + treeerrorsAST);
    if (failOnError && (errors + treeerrorsAST + treeerrorsCDF) > 0Assert.fail() ;
    return errors;
  }
  public int testClassImplementationFile(File cex, boolean failOnError) throws RecognitionException, IOException {
    CharStream stream =
        new NoCaseFileStream(cex.getAbsolutePath());
    ForteLexer lexer = new ForteLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    ForteParser parser = new ForteParser(tokenStream);
    printTree(parser.implementationFile().getTree());
    int errors = parser.getNumberOfSyntaxErrors();
    if (failOnError && errors > 0Assert.fail() ;
    return errors;
  }
 
  private int parseForteASTComponent(CommonTree tree, TokenStream tokens) throws RecognitionException{
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    nodes.setTokenStream(tokens);
    ForteAST walker = new ForteAST(nodes);
    walker.component_definition();
    return walker.getNumberOfSyntaxErrors();

  }
  private int parseUsingCDFTree(CommonTree tree, TokenStream tokens, String name) throws RecognitionException{
    ToolClass cls = new ToolClass(name);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    nodes.setTokenStream(tokens);
    ForteCDFTree walker = new ForteCDFTree(cls, nodes);
    walker.classDeclaration();
    return walker.getNumberOfSyntaxErrors();

  }

}
TOP

Related Classes of tool.model.grammar.TestClass

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.