<commit_before>package scala.tools.eclipse
package structurebuilder


import org.junit._
import Assert._
import scala.tools.eclipse.testsetup.SDTTestUtils
import org.eclipse.jdt.core._
import org.eclipse.core.runtime.Path
import org.eclipse.jdt.core.search._
import scala.collection.mutable.ListBuffer

object StructureBuilderTest {
  lazy val project = SDTTestUtils.setupProject("simple-structure-builder")
  lazy val srcPackageRoot: IPackageFragmentRoot = {
    val javaProject = JavaCore.create(project.underlying)
    
    javaProject.open(null)
    javaProject.findPackageFragmentRoot(new Path("/simple-structure-builder/src"))
  }
  
  Assert.assertNotNull(srcPackageRoot)
    
  srcPackageRoot.open(null)
  println("children: " + srcPackageRoot.getChildren.toList)
}

class StructureBuilderTest {
  import StructureBuilderTest._
  
  def setupWorkspace {
    // auto-building is off
    val desc = SDTTestUtils.workspace.getDescription
    desc.setAutoBuilding(false)
    SDTTestUtils.workspace.setDescription(desc)
    ScalaPlugin
  }
  /** Return the toString output for all compilation units in the given fragment. */
  def compilationUnitsStructure(frag: IPackageFragment): String = {
    val buf = new StringBuilder
    frag.open(null)
    frag.getCompilationUnits.foreach { cu =>
      cu.open(null)
      buf.append(cu)
      buf.append('\n')
    }
    buf.toString
  }
  
  @Test def testAnnotations() {
    val annotsPkg = srcPackageRoot.getPackageFragment("annots");
    assertNotNull(annotsPkg)
    val cu = annotsPkg.getCompilationUnit("ScalaTestSuite.scala")
    assertTrue(cu.exists)
    val tpe = cu.findPrimaryType()
    val m1 = tpe.getMethod("someTestMethod", Array())
    val m2 = tpe.getMethod("anotherTestMethod", Array())
    println(m1.getAnnotations.toList)
    println(m2.getAnnotations.toList)
    
    assertTrue(m1.getAnnotations.length == 1)
    assertTrue(m1.getAnnotation("Test").exists)
    assertTrue(m2.getAnnotations.length == 1)
    assertTrue(m2.getAnnotation("Test").exists)
  }
  
  @Test def testSearchIndexAnnotations() {
    import IJavaSearchConstants._
    val pattern = SearchPattern.createPattern("org.junit.Test", TYPE, ANNOTATION_TYPE_REFERENCE, SearchPattern.R_PREFIX_MATCH)
    val scope = SearchEngine.createJavaSearchScope(Array(srcPackageRoot.getPackageFragment("annots"): IJavaElement))
    
    var elems = Set[IMethod]()
    
    val requestor = new SearchRequestor {
      def acceptSearchMatch(m: SearchMatch) {
        m.getElement match {
          case method: IMethod => elems += method
          case elem => 
            println(elem)
            fail
        }
      }
    }
    
    (new SearchEngine).search(pattern, Array[SearchParticipant](SearchEngine.getDefaultSearchParticipant), scope, requestor, null)
    println(elems)
    assertEquals(elems.size, 2)
  }

  /** Test the structure as seen by the JDT. Use the JDT API to 
   *  retrieve the package `traits' and compare the toString output.
   */
  @Test def testStructure() {
    val fragment = srcPackageRoot.getPackageFragment("traits")
    assertNotNull(fragment)
    assertEquals(expectedFragment, compilationUnitsStructure(fragment).trim)
  }
  
  lazy val expectedFragment = """
T1.scala [in traits [in src [in simple-structure-builder]]]
  package traits
  interface T1
    scala.Nothing notImplemented()
    java.lang.Object t
    java.lang.Object t()
    scala.collection.immutable.List xs
    scala.collection.immutable.List xs()
    scala.Nothing m(int, int)
    java.lang.Object n(int, int, int)
    java.lang.Object T
    java.lang.Object U
    java.lang.Object Z
    class Inner
      Inner()
C1.scala [in traits [in src [in simple-structure-builder]]]
  package traits
  import scala.annotation.*
  import scala.reflect.BeanProperty
  import org.junit.Test
  class C
    int _x
    traits.C.T _y
    C(int, traits.C.T)
    C(traits.C.T)
    int x
    int x()
    int lz
    int lz()
    java.lang.String v
    java.lang.String v()
    void v_$eq(java.lang.String)
    java.lang.Object volVar
    java.lang.Object volVar()
    void volVar_$eq(java.lang.Object)
    java.lang.String CONSTANT
    java.lang.String CONSTANT()
    int beanVal
    int beanVal()
    int getBeanVal()
    int beanVar
    int beanVar()
    void beanVar_$eq(int)
    int getBeanVar()
    void setBeanVar(int)
    boolean nullaryMethod()
    void method()
    void annotatedMethod()
    java.lang.Object curriedMethod(int, int)
    boolean nullaryMethod1()
    void method1()
    void method2()
    java.lang.Object curriedMethod1(int, int)
    class InnerC
      int x
      int x()
      InnerC(int)
    java.lang.Object T
    java.lang.Object U
    java.lang.Object map(scala.Function1)
    scala.Null takeArray(scala.Array)
    scala.Null takeArray2(scala.Array)
    java.lang.Object localClass(int)
      class class Object
        $anon()
        void run()
    int localMethod(int)
    long localVals(int)""".trim
}<commit_msg>Shut down compiler after test is done (saves memory during testing).<commit_after>package scala.tools.eclipse
package structurebuilder


