Log in Help
Print
Homegatewikicowtestunitgateyamtranslate 〉 PrettyTranslatorTests.java
 
/*
 *  PrettyTranslatorTests.java
 *  Copyright (c) 1998-2008, The University of Sheffield.
 *
 *  This code is from the GATE project (http://gate.ac.uk/) and is free
 *  software licenced under the GNU General Public License version 3. It is
 *  distributed without any warranty. For more details see COPYING.txt in the
 *  top level directory (or at http://gatewiki.sf.net/COPYING.txt).
 *  
 *  Hamish Cunningham, 3rd May 2006
 */

package gate.yam.translate;

import java.io.*;
import junit.framework.*;
import org.apache.log4j.Logger;
import gate.*;
import gate.util.*;
import gate.wiki.*;
import gate.yam.*;
import gate.yam.parse.*;
import static gate.yam.translate.NodeKind.*;


/**
 * Unit test for PrettyTranslator.
 */
public class PrettyTranslatorTests extends AbstractTranslatorTest
{
  /** Logger */
  static Logger log =
    Logger.getLogger("gate.yam.translate.PrettyTranslatorTests");

  /** Initialise GATE if it hasn't already been done. */
  static {
    if(! Gate.isInitialised()) {
      File cowGate =
        new File(System.getProperty("user.dir"), "web-app/WEB-INF/gate");
      System.setProperty(
        "gate.user.session",
        new File(cowGate, "user-gate.session").getAbsolutePath()
      );
      System.setProperty(
        "gate.user.config",
        new File(cowGate, "user-gate.xml").getAbsolutePath()
      );
      System.setProperty(
        "gate.site.config",
        new File(cowGate, "site-gate.xml").getAbsolutePath()
      );
      Gate.setGateHome(cowGate);
      try {
        GateClassLoaderCreator.createGateClassLoaderWithParent(
            PrettyTranslatorTests.class.getClassLoader());
        Gate.init();
      } catch(Exception e) {
        log.error("couldn't init Gate");
        log.error(e);
      }
    }
  } // static block

  /** Create the test case. */
  public PrettyTranslatorTests(String testName) { super(testName); }

  /** Paths of example test files. */
  public String[] getTestFilePaths() {
    String testFilePaths[] = {
 /* TODO
      "/yam-errors", // unterminated comment
*/
      "/yam-tables",
      "/yam-small-tables",
      "/yam-tables-bug",
      "/yam-errors-min",
      "/yam-small-error",
      "/yam-minimal",
      "/yam-lists",
      "/yam-includes",
      "/yam-pretty",
      "/yam-anchor-links",
      "/yam-context",
      "/yam-includes2",
      "/yam-lists-bug",
      "/yam-lists-bug-simple",
      "/yam-lists-bug2",
      "/yam-lists-bug2-simple",
      "/yam-scratch",
      "/yam-images",
      "/yam-zero",
      "/yam-first",
      "/yam-urls",
      "/yam-predicates",
      "/yam-heading-increments",
      "/yam-comprehensive",
      "/yam-large",
      "/yam-backlog-bug",
      "/yam-wierd",
      "/yam-headings",
      "/yam-huge",
    };
    return testFilePaths;
  }

  /** Suffix of input files. */
  public String getInputSuffix() { return "yam"; }

  /** Suffix of output files. */
  public String[] getOutputSuffixes() {
    String outputSuffixes[] = {
      "pretty"
    };
    return outputSuffixes;
  }

