Search in sources :

Example 1 with ObjectWrapperWithAPISupport

use of freemarker.template.utility.ObjectWrapperWithAPISupport in project freemarker by apache.

the class SequenceBuiltInTest method testWithCollectionEx.

@Test
public void testWithCollectionEx() throws TemplateException, IOException {
    ObjectWrapperWithAPISupport ow = (ObjectWrapperWithAPISupport) getConfiguration().getObjectWrapper();
    TemplateModel xs = DefaultNonListCollectionAdapter.adapt(ImmutableSet.of("a", "b"), ow);
    assertThat(xs, not(instanceOf(TemplateSequenceModel.class)));
    assertThat(xs, instanceOf(TemplateCollectionModelEx.class));
    addToDataModel("xs", xs);
    try {
        assertOutput("${xs[1]}", "b");
        fail();
    } catch (TemplateException e) {
        // Contains tip to use ?sequence
        assertThat(e.getMessage(), containsString("?sequence"));
    }
    assertOutput("${xs?sequence[1]}", "b");
    // No need for ?sequence
    assertOutput("${xs?size}", "2");
}
Also used : TemplateCollectionModelEx(freemarker.template.TemplateCollectionModelEx) TemplateException(freemarker.template.TemplateException) ObjectWrapperWithAPISupport(freemarker.template.utility.ObjectWrapperWithAPISupport) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 2 with ObjectWrapperWithAPISupport

use of freemarker.template.utility.ObjectWrapperWithAPISupport in project freemarker by apache.

the class MinMaxBITest method basicsTest.

@Test
public void basicsTest() throws Exception {
    getConfiguration().setTimeZone(DateUtil.UTC);
    getConfiguration().setTimeFormat("HH:mm:ss");
    ObjectWrapperWithAPISupport ow = (ObjectWrapperWithAPISupport) getConfiguration().getObjectWrapper();
    for (boolean exposeAsSeq : new boolean[] { true, false }) {
        // Expose xs as SequenceTM or as CollectionTM
        for (InputMinMax testParams : ImmutableList.of(// Test parameters:             List (xs), Expected result for `?min`, For `?max`
        new InputMinMax(ImmutableList.of(1, 2, 3), "1", "3"), new InputMinMax(ImmutableList.of(3, 2, 1), "1", "3"), new InputMinMax(ImmutableList.of(1, 3, 2), "1", "3"), new InputMinMax(ImmutableList.of(2, 1, 3), "1", "3"), new InputMinMax(ImmutableList.of(2), "2", "2"), new InputMinMax(Collections.emptyList(), "-", "-"), new InputMinMax(ImmutableList.of(1.5, -0.5, 1L, 2.25), "-0.5", "2.25"), new InputMinMax(ImmutableList.of(Double.NEGATIVE_INFINITY, 1, Double.POSITIVE_INFINITY), "-\u221E", // \u221E = ∞
        "\u221E"), new InputMinMax(Arrays.asList(new Object[] { null, 1, null, 2, null }), "1", "2"), new InputMinMax(Arrays.asList(new Object[] { null, null, null }), "-", "-"), new InputMinMax(ImmutableList.of(new Time(2000), new Time(3000), new Time(1000)), "00:00:01", "00:00:03"))) {
            addToDataModel("xs", exposeAsSeq ? testParams.input : DefaultIterableAdapter.adapt(testParams.input, ow));
            assertOutput("${xs?min!'-'}", testParams.minExpected);
            assertOutput("${xs?max!'-'}", testParams.maxExpected);
        }
    }
}
Also used : ObjectWrapperWithAPISupport(freemarker.template.utility.ObjectWrapperWithAPISupport) Time(java.sql.Time) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 3 with ObjectWrapperWithAPISupport

use of freemarker.template.utility.ObjectWrapperWithAPISupport in project freemarker by apache.

the class SequenceBuiltInTest method testWithCollection.

@Test
public void testWithCollection() throws TemplateException, IOException {
    ObjectWrapperWithAPISupport ow = (ObjectWrapperWithAPISupport) getConfiguration().getObjectWrapper();
    TemplateModel xs = DefaultIterableAdapter.adapt(ImmutableSet.of("a", "b"), ow);
    assertThat(xs, not(instanceOf(TemplateCollectionModelEx.class)));
    assertThat(xs, not(instanceOf(TemplateSequenceModel.class)));
    addToDataModel("xs", xs);
    try {
        assertOutput("${xs[1]}", "b");
        fail();
    } catch (TemplateException e) {
        // Contains tip to use ?sequence
        assertThat(e.getMessage(), containsString("?sequence"));
    }
    assertOutput("${xs?sequence[1]}", "b");
    try {
        assertOutput("${xs?size}", "2");
        fail();
    } catch (TemplateException e) {
        // Contains tip to use ?sequence
        assertThat(e.getMessage(), containsString("?sequence"));
    }
    assertOutput("${xs?sequence?size}", "2");
}
Also used : TemplateException(freemarker.template.TemplateException) ObjectWrapperWithAPISupport(freemarker.template.utility.ObjectWrapperWithAPISupport) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Aggregations

ObjectWrapperWithAPISupport (freemarker.template.utility.ObjectWrapperWithAPISupport)3 TemplateTest (freemarker.test.TemplateTest)3 Test (org.junit.Test)3 TemplateException (freemarker.template.TemplateException)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateCollectionModelEx (freemarker.template.TemplateCollectionModelEx)1 Time (java.sql.Time)1