Search in sources :

Example 1 with ValueAssertion

use of com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion in project closure-templates by google.

the class RenderVisitor method checkStrictParamType.

/**
 * Check that the given {@code paramValue} matches the static type of {@code param}.
 */
private void checkStrictParamType(final TemplateNode node, final TemplateParam param, @Nullable SoyValueProvider paramValue) {
    Kind kind = param.type().getKind();
    if (kind == Kind.ANY || kind == Kind.UNKNOWN) {
        // Nothing to check.  ANY and UKNOWN match all types.
        return;
    }
    if (paramValue == null) {
        paramValue = NullData.INSTANCE;
    } else if (paramValue instanceof SoyAbstractCachingValueProvider) {
        SoyAbstractCachingValueProvider typedValue = (SoyAbstractCachingValueProvider) paramValue;
        if (!typedValue.isComputed()) {
            // in order to preserve laziness we tell the value provider to assert the type when
            // computation is triggered
            typedValue.addValueAssertion(new ValueAssertion() {

                @Override
                public void check(SoyValue value) {
                    checkValueType(param, value, node);
                }
            });
            return;
        }
    }
    checkValueType(param, paramValue.resolve(), node);
}
Also used : ValueAssertion(com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion) Kind(com.google.template.soy.types.SoyType.Kind) ContentKind(com.google.template.soy.data.SanitizedContent.ContentKind) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) SoyAbstractCachingValueProvider(com.google.template.soy.data.SoyAbstractCachingValueProvider) SoyValue(com.google.template.soy.data.SoyValue)

Example 2 with ValueAssertion

use of com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion in project closure-templates by google.

the class SoyAbstractCachingValueProviderTest method testValueAssertions.

@Test
public void testValueAssertions() {
    final AtomicInteger counter = new AtomicInteger();
    ValueAssertion assertion = new ValueAssertion() {

        @Override
        public void check(SoyValue value) {
            counter.incrementAndGet();
            if (value.integerValue() < 0) {
                throw new IllegalStateException("boom");
            }
        }
    };
    TestValueProvider badValue = new TestValueProvider(-1);
    badValue.addValueAssertion(assertion);
    try {
        badValue.resolve();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageThat().isEqualTo("boom");
        assertThat(counter.get()).isEqualTo(1);
    }
    // Errors are not cached
    try {
        badValue.resolve();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageThat().isEqualTo("Caching was expected");
    }
    counter.set(0);
    TestValueProvider goodValue = new TestValueProvider(1);
    goodValue.addValueAssertion(assertion);
    // Will fail if the underlying one is called twice.
    assertThat(goodValue.resolve().integerValue()).isEqualTo(1);
    assertThat(counter.get()).isEqualTo(1);
    // successes are cached
    assertThat(goodValue.resolve().integerValue()).isEqualTo(1);
    assertThat(counter.get()).isEqualTo(1);
}
Also used : ValueAssertion(com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

ValueAssertion (com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion)2 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyAbstractCachingValueProvider (com.google.template.soy.data.SoyAbstractCachingValueProvider)1 SoyValue (com.google.template.soy.data.SoyValue)1 Kind (com.google.template.soy.types.SoyType.Kind)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1