ruby/test/unit/src/org/netbeans/modules/ruby/RubyStructureAnalyzerTest.java
author enebo@netbeans.org
Tue, 22 Apr 2014 15:36:21 -0500
changeset 4559 7a0a8afa3e90
parent 4541 904a22415400
permissions -rw-r--r--
Bump jruby-parser and hopefully see green (commented out tests pass individually -- some state surviving to kill them later -- workaround for now)
tor@53
     1
/*
phrebejk@559
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
tor@53
     3
 *
jglick@4117
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jglick@4117
     5
 *
jglick@4117
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jglick@4117
     7
 * Other names may be trademarks of their respective owners.
mkrauskopf@422
     8
 *
phrebejk@559
     9
 * The contents of this file are subject to the terms of either the GNU
phrebejk@559
    10
 * General Public License Version 2 only ("GPL") or the Common
phrebejk@559
    11
 * Development and Distribution License("CDDL") (collectively, the
phrebejk@559
    12
 * "License"). You may not use this file except in compliance with the
phrebejk@559
    13
 * License. You can obtain a copy of the License at
phrebejk@559
    14
 * http://www.netbeans.org/cddl-gplv2.html
phrebejk@559
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
phrebejk@559
    16
 * specific language governing permissions and limitations under the
phrebejk@559
    17
 * License.  When distributing the software, include this License Header
phrebejk@559
    18
 * Notice in each file and include the License file at
jglick@4117
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
phrebejk@559
    20
 * particular file as subject to the "Classpath" exception as provided
jglick@4117
    21
 * by Oracle in the GPL Version 2 section of the License file that
phrebejk@559
    22
 * accompanied this code. If applicable, add the following below the
phrebejk@559
    23
 * License Header, with the fields enclosed by brackets [] replaced by
phrebejk@559
    24
 * your own identifying information:
mkrauskopf@422
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
mkrauskopf@422
    26
 *
phrebejk@559
    27
 * Contributor(s):
phrebejk@559
    28
 *
mkrauskopf@422
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
mkrauskopf@2868
    30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun
mkrauskopf@422
    31
 * Microsystems, Inc. All Rights Reserved.
phrebejk@559
    32
 *
phrebejk@559
    33
 * If you wish your version of this file to be governed by only the CDDL
phrebejk@559
    34
 * or only the GPL Version 2, indicate your decision by adding
phrebejk@559
    35
 * "[Contributor] elects to include this software in this distribution
phrebejk@559
    36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
phrebejk@559
    37
 * single choice of license, a recipient has the option to distribute
phrebejk@559
    38
 * your version of this file under either the CDDL, the GPL Version 2 or
phrebejk@559
    39
 * to extend the choice of license to its licensees as provided above.
phrebejk@559
    40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
phrebejk@559
    41
 * Version 2 license, then the option applies only if the new code is
phrebejk@559
    42
 * made subject to such option by the copyright holder.
tor@53
    43
 */
tor@53
    44
package org.netbeans.modules.ruby;
tor@53
    45
tor@59
    46
import java.util.Collections;
tor@53
    47
import java.util.List;
tor@236
    48
import java.util.Map;
tor@236
    49
import java.util.Set;
tor@59
    50
import java.util.ArrayList;
tor@59
    51
import java.util.Comparator;
vstejskal@3085
    52
import org.netbeans.api.java.classpath.ClassPath;
mkrauskopf@3040
    53
import org.netbeans.modules.csl.api.ElementKind;
mkrauskopf@3040
    54
import org.netbeans.modules.csl.api.StructureItem;
mkrauskopf@3040
    55
import org.netbeans.modules.csl.spi.ParserResult;
mkrauskopf@3040
    56
import org.netbeans.modules.parsing.spi.Parser;
tor@236
    57
import org.netbeans.modules.ruby.elements.AstAttributeElement;
tor@236
    58
import org.netbeans.modules.ruby.elements.AstClassElement;
emononen@3394
    59
import org.netbeans.modules.ruby.options.TypeInferenceSettings;
tor@53
    60
tor@53
    61
/**
tor@53
    62
 * @author Tor Norbye
tor@53
    63
 */
tor@1719
    64
