Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 966614a86230722a189c4ef3c79ec5e1e67e4609 (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Cloudsmith Inc. and others.
 * 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:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.metadata.expression;

import org.eclipse.equinox.p2.metadata.expression.*;
import org.eclipse.equinox.p2.tests.AbstractProvisioningTest;

public class ExpressionTest extends AbstractProvisioningTest {
	private static final IExpressionFactory factory = ExpressionUtil.getFactory();

	protected void testExpression(String exprStr, Object expectedOutcome) throws Exception {
		IExpression expr = ExpressionUtil.parse(exprStr);
		assertEquals(expr.evaluate(factory.createContext()), expectedOutcome);
	}

	protected void testMatch(String expr, boolean expectedOutcome) throws Exception {
		testExpression(expr, Boolean.valueOf(expectedOutcome));
	}

	public void testCompare() throws Exception {
		testMatch("'foo' == 'foo'", true);
		testMatch("'foo' == 'fooo'", false);
		testMatch("'foo' != 'foo'", false);
		testMatch("'foo' != 'fooo'", true);
		testMatch("2 < 1", false);
		testMatch("2 <= 1", false);
		testMatch("2 < 2", false);
		testMatch("2 <= 2", true);
		testMatch("2 < 3", true);
		testMatch("2 <= 3", true);
		testMatch("1 > 2", false);
		testMatch("1 >= 2", false);
		testMatch("2 > 2", false);
		testMatch("2 >= 2", true);
		testMatch("3 > 2", true);
		testMatch("3 >= 2", true);
	}

	public void testAutoCoerce() throws Exception {
		testMatch("'12' == 12", true);
		testMatch("'012' == 12", true);
		testMatch("'2' > '10'", true);
		testMatch("'2' > 10", false);
		testMatch("true == 'true'", true);
		testMatch("true == 'True'", true);
		testMatch("false == 'false'", true);
		testMatch("false == 'False'", true);
	}

	public void testLeftToRigthAssociativity() throws Exception {
		testMatch("2 < 10 == true", true);
		try {
			testMatch("true == 2 < 10", false);
			fail("Auto coercion from boolean to integer succeded");
		} catch (IllegalArgumentException e) {
			// OK
		}
	}
}

Back to the top