<commit_before>/*
 * Created on 19/07/2005
 */
package com.python.pydev.analysis;

import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_DUPLICATED_SIGNATURE;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNUSED_IMPORT;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNUSED_VARIABLE;

import java.io.File;
import java.io.FileNotFoundException;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.Document;
import org.python.pydev.core.REF;
import org.python.pydev.core.TestDependent;
import org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule;
import org.python.pydev.editor.codecompletion.revisited.modules.SourceModule;

import com.python.pydev.analysis.messages.IMessage;

public class OcurrencesAnalyzerTest extends AnalysisTestsBase { 

    public static void main(String[] args) {
        try {
            OcurrencesAnalyzerTest analyzer2 = new OcurrencesAnalyzerTest();
            analyzer2.setUp();
            analyzer2.testFromFutureImport();
            analyzer2.tearDown();
            System.out.println("finished");
            
            junit.textui.TestRunner.run(OcurrencesAnalyzerTest.class);
            System.out.println("finished all");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void testUnusedImports(){
            
        prefs.severityForUnusedImport = IMarker.SEVERITY_ERROR;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_ERROR;
        doc = new Document("import testlib\n");
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(1, msgs.length);
        assertEquals("Unused import: testlib", msgs[0].getMessage());
        assertEquals(IMarker.SEVERITY_ERROR, msgs[0].getSeverity());
        assertEquals(TYPE_UNUSED_IMPORT, msgs[0].getType());

        //-----------------
        doc = new Document("import testlib\nprint testlib");
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(0, msgs.length);
        
        //-----------------
        sDoc = "from testlib.unittest import *";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs, 1);
        assertEquals(IMarker.SEVERITY_ERROR, msgs[0].getSeverity());
        assertEquals(6, msgs[0].getStartCol(doc));
        assertEquals(31, msgs[0].getEndCol(doc));
        assertEquals("Unused in wild import: anothertest, guitestcase, main, TestCase, AnotherTest, t, TestCaseAlias, GUITest, testcase", msgs[0].getMessage());

        
        //-----------------
        prefs.severityForUnusedImport = IMarker.SEVERITY_WARNING;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_WARNING;
        sDoc = "from testlib.unittest import *\nprint TestCase";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(1, msgs.length);
        assertEquals(IMarker.SEVERITY_WARNING, msgs[0].getSeverity());
        assertEquals("Unused in wild import: anothertest, guitestcase, main, AnotherTest, t, TestCaseAlias, GUITest, testcase", msgs[0].getMessage());
        assertEquals("TestCase", msgs[0].getAdditionalInfo().get(0));
        
        //-----------------
        prefs.severityForUnusedImport = IMarker.SEVERITY_INFO;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_INFO;

        sDoc = "from testlib.unittest import *\nprint TestCase\nprint testcase";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        //even in ignore mode, we get the message
        assertEquals(1, msgs.length);
        assertEquals("Unused in wild import: anothertest, guitestcase, main, AnotherTest, t, TestCaseAlias, GUITest", msgs[0].getMessage());
        assertEquals("TestCase", msgs[0].getAdditionalInfo().get(0));
        assertEquals("testcase", msgs[0].getAdditionalInfo().get(1));
    }
    
    public void testFromFutureImport(){
        doc = new Document(
                "from __future__ import generators\n"
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,0);
        
    }
    
    public void testClassVar(){
        doc = new Document(
            "class Foo(object):\n"+
            "    x = 1\n"+
            "    def m1(self):\n"+
            "        print x\n" //should access with self.x or Foo.x
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,1);
        
    }
    
    public void testClsInNew(){
        doc = new Document(
            "class C2:\n"+
            "    def __new__(cls):\n"+
            "        print cls\n"+
            ""
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,0);
        
    }
    
    public void testClsInsteadOfSelf(){
        doc = new Document(
                "class C2:\n"+
                "    def foo(cls):\n"+
                "        print cls\n"+
                ""
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,1);
        assertContainsMsg("Method 'foo' should have self as first parameter", msgs);
        
    }

    public void testConsiderAsGlobals(){
    	doc = new Document(
			"print considerGlobal"
    	);
    	analyzer = new OcurrencesAnalyzer();
    	msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
    	
    	printMessages(msgs,0);
    	
    }
    public void testUnusedImports2(){
        
        doc = new Document(
            "from simpleimport import *\n" +
            "print xml"
        );
        analyzer = new OcurrencesAnalyzer();<commit_msg>*** empty log message ***<commit_after>/*
 * Created on 19/07/2005
 */
package com.python.pydev.analysis;

import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_DUPLICATED_SIGNATURE;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNUSED_IMPORT;
import static com.python.pydev.analysis.IAnalysisPreferences.TYPE_UNUSED_VARIABLE;

import java.io.File;
import java.io.FileNotFoundException;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.Document;
import org.python.pydev.core.REF;
import org.python.pydev.core.TestDependent;
import org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule;
import org.python.pydev.editor.codecompletion.revisited.modules.SourceModule;

import com.python.pydev.analysis.messages.IMessage;

public class OcurrencesAnalyzerTest extends AnalysisTestsBase { 

    public static void main(String[] args) {
        try {
            OcurrencesAnalyzerTest analyzer2 = new OcurrencesAnalyzerTest();
            analyzer2.setUp();
            analyzer2.testFromFutureImport();
            analyzer2.tearDown();
            System.out.println("finished");
            
            junit.textui.TestRunner.run(OcurrencesAnalyzerTest.class);
            System.out.println("finished all");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void testUnusedImports(){
            
        prefs.severityForUnusedImport = IMarker.SEVERITY_ERROR;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_ERROR;
        doc = new Document("import testlib\n");
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(1, msgs.length);
        assertEquals("Unused import: testlib", msgs[0].getMessage());
        assertEquals(IMarker.SEVERITY_ERROR, msgs[0].getSeverity());
        assertEquals(TYPE_UNUSED_IMPORT, msgs[0].getType());

        //-----------------
        doc = new Document("import testlib\nprint testlib");
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(0, msgs.length);
        
        //-----------------
        sDoc = "from testlib.unittest import *";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs, 1);
        assertEquals(IMarker.SEVERITY_ERROR, msgs[0].getSeverity());
        assertEquals(6, msgs[0].getStartCol(doc));
        assertEquals(31, msgs[0].getEndCol(doc));
        assertEquals("Unused in wild import: anothertest, guitestcase, main, TestCase, AnotherTest, t, TestCaseAlias, GUITest, testcase", msgs[0].getMessage());

        
        //-----------------
        prefs.severityForUnusedImport = IMarker.SEVERITY_WARNING;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_WARNING;
        sDoc = "from testlib.unittest import *\nprint TestCase";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        assertEquals(1, msgs.length);
        assertEquals(IMarker.SEVERITY_WARNING, msgs[0].getSeverity());
        assertEquals("Unused in wild import: anothertest, guitestcase, main, AnotherTest, t, TestCaseAlias, GUITest, testcase", msgs[0].getMessage());
        assertEquals("TestCase", msgs[0].getAdditionalInfo().get(0));
        
        //-----------------
        prefs.severityForUnusedImport = IMarker.SEVERITY_INFO;
        prefs.severityForUnusedWildImport = IMarker.SEVERITY_INFO;

        sDoc = "from testlib.unittest import *\nprint TestCase\nprint testcase";
        doc = new Document(sDoc);
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        //even in ignore mode, we get the message
        assertEquals(1, msgs.length);
        assertEquals("Unused in wild import: anothertest, guitestcase, main, AnotherTest, t, TestCaseAlias, GUITest", msgs[0].getMessage());
        assertEquals("TestCase", msgs[0].getAdditionalInfo().get(0));
        assertEquals("testcase", msgs[0].getAdditionalInfo().get(1));
    }
    
    public void testFromFutureImport(){
        doc = new Document(
                "from __future__ import generators\n"
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,0);
        
    }
    
    public void testClassVar(){
        doc = new Document(
            "class Foo(object):\n"+
            "    x = 1\n"+
            "    def m1(self):\n"+
            "        print x\n" //should access with self.x or Foo.x
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,1);
        
    }
    
    public void testClsInNew(){
        doc = new Document(
            "class C2:\n"+
            "    def __new__(cls):\n"+
            "        print cls\n"+
            ""
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,0);
        
    }
    
    public void testClsInsteadOfSelf(){
        doc = new Document(
                "class C2:\n" +
                "    @str\n"+
                "    def foo(cls):\n"+
                "        print cls\n"+
                ""
        );
        analyzer = new OcurrencesAnalyzer();
        msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
        
        printMessages(msgs,1);
        assertContainsMsg("Method 'foo' should have self as first parameter", msgs);
        assertEquals(9, msgs[0].getStartCol(doc));
        assertEquals(3, msgs[0].getStartLine(doc));
        
    }

    public void testConsiderAsGlobals(){
    	doc = new Document(
			"print considerGlobal"
    	);
    	analyzer = new OcurrencesAnalyzer();
    	msgs = analyzer.analyzeDocument(nature, (SourceModule) AbstractModule.createModuleFromDoc(null, null, doc, nature, 0), prefs, doc);
    	
    	printMessages(msgs,0);
    	
    }
    public void testUnusedImports2(){
        
        doc = new Document(
            "from simpleimport import *\n" +
            "print xml"
        );
        analyzer = new OcurrencesAnalyzer();
{'fraction_non_alphanumeric': 0.12048611111111111, 'fraction_numerical': 0.008194444444444445, 'mean_word_length': 2.6110832497492478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6082047', 'n_tokens_mistral': 4330, 'n_tokens_neox': 4152, 'n_words': 834}