|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.helidon.build.archetype.engine.v2.util; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import io.helidon.build.archetype.engine.v2.ast.Expression; |
| 22 | +import io.helidon.build.archetype.engine.v2.ast.Value; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 28 | +import static org.hamcrest.Matchers.containsString; |
| 29 | +import static org.hamcrest.Matchers.is; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link ValueHandler} |
| 34 | + */ |
| 35 | +public class ValueHandlerTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testBacktickExpression() { |
| 39 | + Map<String, Value> variables; |
| 40 | + Value result; |
| 41 | + |
| 42 | + variables = Map.of( |
| 43 | + "shape", Value.create("circle"), |
| 44 | + "var1", Value.create(List.of("a", "b", "c")), |
| 45 | + "var2", Value.create("b"), |
| 46 | + "var3", Value.create("c")); |
| 47 | + result = ValueHandler.process("`${shape} == 'circle' ? ${var1} contains ${var2} ? 'circle_b' : 'not_circle_b'" |
| 48 | + + " : ${var1} contains ${var3} ? 'circle_c' : 'not_circle_c'`", variables::get); |
| 49 | + assertThat(result.asText(), is("circle_b")); |
| 50 | + |
| 51 | + variables = Map.of( |
| 52 | + "shape", Value.create("circle"), |
| 53 | + "var1", Value.create(List.of("a", "b", "c")), |
| 54 | + "var2", Value.create("b")); |
| 55 | + result = ValueHandler.process("`${shape} == 'circle' ? ${var1} contains ${var2} : false`", variables::get); |
| 56 | + assertThat(result.asBoolean(), is(true)); |
| 57 | + |
| 58 | + variables = Map.of("shape", Value.create("circle")); |
| 59 | + result = ValueHandler.process("`${shape} == 'circle' ? 'red' : 'blue'`", variables::get); |
| 60 | + assertThat(result.asString(), is("red")); |
| 61 | + |
| 62 | + variables = Map.of("shape", Value.create("circle")); |
| 63 | + result = ValueHandler.process("`${shape}`", variables::get); |
| 64 | + assertThat(result.asString(), is("circle")); |
| 65 | + |
| 66 | + Map<String, Value> varMap = Map.of( |
| 67 | + "shape", Value.create("circle"), |
| 68 | + "var1", Value.create("circle")); |
| 69 | + Expression.FormatException e = assertThrows(Expression.FormatException.class, |
| 70 | + () -> ValueHandler.process("`${shape} == var1`", varMap::get)); |
| 71 | + assertThat(e.getMessage(), containsString("Unexpected token - var1")); |
| 72 | + |
| 73 | + e = assertThrows(Expression.FormatException.class, |
| 74 | + () -> ValueHandler.process("`shape`")); |
| 75 | + assertThat(e.getMessage(), containsString("Unexpected token - shape")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testBraceVarExpression() { |
| 80 | + Map<String, Value> variables; |
| 81 | + Value result; |
| 82 | + |
| 83 | + variables = Map.of( |
| 84 | + "shape", Value.create("circle"), |
| 85 | + "var1", Value.create(List.of("a", "b", "c")), |
| 86 | + "var2", Value.create("b"), |
| 87 | + "var3", Value.create("c")); |
| 88 | + result = ValueHandler.process("#{${shape} == 'circle' ? ${var1} contains ${var2} ? 'circle_b' : 'not_circle_b'" |
| 89 | + + " : ${var1} contains ${var3} ? 'circle_c' : 'not_circle_c'}", variables::get); |
| 90 | + assertThat(result.asText(), is("circle_b")); |
| 91 | + |
| 92 | + variables = Map.of( |
| 93 | + "shape", Value.create("circle"), |
| 94 | + "var1", Value.create(List.of("a", "b", "c")), |
| 95 | + "var2", Value.create("b")); |
| 96 | + result = ValueHandler.process("#{${shape} == 'circle' ? ${var1} contains ${var2} : false}", variables::get); |
| 97 | + assertThat(result.asBoolean(), is(true)); |
| 98 | + |
| 99 | + variables = Map.of("shape", Value.create("circle")); |
| 100 | + result = ValueHandler.process("#{${shape} == 'circle' ? 'red' : 'blue'}", variables::get); |
| 101 | + assertThat(result.asString(), is("red")); |
| 102 | + |
| 103 | + variables = Map.of("shape", Value.create("circle")); |
| 104 | + result = ValueHandler.process("#{${shape}}", variables::get); |
| 105 | + assertThat(result.asString(), is("circle")); |
| 106 | + |
| 107 | + Map<String, Value> varMap = Map.of( |
| 108 | + "shape", Value.create("circle"), |
| 109 | + "var1", Value.create("circle")); |
| 110 | + Expression.FormatException e = assertThrows(Expression.FormatException.class, |
| 111 | + () -> ValueHandler.process("#{${shape} == var1}", varMap::get)); |
| 112 | + assertThat(e.getMessage(), containsString("Unexpected token - var1")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testNoBraceVarExpression() { |
| 117 | + Map<String, Value> variables; |
| 118 | + Value result; |
| 119 | + |
| 120 | + variables = Map.of( |
| 121 | + "shape", Value.create("circle"), |
| 122 | + "var1", Value.create(List.of("a", "b", "c")), |
| 123 | + "var2", Value.create("b"), |
| 124 | + "var3", Value.create("c")); |
| 125 | + result = ValueHandler.process("#{shape == 'circle' ? var1 contains var2 ? 'circle_b' : 'not_circle_b' : " |
| 126 | + + "var1 contains var3 ? 'circle_c' : 'not_circle_c'}", variables::get); |
| 127 | + assertThat(result.asText(), is("circle_b")); |
| 128 | + |
| 129 | + variables = Map.of( |
| 130 | + "shape", Value.create("circle"), |
| 131 | + "var1", Value.create(List.of("a", "b", "c")), |
| 132 | + "var2", Value.create("b")); |
| 133 | + result = ValueHandler.process("#{shape == 'circle' ? var1 contains var2 : false}", variables::get); |
| 134 | + assertThat(result.asBoolean(), is(true)); |
| 135 | + |
| 136 | + variables = Map.of("shape", Value.create("circle")); |
| 137 | + result = ValueHandler.process("#{shape == 'circle' ? 'red' : 'blue'}", variables::get); |
| 138 | + assertThat(result.asString(), is("red")); |
| 139 | + |
| 140 | + variables = Map.of("shape", Value.create("circle")); |
| 141 | + result = ValueHandler.process("#{shape}", variables::get); |
| 142 | + assertThat(result.asString(), is("circle")); |
| 143 | + |
| 144 | + result = ValueHandler.process("`true`"); |
| 145 | + assertThat(result.asBoolean(), is(true)); |
| 146 | + |
| 147 | + result = ValueHandler.process("#{1}"); |
| 148 | + assertThat(result.asInt(), is(1)); |
| 149 | + |
| 150 | + result = ValueHandler.process("`['', 'adc', 'def']`"); |
| 151 | + assertThat(result.asList(), containsInAnyOrder("", "adc", "def")); |
| 152 | + |
| 153 | + Map<String, Value> varMap = Map.of( |
| 154 | + "shape", Value.create("circle"), |
| 155 | + "var1", Value.create("circle")); |
| 156 | + Expression.FormatException e = assertThrows(Expression.FormatException.class, |
| 157 | + () -> ValueHandler.process("#{${shape} ^ var1}", varMap::get)); |
| 158 | + assertThat(e.getMessage(), containsString("Unexpected token - ^")); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testNoExpression() { |
| 163 | + var value = ValueHandler.process("circle"); |
| 164 | + assertThat(value.asString(), is("circle")); |
| 165 | + |
| 166 | + value = ValueHandler.process("true"); |
| 167 | + assertThat(value.asString(), is("true")); |
| 168 | + |
| 169 | + value = ValueHandler.process("1"); |
| 170 | + assertThat(value.asString(), is("1")); |
| 171 | + |
| 172 | + value = ValueHandler.process("['', 'adc', 'def']"); |
| 173 | + assertThat(value.asString(), is("['', 'adc', 'def']")); |
| 174 | + } |
| 175 | +} |
0 commit comments