Skip to content

Commit

Permalink
Qute - fix param declaration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Apr 28, 2022
1 parent 316e5a0 commit 3a7940a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.jandex.ParameterizedType;
import org.junit.jupiter.api.Test;

import io.quarkus.qute.Engine;
Expand All @@ -32,10 +34,9 @@ public void testHintPattern() {
@Test
public void testCreate() throws IOException {
List<Expression> expressions = Engine.builder().build()
.parse("{@io.quarkus.qute.deployment.TypeInfosTest$Foo foo}{config:['foo.bar.baz']}{foo.name}")
.parse("{@io.quarkus.qute.deployment.TypeInfosTest$Foo foo}{@java.util.Map<? extends org.acme.Foo, String> list}{config:['foo.bar.baz']}{foo.name}{list.size}")
.getExpressions();
;
IndexView index = index(Foo.class);
IndexView index = index(Foo.class, Map.class);

List<Info> infos = TypeInfos.create(expressions.get(0), index, id -> "dummy");
assertEquals(1, infos.size());
Expand All @@ -48,6 +49,16 @@ public void testCreate() throws IOException {
assertEquals("io.quarkus.qute.deployment.TypeInfosTest$Foo", infos.get(0).asTypeInfo().rawClass.name().toString());
assertTrue(infos.get(1).isProperty());
assertEquals("name", infos.get(1).value);

infos = TypeInfos.create(expressions.get(2), index, id -> "dummy");
assertEquals(2, infos.size());
assertTrue(infos.get(0).isTypeInfo());
assertEquals("java.util.Map", infos.get(0).asTypeInfo().rawClass.name().toString());
ParameterizedType parameterizedType = infos.get(0).asTypeInfo().resolvedType.asParameterizedType();
assertEquals("org.acme.Foo", parameterizedType.arguments().get(0).toString());
assertEquals("String", parameterizedType.arguments().get(1).toString());
assertTrue(infos.get(1).isProperty());
assertEquals("size", infos.get(1).value);
}

private void assertHints(String hintStr, String... expectedHints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ public boolean isInfixNotationSupported() {

};

static final SplitConfig PARAM_DECLARATION_SPLIT_CONFIG = new SplitConfig() {

@Override
public boolean isSeparator(char candidate) {
return ' ' == candidate;
}

public boolean isInfixNotationSupported() {
return false;
}

@Override
public boolean isLiteralSeparator(char candidate) {
return candidate == '<' || candidate == '>';
}

};

private static final SplitConfig TYPE_INFO_SPLIT_CONFIG = new DefaultSplitConfig() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,14 @@ private void flushTag() {
// Parameter declaration
// {@org.acme.Foo foo}
Scope currentScope = scopeStack.peek();
String[] parts = content.substring(1).trim().split("[ ]{1,}");
if (parts.length != 2) {
// "@org.acme.Foo foo " -> "org.acme.Foo foo" and split
List<String> parts = Expressions.splitParts(content.substring(1).trim(),
Expressions.PARAM_DECLARATION_SPLIT_CONFIG);
if (parts.size() != 2) {
throw parserError("invalid parameter declaration " + START_DELIMITER + buffer.toString() + END_DELIMITER);
}
String value = parts[0];
String key = parts[1];
String value = parts.get(0);
String key = parts.get(1);
currentScope.putBinding(key, Expressions.typeInfoFrom(value));
sectionStack.peek().currentBlock().addNode(new ParameterDeclarationNode(content, origin(0)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ public void testInvalidParamDeclaration() {
"Parser error on line 1: invalid parameter declaration {@}", 1);
assertParserError("{@\n}",
"Parser error on line 1: invalid parameter declaration {@\n}", 1);
assertParserError("{@com.foo.Bar<String baz}",
"Parser error on line 1: invalid parameter declaration {@com.foo.Bar<String baz}", 1);

Engine engine = Engine.builder().addDefaultSectionHelpers().build();
Template template = engine.parse("{@com.foo.Bar<? extends org.acme.Baz, String> bar} {bar.name}");
Expression bar = find(template.getExpressions(), "bar.name");
assertEquals("|com.foo.Bar<org.acme.Baz, String>|", bar.getParts().get(0).getTypeInfo());
}

@Test
Expand Down

0 comments on commit 3a7940a

Please sign in to comment.