Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b50a282cd26a091dad70bf30c325137bef2bf3f (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
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2013 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.etrice.core.room.EnumLiteral;
import org.eclipse.etrice.core.room.EnumerationType;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Henrik Rentz-Reichert
 * 
 */
public class TestEnumerations extends TestBase {
	private Resource res = null;

	@Before
	public void SetUp() {
		prepare(CoreTestsActivator.getInstance().getBundle());
		res = getResource("EnumExample.room");
	}

	@Test
	public void testImplicitEnumValues() {
		EObject obj = res.getEObject("EnumerationType:FirstEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().isEmpty());
		
		int i = 0;
		for (EnumLiteral lit : et.getLiterals()) {
			assertEquals("literal value", i++, lit.getLiteralValue());
		}
	}

	@Test
	public void testImplicitEnumValuesWithStart() {
		EObject obj = res.getEObject("EnumerationType:SecondEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().isEmpty());
		
		int i = 1;
		for (EnumLiteral lit : et.getLiterals()) {
			assertEquals("literal value", i++, lit.getLiteralValue());
		}
	}

	@Test
	public void testPartialExplicitValues() {
		EObject obj = res.getEObject("EnumerationType:ThirdEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().isEmpty());
		
		for (EnumLiteral lit : et.getLiterals()) {
			if (lit.getName().equals("one"))
				assertEquals("literal value", 1, lit.getLiteralValue());
			else if (lit.getName().equals("two"))
				assertEquals("literal value", 2, lit.getLiteralValue());
			else if (lit.getName().equals("five"))
				assertEquals("literal value", 5, lit.getLiteralValue());
		}
	}

	@Test
	public void testExplicitValuesWithHex() {
		EObject obj = res.getEObject("EnumerationType:FourthEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().isEmpty());
		
		for (EnumLiteral lit : et.getLiterals()) {
			if (lit.getName().equals("one"))
				assertEquals("literal value", 1, lit.getLiteralValue());
			else if (lit.getName().equals("three"))
				assertEquals("literal value", 3, lit.getLiteralValue());
			else if (lit.getName().equals("sixtyfive"))
				assertEquals("literal value", 0x41, lit.getLiteralValue());
		}
	}

	@Test
	public void testExplicitValuesWithHexOnly() {
		EObject obj = res.getEObject("EnumerationType:FourthEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().isEmpty());
		
		for (EnumLiteral lit : et.getLiterals()) {
			if (lit.getName().equals("f1"))
				assertEquals("literal value", 0x1, lit.getLiteralValue());
			else if (lit.getName().equals("f2"))
				assertEquals("literal value", 0x2, lit.getLiteralValue());
			else if (lit.getName().equals("f3"))
				assertEquals("literal value", 0x4, lit.getLiteralValue());
			else if (lit.getName().equals("f4"))
				assertEquals("literal value", 0x8, lit.getLiteralValue());
		}
	}

	@Test
	public void testWrongEnumType() {
		EObject obj = res.getEObject("EnumerationType:WrongType");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().size() == 1);
		assertTrue(diag.getChildren().get(0).getMessage().contains("enumerations must be of integer type"));
	}

	@Test
	public void testEmptyEnum() {
		EObject obj = res.getEObject("EnumerationType:EmptyEnum");
		EnumerationType et = (EnumerationType) obj;
		Diagnostic diag = getDiag(et);
		assertTrue(diag.getChildren().size() == 1);
		assertTrue(diag.getChildren().get(0).getMessage().contains("at least one literal has to be specified"));
	}

}

Back to the top