ruby/test/unit/src/org/netbeans/modules/ruby/ArityTest.java
author enebo@netbeans.org
Sun, 24 Nov 2013 15:46:13 -0600
changeset 4553 32cac83e9ae7
parent 4552 77cbb671b344
permissions -rw-r--r--
hmm should have renamed this before last commit
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    31  * Microsystems, Inc. All Rights Reserved.
    32  *
    33  * If you wish your version of this file to be governed by only the CDDL
    34  * or only the GPL Version 2, indicate your decision by adding
    35  * "[Contributor] elects to include this software in this distribution
    36  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    37  * single choice of license, a recipient has the option to distribute
    38  * your version of this file under either the CDDL, the GPL Version 2 or
    39  * to extend the choice of license to its licensees as provided above.
    40  * However, if you add GPL Version 2 code and therefore, elected the GPL
    41  * Version 2 license, then the option applies only if the new code is
    42  * made subject to such option by the copyright holder.
    43  */
    44 
    45 package org.netbeans.modules.ruby;
    46 
    47 import java.util.ArrayList;
    48 import java.util.List;
    49 import org.jrubyparser.ast.Node;
    50 import org.jrubyparser.ast.NodeType;
    51 import org.jrubyparser.ast.INameNode;
    52 import org.jrubyparser.ast.MethodDefNode;
    53 
    54 /**
    55  *
    56  * @author Tor Norbye
    57  */
    58 public class ArityTest extends RubyTestBase {
    59 
    60     public ArityTest(String testName) {
    61         super(testName);
    62     }
    63 
    64     public void testArityMatches() {
    65         assertTrue(Arity.matches(Arity.createTestArity(0, 0), Arity.createTestArity(0,0)));
    66         assertTrue(Arity.matches(Arity.createTestArity(1, 1), Arity.createTestArity(1,1)));
    67         assertTrue(Arity.matches(Arity.createTestArity(3, 3), Arity.createTestArity(3,5)));
    68         assertTrue(Arity.matches(Arity.createTestArity(5, 5), Arity.createTestArity(3,5)));
    69         assertTrue(Arity.matches(Arity.createTestArity(4, 4), Arity.createTestArity(3,5)));
    70         assertTrue(Arity.matches(Arity.createTestArity(4, 4), Arity.createTestArity(3,5)));
    71         assertTrue(Arity.matches(Arity.createTestArity(4, 4), Arity.createTestArity(3,5)));
    72         assertTrue(Arity.matches(Arity.createTestArity(4, 4), Arity.createTestArity(0,Integer.MAX_VALUE)));
    73 
    74         assertFalse(Arity.matches(Arity.createTestArity(2, 2), Arity.createTestArity(1,1)));
    75         assertFalse(Arity.matches(Arity.createTestArity(2, 2), Arity.createTestArity(0,1)));
    76         assertFalse(Arity.matches(Arity.createTestArity(2, 2), Arity.createTestArity(3,4)));
    77     }
    78     
    79     
    80     private void checkCallArity(Node root, String methodName, Arity arity) {
    81         List<Node> nodes = new ArrayList<Node>();
    82         AstUtilities.addNodesByType(root, new NodeType[] { NodeType.CALLNODE, NodeType.VCALLNODE, NodeType.FCALLNODE }, nodes);
    83         for (Node n : nodes) {
    84             assertTrue(AstUtilities.isCall(n));
    85             String name = ((INameNode)n).getName();
    86             if (name.equals(methodName)) {
    87                 assertEquals(arity, Arity.getCallArity(n));
    88                 return;
    89             }
    90         }
    91         assertTrue(methodName + " not found", false);
    92     }
    93     
    94     private void checkDefArity(Node root, String methodName, Arity arity) {
    95         List<Node> nodes = new ArrayList<Node>();
    96         AstUtilities.addNodesByType(root, new NodeType[] { NodeType.DEFNNODE, NodeType.DEFSNODE }, nodes);
    97         for (Node n : nodes) {
    98             String name = ((INameNode)n).getName();
    99             if (name.equals(methodName)) {
   100                 assertEquals(arity, Arity.getArity((MethodDefNode) n));
   101                 return;
   102             }
   103         }
   104         assertTrue(methodName + " not found", false);
   105     }
   106     
   107     public void testCallArity() {
   108         Node root = getRootNode("testfiles/arity.rb");
   109         checkCallArity(root, "foo1!", Arity.createTestArity(0, 0));
   110         checkCallArity(root, "foo2", Arity.createTestArity(1, 1));
   111         checkCallArity(root, "foo3", Arity.createTestArity(2, 2));
   112         checkCallArity(root, "foo4", Arity.createTestArity(1, Integer.MAX_VALUE));
   113     }
   114 
   115     public void testCallArity2() {
   116         Node root = getRootNode("testfiles/arity.rb");
   117         checkCallArity(root, "foo5a", Arity.createTestArity(3, Integer.MAX_VALUE));
   118         checkCallArity(root, "foo5b", Arity.createTestArity(4, Integer.MAX_VALUE));
   119         checkCallArity(root, "foo5c", Arity.createTestArity(4, Integer.MAX_VALUE));
   120     }
   121     
   122     public void testDefArity() {
   123         Node root = getRootNode("testfiles/arity.rb");
   124         checkDefArity(root, "foo6", Arity.createTestArity(0, 0));
   125         checkDefArity(root, "foo7", Arity.createTestArity(1, 1));
   126         checkDefArity(root, "foo8", Arity.createTestArity(2, 2));
   127         checkDefArity(root, "foo9", Arity.createTestArity(2, 4));
   128         checkDefArity(root, "foo10", Arity.createTestArity(2, Integer.MAX_VALUE));
   129     }
   130 
   131     public void testDefArity2() {
   132         Node root = getRootNode("testfiles/arity.rb");
   133         checkDefArity(root, "foo6", Arity.createTestArity(0, 0));
   134         checkDefArity(root, "foo11", Arity.createTestArity(2, 3));
   135     }
   136 }