  /** Run the translator and get the response */
  public Writer doTranslation(
    Reader testReader, Writer responseWriter, String outputType,
    String testName
  ) throws Exception {
    log.debug("testName = " + testName);

    // find the source directory
    String testFilePath =
      this.getClass().getResource(testName + ".yam").getPath();
    File testFileDir = new File(testFilePath).getParentFile();

    YamParseTree tree = null;
    if(outputType.equals("pretty")) {
      YamCommand yam = new YamCommand();
      tree = yam.translate(
        testReader, responseWriter, YamFile.FileType.PRETTY, testFileDir
      );
    } else {
      throw new GateException("unknown output type " + outputType);
    }

    if(testName.equals("/gate/yam/resources/yam-pretty")) {
      log.debug("doing extra tests...");
      doSuperCleverMysteriousTests(tree);
    }

    return responseWriter;
  } // doTranslation(...)

  public void doSuperCleverMysteriousTests(YamParseTree tree) {
    // tree has a first Sep node
    SimpleNode rootNode = tree.getRootNode();
    SimpleNode child = null;
    Class childClass = null;
    int childNumber = 0;
    int numChildren = rootNode.jjtGetNumChildren();
    do {
      child = (SimpleNode) rootNode.jjtGetChild(childNumber++);
    } while(
      childNumber < numChildren && child != null &&
      ( (childClass = child.getClass()) == ASTSep.class )
    );
    log.debug("childClass=" + childClass.getName());
    assertTrue(child != null && childClass != ASTSep.class);

    // Cursor finds the first Sep node, etc.
    Cursor here = new Cursor(rootNode, 1, 3);
    log.debug(
      "previousKind=" + here.getPreviousKind() + "; kind=" + here.getKind() +
      "; nextKind=" + here.getNextKind()
    );
    assertTrue(child == here.getHereNode());
    assertTrue(here.getKind() == NodeKind.UNIT_PARA);
    assertTrue(here.getPreviousKind() == NodeKind.NULL);

    // all children of the root are Sep, Unit or Word
    childNumber = 0;
    for( ; childNumber < numChildren; childNumber++) {
      SimpleNode n = (SimpleNode) rootNode.jjtGetChild(childNumber);
      if(n == null) continue;
      Class nClass = n.getClass();
      log.debug("nClass=" + nClass.getName());
      assertTrue(
        nClass == ASTSep.class || nClass == ASTWord.class ||
        nClass == ASTUnit.class
      );
    }

    log.debug(
      "previousKind=" + here.getPreviousKind() + "; kind=" + here.getKind() +
      "; nextKind=" + here.getNextKind()
    );
    checkHere(here, NULL, UNIT_PARA, UNIT_PARA);
    here.advance();
    checkHere(here, UNIT_PARA, UNIT_PARA, UNIT_INCLUDE);
    here.advance();
    checkHere(here, UNIT_PARA, UNIT_INCLUDE, UNIT_PARA);
    here.advance();
    checkHere(here, UNIT_INCLUDE, UNIT_PARA, UNIT_PARA);
    here.advance();
    checkHere(here, UNIT_PARA, UNIT_PARA, UNIT_SECTION);
    here.advance();
    checkHere(here, UNIT_PARA, UNIT_SECTION, UNIT_PARA);
    here.advance();
    checkHere(here, UNIT_SECTION, UNIT_PARA, NULL);
    here.advance();
    assertTrue(here.atEnd());
    checkHere(here, UNIT_PARA, NULL, NULL);
    here.advance();
    assertTrue(here.atEnd());
    checkHere(here, NULL, NULL, NULL);
    here.advance();
    assertTrue(here.atEnd());
    checkHere(here, NULL, NULL, NULL);
  } // doSuperClever...

  /** check the types at the current cursor position */
  void checkHere(Cursor c, NodeKind prevK, NodeKind hereK, NodeKind nextK) {
    log.debug(
      "previousKind=" + c.getPreviousKind() + "; kind=" + c.getKind() +
      "; nextKind=" + c.getNextKind()
    );
    assertTrue(c.getPreviousKind() == prevK);
    assertTrue(c.getKind() == hereK);
    assertTrue(c.getNextKind() == nextK);
  } // checkHere(Cursor, NodeKind, NodeKind, NodeKind)

} // PrettyTranslatorTests