Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8b330a1f1068bbf1714bdba93863a57986e007ec (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.xtend.check;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.issues.IssuesImpl;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.internal.xtend.xtend.parser.ParseFacade;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExecutionContextImpl;

public class CheckEvaluationTest extends TestCase {

	private ExecutionContext ec;

	@Override
	protected void setUp() throws Exception {
		ec = new ExecutionContextImpl();
		// ec.registerMetaModel(new JavaMetaModel("asdf", new
		// JavaBeansStrategy()));
	}

	private ExtensionFile parse(final String expression) {
		return ParseFacade.file(new StringReader(expression), null);
	}

	public final void testValidate() {
		final ExtensionFile file = parse("context String ERROR this+' not allowed!' : !this.startsWith('test') ;\n"
				+ "\n"
				+ "context Integer ERROR ''+this+' not allowed!' : !(this > 5); \n"
				+ "\n"
				+ "context Object WARNING 'Objects of type '+this.metaType+' not allowed!' : "
				+ "String.isInstance(this) || Integer.isInstance(this); \n");

		final List<Object> toCheck = new ArrayList<Object>();
		toCheck.add("testThis");
		toCheck.add(new Integer(6));
		toCheck.add(Boolean.TRUE);
		final Issues is = new IssuesImpl();
		file.check(ec, toCheck, is, false);
		assertEquals(2, is.getErrors().length);
		assertEquals(1, is.getWarnings().length);
		assertEquals("Objects of type Boolean not allowed!",
				is.getWarnings()[0].getMessage());
		assertEquals(Boolean.TRUE, is.getWarnings()[0].getElement());

	}

	public final void testNot() {

		final ExtensionFile file = parse("context String ERROR 'fehler' : !this.startsWith('a');");
		final Issues is = new IssuesImpl();
		file.check(ec, Collections.singleton("name"), is, false);
		assertFalse(is.hasErrors());

		file.check(ec, Collections.singleton("aName"), is, false);
		assertTrue(is.hasErrors());
	}

	public final void testComplex() {
		final ExtensionFile file = parse("context List ERROR 'test' : this.typeSelect(String).forAll(e| this.typeSelect(Integer).exists(a| a == e.length));");
		final List<Object> list = new ArrayList<Object>();
		list.add("123");
		list.add("1234");
		list.add("12345");
		list.add(new Long(3));
		list.add(new Long(4));
		Issues is = new IssuesImpl();
		file.check(ec, Collections.singletonList(list), is, false);
		System.out.println(is.toString());
		assertTrue(is.hasErrors());
		list.add(new Long(5));
		is = new IssuesImpl();
		file.check(ec, list, is, false);
		assertFalse(is.hasErrors());
	}

	public final void testExists() {
		final ExtensionFile file = parse("context List ERROR 'test' : this.exists(e| e.toString() == \"123\" );");
		final List<Object> list = new ArrayList<Object>();
		list.add("1");
		list.add("12");
		list.add("1234");
		Issues is = new IssuesImpl();
		file.check(ec, Collections.singletonList(list), is, false);
		System.out.println(is.toString());
		assertTrue(is.hasErrors());
		list.add("123");
		is = new IssuesImpl();
		file.check(ec, Collections.singletonList(list), is, false);
		assertFalse(is.hasErrors());
	}

	public final void testNotExists() {
		final ExtensionFile file = parse("context List ERROR 'test' : this.notExists(e| e.toString() == \"123\" );");
		final List<Object> list = new ArrayList<Object>();
		list.add("1");
		list.add("12");
		list.add("1234");
		Issues is = new IssuesImpl();
		file.check(ec, Collections.singletonList(list), is, false);
		System.out.println(is.toString());
		assertFalse(is.hasErrors());
		list.add("123");
		is = new IssuesImpl();
		file.check(ec, Collections.singletonList(list), is, false);
		assertTrue(is.hasErrors());
	}

}

Back to the top