Skip to content

Commit

Permalink
Expression indexes are wrong
Browse files Browse the repository at this point in the history
Fixes #627

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Apr 27, 2022
1 parent 7c27abd commit f7ef2e8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*
* <p>
* {foo}
* </p>
*
* <p>
* {foo ?: foo : 'bar'}
* </p>
*
Expand Down Expand Up @@ -150,7 +153,7 @@ public String getLiteralJavaType() {

public String getContent() {
if (content == null) {
content = getOwnerTemplate().getText(getStart() + 1, getEnd() - 1);
content = getOwnerTemplate().getText(getStartContentOffset(), getEndContentOffset());
}
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ public Parameter getOwnerParameter() {
public Section getOwnerSection() {
return ownerSection;
}

@Override
public int getStartContentOffset() {
return super.getStart();
}

@Override
public int getEndContentOffset() {
return super.getEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public Expression getJavaTypeExpression() {
startExpression = getStartValue();
endExpression = getEndValue();
}
expression = new ExpressionParameter(startExpression - 1, endExpression + 1, getOwnerSection());
expression = new ExpressionParameter(startExpression, endExpression, getOwnerSection());
expression.setParent(this);
expression.setClosed(true);
return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.redhat.qute.parser.template.sections.ForSection;

/**
* Test with template parser which builds a Template AST.
*
Expand Down Expand Up @@ -114,7 +117,7 @@ public void parameterNotClosed() {
assertEquals(26, section.getEndTagOpenOffset()); // |{/}
assertEquals(28, section.getEndTagCloseOffset()); // {/|}
}

@Test
public void infixNotation() {
String content = "{#let name=value}\r\n" + //
Expand All @@ -133,4 +136,41 @@ public void infixNotation() {
assertEquals(25, section.getEndTagOpenOffset());
assertEquals(30, section.getEndTagCloseOffset());
}

@Test
public void for1() {
String content = "{#for item in items}" + //
"{/for}";
Template template = TemplateParser.parse(content, "test.qute");
assertEquals(1, template.getChildCount());
Node first = template.getChild(0);
assertEquals(NodeKind.Section, first.getKind());
Section section = (Section) first;
assertEquals(SectionKind.FOR, section.getSectionKind());
assertTrue(section.isClosed());
assertEquals(0, section.getStartTagOpenOffset()); // |{#let
assertEquals(19, section.getStartTagCloseOffset()); // {#let name=value|}
assertEquals(20, section.getEndTagOpenOffset());
assertEquals(25, section.getEndTagCloseOffset());

ForSection forSection = (ForSection) section;
assertEquals(3, forSection.getParameters().size());
Parameter parameter = forSection.getParameters().get(0);
assertEquals(6, parameter.getStart());
assertEquals(10, parameter.getEnd());
assertEquals("item", parameter.getName());
parameter = forSection.getParameters().get(1);
assertEquals(11, parameter.getStart());
assertEquals(13, parameter.getEnd());
assertEquals("in", parameter.getName());
parameter = forSection.getParameters().get(2);
assertEquals(14, parameter.getStart());
assertEquals(19, parameter.getEnd());
assertEquals("items", parameter.getName());
assertNotNull(parameter.getJavaTypeExpression());
Expression expression = parameter.getJavaTypeExpression();
assertEquals(14, expression.getStart());
assertEquals(19, expression.getEnd());
}

}

0 comments on commit f7ef2e8

Please sign in to comment.