Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c4e4e046b57827fa2d9d770a1b154e4fd044410 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2016, 2018 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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));
	}
}

Back to the top