|
| 1 | +package com.jayway.jsonpath.internal.filter; |
| 2 | + |
| 3 | +import com.jayway.jsonpath.BaseTest; |
| 4 | +import com.jayway.jsonpath.Predicate; |
| 5 | +import com.jayway.jsonpath.internal.Path; |
| 6 | +import com.jayway.jsonpath.internal.path.CompiledPath; |
| 7 | +import com.jayway.jsonpath.internal.path.PathTokenFactory; |
| 8 | +import org.assertj.core.util.Maps; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.junit.runners.Parameterized; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 16 | +import static org.hamcrest.CoreMatchers.is; |
| 17 | +import static org.junit.Assert.assertThat; |
| 18 | + |
| 19 | +import static com.jayway.jsonpath.internal.filter.ValueNode.*; |
| 20 | + |
| 21 | +@RunWith(Parameterized.class) |
| 22 | +public class RegexpEvaluatorTest extends BaseTest { |
| 23 | + |
| 24 | + private String regexp; |
| 25 | + private ValueNode valueNode; |
| 26 | + private boolean expectedResult; |
| 27 | + |
| 28 | + public RegexpEvaluatorTest(String regexp, ValueNode valueNode, boolean expectedResult) { |
| 29 | + this.regexp = regexp; |
| 30 | + this.valueNode = valueNode; |
| 31 | + this.expectedResult = expectedResult; |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void should_evaluate_regular_expression() { |
| 36 | + //given |
| 37 | + Evaluator evaluator = EvaluatorFactory.createEvaluator(RelationalOperator.REGEX); |
| 38 | + ValueNode patternNode = createPatternNode(regexp); |
| 39 | + Predicate.PredicateContext ctx = createPredicateContext(); |
| 40 | + |
| 41 | + //when |
| 42 | + boolean result = evaluator.evaluate(patternNode, valueNode, ctx); |
| 43 | + |
| 44 | + //then |
| 45 | + assertThat(result, is(equalTo(expectedResult))); |
| 46 | + } |
| 47 | + |
| 48 | + @Parameterized.Parameters(name="Regexp {0} for {1} node should evaluate to {2}") |
| 49 | + public static Iterable data() { |
| 50 | + return Arrays.asList( |
| 51 | + new Object[][]{ |
| 52 | + { "/true|false/", createStringNode("true", true), true }, |
| 53 | + { "/9.*9/", createNumberNode("9979"), true }, |
| 54 | + { "/fa.*se/", createBooleanNode("false"), true }, |
| 55 | + { "/Eval.*or/", createClassNode(String.class), false }, |
| 56 | + { "/JsonNode/", createJsonNode(json()), false }, |
| 57 | + { "/PathNode/", createPathNode(path()), false }, |
| 58 | + { "/Undefined/", createUndefinedNode(), false }, |
| 59 | + { "/NullNode/", createNullNode(), false } |
| 60 | + } |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + private static Path path() { |
| 65 | + return new CompiledPath(PathTokenFactory.createRootPathToken('$'), true); |
| 66 | + } |
| 67 | + |
| 68 | + private static String json() { |
| 69 | + return "{ 'some': 'JsonNode' }"; |
| 70 | + } |
| 71 | + |
| 72 | + private Predicate.PredicateContext createPredicateContext() { |
| 73 | + return createPredicateContext(Maps.newHashMap()); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments