Method: AcceleoStandardLibraryParsedTest()

1: /*******************************************************************************
2: * Copyright (c) 2009, 2012 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.engine.tests.unit.environment;
12:
13: import static org.junit.Assert.assertEquals;
14: import static org.junit.Assert.fail;
15:
16: import java.util.Map;
17:
18: import org.eclipse.acceleo.engine.tests.unit.AbstractAcceleoTest;
19: import org.junit.Before;
20: import org.junit.Test;
21:
22: /**
23: * This will test the behavior of the Acceleo standard library's operations when called from a parsed
24: * generator file.
25: *
26: * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
27: */
28: @SuppressWarnings("nls")
29: public class AcceleoStandardLibraryParsedTest extends AbstractAcceleoTest {
30:         /** This will be generated from setup. */
31:         private Map<String, String> generatedPreview;
32:
33:         /** This is the output we expect from each generated file. */
34:         private final static String OUTPUT = "\t\tconstant output" + System.getProperty("line.separator");
35:
36:         /**
37:          * {@inheritDoc}
38:          *
39:          * @see org.eclipse.acceleo.engine.tests.unit.AbstractAcceleoTest#setUp()
40:          */
41:         @Before
42:         @Override
43:         public void setUp() {
44:                 super.setUp();
45:                 this.init("StdLib");
46:                 generatedPreview = generate("test_stdlib", previewStrategy); //$NON-NLS-1$
47:         }
48:
49:         /**
50:          * {@inheritDoc}
51:          *
52:          * @see org.eclipse.acceleo.engine.tests.unit.AbstractAcceleoTest#getModuleLocation()
53:          */
54:         @Override
55:         public String getModuleLocation() {
56:                 return "data/Library/stdlib.mtl";
57:         }
58:
59:         /**
60:          * {@inheritDoc}
61:          *
62:          * @see org.eclipse.acceleo.engine.tests.unit.AbstractAcceleoTest#getReferencePath()
63:          */
64:         @Override
65:         public String getReferencePath() {
66:                 return "StandardLibrary";
67:         }
68:
69:         /**
70:          * Tests the behavior of the standard "first(int)" operation on Strings. Expects the result to be the same
71:          * as source.substring(0, int).
72:          */
73:         @Test
74:         public void testStringFirst() {
75:                 final String fileName = "test_string_first";
76:                 boolean fileFound = false;
77:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
78:                         if (filePreview.getKey().endsWith(fileName)) {
79:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
80:                                 fileFound = true;
81:                         }
82:                 }
83:                 if (!fileFound) {
84:                         fail("expected file hasn't been generated.");
85:                 }
86:         }
87:
88:         /**
89:          * Tests the behavior of the standard "index(String)" operation on Strings. Expects the result to be the
90:          * same as source.indexOf(String).
91:          */
92:         @Test
93:         public void testStringIndex() {
94:                 final String fileName = "test_string_index";
95:                 boolean fileFound = false;
96:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
97:                         if (filePreview.getKey().endsWith(fileName)) {
98:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
99:                                 fileFound = true;
100:                         }
101:                 }
102:                 if (!fileFound) {
103:                         fail("expected file hasn't been generated.");
104:                 }
105:         }
106:
107:         /**
108:          * Tests the behavior of the standard "isAlpha()" operation on Strings. Expects the result to be
109:          * <code>true</code> if and only if {@link Character#isLetter(char)} returns <code>true</code> for each
110:          * and every character of the source value.
111:          */
112:         @Test
113:         public void testStringIsAlpha() {
114:                 final String fileName = "test_string_isAlpha";
115:                 boolean fileFound = false;
116:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
117:                         if (filePreview.getKey().endsWith(fileName)) {
118:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
119:                                 fileFound = true;
120:                         }
121:                 }
122:                 if (!fileFound) {
123:                         fail("expected file hasn't been generated.");
124:                 }
125:         }
126:
127:         /**
128:          * Tests the behavior of the standard "isAlphanum()" operation on Strings. Expects the result to be
129:          * <code>true</code> if and only if {@link Character#isLetterOrDigit(char)} returns <code>true</code> for
130:          * each and every character of the source value.
131:          */
132:         @Test
133:         public void testStringIsAlphanum() {
134:                 final String fileName = "test_string_isAlphanum";
135:                 boolean fileFound = false;
136:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
137:                         if (filePreview.getKey().endsWith(fileName)) {
138:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
139:                                 fileFound = true;
140:                         }
141:                 }
142:                 if (!fileFound) {
143:                         fail("expected file hasn't been generated.");
144:                 }
145:         }
146:
147:         /**
148:          * Tests the behavior of the standard "last(int)" operation on Strings. Expects the result to be the same
149:          * as source.substring(source.length() - int, source.length()).
150:          */
151:         @Test
152:         public void testStringLast() {
153:                 final String fileName = "test_string_last";
154:                 boolean fileFound = false;
155:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
156:                         if (filePreview.getKey().endsWith(fileName)) {
157:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
158:                                 fileFound = true;
159:                         }
160:                 }
161:                 if (!fileFound) {
162:                         fail("expected file hasn't been generated.");
163:                 }
164:         }
165:
166:         /**
167:          * Tests the behavior of the standard "strcmp(String)" operation on Strings. Expects the result to be the
168:          * same as source.compareTo(String).
169:          */
170:         @Test
171:         public void testStringStrcmp() {
172:                 final String fileName = "test_string_strcmp";
173:                 boolean fileFound = false;
174:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
175:                         if (filePreview.getKey().endsWith(fileName)) {
176:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
177:                                 fileFound = true;
178:                         }
179:                 }
180:                 if (!fileFound) {
181:                         fail("expected file hasn't been generated.");
182:                 }
183:         }
184:
185:         /**
186:          * Tests the behavior of the standard "strstr(String)" operation on Strings. Expects the result to be the
187:          * same as source.contains(String).
188:          */
189:         @Test
190:         public void testStringStrstr() {
191:                 final String fileName = "test_string_strstr";
192:                 boolean fileFound = false;
193:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
194:                         if (filePreview.getKey().endsWith(fileName)) {
195:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
196:                                 fileFound = true;
197:                         }
198:                 }
199:                 if (!fileFound) {
200:                         fail("expected file hasn't been generated.");
201:                 }
202:         }
203:
204:         /**
205:          * Tests the behavior of the standard "strtok(String, int)" operation on Strings.
206:          */
207:         @Test
208:         public void testStringStrtok() {
209:                 final String fileName = "test_string_strtok";
210:                 boolean fileFound = false;
211:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
212:                         if (filePreview.getKey().endsWith(fileName)) {
213:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
214:                                 fileFound = true;
215:                         }
216:                 }
217:                 if (!fileFound) {
218:                         fail("expected file hasn't been generated.");
219:                 }
220:         }
221:
222:         /**
223:          * Tests the behavior of the standard String.substitute(String, String) operation.
224:          * <p>
225:          * We expect the call to replace the first occurence of the substring by the replacement, not considering
226:          * both parameters as regular expressions.
227:          * </p>
228:          */
229:         @Test
230:         public void testStringSubstitute() {
231:                 final String fileName = "test_string_substitute";
232:                 boolean fileFound = false;
233:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
234:                         if (filePreview.getKey().endsWith(fileName)) {
235:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
236:                                 fileFound = true;
237:                         }
238:                 }
239:                 if (!fileFound) {
240:                         fail("expected file hasn't been generated.");
241:                 }
242:         }
243:
244:         /**
245:          * Tests the behavior of the standard String.toLowerFirst() operation.
246:          */
247:         @Test
248:         public void testStringToLowerFirst() {
249:                 final String fileName = "test_string_toLowerFirst";
250:                 boolean fileFound = false;
251:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
252:                         if (filePreview.getKey().endsWith(fileName)) {
253:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
254:                                 fileFound = true;
255:                         }
256:                 }
257:                 if (!fileFound) {
258:                         fail("expected file hasn't been generated.");
259:                 }
260:         }
261:
262:         /**
263:          * Tests the behavior of the standard String.toUpperFirst() operation.
264:          */
265:         @Test
266:         public void testStringToUpperFirst() {
267:                 final String fileName = "test_string_toUpperFirst";
268:                 boolean fileFound = false;
269:                 for (Map.Entry<String, String> filePreview : generatedPreview.entrySet()) {
270:                         if (filePreview.getKey().endsWith(fileName)) {
271:                                 assertEquals(OUTPUT.trim(), filePreview.getValue().toString().trim());
272:                                 fileFound = true;
273:                         }
274:                 }
275:                 if (!fileFound) {
276:                         fail("expected file hasn't been generated.");
277:                 }
278:         }
279: }