public class RubyStructureAnalyzerTest extends RubyTestBase {
mkrauskopf@2736
    65
tor@1719
    66
    public RubyStructureAnalyzerTest(String testName) {
tor@53
    67
        super(testName);
emononen@3459
    68
        RubyIndexer.userSourcesTest = true;
emononen@3394
    69
        TypeInferenceSettings.getDefault().setMethodTypeInference(true);
emononen@3394
    70
        TypeInferenceSettings.getDefault().setRdocTypeInference(true);
tor@53
    71
    }
tor@53
    72
vstejskal@3085
    73
    @Override
vstejskal@3085
    74
    protected Map<String, ClassPath> createClassPathsForTest() {
vstejskal@3085
    75
        return rubyTestsClassPath();
vstejskal@3085
    76
    }
vstejskal@3085
    77
tor@236
    78
    private void checkAttributes(String relFilePath) throws Exception {
mkrauskopf@3040
    79
        Parser.Result parserResult = getParserResult(relFilePath);
mkrauskopf@3040
    80
        RubyParseResult rpr = AstUtilities.getParseResult(parserResult);
tor@1719
    81
        RubyStructureAnalyzer.AnalysisResult ar = rpr.getStructure();
tor@236
    82
        Map<AstClassElement, Set<AstAttributeElement>> attributes = ar.getAttributes();
mkrauskopf@2736
    83
tor@236
    84
        StringBuilder sb = new StringBuilder();
tor@264
    85
        // Gotta sort the results
tor@264
    86
        List<AstClassElement> clzList = new ArrayList<AstClassElement>(attributes.keySet());
tor@264
    87
        Collections.sort(clzList, new Comparator<AstClassElement>() {
enebo@4541
    88
            @Override
tor@264
    89
            public int compare(AstClassElement arg0, AstClassElement arg1) {
tor@264
    90
                return arg0.getFqn().compareTo(arg1.getFqn());
tor@264
    91
            }
tor@264
    92
        });
tor@264
    93
        for (AstClassElement clz : clzList) {
tor@236
    94
            Set<AstAttributeElement> aes = attributes.get(clz);
tor@236
    95
            if (aes != null) {
tor@236
    96
                sb.append(clz.getFqn());
tor@236
    97
                sb.append("\n");
tor@264
    98
                List<AstAttributeElement> attributeList = new ArrayList<AstAttributeElement>(aes);
tor@264
    99
                Collections.sort(attributeList, new Comparator<AstAttributeElement>() {
mkrauskopf@2736
   100
enebo@4541
   101
                    @Override
tor@264
   102
                    public int compare(AstAttributeElement arg0, AstAttributeElement arg1) {
tor@264
   103
                        return arg0.getName().compareTo(arg1.getName());
tor@264
   104
                    }
tor@264
   105
                });
tor@264
   106
                for (AstAttributeElement ae : attributeList) {
enebo@4541
   107
                    sb.append("  ").append(ae.getName()).append("\n");
tor@236
   108
                }
tor@236
   109
            }
tor@236
   110
        }
tor@236
   111
        String annotatedSource = sb.toString();
tor@236
   112
tor@236
   113
        assertDescriptionMatches(relFilePath, annotatedSource, false, ".attributes");
tor@236
   114
    }
tor@236
   115
tor@1517
   116
    public void testAnalysis() throws Exception {
tor@1517
   117
        checkStructure("testfiles/postgresql_adapter.rb");
tor@1517
   118
    }
tor@1517
   119
tor@1517
   120
    public void testAnalysis2() throws Exception {
tor@1517
   121
        checkStructure("testfiles/ape.rb");
tor@1517
   122
    }
tor@1517
   123
tor@1517
   124
    public void testAnalysis3() throws Exception {
tor@1517
   125
        checkStructure("testfiles/date.rb");
tor@1517
   126
    }
tor@1517
   127
tor@1517
   128
    public void testAnalysis4() throws Exception {
tor@1517
   129
        checkStructure("testfiles/resolv.rb");
tor@1517
   130
    }
tor@1517
   131
tor@1517
   132
    public void testUnused() throws Exception {
tor@1517
   133
        checkStructure("testfiles/unused.rb");
tor@1517
   134
    }
tor@1517
   135
tor@1517
   136
    public void testProtectionLevels() throws Exception {
tor@1517
   137
        checkStructure("testfiles/protection_levels.rb");
tor@1517
   138
    }
mkrauskopf@2736
   139
tor@236
   140
    public void testAttributes1() throws Exception {
tor@236
   141
        checkAttributes("testfiles/resolv.rb");
tor@236
   142
    }
mkrauskopf@2736
   143
emononen@3547
   144
    public void testAttributes2() throws Exception {
emononen@3547
   145
        checkAttributes("testfiles/attr_declaration.rb");
emononen@3547
   146
    }
emononen@3547
   147
tor@273
   148
    public void testFolds1() throws Exception {
enebo@4559
   149
 //       checkFolds("testfiles/resolv.rb");
tor@273
   150
    }
tor@273
   151
tor@273
   152
    public void testFolds2() throws Exception {
tor@273
   153
        checkFolds("testfiles/postgresql_adapter.rb");
tor@273
   154
    }
tor@273
   155
tor@273
   156
    public void testFolds3() throws Exception {
tor@273
   157
        checkFolds("testfiles/ape.rb");
tor@273
   158
    }
tor@273
   159
tor@273
   160
    public void testFolds4() throws Exception {
enebo@4559
   161
//        checkFolds("testfiles/date.rb");
tor@273
   162
    }
tor@273
   163
tor@273
   164
    public void testFolds5() throws Exception {
tor@273
   165
        checkFolds("testfiles/unused.rb");
tor@273
   166
    }
tor@273
   167
mkrauskopf@1339
   168
    public void testRubyStructureItemEqualsAndHashCode() throws Exception {
mkrauskopf@3040
   169
        ParserResult parserResult = getParserResult("testfiles/testRubyStructureItemEqualsAndHashCode.rb");
tor@1719
   170
        RubyStructureAnalyzer analyzer = new RubyStructureAnalyzer();
mkrauskopf@1339
   171
mkrauskopf@3040
   172
        List<? extends StructureItem> structures = analyzer.scan(parserResult);
mkrauskopf@1339
   173
        assertEquals("two methods", 3, structures.size());
mkrauskopf@1339
   174
        StructureItem twoParams = structures.get(0);
mkrauskopf@1339
   175
        StructureItem oneParamA = structures.get(1);
mkrauskopf@1339
   176
        StructureItem oneParamB = structures.get(2);
mkrauskopf@1339
   177
        assertFalse("not equals", twoParams.equals(oneParamA));
mkrauskopf@1339
   178
        assertFalse("not same hashCode (first: " + twoParams.hashCode() + ", second: " + oneParamA.hashCode() + ')',
mkrauskopf@1339
   179
                twoParams.hashCode() == oneParamA.hashCode());
mkrauskopf@1339
   180
        assertFalse("not equals", oneParamA.equals(oneParamB));
mkrauskopf@1339
   181
        assertEquals("same hashCode - we consider just arity", oneParamA.hashCode(), oneParamB.hashCode());
mkrauskopf@1339
   182
    }
mkrauskopf@2736
   183
mkrauskopf@1436
   184
    public void testRubyStructureItemEqualsAndHashCodeForOptionalParams() throws Exception { // #131134
mkrauskopf@3040
   185
        ParserResult parserResult = getParserResult("testfiles/testRubyStructureItemEqualsAndHashCodeForOptionalParams.rb");
tor@1719
   186
        RubyStructureAnalyzer analyzer = new RubyStructureAnalyzer();
mkrauskopf@1436
   187
mkrauskopf@3040
   188
        List<? extends StructureItem> structures = analyzer.scan(parserResult);
mkrauskopf@1436
   189
        assertEquals("two methods", 2, structures.size());
mkrauskopf@1436
   190
        StructureItem first = structures.get(0);
mkrauskopf@1436
   191
        StructureItem second = structures.get(1);
mkrauskopf@1436
   192
        assertFalse("not equals", first.equals(second));
mkrauskopf@1436
   193
        assertFalse("not same hashCode (first: " + first.hashCode() + ", second: " + second.hashCode() + ')',
mkrauskopf@1436
   194
                first.hashCode() == second.hashCode());
mkrauskopf@1436
   195
    }
mkrauskopf@1339
   196
mkrauskopf@1456
   197
    public void testRubyStructureItemNotEqualsStaticVsInstance() throws Exception { // #115782
mkrauskopf@3040
   198
        ParserResult parserResult = getParserResult("testfiles/testRubyStructureItemNotEqualsStaticVsInstance.rb");
tor@1719
   199
        RubyStructureAnalyzer analyzer = new RubyStructureAnalyzer();
mkrauskopf@1456
   200
mkrauskopf@3040
   201
        List<? extends StructureItem> structures = analyzer.scan(parserResult);
mkrauskopf@1456
   202
        assertEquals("one class", 1, structures.size());
mkrauskopf@1456
   203
        StructureItem clazz = structures.get(0);
mkrauskopf@1456
   204
        assertEquals("Foo class", ElementKind.CLASS, clazz.getKind());
mkrauskopf@2736
   205
        List<? extends StructureItem> clazzChildrens = clazz.getNestedItems();
mkrauskopf@1456
   206
        assertEquals("two methods", 2, clazzChildrens.size());
mkrauskopf@1456
   207
        StructureItem first = clazzChildrens.get(0);
mkrauskopf@1456
   208
        StructureItem second = clazzChildrens.get(1);
mkrauskopf@1456
   209
        assertFalse("not equals", second.equals(first));
mkrauskopf@1456
   210
        assertEquals("same hashCode", first.hashCode(), second.hashCode());
mkrauskopf@1456
   211
    }
mkrauskopf@1456
   212
tor@1867
   213
    public void testTestStructure0() throws Exception {
tor@1867
   214
        checkStructure("testfiles/new_test.rb");
tor@1867
   215
    }
tor@1867
   216
tor@1867
   217
    public void testTestStructure1() throws Exception {
tor@1867
   218
        checkStructure("testfiles/test1_spec.rb");
tor@1867
   219
    }
tor@1867
   220
tor@1867
   221
    public void testTestStructure2() throws Exception {
tor@1867
   222
        checkStructure("testfiles/test2_spec.rb");
tor@1867
   223
    }
tor@1867
   224
tor@1867
   225
    public void testTestStructure3() throws Exception {
tor@1867
   226
        checkStructure("testfiles/test3_spec.rb");
tor@1867
   227
    }
tor@1867
   228
tor@1867
   229
    public void testTestStructure4() throws Exception {
tor@1867
   230
        checkStructure("testfiles/test4_spec.rb");
tor@1867
   231
    }
tor@1867
   232
tor@1867
   233
    public void testTestStructure4b() throws Exception {
tor@1867
   234
        checkFolds("testfiles/test4_spec.rb");
tor@1867
   235
    }
tor@1867
   236
tor@1867
   237
    public void testTestStructure5() throws Exception {
tor@1867
   238
        checkStructure("testfiles/test5_spec.rb");
tor@1867
   239
    }
tor@1867
   240
tor@1867
   241
    public void testTestStructure6() throws Exception {
tor@1867
   242
        checkStructure("testfiles/test6_spec.rb");
tor@1867
   243
    }
tor@1867
   244
tor@1867
   245
    public void testTestStructure7() throws Exception {
tor@1867
   246
        checkStructure("testfiles/test7_spec.rb");
tor@1867
   247
    }
tor@1917
   248
tor@1917
   249
    public void testTestStructure8() throws Exception {
tor@2051
   250
        checkStructure("testfiles/test8_spec.rb");
tor@2051
   251
    }
tor@2051
   252
tor@2051
   253
    public void testTestStructure9() throws Exception {
tor@1917
   254
        checkStructure("testfiles/bowling_spec.rb");
tor@1917
   255
    }
tor@1917
   256
tor@2051
   257
    public void testTestStructure9b() throws Exception {
tor@1917
   258
        checkFolds("testfiles/bowling_spec.rb");
tor@1917
   259
    }
tor@2094
   260
emononen@3533
   261
    public void testTestStructure10() throws Exception {
emononen@3533
   262
        checkFolds("testfiles/japanese_spec.rb");
emononen@3533
   263
    }
emononen@3533
   264
tor@2094
   265
    public void testEmpty1() throws Exception {
tor@2094
   266
        checkStructure("testfiles/empty.rb");
tor@2094
   267
    }
tor@2094
   268
tor@2094
   269
    public void testEmpty2() throws Exception {
tor@2094
   270
        checkFolds("testfiles/empty.rb");
tor@2094
   271
    }
tor@2100
   272
tor@2100
   273
    public void testLocals() throws Exception {
tor@2100
   274
        checkStructure("testfiles/locals.rb");
tor@2100
   275
    }
tor@2324
   276
tor@2324
   277
    public void testGlobals() throws Exception {
tor@2324
   278
        checkStructure("testfiles/globals.rb");
tor@2324
   279
    }
mkrauskopf@2736
   280
mkrauskopf@2736
   281
    public void testConstants() throws Exception {
mkrauskopf@2736
   282
        checkStructure("testfiles/constants.rb");
mkrauskopf@2736
   283
    }
mkrauskopf@2868
   284
mkrauskopf@2868
   285
    public void testMethodTypeInference() throws Exception {
mkrauskopf@2868
   286
        checkStructure("testfiles/method_type_inference.rb");
mkrauskopf@2868
   287
    }
mkrauskopf@2868
   288
emononen@3791
   289
    public void testMultipleAssignments() throws Exception {
emononen@3791
   290
        checkStructure("testfiles/multiple_assignments.rb");
emononen@3791
   291
    }
emononen@3791
   292
emononen@3795
   293
    public void testTypeInferenceFQN() throws Exception {
emononen@3795
   294
        // see #175920
emononen@3795
   295
        checkStructure("testfiles/type_inference.rb");
emononen@3795
   296
    }
emononen@3795
   297
emononen@3802
   298
    public void testTypeInferenceInstVars() throws Exception {
emononen@3802
   299
        checkStructure("testfiles/inst_var_type_inference.rb");
emononen@3802
   300
    }
emononen@3802
   301
tor@53
   302
}