diff --git a/docs/SYNTAX.md b/docs/SYNTAX.md index 8435a56..9396dcb 100644 --- a/docs/SYNTAX.md +++ b/docs/SYNTAX.md @@ -243,6 +243,38 @@ both ```i am in the "a" block``` and ```i am in the "b" block"``` would be outpu } } +### Enhanced Switch - case - default blocks (@switch) JDK17+ + +Enhanced Java switch - case - default control flow. The left curly character ```{``` indicates +the start of a block and the right curly character ```}``` marks the end. + +Switch block with case and default example. + + @switch (s) { + case ("a") -> { + i am in the "a" block + } + case ("b") -> { + i am in the "b" block + } + default -> { + i am in the "default" block + } + } + +Without a default block. + + @switch (v) { + case (1) -> { + i am in the "a" block + } + case (2) -> { + i am in the "b" block + } + } + +The Difference from normal Switch block is that @break is not required and each case is separate from the other. + ### With blocks (set one or more variables) (@with) As of v0.12.0 a `@with` block sets a variable to a value within a scoped block. Once the block diff --git a/pom.xml b/pom.xml index f994ea1..36707db 100644 --- a/pom.xml +++ b/pom.xml @@ -174,5 +174,27 @@ + + + + java17-settings + + [17,) + + + -Xdoclint:none + + + rocker-runtime + rocker-compiler + rocker-maven-plugin + rocker-gradle-plugin + rocker-test-template + rocker-test-template17 + rocker-test-reload + rocker-bom + + + diff --git a/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerLexer.g4 b/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerLexer.g4 index 148be33..bc8724f 100644 --- a/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerLexer.g4 +++ b/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerLexer.g4 @@ -16,10 +16,19 @@ CASE : Ws? 'case' Ws? Parentheses Ws? '{' ; +//switch block should not allow WS as this will generate statments within switch block which are not case or default +CASE_EXPRESSION + : Ws? 'case' Ws? Parentheses Ws? '->' Ws? '{' + ; + //switch block should not allow WS as this will generate statments within switch block which are not case or default DEFAULT : Ws? 'default' Ws? '{' Ws?; +//switch block should not allow WS as this will generate statments within switch block which are not case or default +DEFAULT_EXPRESSION + : Ws? 'default' Ws? '->' Ws? '{' Ws?; + LCURLY : '{' diff --git a/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerParser.g4 b/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerParser.g4 index 2149ce9..04dafe1 100644 --- a/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerParser.g4 +++ b/rocker-compiler/src/main/antlr4/com/fizzed/rocker/antlr4/RockerParser.g4 @@ -59,7 +59,7 @@ templateContent ; block - : (ifBlock | forBlock | withBlock|switchBlock) + : (ifBlock | forBlock | withBlock | switchBlock | switchExpressionBlock) ; ifBlock @@ -127,7 +127,7 @@ evalExpression ; switchBlock - : AT MV_SWITCH (switchCase|switchDefault|plain|comment)* RCURLY + : AT MV_SWITCH ( switchCase | switchDefault | plain | comment)* RCURLY ; switchCase @@ -138,4 +138,15 @@ switchDefault : DEFAULT templateContent* RCURLY ; +switchExpressionBlock + : AT MV_SWITCH ( switchExpressionCase | switchExpressionDefault | plain | comment)* RCURLY + ; + +switchExpressionCase + : CASE_EXPRESSION templateContent* RCURLY + ; + +switchExpressionDefault + : DEFAULT_EXPRESSION templateContent* RCURLY + ; diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java index fc1d3cf..4263f34 100644 --- a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java @@ -1053,6 +1053,63 @@ else if (unit instanceof SwitchDefaultBlockEnd) { .append(blockEnd.pop()) .append(" // default end ").append(sourceRef(unit)).append(CRLF); } + + else if(unit instanceof SwitchExpressionBlock){ + + SwitchExpressionBlock block = (SwitchExpressionBlock) unit; + + tab(w, depth+indent) + .append("switch ") + .append(block.getExpression()) + .append(" {").append(CRLF); + + blockEnd.push("}"); + + depth++; + } else if (unit instanceof SwitchExpressionBlockEnd) { + depth--; + + tab(w, depth+indent) + .append(blockEnd.pop()) + .append(" // switch end ").append(sourceRef(unit)).append(CRLF); + + + } else if (unit instanceof SwitchCaseExpressionBlock) { + + SwitchCaseExpressionBlock block = (SwitchCaseExpressionBlock) unit; + + tab(w, depth+indent) + .append("case ") + .append(block.getExpression()) + .append(" -> ") + .append(" {").append(CRLF); + + blockEnd.push("}"); + + depth++; + } else if (unit instanceof SwitchCaseExpressionBlockEnd) { + depth--; + + tab(w, depth+indent) + .append(blockEnd.pop()) + .append(" // case end ").append(sourceRef(unit)).append(CRLF); + }else if (unit instanceof SwitchDefaultExpressionBlock) { + + tab(w, depth+indent) + .append("default ") + .append("-> ") + .append(" {").append(CRLF); + + blockEnd.push("}"); + depth++; + } + else if (unit instanceof SwitchDefaultExpressionBlockEnd) { + depth--; + + tab(w, depth+indent) + .append(blockEnd.pop()) + .append(" // default end ").append(sourceRef(unit)).append(CRLF); + } //log.info(" src (@ {}): [{}]", unit.getSourceRef(), unit.getSourceRef().getConsoleFriendlyText()); } @@ -1149,5 +1206,7 @@ private TemplateModel postProcess(TemplateModel templateModel) throws PostProces return templateModel; } + + } diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/TemplateParser.java b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/TemplateParser.java index 4265d1c..9f00fb4 100644 --- a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/TemplateParser.java +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/TemplateParser.java @@ -950,6 +950,55 @@ public void exitSwitchBlock(RockerParser.SwitchBlockContext ctx) { verifySwitchBlock(); } + @Override + public void enterSwitchExpressionBlock(RockerParser.SwitchExpressionBlockContext ctx) { + + SourceRef sourceRef = createSourceRef(ctx); + final String text = ctx.MV_SWITCH().getText(); + final int idxFirst = text.indexOf('('); + final int idxLast = text.lastIndexOf(')'); + final String expression = text.substring(idxFirst, idxLast + 1); + model.getUnits().add(new SwitchExpressionBlock(sourceRef, expression)); + } + + @Override + public void exitSwitchExpressionBlock(RockerParser.SwitchExpressionBlockContext ctx) { + SourceRef sourceRef = createSourceRef(ctx); + model.getUnits().add(new SwitchExpressionBlockEnd(sourceRef)); + verifySwitchExpressionBlock(); + } + + @Override + public void enterSwitchExpressionCase(RockerParser.SwitchExpressionCaseContext ctx) { + + SourceRef sourceRef = createSourceRef(ctx); + final String text = ctx.CASE_EXPRESSION().getText(); + final int idxFirst = text.indexOf('('); + final int idxLast = text.lastIndexOf(')'); + final String expression = text.substring(idxFirst+1, idxLast ); + model.getUnits().add(new SwitchCaseExpressionBlock(sourceRef, expression)); + + } + + @Override + public void exitSwitchExpressionCase(RockerParser.SwitchExpressionCaseContext ctx) { + SourceRef sourceRef = createSourceRef(ctx); + model.getUnits().add(new SwitchCaseExpressionBlockEnd(sourceRef)); + } + + @Override + public void enterSwitchExpressionDefault(RockerParser.SwitchExpressionDefaultContext ctx) { + SourceRef sourceRef = createSourceRef(ctx); + model.getUnits().add(new SwitchDefaultExpressionBlock(sourceRef)); + } + + @Override + public void exitSwitchExpressionDefault(RockerParser.SwitchExpressionDefaultContext ctx) { + SourceRef sourceRef = createSourceRef(ctx); + model.getUnits().add(new SwitchDefaultExpressionBlockEnd(sourceRef)); + + } + private void verifySwitchBlock() { List units = this.model.getUnits(); @@ -971,6 +1020,27 @@ private void verifySwitchBlock() { } units.removeAll(toRemove); } + private void verifySwitchExpressionBlock() { + + List units = this.model.getUnits(); + List toRemove=new ArrayList<>(); + for (int i = 0, unitsSize = units.size(); i < unitsSize; i++) { + TemplateUnit unit = units.get(i); + if (unit instanceof PlainText) { + PlainText plain = (PlainText) unit; + if (inSwitchExpressionButNotCaseOrDefault(i)) { + if (plain.isWhitespace()) { + toRemove.add(unit); + } else { + // no plain allowed + SourcePosition pos = plain.findSourcePositionOfNonWhitespace(); + throw new ParserRuntimeException(pos.getLineNumber(), pos.getPosInLine(), "plain text not allowed before end of switch block"); + } + } + } + } + units.removeAll(toRemove); + } private boolean inSwitchButNotCaseOrDefault(int i) { for (int j = i; j >= 0; j--) { @@ -984,6 +1054,18 @@ private boolean inSwitchButNotCaseOrDefault(int i) { } return false; } + private boolean inSwitchExpressionButNotCaseOrDefault(int i) { + for (int j = i; j >= 0; j--) { + TemplateUnit templateUnit = model.getUnits().get(j); + if (templateUnit instanceof SwitchCaseExpressionBlockEnd || templateUnit instanceof SwitchDefaultExpressionBlockEnd || templateUnit instanceof SwitchExpressionBlock) { + return true; + } + if (templateUnit instanceof SwitchCaseExpressionBlock || templateUnit instanceof SwitchDefaultExpressionBlock || templateUnit instanceof SwitchExpressionBlockEnd) { + return false; + } + } + return false; + } @Override public void enterSwitchCase(RockerParser.SwitchCaseContext ctx) { diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlock.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlock.java new file mode 100644 index 0000000..24cba63 --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlock.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * Represents else-if part of an if block. + */ +public class SwitchCaseExpressionBlock extends BlockBegin { + + public SwitchCaseExpressionBlock(final SourceRef sourceRef, final String expression) { + super(sourceRef, expression); + } + @Override + public boolean supportsSourceJournaling() { + return false; + } +} diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlockEnd.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlockEnd.java new file mode 100644 index 0000000..c9d03cc --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchCaseExpressionBlockEnd.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * + * @author joelauer + */ +public class SwitchCaseExpressionBlockEnd extends BlockEnd { + + public SwitchCaseExpressionBlockEnd(SourceRef sourceRef) { + super(sourceRef); + } + @Override + public boolean supportsSourceJournaling() { + return false; + } +} diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlock.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlock.java new file mode 100644 index 0000000..2ffa2f3 --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlock.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * Represents else-if part of an if block. + */ +public class SwitchDefaultExpressionBlock extends BlockBegin { + + public SwitchDefaultExpressionBlock(final SourceRef sourceRef) { + super(sourceRef,null ); + } + @Override + public boolean supportsSourceJournaling() { + return false; + } +} diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlockEnd.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlockEnd.java new file mode 100644 index 0000000..fcbf97e --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchDefaultExpressionBlockEnd.java @@ -0,0 +1,32 @@ +/* + * Copyright 2015 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * + * @author joelauer + */ +public class SwitchDefaultExpressionBlockEnd extends BlockEnd { + + public SwitchDefaultExpressionBlockEnd(SourceRef sourceRef) { + super(sourceRef); + } + + @Override + public boolean supportsSourceJournaling() { + return false; + } +} diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlock.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlock.java new file mode 100644 index 0000000..ce73277 --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlock.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * Represents else-if part of an if block. + */ +public class SwitchExpressionBlock extends BlockBegin { + + public SwitchExpressionBlock(final SourceRef sourceRef, final String expression) { + super(sourceRef, expression); + } + + @Override + public boolean supportsSourceJournaling() { + return false; + } +} diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlockEnd.java b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlockEnd.java new file mode 100644 index 0000000..aea2624 --- /dev/null +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/model/SwitchExpressionBlockEnd.java @@ -0,0 +1,32 @@ +/* + * Copyright 2015 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.model; + +/** + * + * @author joelauer + */ +public class SwitchExpressionBlockEnd extends BlockEnd { + + public SwitchExpressionBlockEnd(SourceRef sourceRef) { + super(sourceRef); + } + @Override + public boolean supportsSourceJournaling() { + return false; + } + +} diff --git a/rocker-compiler/src/test/java/com/fizzed/rocker/compiler/TemplateParserTest.java b/rocker-compiler/src/test/java/com/fizzed/rocker/compiler/TemplateParserTest.java index 61f19d5..a26a2ad 100644 --- a/rocker-compiler/src/test/java/com/fizzed/rocker/compiler/TemplateParserTest.java +++ b/rocker-compiler/src/test/java/com/fizzed/rocker/compiler/TemplateParserTest.java @@ -829,6 +829,60 @@ public void testSwitchBlockWithoutDefault() throws Exception { assertThat(case2.getExpression(), is("\"test2\"")); + } + @Test + public void testSwitchExpressionBlockWithDefault() throws Exception { + + TemplateParser parser = createParser(); + File f = findTemplate("rocker/parser/SwitchBlockExpressionWithDefault.rocker.html"); + TemplateModel model = parser.parse(f); + SwitchExpressionBlock switchBlock = model.findUnitByOccurrence(SwitchExpressionBlock.class, 1); + assertThat(switchBlock.getExpression(), is("(s)")); + + SwitchCaseExpressionBlock case1 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 1); + assertThat(case1.getExpression(), is("\"test\"")); + + SwitchCaseExpressionBlock case2 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 2); + assertThat(case2.getExpression(), is("\"test2\"")); + + SwitchDefaultExpressionBlock defaultBlock = model.findUnitByOccurrence(SwitchDefaultExpressionBlock.class, 1); + assertNull(defaultBlock.getExpression()); + } + + @Test + public void testSwitchExpressionBlockWithDefaultSpaces() throws Exception { + + TemplateParser parser = createParser(); + File f = findTemplate("rocker/parser/SwitchBlockExpressionWithDefaultSpaces.rocker.html"); + TemplateModel model = parser.parse(f); + SwitchExpressionBlock switchBlock = model.findUnitByOccurrence(SwitchExpressionBlock.class, 1); + assertThat(switchBlock.getExpression(), is("(s)")); + + SwitchCaseExpressionBlock case1 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 1); + assertThat(case1.getExpression(), is("\"test\"")); + + SwitchCaseExpressionBlock case2 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 2); + assertThat(case2.getExpression(), is("\"test2\"")); + + SwitchDefaultExpressionBlock defaultBlock = model.findUnitByOccurrence(SwitchDefaultExpressionBlock.class, 1); + assertNull(defaultBlock.getExpression()); + } + + @Test + public void testSwitchExpressionBlockWithoutDefault() throws Exception { + TemplateParser parser = createParser(); + File f = findTemplate("rocker/parser/SwitchBlockExpressionWithoutDefault.rocker.html"); + TemplateModel model = parser.parse(f); + SwitchExpressionBlock switchBlock = model.findUnitByOccurrence(SwitchExpressionBlock.class, 1); + assertThat(switchBlock.getExpression(), is("(s)")); + + SwitchCaseExpressionBlock case1 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 1); + assertThat(case1.getExpression(), is("\"test\",\"test1\"")); + + SwitchCaseExpressionBlock case2 = model.findUnitByOccurrence(SwitchCaseExpressionBlock.class, 2); + assertThat(case2.getExpression(), is("\"test2\"")); + + } @Test diff --git a/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefault.rocker.html b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefault.rocker.html new file mode 100644 index 0000000..cd389c5 --- /dev/null +++ b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefault.rocker.html @@ -0,0 +1,18 @@ + +@args (String s) +@switch (s) { +case("test") -> { this is a test} + + + +case("test2")-> +{ this is a test2} + + + +default->{ this is default } + +} + + +this is a test diff --git a/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefaultSpaces.rocker.html b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefaultSpaces.rocker.html new file mode 100644 index 0000000..603de95 --- /dev/null +++ b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithDefaultSpaces.rocker.html @@ -0,0 +1,18 @@ + +@args (String s) +@switch (s) { +case ("test") -> { this is a test } + + + +case ("test2") -> +{ this is a test2 } + + + +default -> { this is default } + +} + + +this is a test diff --git a/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithoutDefault.rocker.html b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithoutDefault.rocker.html new file mode 100644 index 0000000..383e79e --- /dev/null +++ b/rocker-compiler/src/test/resources/rocker/parser/SwitchBlockExpressionWithoutDefault.rocker.html @@ -0,0 +1,10 @@ + +@args (String s) +@switch (s) { +case("test","test1") -> { this is a test or test1} +case("test2") -> { this is a test2} + } + + +this is a test + diff --git a/rocker-test-template17/pom.xml b/rocker-test-template17/pom.xml new file mode 100644 index 0000000..448f5a1 --- /dev/null +++ b/rocker-test-template17/pom.xml @@ -0,0 +1,148 @@ + + + 4.0.0 + rocker-test-template17 + rocker-test-template17 + jar + + + com.fizzed + rocker + 2.0.2-SNAPSHOT + + + + true + 17 + + + + + + maven-deploy-plugin + + true + + + + com.fizzed + rocker-maven-plugin + ${project.version} + + + generate-rocker-templates + generate-test-sources + + generate + + + ${java.version} + ${basedir}/src/test/java + ${basedir}/target/generated-test-sources/rocker + false + true + STATIC_BYTE_ARRAYS + true + + com.fizzed.rocker.processor.LoggingProcessor + + + + + + + + + + + exec-java6test + + + + org.codehaus.mojo + exec-maven-plugin + 1.3.2 + + + exec-run + test + + java + + + + + + + + + exec-java8test + + + + org.codehaus.mojo + exec-maven-plugin + 1.3.2 + + + exec-run + test + + java + + + + + + + + + + + + + + com.fizzed + rocker-maven-plugin + + + + + + com.google.guava + guava + test + + + + junit + junit + + + + joda-time + joda-time + + + + ch.qos.logback + logback-classic + + + + commons-io + commons-io + + + + io.undertow + undertow-core + + + + io.netty + netty-all + + + + diff --git a/rocker-test-template17/src/test/java/com/fizzed/rocker/compiler/CompiledTemplateTest.java b/rocker-test-template17/src/test/java/com/fizzed/rocker/compiler/CompiledTemplateTest.java new file mode 100644 index 0000000..10dfab6 --- /dev/null +++ b/rocker-test-template17/src/test/java/com/fizzed/rocker/compiler/CompiledTemplateTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2015 Fizzed Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.fizzed.rocker.compiler; + +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CompiledTemplateTest { + static private final Logger log = LoggerFactory.getLogger(CompiledTemplateTest.class); + + @Test + public void switchBlockExpressionWithDefault() throws Exception { + String html = new rocker17.SwitchBlockExpressionWithDefault().s("test").render().toString(); + + Assert.assertEquals( + """ + + this is a test + + +this is a test + """, html); + + } + + @Test + public void switchBlockExpressionWithDefaultAndCommaDelimited() throws Exception { + String html = new rocker17.SwitchBlockExpressionWithDefaultAndCommaDelimited().s("test").render().toString(); + + Assert.assertEquals( + """ + + this is a test or test1 + + +this is a test + """, html); + + } + + @Test + public void switchBlockExpressionWithDefaultSpaces() throws Exception { + String html = new rocker17.SwitchBlockExpressionWithDefaultSpaces().s("test").render().toString(); + + Assert.assertEquals( + """ + + this is a test + + +this is a test +""", html); + } + + @Test + public void switchBlockExpressionWithoutDefault() throws Exception { + String html = new rocker17.SwitchBlockExpressionWithoutDefault().s("test").render().toString(); + + Assert.assertEquals( + """ + + this is a test + + +this is a test + + """, html); + } +} diff --git a/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefault.rocker.html b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefault.rocker.html new file mode 100644 index 0000000..cd389c5 --- /dev/null +++ b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefault.rocker.html @@ -0,0 +1,18 @@ + +@args (String s) +@switch (s) { +case("test") -> { this is a test} + + + +case("test2")-> +{ this is a test2} + + + +default->{ this is default } + +} + + +this is a test diff --git a/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultAndCommaDelimited.rocker.html b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultAndCommaDelimited.rocker.html new file mode 100644 index 0000000..ae74bf8 --- /dev/null +++ b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultAndCommaDelimited.rocker.html @@ -0,0 +1,18 @@ + +@args (String s) +@switch (s) { +case("test","test1") -> { this is a test or test1} + + + +case("test2","test3")-> +{ this is a test2 or test3} + + + +default->{ this is default } + +} + + +this is a test diff --git a/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultSpaces.rocker.html b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultSpaces.rocker.html new file mode 100644 index 0000000..ed87833 --- /dev/null +++ b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithDefaultSpaces.rocker.html @@ -0,0 +1,18 @@ + +@args (String s) +@switch (s) { +case ("test") -> { this is a test} + + + +case ("test2") -> +{ this is a test2 } + + + +default -> { this is default } + +} + + +this is a test diff --git a/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithoutDefault.rocker.html b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithoutDefault.rocker.html new file mode 100644 index 0000000..5ab084f --- /dev/null +++ b/rocker-test-template17/src/test/java/rocker17/SwitchBlockExpressionWithoutDefault.rocker.html @@ -0,0 +1,10 @@ + +@args (String s) +@switch (s) { +case("test") -> { this is a test} +case("test2") -> { this is a test2} + } + + +this is a test +