Search in sources :

Example 1 with Script

use of org.junit.jupiter.engine.script.Script in project junit5 by junit-team.

the class ScriptExecutionEvaluatorTests method getJUnitConfigurationParameterWithJavaScript.

@Test
void getJUnitConfigurationParameterWithJavaScript() {
    Script script = script(DisabledIf.class, "junitConfigurationParameter.get('XXX')");
    Exception exception = assertThrows(ScriptEvaluationException.class, () -> evaluate(script));
    assertThat(exception.getMessage()).contains("Script returned `null`");
}
Also used : Script(org.junit.jupiter.engine.script.Script) ScriptEvaluationException(org.junit.jupiter.api.extension.ScriptEvaluationException) Test(org.junit.jupiter.api.Test) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 2 with Script

use of org.junit.jupiter.engine.script.Script in project junit5 by junit-team.

the class ScriptExecutionEvaluatorTests method computeConditionEvaluationResultWithDefaultReasonMessage.

@Test
void computeConditionEvaluationResultWithDefaultReasonMessage() {
    Script script = script(EnabledIf.class, "?");
    ConditionEvaluationResult result = evaluator.computeConditionEvaluationResult(script, "!");
    assertTrue(result.isDisabled());
    assertThat(result.getReason()).contains("Script `?` evaluated to: !");
}
Also used : Script(org.junit.jupiter.engine.script.Script) ConditionEvaluationResult(org.junit.jupiter.api.extension.ConditionEvaluationResult) Test(org.junit.jupiter.api.Test) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 3 with Script

use of org.junit.jupiter.engine.script.Script in project junit5 by junit-team.

the class ScriptExecutionCondition method createDisabledIfScriptOrNull.

private Script createDisabledIfScriptOrNull(AnnotatedElement annotatedElement) {
    Optional<DisabledIf> disabled = findAnnotation(annotatedElement, DisabledIf.class);
    if (!disabled.isPresent()) {
        return null;
    }
    DisabledIf annotation = disabled.get();
    String source = createSource(annotation.value());
    return new Script(annotation, annotation.engine(), source, annotation.reason());
}
Also used : Script(org.junit.jupiter.engine.script.Script) DisabledIf(org.junit.jupiter.api.condition.DisabledIf)

Example 4 with Script

use of org.junit.jupiter.engine.script.Script in project junit5 by junit-team.

the class ScriptExecutionCondition method createEnabledIfScriptOrNull.

private Script createEnabledIfScriptOrNull(AnnotatedElement annotatedElement) {
    Optional<EnabledIf> enabled = findAnnotation(annotatedElement, EnabledIf.class);
    if (!enabled.isPresent()) {
        return null;
    }
    EnabledIf annotation = enabled.get();
    String source = createSource(annotation.value());
    return new Script(annotation, annotation.engine(), source, annotation.reason());
}
Also used : Script(org.junit.jupiter.engine.script.Script) EnabledIf(org.junit.jupiter.api.condition.EnabledIf)

Example 5 with Script

use of org.junit.jupiter.engine.script.Script in project junit5 by junit-team.

the class ScriptExecutionCondition method evaluateExecutionCondition.

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    // Context without an annotated element?
    Optional<AnnotatedElement> element = context.getElement();
    if (!element.isPresent()) {
        return ENABLED_NO_ELEMENT;
    }
    AnnotatedElement annotatedElement = element.get();
    // Always try to create script instances.
    Script disabledScript = createDisabledIfScriptOrNull(annotatedElement);
    Script enabledScript = createEnabledIfScriptOrNull(annotatedElement);
    // If no scripts are created, no annotation of interest is attached to the underlying element.
    if (disabledScript == null && enabledScript == null) {
        return ENABLED_NO_ANNOTATION;
    }
    // Prepare list with single or two script elements.
    List<Script> scripts = new ArrayList<>();
    if (disabledScript != null) {
        scripts.add(disabledScript);
    }
    if (enabledScript != null) {
        scripts.add(enabledScript);
    }
    // Let the evaluator do its work.
    return evaluator.evaluate(context, scripts);
}
Also used : Script(org.junit.jupiter.engine.script.Script) AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList)

Aggregations

Script (org.junit.jupiter.engine.script.Script)8 DynamicTest (org.junit.jupiter.api.DynamicTest)4 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)4 Test (org.junit.jupiter.api.Test)4 ConditionEvaluationResult (org.junit.jupiter.api.extension.ConditionEvaluationResult)3 ScriptEvaluationException (org.junit.jupiter.api.extension.ScriptEvaluationException)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 DisabledIf (org.junit.jupiter.api.condition.DisabledIf)1 EnabledIf (org.junit.jupiter.api.condition.EnabledIf)1