### Question:
ExpressionParser { static Expression parse(String str) { return new ExpressionParser(str).expression(false); } ExpressionParser(String str);   }### Answer:
@Test public void testEmpty() { Expression expr = parse(""); assertThat(expr.getNodes()).hasSize(0); }
@Test public void testTextOnly() { Expression expr = parse("hello"); assertThat(expr.getNodes()).hasSize(1); assertThat(expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("hello"); }
@Test public void testVariableOnly() { Expression expr = parse("${my.var}"); assertThat(expr.getNodes()).hasSize(1); assertThat(expr.getNodes().get(0)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) expr.getNodes().get(0)).getVariableName()).isEqualTo("my.var"); }
@Test public void testTextWithEscapedDollarSign() { Expression expr = parse("\\${my.var}"); assertThat(expr.getNodes()).hasSize(1); assertThat(expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("${my.var}"); }
@Test public void testTextWithEscapedComma() { Expression expr = parse("foo\\,bar"); assertThat(expr.getNodes()).hasSize(1); assertThat(expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("foo\\,bar"); }
@Test public void testMixed1() { Expression expr = parse("foo${my.var}bar"); assertThat(expr.getNodes()).hasSize(3); assertThat(expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("foo"); assertThat(expr.getNodes().get(1)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) expr.getNodes().get(1)).getVariableName()).isEqualTo("my.var"); assertThat(expr.getNodes().get(2)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(2)).getText()).isEqualTo("bar"); }
@Test public void testMixed2() { Expression expr = parse("foo${my.var}"); assertThat(expr.getNodes()).hasSize(2); assertThat(expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("foo"); assertThat(expr.getNodes().get(1)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) expr.getNodes().get(1)).getVariableName()).isEqualTo("my.var"); }
@Test public void testMixed3() { Expression expr = parse("${my.var}bar"); assertThat(expr.getNodes()).hasSize(2); assertThat(expr.getNodes().get(0)).isInstanceOf(VariableNode.class); Assertions.assertThat(((VariableNode) expr.getNodes().get(0)).getVariableName()).isEqualTo("my.var"); assertThat(expr.getNodes().get(1)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(1)).getText()).isEqualTo("bar"); }
@Test public void testRandomDollarSigns() { Expression expr = parse("%4$-"); assertThat( expr.getNodes()).hasSize(1); assertThat( expr.getNodes().get(0)).isInstanceOf(TextNode.class); assertThat(((TextNode) expr.getNodes().get(0)).getText()).isEqualTo("%4$-"); }