### Question:
ExpressionParser { VariableNode variableNode() { consume("${"); char c = 0; StringBuilder varName = new StringBuilder(); Expression defaultExpr = null; LOOP: while ((c = la()) >= 0) { switch (c) { case 0: throw new IllegalArgumentException("Missing '}' " + varName); case '}': consume('}'); break LOOP; case ':': consume(':'); defaultExpr = expression(true); break; default: varName.append(consume(c)); break; } } return new VariableNode(varName.toString(), defaultExpr); } ExpressionParser(String str);   }### Answer:
@Test public void testVariableNodeWithLiteralDefault() { VariableNode node = new ExpressionParser("${my.var:42}").variableNode(); Assertions.assertThat(node).isNotNull(); Assertions.assertThat(node.getVariableName()).isEqualTo("my.var"); Expression defaultExpr = node.getDefaultExpression(); assertThat(defaultExpr).isNotNull(); assertThat(defaultExpr.getNodes()).hasSize(1); assertThat(defaultExpr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) defaultExpr.getNodes().get(0)).getText()).isEqualTo("42"); }
@Test public void testVariableNodeWithVariableDefault() { VariableNode node = new ExpressionParser("${my.var:${foo.bar}}").variableNode(); Assertions.assertThat(node).isNotNull(); Assertions.assertThat(node.getVariableName()).isEqualTo("my.var"); Expression defaultExpr = node.getDefaultExpression(); assertThat(defaultExpr).isNotNull(); assertThat(defaultExpr.getNodes()).hasSize(1); assertThat(defaultExpr.getNodes().get(0)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) defaultExpr.getNodes().get(0)).getVariableName()).isEqualTo("foo.bar"); }
@Test public void testVariableNodeWithVariableDefaultWithLiteralDefault() { VariableNode node = new ExpressionParser("${my.var:tacos${my.other.var:42}}").variableNode(); Assertions.assertThat(node).isNotNull(); Assertions.assertThat(node.getVariableName()).isEqualTo("my.var"); Expression defaultExpr1 = node.getDefaultExpression(); assertThat(defaultExpr1).isNotNull(); assertThat(defaultExpr1.getNodes()).hasSize(2); assertThat(defaultExpr1.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) defaultExpr1.getNodes().get(0)).getText()).isEqualTo("tacos"); assertThat(defaultExpr1.getNodes().get(1)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) defaultExpr1.getNodes().get(1)).getVariableName()).isEqualTo("my.other.var"); Expression defaultExpr2 = ((VariableNode) defaultExpr1.getNodes().get(1)).getDefaultExpression(); assertThat(defaultExpr2.getNodes()).hasSize(1); assertThat(defaultExpr2.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode)defaultExpr2.getNodes().get(0)).getText()).isEqualTo("42"); }
@Test public void testUnclosedVariableNode() { try { new ExpressionParser("${my.var").variableNode(); fail("should have thrown"); } catch (IllegalArgumentException e) { } }
@Test public void testVariableNode() { VariableNode node = new ExpressionParser("${my.var}").variableNode(); Assertions.assertThat(node).isNotNull(); Assertions.assertThat(node.getVariableName()).isEqualTo("my.var"); Assertions.assertThat(node.getDefaultExpression()).isNull(); }