public static TagCycleException create(Class<? extends TagCycleException> clazz, String path, Optional<Integer> lineNumber, Optional<Integer> startPosition) { final Integer line = lineNumber.orElse(-1); final Integer position = startPosition.orElse(-1); if (clazz != null) { if (clazz.equals(ExtendsTagCycleException.class)) { return new ExtendsTagCycleException(path, line, position); } else if (clazz.equals(ImportTagCycleException.class)) { return new ImportTagCycleException(path, line, position); } else if (clazz.equals(IncludeTagCycleException.class)) { return new IncludeTagCycleException(path, line, position); } else if (clazz.equals(MacroTagCycleException.class)) { return new MacroTagCycleException(path, line, position); } else if (clazz.equals(FromTagCycleException.class)) { return new FromTagCycleException(path, line, position); } } return new TagCycleException("", path, line, position); }
public void push(String path, int lineNumber, int startPosition) { if (contains(path)) { throw TagCycleException.create(exceptionClass, path, Optional.of(lineNumber), Optional.of(startPosition)); } stack.push(path); }
@Test public void itAvoidsSimpleExtendsCycles() throws IOException { try { jinjava.render(locator.fixture("extends-self.jinja"), new HashMap<String, Object>()); fail("expected extends tag cycle exception"); } catch (FatalTemplateErrorsException e) { TagCycleException cycleException = (TagCycleException) e.getErrors().iterator().next().getException(); assertThat(cycleException.getPath()).isEqualTo("extends-self.jinja"); } }
@Test public void itAvoidsNestedExtendsCycles() throws IOException { try { jinjava.render(locator.fixture("a-extends-b.jinja"), new HashMap<String, Object>()); fail("expected extends tag cycle exception"); } catch (FatalTemplateErrorsException e) { TagCycleException cycleException = (TagCycleException) e.getErrors().iterator().next().getException(); assertThat(cycleException.getPath()).isEqualTo("b-extends-a.jinja"); } }
@Test public void itAvoidsSimpleExtendsCycles() throws IOException { try { jinjava.render(locator.fixture("extends-self.jinja"), new HashMap<String, Object>()); fail("expected extends tag cycle exception"); } catch (FatalTemplateErrorsException e) { TagCycleException cycleException = (TagCycleException) e.getErrors().iterator().next().getException(); assertThat(cycleException.getPath()).isEqualTo("extends-self.jinja"); } }
@Test public void itAvoidsNestedExtendsCycles() throws IOException { try { jinjava.render(locator.fixture("a-extends-b.jinja"), new HashMap<String, Object>()); fail("expected extends tag cycle exception"); } catch (FatalTemplateErrorsException e) { TagCycleException cycleException = (TagCycleException) e.getErrors().iterator().next().getException(); assertThat(cycleException.getPath()).isEqualTo("b-extends-a.jinja"); } }