public void testNullCoalescingAndConditionalOps() {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pa: unknown}",
                   "{@param pi: int}",
                   "{@param pf: float}",
                   "{@param? ni: int}",
                   "{captureType($pa ?: $pi)}",
                   "{captureType($pi ?: $pf)}",
                   "{captureType($pa ? $pi : $pf)}",
                   "{captureType($ni ?: 0)}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .doRunInitialParsingPasses(false)
           .typeRegistry(typeRegistry)
           .parse()
           .fileSet();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   List<SoyType> types = getCapturedTypes(soyTree);
   assertThat(types.get(0)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(1))
       .isEqualTo(UnionType.of(IntType.getInstance(), FloatType.getInstance()));
   assertThat(types.get(2))
       .isEqualTo(UnionType.of(IntType.getInstance(), FloatType.getInstance()));
   assertThat(types.get(3)).isEqualTo(IntType.getInstance());
 }
  public void testArithmeticOps() {
    SoyFileSetNode soyTree =
        SoyFileSetParserBuilder.forFileContents(
                constructTemplateSource(
                    "{@param pa: unknown}",
                    "{@param pi: int}",
                    "{@param pf: float}",
                    "{@param ps: string}",
                    "{captureType($pa + $pa)}",
                    "{captureType($pi + $pi)}",
                    "{captureType($pf + $pf)}",
                    "{captureType($pa - $pa)}",
                    "{captureType($pi - $pi)}",
                    "{captureType($pf - $pf)}",
                    "{captureType($pa * $pa)}",
                    "{captureType($pi * $pi)}",
                    "{captureType($pf * $pf)}",
                    "{captureType($pa / $pa)}",
                    "{captureType($pi / $pi)}",
                    "{captureType($pf / $pf)}",
                    "{captureType($pa % $pa)}",
                    "{captureType($pi % $pi)}",
                    "{captureType($pf % $pf)}",
                    "{captureType(-$pa)}",
                    "{captureType(-$pi)}",
                    "{captureType(-$pf)}",
                    // The remainder are all logically template errors but are not enforced by the
                    // compiler
                    "{captureType(-$ps)}",
                    "{captureType($ps / $pf)}"))
            .declaredSyntaxVersion(SyntaxVersion.V2_0)
            .typeRegistry(typeRegistry)
            .parse()
            .fileSet();
    createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
    createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
    List<SoyType> types = getCapturedTypes(soyTree);
    assertThat(types.get(0)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(1)).isEqualTo(IntType.getInstance());
    assertThat(types.get(2)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(3)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(4)).isEqualTo(IntType.getInstance());
    assertThat(types.get(5)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(6)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(7)).isEqualTo(IntType.getInstance());
    assertThat(types.get(8)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(9)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(10)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(11)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(12)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(13)).isEqualTo(IntType.getInstance());
    assertThat(types.get(14)).isEqualTo(FloatType.getInstance());
    assertThat(types.get(15)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(16)).isEqualTo(IntType.getInstance());
    assertThat(types.get(17)).isEqualTo(FloatType.getInstance());

    // These are the 'error' cases
    assertThat(types.get(18)).isEqualTo(UnknownType.getInstance());
    assertThat(types.get(19)).isEqualTo(UnknownType.getInstance());
  }
 public void testDataRefTypesWithUnknown() {
   // Test that data with the 'unknown' type is allowed to function as a map or list.
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pa: unknown}",
                   "{@param pb: map<string, float>}",
                   "{@param pc: map<int, string>}",
                   "{captureType($pa[0])}",
                   "{captureType($pa.xxx)}",
                   "{captureType($pa.xxx.yyy)}",
                   "{captureType($pb[$pa])}",
                   "{captureType($pc[$pa])}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .doRunInitialParsingPasses(false)
           .typeRegistry(typeRegistry)
           .parse()
           .fileSet();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   List<SoyType> types = getCapturedTypes(soyTree);
   assertThat(types.get(0)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(1)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(2)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(3)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(4)).isEqualTo(StringType.getInstance());
 }
 public void testArithmeticOps() {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pa: unknown}",
                   "{@param pi: int}",
                   "{@param pf: float}",
                   "{$pa + $pa}",
                   "{$pi + $pi}",
                   "{$pf + $pf}",
                   "{$pa - $pa}",
                   "{$pi - $pi}",
                   "{$pf - $pf}",
                   "{$pa * $pa}",
                   "{$pi * $pi}",
                   "{$pf * $pf}",
                   "{$pa / $pa}",
                   "{$pi / $pi}",
                   "{$pf / $pf}",
                   "{$pa % $pa}",
                   "{$pi % $pi}",
                   "{$pf % $pf}",
                   "{-$pa}",
                   "{-$pi}",
                   "{-$pf}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .doRunInitialParsingPasses(false)
           .typeRegistry(typeRegistry)
           .parse();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   List<SoyType> types = getPrintStatementTypes(soyTree);
   assertThat(types.get(0)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(1)).isEqualTo(IntType.getInstance());
   assertThat(types.get(2)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(3)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(4)).isEqualTo(IntType.getInstance());
   assertThat(types.get(5)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(6)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(7)).isEqualTo(IntType.getInstance());
   assertThat(types.get(8)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(9)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(10)).isEqualTo(IntType.getInstance());
   assertThat(types.get(11)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(12)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(13)).isEqualTo(IntType.getInstance());
   assertThat(types.get(14)).isEqualTo(FloatType.getInstance());
   assertThat(types.get(15)).isEqualTo(UnknownType.getInstance());
   assertThat(types.get(16)).isEqualTo(IntType.getInstance());
   assertThat(types.get(17)).isEqualTo(FloatType.getInstance());
 }
