Search in sources :

Example 1 with IContext

use of com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext in project randomizedtesting by randomizedtesting.

the class TestFilterExpressionParser method testFalseExpressions.

@Test
public void testFalseExpressions() {
    // {groups}, {rules}
    Deque<String[]> examples = new ArrayDeque<String[]>(Arrays.asList(new String[][] { {}, { "@foo", "default" }, { "@nightly" }, { "not @nightly", "@nightly and @foo" }, { "@nightly", "@slow" }, { "not(@nightly or @slow)" } }));
    while (!examples.isEmpty()) {
        final List<String> groups = Arrays.asList((String[]) examples.pop());
        IContext context = new IContext() {

            @Override
            public boolean defaultValue() {
                return hasGroup("default");
            }

            @Override
            public boolean hasGroup(String value) {
                return groups.contains(value);
            }
        };
        for (String rule : examples.pop()) {
            Assertions.assertThat(new FilterExpressionParser().parse(rule).evaluate(context)).as("ctx=" + groups + ", rule=" + rule).isEqualTo(false);
        }
    }
}
Also used : IContext(com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 2 with IContext

use of com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext in project randomizedtesting by randomizedtesting.

the class TestFilterExpressionParser method testTrueExpressions.

@Test
public void testTrueExpressions() {
    // {groups}, {rules}
    Deque<String[]> examples = new ArrayDeque<String[]>(Arrays.asList(new String[][] { {}, { "not @foo", "not default" }, { "@nightly" }, { "@nightly", "@nightly or @foo", "@foo or @nightly", "not not @nightly" }, { "@nightly", "@slow" }, { "@nightly and @slow", "@nightly and not @foo", "not @nightly or @slow" }, { "default" }, { "", "default" } }));
    while (!examples.isEmpty()) {
        final List<String> groups = Arrays.asList((String[]) examples.pop());
        IContext context = new IContext() {

            @Override
            public boolean defaultValue() {
                return hasGroup("default");
            }

            @Override
            public boolean hasGroup(String value) {
                return groups.contains(value);
            }
        };
        for (String rule : examples.pop()) {
            Assertions.assertThat(new FilterExpressionParser().parse(rule).evaluate(context)).as("ctx=" + groups + ", rule=" + rule).isEqualTo(true);
        }
    }
}
Also used : IContext(com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 3 with IContext

use of com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext in project randomizedtesting by randomizedtesting.

the class GroupEvaluator method getIgnoreReason.

/**
   * @return Returns a non-null string with the reason why the annotated element (class, test or test-class pair) 
   *         should be ignored in the execution. This is an expert-level method, typically tests 
   *         shouldn't be concerned with this.
   */
public String getIgnoreReason(AnnotatedElement... elements) {
    final Map<String, Annotation> annotations = new HashMap<String, Annotation>();
    for (AnnotatedElement element : elements) {
        for (Annotation ann : element.getAnnotations()) {
            Class<? extends Annotation> annType = ann.annotationType();
            if (annType.isAnnotationPresent(TestGroup.class)) {
                if (!testGroups.containsKey(annType)) {
                    testGroups.put(annType, new TestGroupInfo(annType));
                }
                annotations.put(testGroups.get(annType).name, ann);
            }
        }
    }
    String defaultState = null;
    for (Annotation ann : annotations.values()) {
        TestGroupInfo g = testGroups.get(ann.annotationType());
        if (!g.enabled) {
            defaultState = "'" + g.name + "' test group is disabled (" + toString(ann) + ")";
            break;
        }
    }
    if (hasFilteringExpression()) {
        final String defaultStateCopy = defaultState;
        boolean enabled = filter.evaluate(new IContext() {

            @Override
            public boolean defaultValue() {
                return defaultStateCopy == null;
            }

            @Override
            public boolean hasGroup(String value) {
                if (value.startsWith("@"))
                    value = value.substring(1);
                for (Annotation ann : annotations.values()) {
                    if (value.equalsIgnoreCase(testGroups.get(ann.annotationType()).name)) {
                        return true;
                    }
                }
                return false;
            }
        });
        return enabled ? null : "Test filter condition: " + filterExpression;
    } else {
        return defaultState;
    }
}
Also used : IContext(com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext) HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Annotation(java.lang.annotation.Annotation)

Aggregations

IContext (com.carrotsearch.randomizedtesting.FilterExpressionParser.IContext)3 ArrayDeque (java.util.ArrayDeque)2 Test (org.junit.Test)2 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 HashMap (java.util.HashMap)1