public void testAssociateNode() {

    String message = "Some error happened.";
    Throwable cause = new Throwable();
    SoySyntaxException sse = SoySyntaxException.createCausedWithoutMetaInfo(message, cause);

    String testFileContent =
        "{namespace boo}\n"
            + "\n"
            + "/** @param goo */\n"
            + "{template name=\".foo\"}\n"
            + "  {$goo}\n"
            + "{/template}\n";
    SoyFileSetNode soyTree = SharedTestUtils.parseSoyFiles(testFileContent);
    PrintNode pn = (PrintNode) soyTree.getChild(0).getChild(0).getChild(0);

    // Before.
    assertTrue(sse.getMessage().contains(message));
    assertEquals(cause, sse.getCause());
    assertEquals("unknown", sse.getSourceLocation().getFilePath());
    assertEquals(null, sse.getTemplateName());

    SoySyntaxExceptionUtils.associateNode(sse, pn);

    // After.
    assertTrue(sse.getMessage().contains(message));
    assertEquals(cause, sse.getCause());
    assertEquals("no-path", sse.getSourceLocation().getFilePath());
    assertEquals("boo.foo", sse.getTemplateName());
  }
Example #2
0
 private void assertParseError(String typeInput, String msg) {
   try {
     parseType(typeInput);
     fail("Input string '" + typeInput + "' should have failed to parse.");
   } catch (SoySyntaxException e) {
     assertThat(e.getMessage()).isEqualTo(msg);
   }
 }
  private void assertCheckIcuEscapingIsNotNeededFails(
      String rawText, String expectedErrorMsgSubstr) {

    try {
      IcuSyntaxUtils.checkIcuEscapingIsNotNeeded(rawText);
      fail();
    } catch (SoySyntaxException sse) {
      assertThat(sse.getMessage()).contains(expectedErrorMsgSubstr);
    }
  }
 /** Private helper for {@code testGenNoncollidingBaseNames()}. */
 private void assertErrorMsgWhenGenNoncollidingBaseNamesForExprs(
     String expectedErrorMsg, String exprListText) {
   List<ExprRootNode<?>> exprRoots =
       ExprParseUtils.parseExprListElseThrowSoySyntaxException(exprListText, "");
   try {
     MsgSubstUnitBaseVarNameUtils.genNoncollidingBaseNamesForExprs(exprRoots, "FALLBACK");
     MsgNodeTest.fail();
   } catch (SoySyntaxException sse) {
     MsgNodeTest.assertTrue(sse.getMessage().contains(expectedErrorMsg));
   }
 }
 /**
  * Assertions function that checks to make sure that name resolution fails with the expected
  * exception.
  *
  * @param fileContent The template source.
  * @param expectedError The expected failure message (a substring).
  */
 private void assertResolveExpressionTypesFails(String expectedError, String fileContent) {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(fileContent)
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .doRunInitialParsingPasses(false)
           .typeRegistry(typeRegistry)
           .parse();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   try {
     createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
     fail("Expected SoySyntaxException");
   } catch (SoySyntaxException e) {
     assertThat(e.getMessage()).contains(expectedError);
   } catch (IllegalStateException e) {
     // from the exploding error reporter
     assertThat(e.getMessage()).startsWith("Unexpected SoyError:");
     assertThat(e.getMessage()).contains(expectedError);
   }
 }
  public void testAssertNoUnboundGlobals() throws Exception {

    ExprRootNode<?> expr = (new ExpressionParser("BOO + 'aaa' + foo.GOO")).parseExpression();

    Map<String, PrimitiveData> globals =
        ImmutableMap.<String, PrimitiveData>of(
            "BOO",
            StringData.forValue("boo"),
            "GOO",
            StringData.forValue("goo"),
            "foo.MOO",
            StringData.forValue("moo"));

    try {
      ((new SubstituteGlobalsVisitor(globals, null, true)).new SubstituteGlobalsInExprVisitor())
          .exec(expr);
      fail();
    } catch (SoySyntaxException sse) {
      assertTrue(sse.getMessage().contains("Found unbound global 'foo.GOO'."));
    }
  }