예제 #5
0
 public void testParseRecordTypes() {
   assertTypeEquals(
       RecordType.of(
           ImmutableMap.<String, SoyType>builder().put("a", IntType.getInstance()).build()),
       "[a:int]");
   assertTypeEquals(
       RecordType.of(
           ImmutableMap.<String, SoyType>builder()
               .put("a", IntType.getInstance())
               .put("b", FloatType.getInstance())
               .build()),
       "[a:int, b:float]");
   assertTypeEquals(
       RecordType.of(
           ImmutableMap.<String, SoyType>builder()
               .put("a", IntType.getInstance())
               .put("b", ListType.of(StringType.getInstance()))
               .build()),
       "[a:int, b:list<string>]");
 }
 public void testListLiteral() {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pi: int}",
                   "{@param pf: float}",
                   "{let $list: [$pi, $pf]/}",
                   "{captureType($list)}",
                   "{captureType(length($list))}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .typeRegistry(typeRegistry)
           .parse()
           .fileSet();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   List<SoyType> types = getCapturedTypes(soyTree);
   assertThat(types.get(0))
       .isEqualTo(ListType.of(UnionType.of(IntType.getInstance(), FloatType.getInstance())));
   assertThat(types.get(1)).isEqualTo(UnknownType.getInstance());
 }
 public void testMapLiteralAsRecord() {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pi: int}",
                   "{@param pf: float}",
                   "{let $map: ['a': $pi, 'b':$pf]/}",
                   "{captureType($map)}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .typeRegistry(typeRegistry)
           .parse()
           .fileSet();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   List<SoyType> types = getCapturedTypes(soyTree);
   assertThat(types.get(0))
       .isEqualTo(
           RecordType.of(
               ImmutableMap.<String, SoyType>of(
                   "a", IntType.getInstance(), "b", FloatType.getInstance())));
 }
 public void testMapLiteral() {
   SoyFileSetNode soyTree =
       SoyFileSetParserBuilder.forFileContents(
               constructTemplateSource(
                   "{@param pi: int}",
                   "{@param pf: float}",
                   "{let $map: [1: $pi, 2:$pf]/}",
                   "{captureType($map)}"))
           .declaredSyntaxVersion(SyntaxVersion.V2_0)
           .typeRegistry(typeRegistry)
           .parse()
           .fileSet();
   createResolveNamesVisitorForMaxSyntaxVersion().exec(soyTree);
   createResolveExpressionTypesVisitorForMaxSyntaxVersion().exec(soyTree);
   SoyType type = Iterables.getOnlyElement(getCapturedTypes(soyTree));
   assertThat(type)
       .isEqualTo(
           MapType.of(
               IntType.getInstance(),
               UnionType.of(IntType.getInstance(), FloatType.getInstance())));
 }