1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.syntax;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.millscript.expr.Expr;
25  
26  /**
27   * Tests for function header parsing.
28   */
29  public class FunctionHeaderTest extends AbstractSyntaxTestCase {
30  
31      /**
32       * Constructor for FunctionHeaderTest.
33       * @param arg0
34       */
35      public FunctionHeaderTest(String arg0) {
36          super(arg0);
37      }
38  
39      /**
40       * Tests function header parsing with no function name.
41       */
42      public void testFunctionNoName() {
43          final Parser p = getStringParser( "( x ) =>" );
44          final Expr e = p.readExprTo( "=>" );
45          // There should be no problem here, so we don't check
46          new FunctionHeader().flatten( e, false );
47          try {
48              // We are expecting a problem here
49              new FunctionHeader().flatten( e, true );
50          } catch ( Alert ex ) {
51              assertEquals( "Invalid procedure definition", ex.getComplaint() );
52          }
53      }
54  
55      /**
56       * Tests function header parsing with an invalid function name.
57       */
58      public void testFunctionWithInvalidArgument() {
59          final Parser p = getStringParser( "f( x, `not an arg` ) =>" );
60          final Expr e = p.readExprTo( "=>" );
61          try {
62              // We are expecting a problem here
63              new FunctionHeader().flatten( e, true );
64          } catch ( Alert ex ) {
65              assertEquals( "Invalid procedure definition", ex.getComplaint() );
66          }
67      }
68  
69      /**
70       * Tests function header parsing with an invalid function name.
71       */
72      public void testFunctionWithInvalidName() {
73          final Parser p = getStringParser( "`not a function`( x ) =>" );
74          final Expr e = p.readExprTo( "=>" );
75          try {
76              // We are expecting a problem here
77              new FunctionHeader().flatten( e, true );
78          } catch ( Alert ex ) {
79              assertEquals( "Invalid procedure definition", ex.getComplaint() );
80          }
81      }
82  
83      /**
84       * Tests function header parsing with a function name.
85       */
86      public void testFunctionWithName() {
87          final Parser p = getStringParser( "f( x ) =>" );
88          final Expr e = p.readExprTo( "=>" );
89          // There should be no problem here, so we don't check
90          new FunctionHeader().flatten( e, true );
91          // There should be no problem here either, so we don't check
92          new FunctionHeader().flatten( e, false );
93      }
94  
95  }