<commit_before>package sword.java.class_analyzer.java_type

import org.scalatest.FunSuite

class JavaTypeTest extends FunSuite {

  val primitiveSignatures = "V" :: "Z" :: "B" :: "C" :: "S" :: "I" :: "J" :: "F" :: "D" :: List()
  val arraySignatures = primitiveSignatures.map( "[" + _ )
  val someClassRef = "Ljava/util/List;" :: "Ljava/lang/Class;" :: "Lorg/scalatest/FunSuite;" :: List()
  val wrongClassRefs = null :: "" :: "L;" :: "Ljava/util/List" :: "java/util/List;" :: "Java/util/List" :: "List" :: "Ljava.util.List;" :: List()

  val validTypeLists = "[ZI" :: "D[[B" :: "ZLjava/lang/String;[I" :: "[Ljava/util/List;D" :: List()
  val validMethodSignatures = validTypeLists.map(x => "(" + x + ")I") ::: ("()V" :: List())

  val allValidValues = primitiveSignatures ::: arraySignatures ::: someClassRef ::: validTypeLists ::: validMethodSignatures
  val allTestingValues = wrongClassRefs ::: allValidValues

  test("Valid values for signatures never gives null references") {
    allValidValues foreach { x => assert( JavaType.getFromSignature(x) ne null, "null reference for " + x) }
  }

  test("Invalid signatures returns null references") {
    wrongClassRefs foreach { x => assert( JavaType.getFromSignature(x) eq null, "not null reference for " + x) }
  }

  test("Signature is the one given") {
    allValidValues foreach { x =>
      assert(JavaType.getFromSignature(x).signature() === x)
    }
  }

  test("Same instance") {
    allTestingValues.foreach { x =>
      val a = JavaType getFromSignature x
      val b = JavaType getFromSignature x
      assert(a eq b, s"Calling getFromSignature for $x do not always returns the same instance. " + (if (a != null) a.signature else "null"))
    }
  }

  test("different instances") {
    allValidValues foreach {
      x => allValidValues.foreach {
        y => if (x != y) {
          val a = JavaType getFromSignature x
          val b = JavaType getFromSignature y
          assert(a ne b)
        }
      }
    }
  }

  test("only JavaTypeList return true on isTypeList method") {
    allValidValues foreach { x =>
      val isList = validTypeLists.contains(x)
      val javaTypeList = JavaType.getFromSignature(x)
      assert(javaTypeList.isTypeList() == isList, "isList returns" + !isList + " for " + x)
    }
  }

  test("only JavaMethod returns a non null instance on tryCastingToMethod") {
    allValidValues foreach { x =>
      val isMethod = validMethodSignatures.contains(x)
      val javaType = JavaType.getFromSignature(x)
      if (isMethod) {
        val method = javaType.tryCastingToMethod
        assert((method ne null) && method.isInstanceOf[JavaMethod])
      }
      else {
        val method = javaType.tryCastingToMethod
        assert(method eq null)
      }
    }
  }
}<commit_msg>Updated test for the java_type package<commit_after>package sword.java.class_analyzer.java_type

import org.scalatest.FunSuite
import sword.java.class_analyzer.independent_type.IndependentTypeTest
import sword.java.class_analyzer.ref.RootReference

class JavaTypeTest extends FunSuite {

  val primitiveSignatures = IndependentTypeTest.primitiveSignatures
  val arraySignatures = IndependentTypeTest.arraySignatures
  val someClassRef = "Ljava/util/List;" :: "Ljava/lang/Class;" :: "Lorg/scalatest/FunSuite;" :: List()
  val wrongClassRefs = null :: "" :: "L;" :: "Ljava/util/List" :: "java/util/List;" :: "Java/util/List" :: "List" :: "Ljava.util.List;" :: List()

  val typeLists: List[List[String]] =
    ("[Z" :: "I" :: List()) ::
    ("D" :: "[[B" :: List()) ::
    ("Z" :: "Ljava/lang/String;" :: "[I" :: List()) ::
    List()
  //val validTypeLists = "[ZI" :: "D[[B" :: "ZLjava/lang/String;[I" :: "[Ljava/util/List;D" :: List()
  val validTypeLists = typeLists.map( x => x.foldLeft("")(_ + _))
  val validMethodSignatures = validTypeLists.map(x => "(" + x + ")I") ::: ("()V" :: List())

  val allValidValues = primitiveSignatures ::: arraySignatures ::: someClassRef ::: validTypeLists ::: validMethodSignatures
  val allTestingValues = wrongClassRefs ::: allValidValues

  val someWrongValues = "X" :: "S[" :: List()
  val allWrongValues = wrongClassRefs ::: someWrongValues

  test("Valid values for signatures never gives null references") {
    val factory = new ExtendedTypeFactory(new RootReference)
    allValidValues foreach { x => assert( factory.getFromSignature(x) ne null, "null reference for " + x) }
  }

  test("Invalid signatures returns null references") {
    val factory = new ExtendedTypeFactory(new RootReference)
    allWrongValues foreach { x => assert( factory.getFromSignature(x) eq null, "not null reference for " + x) }
  }

  test("Signature is the one given") {
    val factory = new ExtendedTypeFactory(new RootReference)
    allValidValues foreach { x =>
      assert(factory.getFromSignature(x).signature === x)
    }
  }

  test("JavaTypeList.toArray return the expected values") {
    val factory = new ExtendedTypeFactory(new RootReference)
    var lists = typeLists
    validTypeLists foreach { x =>
      val list = factory.getFromSignature(x).asInstanceOf[JavaTypeList]
      var expectedList = lists.head
      list.toArray() foreach { element =>
        assert(element.signature === expectedList.head)
        expectedList = expectedList.tail
      }

      lists = lists.tail
    }
  }

  test("JavaMethod includes the expected list and returning value") {
    val factory = new ExtendedTypeFactory(new RootReference)
    validMethodSignatures foreach { signature =>
      val splitPosition = signature.lastIndexOf(")");
      val paramsSignature = signature.substring(1, splitPosition)
      val retSignature = signature.substring(splitPosition + 1)
      val method = factory.getFromSignature(signature).asInstanceOf[JavaMethod]
      assert(method.getParameterTypeList.signature === paramsSignature)
      assert(method.getReturningType.signature === retSignature)
    }
  }
}
{'fraction_non_alphanumeric': 0.1361247669096457, 'fraction_numerical': 0.0003390405153415833, 'mean_word_length': 3.6825396825396823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '2893695', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1768, 'n_words': 442}