Search in sources :

Example 26 with Options

use of com.github.jknack.handlebars.Options in project hippo by NHS-digital-website.

the class IndentationHelperTest method returnsWeightedIndentationLevel_whenWeightingSet.

@Test
public void returnsWeightedIndentationLevel_whenWeightingSet() {
    // given
    final int ancestorCount = RandomUtils.nextInt();
    final double weighting = 1.5;
    final String expectedIndentationLevel = String.format("%.1f", ancestorCount * weighting);
    final Context context = Context.newContext(new Object());
    final ContextModelsStack contextModelsStack = mock(ContextModelsStack.class);
    given(contextModelsStack.ancestorModelsCount()).willReturn(ancestorCount);
    final ContextModelsStack.Factory contextStackFactory = mock(ContextModelsStack.Factory.class);
    given(contextStackFactory.from(context)).willReturn(contextModelsStack);
    final IndentationHelper indentationHelper = IndentationHelper.with(contextStackFactory);
    final Hash hash = Hash.of("weighting", weighting);
    final Options options = OptionsStub.with(context, hash);
    // when
    final String actualIndentationLevel = indentationHelper.apply(null, options);
    // then
    assertThat("Total number of ancestor Contexts is returned.", actualIndentationLevel, is(expectedIndentationLevel));
}
Also used : Context(com.github.jknack.handlebars.Context) Options(com.github.jknack.handlebars.Options) Hash(uk.nhs.digital.common.components.apispecification.handlebars.OptionsStub.Hash) Test(org.junit.Test)

Example 27 with Options

use of com.github.jknack.handlebars.Options in project hippo by NHS-digital-website.

the class MarkdownHelperTest method throwsException_onMarkdownRenderingFailure.

@Test
public void throwsException_onMarkdownRenderingFailure() {
    // given
    final RuntimeException expectedCause = new RuntimeException();
    given(commonmarkMarkdownConverter.toHtml(any(), any(), any(Integer.class))).willThrow(expectedCause);
    final Options options = OptionsStub.empty();
    final String markdown = "some markdown";
    // when
    final ThrowingRunnable action = () -> markdownHelper.apply(markdown, options);
    // then
    final RuntimeException actualException = assertThrows(RuntimeException.class, action);
    assertThat("Exception message provides failure's details.", actualException.getMessage(), is("Failed to render Markdown: " + markdown));
    assertThat("Cause exception is included.", actualException.getCause(), sameInstance(expectedCause));
}
Also used : Options(com.github.jknack.handlebars.Options) RandomTestUtils.randomString(uk.nhs.digital.test.util.RandomTestUtils.randomString) ThrowingRunnable(org.junit.function.ThrowingRunnable) Test(org.junit.Test)

Example 28 with Options

use of com.github.jknack.handlebars.Options in project hippo by NHS-digital-website.

the class StringBooleanVariableHelperTest method rendersMainBlock_whenStringVariableOfGivenNameIsFalse.

@Test
public void rendersMainBlock_whenStringVariableOfGivenNameIsFalse() throws IOException {
    // given
    final Options options = OptionsStub.with(buffer, Data.of(variableName, "false"));
    // when
    final Options.Buffer actualBuffer = helper.apply(variableName, options);
    // then
    then(buffer).should().append(TEMPLATE_CONTENT_FROM_MAIN_BLOCK);
    assertThat("Returns buffer provided by Handlebars through Options.", buffer, sameInstance(actualBuffer));
}
Also used : Options(com.github.jknack.handlebars.Options) Test(org.junit.Test)

Example 29 with Options

use of com.github.jknack.handlebars.Options in project hippo by NHS-digital-website.

the class StringBooleanVariableHelperTest method rendersMainBlock_whenStringVariableOfGivenNameIsEmptyString.

@Test
public void rendersMainBlock_whenStringVariableOfGivenNameIsEmptyString() throws IOException {
    // given
    final Options options = OptionsStub.with(buffer, Data.of(variableName, ""));
    // when
    final Options.Buffer actualBuffer = helper.apply(variableName, options);
    // then
    then(buffer).should().append(TEMPLATE_CONTENT_FROM_MAIN_BLOCK);
    assertThat("Returns buffer provided by Handlebars through Options.", actualBuffer, sameInstance(actualBuffer));
}
Also used : Options(com.github.jknack.handlebars.Options) Test(org.junit.Test)

Example 30 with Options

use of com.github.jknack.handlebars.Options in project hippo by NHS-digital-website.

the class StringBooleanVariableHelperTest method throwsException_onSchemaRenderingFailure.

@Test
public void throwsException_onSchemaRenderingFailure() throws IOException {
    // given
    final RuntimeException expectedCauseException = new RuntimeException();
    given(buffer.append(any())).willThrow(expectedCauseException);
    final Options options = OptionsStub.with(buffer, Data.of(variableName, "irrelevant value"));
    expectedException.expectMessage("Failed to interpret value of variable " + variableName + ".");
    expectedException.expect(TemplateRenderingException.class);
    expectedException.expectCause(sameInstance(expectedCauseException));
    // when
    helper.apply(variableName, options);
// then
// expectations set up in 'given' are satisfied
}
Also used : Options(com.github.jknack.handlebars.Options) Test(org.junit.Test)

Aggregations

Options (com.github.jknack.handlebars.Options)36 Test (org.junit.Test)35 Context (com.github.jknack.handlebars.Context)8 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 IOException (java.io.IOException)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)3 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)3 Schema (io.swagger.v3.oas.models.media.Schema)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Metacard (ddf.catalog.data.Metacard)2 ThrowingRunnable (org.junit.function.ThrowingRunnable)2 UncheckedIOException (java.io.UncheckedIOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Hash (uk.nhs.digital.common.components.apispecification.handlebars.OptionsStub.Hash)1 RandomTestUtils.randomString (uk.nhs.digital.test.util.RandomTestUtils.randomString)1