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.ApplyExpr;
25  import org.millscript.millscript.expr.CommaExpr;
26  import org.millscript.millscript.expr.Expr;
27  import org.millscript.millscript.expr.NameExpr;
28  
29  /**
30   * Tests dot syntax.
31   */
32  public class DotSyntaxTest extends AbstractSyntaxTestCase {
33  
34      /**
35       * Constructor for DotSyntaxTest.
36       * @param arg0
37       */
38      public DotSyntaxTest(String arg0) {
39          super(arg0);
40      }
41  
42      /**
43       * Tests for bug 1024272
44       */
45      public void testBug1024272() {
46          try {
47              // We are expecting a problem parsing this 
48              final Parser p = getStringParser( "`hello`.function<br />" );
49              // bug 1024272 fixed a problem where the expression after the
50              // function name would be read as another argument to the function
51              // So, before the bug was fixed the above code would have parsed
52              // successfully as an ApplyExpr, after the fix it should generate
53              // an exception
54              p.readExpr();
55              // Just in case 
56              fail( "Function application is invalid" );
57          } catch ( Alert ex ) {
58              assertEquals( "Unexpected symbol", ex.getComplaint() );
59          }
60          try {
61              // We are expecting a problem parsing this 
62              final Parser p = getStringParser( "`hello`.function`there`" );
63              // bug 1024272 fixed a problem where the expression after the
64              // function name would be read as another argument to the function
65              // So, before the bug was fixed the above code would have parsed
66              // successfully as an ApplyExpr, after the fix it should generate
67              // an exception
68              p.readExpr();
69              // Just in case 
70              fail( "Function application is invalid" );
71          } catch ( Alert ex ) {
72              assertEquals( "Unexpected symbol", ex.getComplaint() );
73          }
74      }
75  
76      /**
77       * Tests general function application using the dot syntax.
78       */
79      public void testGeneralApplication() {
80          // The following should be fine
81          final Parser p = getStringParser( "arg1.f( arg2 )" );
82          final Expr e = p.readExpr();
83          // We should have read an apply expression
84          assertTrue( e instanceof ApplyExpr );
85          final ApplyExpr ae = (ApplyExpr) e;
86          // Check the type of the function and argument expressions
87          assertTrue( ae.getFun() instanceof NameExpr );
88          assertTrue( ae.getArgs() instanceof CommaExpr );
89          final NameExpr fne = (NameExpr) ae.getFun();
90          final CommaExpr fae = (CommaExpr) ae.getArgs();
91          // Check the functions name
92          assertEquals( "f", fne.getName() );
93          // Check the functions arguments
94          assertTrue( fae.getLeft() instanceof NameExpr );
95          assertTrue( fae.getRight() instanceof NameExpr );
96          final NameExpr left = (NameExpr) fae.getLeft();
97          final NameExpr right = (NameExpr) fae.getRight();
98          assertEquals( "arg1", left.getName() );
99          assertEquals( "arg2", right.getName() );
100     }
101 
102 }