diff options
author | Stéphane Bégaudeau | 2016-05-03 08:35:30 +0000 |
---|---|---|
committer | Stéphane Bégaudeau | 2016-05-04 12:00:50 +0000 |
commit | b9d3994e3c5a0489e85f2abe28d4e7561abd6e25 (patch) | |
tree | 1b52a7736d7862e06c5e0b4d570d89ef3dc4bb14 /tests | |
parent | 6c83556399d91f3925425db6942f28490a0b5f7e (diff) | |
download | org.eclipse.eef-b9d3994e3c5a0489e85f2abe28d4e7561abd6e25.tar.gz org.eclipse.eef-b9d3994e3c5a0489e85f2abe28d4e7561abd6e25.tar.xz org.eclipse.eef-b9d3994e3c5a0489e85f2abe28d4e7561abd6e25.zip |
Improve the API of Eval
1) The API of Eval has been modified in order to simplify its use by
reducing the number of methods with a similar signature that can be
called. The new methods have also a much more explicit name.
2) Simplify some usage of Eval in order to reduce the number of message
logged for the end users. If the absence of an expression does not
prevent the proper execution of the code, nothing is logged
Change-Id: I67566cdb1c56c0e1505d64cb36a0d81b4df63908
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/AllTests.java | 5 | ||||
-rw-r--r-- | tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/core/EvalTests.java | 98 |
2 files changed, 101 insertions, 2 deletions
diff --git a/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/AllTests.java b/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/AllTests.java index 951a0052f..4b99e3edf 100644 --- a/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/AllTests.java +++ b/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/AllTests.java @@ -17,6 +17,7 @@ import org.eclipse.eef.tests.internal.controllers.EEFRadioControllerTests; import org.eclipse.eef.tests.internal.controllers.EEFSelectControllerTests; import org.eclipse.eef.tests.internal.controllers.EEFTextControllerTests; import org.eclipse.eef.tests.internal.core.EEFDomainClassTesterTests; +import org.eclipse.eef.tests.internal.core.EvalTests; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -27,8 +28,8 @@ import org.junit.runners.Suite.SuiteClasses; * @author sbegaudeau */ @RunWith(Suite.class) -@SuiteClasses({ EEFDataTests.class, EEFTextControllerTests.class, EEFLabelControllerTests.class, EEFButtonControllerTests.class, - EEFCheckboxControllerTests.class, EEFRadioControllerTests.class, EEFSelectControllerTests.class, EEFDomainClassTesterTests.class }) +@SuiteClasses({ EvalTests.class, EEFDataTests.class, EEFTextControllerTests.class, EEFLabelControllerTests.class, EEFButtonControllerTests.class, + EEFCheckboxControllerTests.class, EEFRadioControllerTests.class, EEFSelectControllerTests.class, EEFDomainClassTesterTests.class }) public final class AllTests { /** * The constructor. diff --git a/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/core/EvalTests.java b/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/core/EvalTests.java new file mode 100644 index 000000000..c8c643b03 --- /dev/null +++ b/tests/org.eclipse.eef.tests/src/org/eclipse/eef/tests/internal/core/EvalTests.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2016 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.eef.tests.internal.core; + +import java.util.Map; + +import org.eclipse.eef.core.api.utils.EvalFactory; +import org.eclipse.sirius.common.interpreter.api.EvaluationResult; +import org.eclipse.sirius.common.interpreter.api.IInterpreter; +import org.eclipse.sirius.common.interpreter.api.IVariableManager; +import org.eclipse.sirius.common.interpreter.api.VariableManagerFactory; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; + +/** + * Tests of the {@link EvalFactory} utility class. + * + * @author sbegaudeau + */ +@SuppressWarnings({ "checkstyle:javadocmethod" }) +public class EvalTests { + + /** + * The first AQL based expression used for the test. + */ + private static final String FIRST_EXPRESSION = "aql:self.name"; //$NON-NLS-1$ + + /** + * The result of the first AQL based expression. + */ + private static final String FIRST_RESULT = "John Doe"; //$NON-NLS-1$ + + /** + * The second AQL based expression used for the test. + */ + private static final String SECOND_EXPRESSION = "aql:self.abstract"; //$NON-NLS-1$ + + /** + * The result of the second AQL based expression. + */ + private static final boolean SECOND_RESULT = true; + + @Test + public void testGetResult() { + IInterpreter interpreter = (Map<String, Object> variables, String expressionBody) -> { + if (FIRST_EXPRESSION.equals(expressionBody)) { + return EvaluationResult.of(FIRST_RESULT); + } + return EvaluationResult.of(Boolean.valueOf(SECOND_RESULT)); + }; + + IVariableManager variableManager = new VariableManagerFactory().createVariableManager(); + Object result = EvalFactory.of(interpreter, variableManager).evaluate(FIRST_EXPRESSION); + assertThat(result, is(FIRST_RESULT)); + } + + @Test + public void testGetDefaultValue() { + IInterpreter interpreter = (Map<String, Object> variables, String expressionBody) -> { + return EvaluationResult.of(null); + }; + IVariableManager variableManager = new VariableManagerFactory().createVariableManager(); + + assertThat(EvalFactory.of(interpreter, variableManager).evaluate(FIRST_EXPRESSION), nullValue()); + + Object result = EvalFactory.of(interpreter, variableManager).defaultValue(FIRST_RESULT).evaluate(FIRST_EXPRESSION); + assertThat(result, is(FIRST_RESULT)); + } + + @Test + public void testGetCastValue() { + IInterpreter interpreter = (Map<String, Object> variables, String expressionBody) -> { + if (FIRST_EXPRESSION.equals(expressionBody)) { + return EvaluationResult.of(FIRST_RESULT); + } + return EvaluationResult.of(Boolean.valueOf(SECOND_RESULT)); + }; + + IVariableManager variableManager = new VariableManagerFactory().createVariableManager(); + + String stringResult = EvalFactory.of(interpreter, variableManager).logIfInvalidType(String.class).evaluate(FIRST_EXPRESSION); + assertThat(stringResult, is(FIRST_RESULT)); + + Boolean booleanResult = EvalFactory.of(interpreter, variableManager).logIfInvalidType(Boolean.class).evaluate(SECOND_EXPRESSION); + assertThat(booleanResult, is(SECOND_RESULT)); + } +} |