Skip to content

Commit

Permalink
Add expressions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 13, 2024
1 parent ff3ca38 commit f3eefdd
Showing 1 changed file with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.smallrye.common.expression;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static io.smallrye.common.expression.Expression.Flag.*;
import static io.smallrye.common.expression.Expression.Flag.NO_SMART_BRACES;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -306,7 +306,7 @@ public void testPoint12() {

@Test
public void testPoint13() {
final Expression expression = Expression.compile("foo$$bar", Expression.Flag.MINI_EXPRS);
final Expression expression = Expression.compile("foo$$bar", MINI_EXPRS);
assertEquals("foorizbar", expression.evaluate((c, b) -> {
assertEquals("$", c.getKey());
b.append("riz");
Expand All @@ -323,7 +323,7 @@ public void testPoint14() {

@Test
public void testPoint15() {
final Expression expression = Expression.compile("foo$}bar", Expression.Flag.MINI_EXPRS);
final Expression expression = Expression.compile("foo$}bar", MINI_EXPRS);
assertEquals("foorizbar", expression.evaluate((c, b) -> {
assertEquals("}", c.getKey());
b.append("riz");
Expand Down Expand Up @@ -369,7 +369,7 @@ public void testPoint19() {

@Test
public void testPoint20() {
final Expression expression = Expression.compile("foo$:baz", Expression.Flag.MINI_EXPRS);
final Expression expression = Expression.compile("foo$:baz", MINI_EXPRS);
assertEquals("foobarbaz", expression.evaluate((c, b) -> {
assertEquals(":", c.getKey());
b.append("bar");
Expand Down Expand Up @@ -416,7 +416,7 @@ public void testPoint24() {

@Test
public void testPoint25() {
final Expression expression = Expression.compile("foo$xbar", Expression.Flag.MINI_EXPRS);
final Expression expression = Expression.compile("foo$xbar", MINI_EXPRS);
assertEquals("foobazbar", expression.evaluate((c, b) -> {
assertEquals("x", c.getKey());
b.append("baz");
Expand Down Expand Up @@ -604,4 +604,93 @@ public void testPoint46() {
assertEquals("", c.getExpandedDefault());
}), "Should expand to empty string");
}

@Test
void expressions() {
// No Flags
assertEquals("a", Expression.compile("a").evaluate((c, b) -> {
}));
assertEquals("a", Expression.compile("a").evaluate((c, b) -> {
assertNull(c.getKey());
}));
assertEquals("$", Expression.compile("$$").evaluate((c, b) -> {
}));
assertEquals("$", Expression.compile("$$").evaluate((c, b) -> {
assertNull(c.getKey());
}));
assertEquals("", Expression.compile("${foo}").evaluate((c, b) -> {
}));
assertThrows(IllegalArgumentException.class, () -> Expression.compile("${{foo}").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 7");
assertEquals("d", Expression.compile("${{foo}:d}").evaluate((c, b) -> {
assertEquals("{foo}", c.getKey());
b.append(c.getExpandedDefault());
}));
assertThrows(IllegalArgumentException.class, () -> Expression.compile("${{foo}:d{}").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 7");
assertEquals("d}", Expression.compile("${{foo}:d}}").evaluate((c, b) -> {
assertEquals("{foo}", c.getKey());
b.append(c.getExpandedDefault());
}));
assertEquals("d{}", Expression.compile("${{foo}:d{}}").evaluate((c, b) -> {
assertEquals("{foo}", c.getKey());
b.append(c.getExpandedDefault());
}));
assertThrows(IllegalArgumentException.class, () -> Expression.compile("$").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 0");
assertThrows(IllegalArgumentException.class, () -> Expression.compile("$foo").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 1");
assertEquals("\\a", Expression.compile("\\a").evaluate((c, b) -> {
}));
assertEquals("\\$", Expression.compile("\\$$").evaluate((c, b) -> {
}));
assertEquals("\\", Expression.compile("\\${foo}").evaluate((c, b) -> {
}));
assertThrows(IllegalArgumentException.class, () -> Expression.compile("\\${{foo}").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 8");
assertEquals("\\", Expression.compile("\\${{foo}}").evaluate((c, b) -> {
}));
assertThrows(IllegalArgumentException.class, () -> Expression.compile("\\$").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 0");
assertThrows(IllegalArgumentException.class, () -> Expression.compile("\\$foo").evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 1");

// MINI_EXPR
assertEquals("a", Expression.compile("a", MINI_EXPRS).evaluate((c, b) -> {
}));
assertEquals("a", Expression.compile("a", MINI_EXPRS).evaluate((c, b) -> {
assertEquals("a", c.getKey());
}));
assertEquals("", Expression.compile("$$", MINI_EXPRS).evaluate((c, b) -> {
assertEquals("$", c.getKey());
}));
assertEquals("", Expression.compile("${foo}", MINI_EXPRS).evaluate((c, b) -> {
}));
assertEquals("", Expression.compile("${{foo}}", MINI_EXPRS).evaluate((c, b) -> {
}));

// ESCAPES
assertThrows(IllegalArgumentException.class, () -> Expression.compile("\\a", ESCAPES).evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 0");
assertThrows(IllegalArgumentException.class, () -> Expression.compile("a\\", ESCAPES).evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 0");
assertThrows(IllegalArgumentException.class, () -> Expression.compile("\\${foo}", ESCAPES).evaluate((c, b) -> {
}), "SRCOM01000: Invalid expression syntax at position 0");

// NO_SMART_BRACES
assertEquals("", Expression.compile("${foo}", NO_SMART_BRACES).evaluate((c, b) -> {
}));
assertEquals("}", Expression.compile("${{foo}}", NO_SMART_BRACES).evaluate((c, b) -> {
assertEquals("{foo", c.getKey());
}));
assertEquals("d{", Expression.compile("${foo:d{}", NO_SMART_BRACES).evaluate((c, b) -> {
b.append(c.getExpandedDefault());
}));
assertEquals("{d", Expression.compile("${foo:{d}", NO_SMART_BRACES).evaluate((c, b) -> {
b.append(c.getExpandedDefault());
}));
assertEquals("{d}", Expression.compile("${foo:{d}}", NO_SMART_BRACES).evaluate((c, b) -> {
b.append(c.getExpandedDefault());
}));
}
}

0 comments on commit f3eefdd

Please sign in to comment.