Skip to content

Commit

Permalink
Merge branch 'feature-scala-3.3.0' of https://github.com/cheeseng/sca…
Browse files Browse the repository at this point in the history
…latest into main
  • Loading branch information
cheeseng committed Jun 15, 2023
2 parents 5424083 + 044e198 commit b57d55b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
Expand Up @@ -55,7 +55,7 @@ object ObjectMeta {
replaceAllLiterally("$$und", "_").
replaceAllLiterally("$mcI", "").
replaceAllLiterally("$sp", "").
replaceAllLiterally("$f", "")
replaceAllLiterally("$f", "")

val decodedKey10 = {
val idx = decodedKey.indexOf("__f_")
Expand Down
Expand Up @@ -16,31 +16,27 @@
package org.scalactic.source

import org.scalatest._
//SCALATESTJS-ONLY import scala.scalajs.js.annotation.JSExport

class ObjectMetaSpec extends funspec.AnyFunSpec with matchers.should.Matchers {

case class Person(name: String, private val age: Int) {
//SCALATESTJS-ONLY @JSExport
val otherField = "test other field"
private val privateField = "test private field"
}

describe("ObjectMeta") {

it("should extract case class attribute names correctly") {
// SKIP-DOTTY-START
ObjectMeta(Person("test", 33)).fieldNames should contain theSameElementsAs Set("name", "age", "otherField", "privateField")
// SKIP-DOTTY-END
//DOTTY-ONLY ObjectMeta(Person("test", 33)).fieldNames should contain theSameElementsAs Set("name", "age", "otherField")
ObjectMeta(Person("test", 33)).fieldNames should contain theSameElementsAs Set("name", "age", "otherField")
}

it("should extract dynamically field value correctly") {
val meta = ObjectMeta(Person("test", 33))
meta.value("name") shouldBe "test"
meta.value("age") shouldBe 33
meta.value("otherField") shouldBe "test other field"
// SKIP-DOTTY-START
meta.value("privateField") shouldBe "test private field"
// SKIP-DOTTY-END
}

it("should throw IllegalArgumentException when invalid attribute name is used to retrieve value") {
Expand All @@ -51,12 +47,12 @@ class ObjectMetaSpec extends funspec.AnyFunSpec with matchers.should.Matchers {
e.getMessage shouldBe "'invalid' is not attribute for this instance."
}

//DOTTY-ONLY it("should throw IllegalArgumentException when private attribute name is used to retrieve value") {
//DOTTY-ONLY val meta = ObjectMeta(Person("test", 33))
//DOTTY-ONLY val e = intercept[IllegalArgumentException] {
//DOTTY-ONLY meta.value("invalid")
//DOTTY-ONLY }
//DOTTY-ONLY e.getMessage shouldBe "'invalid' is not attribute for this instance."
//DOTTY-ONLY }
it("should throw IllegalArgumentException when private attribute name is used to retrieve value") {
val meta = ObjectMeta(Person("test", 33))
val e = intercept[IllegalArgumentException] {
meta.value("invalid")
}
e.getMessage shouldBe "'invalid' is not attribute for this instance."
}
}
}
16 changes: 11 additions & 5 deletions jvm/scalactic/src/main/scala/org/scalactic/source/ObjectMeta.scala
Expand Up @@ -35,14 +35,20 @@ object ObjectMeta {

def objectMetaUsingJavaReflection(v: Any): ObjectMeta =
new ObjectMeta {
val primaryConstructorParameterNames = v.getClass.getDeclaredConstructors.toList.sortBy(_.getParameterCount).head.getParameters.map(_.getName)
lazy val privFields = v.getClass.getDeclaredFields.filter(!_.isAccessible).map(_.getName)

val declaredMethods = v.getClass.getDeclaredMethods

lazy val fieldNames = {
v.getClass.getDeclaredMethods.filter { m =>
// SKIP-DOTTY-START
m.getParameterTypes.isEmpty && privFields.contains(m.getName)
// SKIP-DOTTY-END
//DOTTY-ONLY m.getParameterTypes.isEmpty && privFields.contains(m.getName) && m.getName != "$outer"
declaredMethods.filter { m =>
m.getParameterTypes.isEmpty &&
privFields.contains(m.getName) &&
(primaryConstructorParameterNames.contains(m.getName) ||
java.lang.reflect.Modifier.isPublic(m.getModifiers) ||
declaredMethods.find(_.getName.startsWith(s"${m.getName}$$")).isDefined // for Scala 2.11's case class private field, we can remove this when we drop support for Scala 2.11.
) &&
m.getName != "$outer"
}.map { f =>
if (f.getName.endsWith("$mcI$sp"))
f.getName.dropRight(7)
Expand Down
2 changes: 1 addition & 1 deletion project/DottyBuild.scala
Expand Up @@ -18,7 +18,7 @@ trait DottyBuild { this: BuildCommons =>

// List of available night build at https://repo1.maven.org/maven2/ch/epfl/lamp/dotty-compiler_0.27/
// lazy val dottyVersion = dottyLatestNightlyBuild.get
lazy val dottyVersion = System.getProperty("scalatest.dottyVersion", "3.1.3")
lazy val dottyVersion = System.getProperty("scalatest.dottyVersion", "3.3.0")
lazy val dottySettings = List(
scalaVersion := dottyVersion,
scalacOptions ++= List("-language:implicitConversions", "-noindent", "-Xprint-suspension")
Expand Down
4 changes: 2 additions & 2 deletions project/plugins.sbt
Expand Up @@ -4,15 +4,15 @@ addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.9.6")

val scalaJSVersion = Option(System.getenv("SCALAJS_VERSION")).getOrElse("1.9.0")
val scalaJSVersion = Option(System.getenv("SCALAJS_VERSION")).getOrElse("1.13.1")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.1")

addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "0.6.1")

val scalaNativeVersion = Option(System.getenv("SCALANATIVE_VERSION")).getOrElse("0.4.7")
val scalaNativeVersion = Option(System.getenv("SCALANATIVE_VERSION")).getOrElse("0.4.14")

addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeVersion)

Expand Down

0 comments on commit b57d55b

Please sign in to comment.