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              Exception cause = e.getUnit().getException(0);
41              if( cause instanceof RuntimeParserException ) {
42                  return;
43              }
44              throw e;
45          }
46          fail("Should have caught a RuntimeParserException");
47      }
48  
49  }