1 package org.codehaus.groovy.syntax.parser;
2
3 import java.io.ByteArrayInputStream;
4
5 import org.codehaus.groovy.classgen.TestSupport;
6 import org.codehaus.groovy.control.CompilationFailedException;
7
8 /***
9 * Tests that void/value return mismatches can be detected.
10 * @author Steve Goetze
11 */
12 public class ReturnTypeErrorTest extends TestSupport {
13
14 public void testInvalidValueReturnStatement() throws Exception {
15 doCompile(
16 "class zup {\n"
17 + " void foo() {\n"
18 + " return 3;"
19 + " }\n"
20 + "}\n");
21 }
22
23 public void testInvalidValueReturnStatement2() throws Exception {
24 doCompile(
25 "class zup {\n"
26 + " void foo() {\n"
27 + " if (true) \n"
28 + " return \n"
29 + " else \n"
30 + " return 'Foo' \n"
31 + " }\n"
32 + "}\n");
33 }
34
35 protected void doCompile(String code) throws Exception {
36 try {
37 loader.parseClass(new ByteArrayInputStream(code.getBytes()), getMethodName() + ".groovy");
38 }
39 catch(CompilationFailedException e ) {
40 if (e.getCause() instanceof RuntimeParserException) {
41 return;
42 }
43 }
44
45 fail("Should have caught a RuntimeParserException");
46 }
47
48 }