|
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.Arrays; |
22 | 22 | import java.util.List; |
| 23 | +import java.util.Map; |
23 | 24 | import java.util.stream.Stream; |
24 | 25 |
|
25 | 26 | public class SegmentEvaluatorTest { |
@@ -62,6 +63,46 @@ public void testContextInSegment(SegmentContext segment, List<TraitModel> identi |
62 | 63 | Assertions.assertEquals(expectedResponse, actualResult); |
63 | 64 | } |
64 | 65 |
|
| 66 | + private static Stream<Arguments> numericTraitComparisons() { |
| 67 | + return Stream.of( |
| 68 | + Arguments.of(7, SegmentConditions.EQUAL, "7", true), |
| 69 | + Arguments.of("7", SegmentConditions.EQUAL, "7", true), |
| 70 | + Arguments.of("+7", SegmentConditions.EQUAL, "7", true), |
| 71 | + Arguments.of("007", SegmentConditions.EQUAL, "7", true), |
| 72 | + Arguments.of("-7", SegmentConditions.LESS_THAN, "0", true), |
| 73 | + Arguments.of("7.5", SegmentConditions.GREATER_THAN, "7", true), |
| 74 | + Arguments.of(".5", SegmentConditions.LESS_THAN, "1", true), |
| 75 | + Arguments.of(" 7 ", SegmentConditions.EQUAL, "7.0", true), |
| 76 | + Arguments.of("7e2", SegmentConditions.EQUAL, "700", true), |
| 77 | + Arguments.of("NaN", SegmentConditions.EQUAL, "NaN", true), |
| 78 | + Arguments.of("-Infinity", SegmentConditions.LESS_THAN, "0", true), |
| 79 | + Arguments.of("plan-7", SegmentConditions.EQUAL, "7", false), |
| 80 | + Arguments.of("", SegmentConditions.EQUAL, "0", false), |
| 81 | + Arguments.of(" ", SegmentConditions.EQUAL, "0", false) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @MethodSource("numericTraitComparisons") |
| 87 | + public void testTraitValuesThatReadAsNumbersAreComparedAsNumbers(Object traitValue, |
| 88 | + SegmentConditions conditionOperator, String conditionValue, Boolean expectedResult) { |
| 89 | + // Given |
| 90 | + final EvaluationContext context = EngineMappers.mapContextAndIdentityDataToContext( |
| 91 | + FlagsmithTestHelper.evaluationContext(), "foo", Map.of("score", traitValue)); |
| 92 | + SegmentContext segment = new SegmentContext().withName("testSegment").withRules( |
| 93 | + Arrays.asList(new SegmentRule().withType(SegmentRule.Type.ALL).withConditions( |
| 94 | + Arrays.asList(new SegmentCondition() |
| 95 | + .withOperator(conditionOperator) |
| 96 | + .withProperty("score") |
| 97 | + .withValue(conditionValue))))); |
| 98 | + |
| 99 | + // When |
| 100 | + Boolean inSegment = SegmentEvaluator.isContextInSegment(context, segment); |
| 101 | + |
| 102 | + // Then |
| 103 | + Assertions.assertEquals(expectedResult, inSegment); |
| 104 | + } |
| 105 | + |
65 | 106 | private static Stream<Arguments> traitExistenceChecks() { |
66 | 107 | return Stream.of( |
67 | 108 | Arguments.of(SegmentConditions.IS_SET, "foo", new ArrayList<>(), false), |
|
0 commit comments