import org.junit._
import Assert._
import scala.tools.eclipse.testsetup.SDTTestUtils
import org.eclipse.jdt.core._
import org.eclipse.core.runtime.Path
import org.eclipse.jdt.core.search._
import scala.collection.mutable.ListBuffer

object StructureBuilderTest {
  lazy val project = SDTTestUtils.setupProject("simple-structure-builder")
  lazy val srcPackageRoot: IPackageFragmentRoot = {
    val javaProject = JavaCore.create(project.underlying)
    
    javaProject.open(null)
    javaProject.findPackageFragmentRoot(new Path("/simple-structure-builder/src"))
  }
  
  Assert.assertNotNull(srcPackageRoot)
    
  srcPackageRoot.open(null)
  println("children: " + srcPackageRoot.getChildren.toList)
  
  @AfterClass def shutdown {
    project.resetCompilers
  }
}

class StructureBuilderTest {
  import StructureBuilderTest._
  
  def setupWorkspace {
    // auto-building is off
    val desc = SDTTestUtils.workspace.getDescription
    desc.setAutoBuilding(false)
    SDTTestUtils.workspace.setDescription(desc)
  }
  
  /** Return the toString output for all compilation units in the given fragment. */
  def compilationUnitsStructure(frag: IPackageFragment): String = {
    val buf = new StringBuilder
    frag.open(null)
    frag.getCompilationUnits.foreach { cu =>
      cu.open(null)
      buf.append(cu)
      buf.append('\n')
    }
    buf.toString
  }
  
  @Test def testAnnotations() {
    val annotsPkg = srcPackageRoot.getPackageFragment("annots");
    assertNotNull(annotsPkg)
    val cu = annotsPkg.getCompilationUnit("ScalaTestSuite.scala")
    assertTrue(cu.exists)
    val tpe = cu.findPrimaryType()
    val m1 = tpe.getMethod("someTestMethod", Array())
    val m2 = tpe.getMethod("anotherTestMethod", Array())
    println(m1.getAnnotations.toList)
    println(m2.getAnnotations.toList)
    
    assertTrue(m1.getAnnotations.length == 1)
    assertTrue(m1.getAnnotation("Test").exists)
    assertTrue(m2.getAnnotations.length == 1)
    assertTrue(m2.getAnnotation("Test").exists)
  }
  
  @Test def testSearchIndexAnnotations() {
    import IJavaSearchConstants._
    val pattern = SearchPattern.createPattern("org.junit.Test", TYPE, ANNOTATION_TYPE_REFERENCE, SearchPattern.R_PREFIX_MATCH)
    val scope = SearchEngine.createJavaSearchScope(Array(srcPackageRoot.getPackageFragment("annots"): IJavaElement))
    
    var elems = Set[IMethod]()
    
    val requestor = new SearchRequestor {
      def acceptSearchMatch(m: SearchMatch) {
        m.getElement match {
          case method: IMethod => elems += method
          case elem => 
            println(elem)
            fail
        }
      }
    }
    
    (new SearchEngine).search(pattern, Array[SearchParticipant](SearchEngine.getDefaultSearchParticipant), scope, requestor, null)
    println(elems)
    assertEquals(elems.size, 2)
  }

  /** Test the structure as seen by the JDT. Use the JDT API to 
   *  retrieve the package `traits' and compare the toString output.
   */
  @Test def testStructure() {
    val fragment = srcPackageRoot.getPackageFragment("traits")
    assertNotNull(fragment)
    assertEquals(expectedFragment, compilationUnitsStructure(fragment).trim)
  }
  
  lazy val expectedFragment = """
T1.scala [in traits [in src [in simple-structure-builder]]]
  package traits
  interface T1
    scala.Nothing notImplemented()
    java.lang.Object t
    java.lang.Object t()
    scala.collection.immutable.List xs
    scala.collection.immutable.List xs()
    scala.Nothing m(int, int)
    java.lang.Object n(int, int, int)
    java.lang.Object T
    java.lang.Object U
    java.lang.Object Z
    class Inner
      Inner()
C1.scala [in traits [in src [in simple-structure-builder]]]
  package traits
  import scala.annotation.*
  import scala.reflect.BeanProperty
  import org.junit.Test
  class C
    int _x
    traits.C.T _y
    C(int, traits.C.T)
    C(traits.C.T)
    int x
    int x()
    int lz
    int lz()
    java.lang.String v
    java.lang.String v()
    void v_$eq(java.lang.String)
    java.lang.Object volVar
    java.lang.Object volVar()
    void volVar_$eq(java.lang.Object)
    java.lang.String CONSTANT
    java.lang.String CONSTANT()
    int beanVal
    int beanVal()
    int getBeanVal()
    int beanVar
    int beanVar()
    void beanVar_$eq(int)
    int getBeanVar()
    void setBeanVar(int)
    boolean nullaryMethod()
    void method()
    void annotatedMethod()
    java.lang.Object curriedMethod(int, int)
    boolean nullaryMethod1()
    void method1()
    void method2()
    java.lang.Object curriedMethod1(int, int)
    class InnerC
      int x
      int x()
      InnerC(int)
    java.lang.Object T
    java.lang.Object U
    java.lang.Object map(scala.Function1)
    scala.Null takeArray(scala.Array)
    scala.Null takeArray2(scala.Array)
    java.lang.Object localClass(int)
      class class Object
        $anon()
        void run()
    int localMethod(int)
    long localVals(int)""".trim
}
{'fraction_non_alphanumeric': 0.09955067398901647, 'fraction_numerical': 0.00399400898652022, 'mean_word_length': 3.2333051563820794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5532867', 'n_tokens_mistral': 3161, 'n_tokens_neox': 3109, 'n_words': 687}