|
| 1 | +package com.hubspot.jinjava.interpret; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 5 | + |
| 6 | +import com.hubspot.jinjava.Jinjava; |
| 7 | +import com.hubspot.jinjava.JinjavaConfig; |
| 8 | +import com.hubspot.jinjava.LegacyOverrides; |
| 9 | +import java.util.HashMap; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class LegacyWhitespaceControlParsingTest { |
| 14 | + Jinjava legacy; |
| 15 | + Jinjava modern; |
| 16 | + |
| 17 | + @Before |
| 18 | + public void setUp() throws Exception { |
| 19 | + legacy = |
| 20 | + new Jinjava( |
| 21 | + JinjavaConfig.newBuilder().withLegacyOverrides(LegacyOverrides.NONE).build() |
| 22 | + ); |
| 23 | + modern = |
| 24 | + new Jinjava( |
| 25 | + JinjavaConfig |
| 26 | + .newBuilder() |
| 27 | + .withLegacyOverrides( |
| 28 | + LegacyOverrides.newBuilder().withParseWhitespaceControlStrictly(true).build() |
| 29 | + ) |
| 30 | + .build() |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void itInterpretsStandaloneNegatives() { |
| 36 | + String template = "{{ -10 }}"; |
| 37 | + |
| 38 | + assertThat(legacy.render(template, new HashMap<>())).isEqualTo("10"); |
| 39 | + assertThat(modern.render(template, new HashMap<>())).isEqualTo("-10"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void itInterpretsStandaloneNegativesWithWhitespace() { |
| 44 | + String template = "{{ - 10 }}"; |
| 45 | + |
| 46 | + assertThat(legacy.render(template, new HashMap<>())).isEqualTo("10"); |
| 47 | + |
| 48 | + // Somewhat surprising, but this aligns with Jinja |
| 49 | + assertThat(modern.render(template, new HashMap<>())).isEqualTo("-10"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void itErrorsOnTrailingDash() { |
| 54 | + String template = "{{ 10- }}"; |
| 55 | + |
| 56 | + assertThat(legacy.render(template, new HashMap<>())).isEqualTo("10"); |
| 57 | + assertThatExceptionOfType(FatalTemplateErrorsException.class) |
| 58 | + .isThrownBy(() -> modern.render(template, new HashMap<>())) |
| 59 | + .withMessageContaining("syntax error at position 6"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void itErrorsOnSpacedTrailingDash() { |
| 64 | + String template = "{{ 10 - }}"; |
| 65 | + |
| 66 | + assertThat(legacy.render(template, new HashMap<>())).isEqualTo("10"); |
| 67 | + assertThatExceptionOfType(FatalTemplateErrorsException.class) |
| 68 | + .isThrownBy(() -> modern.render(template, new HashMap<>())) |
| 69 | + .withMessageContaining("syntax error at position 7"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void itErrorsOnSpacedDashesOnBothSides() { |
| 74 | + String template = "{{ - 10 - }}"; |
| 75 | + |
| 76 | + assertThat(legacy.render(template, new HashMap<>())).isEqualTo("10"); |
| 77 | + assertThatExceptionOfType(FatalTemplateErrorsException.class) |
| 78 | + .isThrownBy(() -> modern.render(template, new HashMap<>())) |
| 79 | + .withMessageContaining("syntax error at position 9"); |
| 80 | + } |
| 81 | +} |
0 commit comments