Package tool.model.grammar

Source Code of tool.model.grammar.TestMethodImplementation

package tool.model.grammar;

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

import junit.framework.Assert;

import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;

import tool.model.grammar.ForteParser.expression_return;
import tool.model.grammar.ForteParser.methodImplementation_return;

public class TestMethodImplementation extends ANTLRTest{


  @Test
  public void simpleTest_1() {
    try {
      CommonTokenStream tokenStream = getStream("method UserExists.Init\r\n" +
          "begin\r\n" +
          "super.Init();\r\n" +
          "end method;\r\n");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("simpleTest_1", parser.methodImplementation().getTree());
      syntaxErrors(parser);
    } catch (RecognitionException e) {
      Assert.fail(e.getMessage());
    }

  }
  @Test
  public void simpleTest_2() {
    try {
      CommonTokenStream tokenStream = getStream("method UserPreferences.Init\r\n" +
          "begin\r\n" +
          "super.Init();\r\n" +
          "\r\n" +
          "ApplicationID  = new();\r\n" +
          "PreferenceName  = new();\r\n" +
          "PreferenceValue  = new();\r\n" +
          "UserID      = new();\r\n" +
          "WindowName    = new();\r\n" +
          "end method;\r\n" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("simpleTest_2", parser.methodImplementation().getTree());
      syntaxErrors(parser);
    } catch (RecognitionException e) {
      Assert.fail(e.getMessage());
    }

  }
  @Test
  public void assignmentWithCast() throws RecognitionException {
      CommonTokenStream tokenStream = getStream("outFloat = (float) (tmpDouble);");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("assignmentWithCast", parser.statement().getTree());
      syntaxErrors(parser);

  }

 
  @Test
  public void castCStyle() throws RecognitionException {
      CommonTokenStream tokenStream = getStream("float(bob)");
      ForteParser parser = new ForteParser(tokenStream);
      expression_return ret = parser.expression();
      printTree("castCStyle", parser.expression().getTree());
      syntaxErrors(parser);

  }
  @Test
  public void castWithParen() throws RecognitionException {
      CommonTokenStream tokenStream = getStream("(TextData)(bob)");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("castWithParen", parser.expression().getTree());
      syntaxErrors(parser);

  }
  @Test
  public void castArrayOf() throws RecognitionException {
      CommonTokenStream tokenStream = getStream("(array of DisplayNode)(nil)");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("castArrayOf", parser.expression().getTree());
      syntaxErrors(parser);

  }
  @Test
  public void ifWithElse() throws RecognitionException {
      CommonTokenStream tokenStream = getStream("  if not self.PromptForInput(inPrompt, tmpDouble) then\r\n" +
          "    return false;\r\n" +
          "  else\r\n" +
          "    outFloat = (float) (tmpDouble);\r\n" +
          "    return true;\r\n" +
          "  end if;\r\n" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("ifWithElse", parser.ifStatement().getTree());
      syntaxErrors(parser);
  }

  @Test
  public void eventLoop() {
    try {
      CommonTokenStream tokenStream = getStream("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 = 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" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      printTree("eventLoop", parser.eventLoopStatement().getTree());
      syntaxErrors(parser);
    } catch (RecognitionException e) {
      Assert.fail(e.getMessage());
    }

  }


  @Test
  public void complexTest_1() {
    try {
      CommonTokenStream tokenStream = getStream("method UserSecurityProfile.ChangeUserPreference(input pApplicationID: Framework.string,\r\n" +
          "  input pWindow: Framework.string,\r\n" +
          "  input pPreferenceName: Framework.string,\r\n" +
          "  input pPreferenceValue: Framework.string,\r\n" +
          "  output pFound: Framework.boolean): NewCommonObjects.UserPreferences\r\n" +
          "begin\r\n" +
          "found    : boolean = FALSE;\r\n" +
          "ReturnObj  : UserPreferences = new();\r\n" +
          "\r\n" +
          "if self.PreferenceList = Nil then\r\n" +
          "  self.PreferenceList = new();\r\n" +
          "end if;\r\n" +
          "\r\n" +
          "for i in 1 to PreferenceList.items do\r\n" +
          "  if (PreferenceList[i].WindowName.ToUpper().Value = pWindow) AND\r\n" +
          "     (PreferenceList[i].PreferenceName.ToUpper().Value = pPreferenceName) then\r\n" +
          "    PreferenceList[i].PreferenceValue.SetValue(pPreferenceValue);\r\n" +
          "    ReturnObj = PreferenceList[i].Clone(TRUE);\r\n" +
          "    found = TRUE;\r\n" +
          "    exit;\r\n" +
          "  end if;\r\n" +
          "end for;\r\n" +
          "\r\n" +
          "if not found then\r\n" +
          "  aPref : UserPreferences = new();\r\n" +
          "  aPref.UserID.SetValue(self.User.UserID.Value);\r\n" +
          "  aPref.ApplicationID.SetValue(pApplicationID);\r\n" +
          "  aPref.WindowName.SetValue(pWindow);\r\n" +
          "  aPref.PreferenceName.SetValue(pPreferenceName);\r\n" +
          "  aPref.PreferenceValue.SetValue(pPreferenceValue);\r\n" +
          "  self.PreferenceList.AppendRow(aPref);\r\n" +
          "  ReturnObj = aPref.Clone(TRUE);\r\n" +
          "end if;\r\n" +
          "\r\n" +
          "pFound = found;\r\n" +
          "Return ReturnObj;\r\n" +
          "end method;\r\n" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      parser.methodImplementation();
      syntaxErrors(parser);
    } catch (RecognitionException e) {
      Assert.fail(e.getMessage());
    }

  }
  //@Test
  public void fileTest_1() throws RecognitionException, IOException {
      CommonTokenStream tokenStream = getStream(new File("TestSmall//Auction//BidWindow.cex"));
      ForteParser parser = new ForteParser(tokenStream);
      parser.methodImplementation();
      printTree("fileTest_1", parser.methodImplementation().getTree());
      syntaxErrors(parser);

  }

  @Test
  public void wackoTest_1() throws RecognitionException {
      CommonTokenStream tokenStream = getStream(
          "method ClipboardWindow.Display\r\n" +
          "begin\r\n" +
          "-- This sample application demonstrates the use of Forte\r\n" +
          "-- with the system clipboard.  It shows an image_field\r\n" +
          "-- (PictureButton) with an image, and a text_field \r\n" +
          "-- (TextField) which contains some initial data.\r\n" +
          "--\r\n" +
          "-- The system displays buttons allowing the image or text\r\n" +
          "-- to be moved to the system clipboard.  It also contains\r\n" +
          "-- a button that will allow to move whatever is in the\r\n" +
          "-- system clipboard to the window.  If text is in the\r\n" +
          "-- clipboard, it will be moved to text_field.  If an image\r\n" +
          "-- is in the clipboard, it is moved to the image_field.\r\n" +
          "-- If neither is there, an error is displayed.\r\n" +
          "\r\n" +
          "self.Open();\r\n" +
          "\r\n" +
          "-- First, see if anything is in the clipboard, and set\r\n" +
          "-- the button appropriately.\r\n" +
          "d_in_clipboard: boolean;\r\n" +
          "case self.Window.WindowSystem.GetClipboardDatatype() is\r\n" +
          " when SD_TEXT do\r\n" +
          "  d_in_clipboard = TRUE;\r\n" +
          " when SD_IMAGE do\r\n" +
          "  d_in_clipboard = TRUE;\r\n" +
          " else\r\n" +
          "  d_in_clipboard = FALSE;\r\n" +
          "end case;\r\n" +
          "if d_in_clipboard then\r\n" +
          "  <clipboard_to_window>.State = FS_UPDATE;\r\n" +
          "else\r\n" +
          "  <clipboard_to_window>.State = FS_DISABLED;\r\n" +
          "end if;\r\n" +
          "\r\n" +
          "event loop\r\n" +
          "  -- Standard shutdown.\r\n" +
          "  when task.Shutdown do\r\n" +
          "    exit;\r\n" +
          "\r\n" +
          "  -- When clipboard data changes.\r\n" +
          "  when self.Window.WindowSystem.AfterClipboardChange do\r\n" +
          "    -- First, see if anything is in the clipboard, and \r\n" +
          "    -- set the button appropriately.\r\n" +
          "    d_in_clipboard: boolean;\r\n" +
          "    case self.Window.WindowSystem.GetClipboardDatatype() \r\n" +
          "      is\r\n" +
          "\r\n" +
          "      when SD_TEXT do\r\n" +
          "        d_in_clipboard = TRUE;\r\n" +
          "      when SD_IMAGE do\r\n" +
          "        d_in_clipboard = TRUE;\r\n" +
          "      else\r\n" +
          "        d_in_clipboard = FALSE;\r\n" +
          "    end case;\r\n" +
          "    if d_in_clipboard then\r\n" +
          "      <clipboard_to_window>.State = FS_UPDATE;\r\n" +
          "    else\r\n" +
          "      <clipboard_to_window>.State = FS_DISABLED;\r\n" +
          "    end if;\r\n" +
          "\r\n" +
          "  when <clipboard_to_window>.Click do\r\n" +
          "    -- Copy the appropriate type of data.\r\n" +
          "    case self.Window.WindowSystem.GetClipboardDatatype() \r\n" +
          "      is\r\n" +
          "      when SD_TEXT do\r\n" +
          "        -- Data is text.  Move to text_field.\r\n" +
          "        o : Object;    -- to hold clipboard object\r\n" +
          "        o = self.Window.WindowSystem.\r\n" +
          "          CopyFromClipboard(objectDataType=SD_TEXT);\r\n" +
          "        -- Since it is a generic object, cast it.\r\n" +
          "        text_field.SetValue(TextData(o));\r\n" +
          "\r\n" +
          "      when SD_IMAGE do\r\n" +
          "        -- Data is image.  Move to image_field.\r\n" +
          "        o : Object;    -- to hold clipboard object\r\n" +
          "        o = self.Window.WindowSystem.\r\n" +
          "          CopyFromClipboard\r\n" +
          "            (objectDataType=SD_IMAGE);\r\n" +
          "        -- Since it is a generic object, cast it.\r\n" +
          "        <image_field>.ImageValue = (ImageData)(o);\r\n" +
          "    end case;\r\n" +
          "\r\n" +
          "  -- Copy this image_field data to the clipboard.\r\n" +
          "  when <image_to_clipboard>.Click do\r\n" +
          "    self.Window.WindowSystem.CopyToClipboard\r\n" +
          "      (object = <image_field>.ImageValue);\r\n" +
          "\r\n" +
          "  -- Copy this text to the clipboard.\r\n" +
          "  when <text_to_clipboard>.Click do\r\n" +
          "    self.Window.WindowSystem.CopyToClipboard\r\n" +
          "      (object = text_field);  \r\n" +
          "end event;\r\n" +
          "self.Close();\r\n" +
          "\r\n" +
          "end method;\r\n" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      parser.eventLoopStatement();
      printTree("wackoTest_1", parser.eventLoopStatement().getTree());
      syntaxErrors(parser);

  }

  @Test
  public void __retrieveLedgerBalance() throws RecognitionException {
      CommonTokenStream tokenStream = getStream(
          "method AccBalSummaryDBHandler.__retrieveLedgerBalance(input pLedgerNumber: Framework.integer): Framework.DecimalNullable\r\n" +
          "begin\r\n" +
          "/******************************************************************************\r\n" +
          "\r\n" +
          "NAME            :   __retrieveLedgerBalance\r\n" +
          "\r\n" +
          "DESCRIPTION     :   Retrieves from the database, the current balance of the\r\n" +
          "                    ledger account.  This passes in the ledger account number\r\n" +
          "                    as an input parameter so this method can be used by other\r\n" +
          "                    methods in this handler, without having to alter the\r\n" +
          "                    ledger account number set on the business object.\r\n" +
          "\r\n" +
          "PRE CONDITION \r\n" +
          "PARAMETERS      :   None\r\n" +
          "\r\n" +
          "POST CONDITION      \r\n" +
          "PARAMETERS      :   None\r\n" +
          "\r\n" +
          "RETURN VALUE    :   DecimalNullable     The value of the current ledger balance\r\n" +
          "\r\n" +
          "EXCEPTIONS      :   None\r\n" +
          "\r\n" +
          "******************************************************************************/ \r\n" +
          "\r\n" +
          "lSQLText                : TextData = new();\r\n" +
          "lWhereParameters        : Array of DataValue = new();\r\n" +
          "lSummary                : AccBalSummary = getAccountBalanceSummary();\r\n" +
          "lBalance                : DecimalNullable = new(IsNull = TRUE);\r\n" +
          "lGetCurrent             : boolean = FALSE;\r\n" +
          "lResultSet              : Array of Object = NIL;\r\n" +
          "\r\n" +
//          "lSingleQuote            : String = '\\'' ;\r\n" +
          "lTimestampFormat        : TextData =  new (Value= lSingleQuote) ;\r\n" +
          "lTimestampFormat.Concat(FMT_TIMESTAMP) ;\r\n" +
          "lTimestampFormat.Concat(lSingleQuote) ;\r\n" +
          "\r\n" +
          "if lSummary.dnEffectiveFromDate.IsNull then\r\n" +
          "\r\n" +
          "    lGetCurrent = TRUE;\r\n" +
          "    \r\n" +
          "else\r\n" +
          "\r\n" +
          "    lGetCurrent = FALSE;\r\n" +
          "    \r\n" +
          "end if;\r\n" +
          "\r\n" +
          "lSQLText.Concat(source = 'SELECT  SUM(ledger_acc_bal)\\n');\r\n" +
          "lSQLText.Concat(source = 'FROM    acc_bal_summary\\n');\r\n" +
          "lSQLText.Concat(source = 'WHERE   acc_id = ?\\n');\r\n" +
          "lSQLText.Concat(source = 'AND     acc_type = ?\\n');\r\n" +
          "lSQLText.Concat(source = 'AND     ledger_acc_num = ?\\n');\r\n" +
          "\r\n" +
          "lWhereParameters.AppendRow(Object = DoubleData(DoubleValue = lSummary.dAccId));\r\n" +
          "lWhereParameters.AppendRow(Object = TextData(value = lSummary.sAccType));\r\n" +
          "lWhereParameters.AppendRow(Object = IntegerData(IntegerValue = pLedgerNumber));\r\n" +
          "\r\n" +
          "if lGetCurrent then\r\n" +
          "\r\n" +
          "    lSQLText.Concat(source = 'AND     (eff_to_date IS NULL)\\n');\r\n" +
          "    \r\n" +
          "else\r\n" +
          "\r\n" +
          "    lDateNoTime : DateTimeNullable = Date().TruncateTime(dnSeedDate = lSummary.dnEffectiveFromDate);\r\n" +
          "    \r\n" +
          "    lSQLText.Concat(source = 'AND     eff_from_date <= TO_TIMESTAMP ( ? , ');\r\n" +
          "    lSQLText.Concat(lTimeStampFormat );\r\n" +
          "    lSQLText.Concat(source = ' ) ');\r\n" +
          "    lSQLText.Concat(source = 'AND     (eff_to_date IS NULL OR eff_to_date >= TO_TIMESTAMP( ? , ');\r\n" +
          "    lSQLText.Concat(lTimeStampFormat);\r\n" +
          "    lSQLText.Concat(source = ' )) ');   \r\n" +
          "    lWhereParameters.AppendRow(Object = lDateNoTime);\r\n" +
          "    lWhereParameters.AppendRow(Object = lDateNoTime);\r\n" +
          "\r\n" +
          "    \r\n" +
          "end if; \r\n" +
          "\r\n" +
          "// The following will return only one row because query is an aggregate\r\n" +
          "lResultSet = FetchData(pSQLCommand = lSQLText,\r\n" +
          "                       pWhereParameters = lWhereParameters,\r\n" +
          "                       pAppendResultToResultObj = FALSE,\r\n" +
          "                       pPopulateStyle = PS_SUM);\r\n" +
          "                    \r\n" +
          "if (lResultSet <> NIL) then\r\n" +
          "\r\n" +
          "    lResult : DecimalNullable = (DecimalNullable)(lResultSet[1]);\r\n" +
          "    \r\n" +
          "    if (lResult <> NIL) and (not lResult.IsNull) then\r\n" +
          "    \r\n" +
          "        lBalance.SetValue(source = lResult);\r\n" +
          "        \r\n" +
          "    end if;\r\n" +
          "\r\n" +
          "end if;\r\n" +
          "\r\n" +
          "return lBalance;\r\n" +
          "\r\n" +
          "end method;\r\n" +
          "");
      ForteParser parser = new ForteParser(tokenStream);
      methodImplementation_return result = parser.methodImplementation();
      //printTree("__retrieveLedgerBalance", result.getTree());
      syntaxErrors(parser);

  }

}
TOP

Related Classes of tool.model.grammar.TestMethodImplementation

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.