Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoSuchMethodError MyType$.$anonfun$assertion$1$adapted(Ljava/lang/Object;)Lscala/util/Either; #1205

Open
domdorn opened this issue Sep 27, 2023 · 0 comments

Comments

@domdorn
Copy link
Contributor

domdorn commented Sep 27, 2023

When using assertCustom with a Either, zio-prelude fails at runtime,
e.g. the below class compiles fine, but the tests for SimplexOptimalityTolerance fail at runtime with a NoSuchMethodError.
Currently, this means I have to migrate all assertions to be the ones like in the SolutionType.

zio-version: 2.0.18
zio-prelude: 1.0.0-RC21

Exception:

Exception in thread "zio-fiber-464" java.lang.NoSuchMethodError: com.acme.domain.SolverConfig$CPLEX$SimplexOptimalityTolerance$.$anonfun$assertion$1$adapted(Ljava/lang/Object;)Lscala/util/Either;
        	at com.acme.domain.SolverConfig$CPLEX$SimplexOptimalityTolerance$$anon$4.run(SolverConfig.scala:65)
        	at com.acme.domain.SolverConfig$CPLEX$SimplexOptimalityTolerance$$anon$4.run(SolverConfig.scala:65)
        	at com.acme.domain.SolverConfigSpec$.$anonfun$spec$101(SolverConfigSpec.scala:100)
        	at com.acme.domain.SolverConfigSpec.spec(SolverConfigSpec.scala:99)

types:

import zio.prelude
import zio.prelude.Assertion._
sealed trait SolverConfig[T <: Solver]

object SolverConfig {
  object CPLEX {
    import zio.prelude._

    /*
     https://www.gams.com/latest/docs/S_CPLEX.html#CPLEXsolutiontype
     type of solution (basic or non basic) for an LP or QP
     0, 1, 2, default 0
     */
    object SolutionType extends Subtype[Int] {
      override def assertion =
        assert(Assertion.greaterThanOrEqualTo(0) && lessThanOrEqualTo(2))

    }

    object SimplexOptimalityTolerance extends Subtype[Double] {
      override def assertion = assertCustom { value: Double =>
        Either.cond[AssertionError, Unit](value >= 1.0e-09 && value <= 0.1, (), prelude.AssertionError.Failure("value must be between 1.0e-09 and 0.1"))
      }
    }
}
}

Test-Code:

package domain

import SolverConfig.CPLEX
import SolverConfig.CPLEX._
import zio.prelude.Validation
import zio.test._

object SolverConfigSpec extends ZIOSpecDefault {

  override def spec = suite("SolverConfigSpec")(
    suite("CPLEX")(
      suite("SolutionType")(
        test("fail on -1"){
          val st: Validation[String, CPLEX.SolutionType.Type] = SolutionType.make(-1)
          assertTrue(st.toEither.isLeft)
        },
        test("work on 0") {
            val st: Validation[String, CPLEX.SolutionType.Type] = SolutionType.make(0)
            assertTrue(st.toEither.isRight)
        },
        test("work on 1") {
            val st: Validation[String, CPLEX.SolutionType.Type] = SolutionType.make(1)
            assertTrue(st.toEither.isRight)
        },
        test("work on 2") {
            val st: Validation[String, CPLEX.SolutionType.Type] = SolutionType.make(2)
            assertTrue(st.toEither.isRight)
        },
        test("fail on 3") {
            val st: Validation[String, CPLEX.SolutionType.Type] = SolutionType.make(3)
            assertTrue(st.toEither.isLeft)
        }
      ),
      suite("SimplexOptimalityTolerance")(
        test("fail on smaller than 1.0e-09"){
          val sot: Validation[String, CPLEX.SimplexOptimalityTolerance.Type] = SimplexOptimalityTolerance.make(1.0e-10)
          assertTrue(sot.toEither.isLeft)
        },
        test("work on 1.0e-09"){
            val sot: Validation[String, CPLEX.SimplexOptimalityTolerance.Type] = SimplexOptimalityTolerance.make(1.0e-09)
            assertTrue(sot.toEither.isRight)
        },
        test("work on value between 1.0e-09 and 0.1"){
            val sot: Validation[String, CPLEX.SimplexOptimalityTolerance.Type] = SimplexOptimalityTolerance.make(0.05)
            assertTrue(sot.toEither.isRight)
        },
        test("work on 0.1"){
            val sot: Validation[String, CPLEX.SimplexOptimalityTolerance.Type] = SimplexOptimalityTolerance.make(0.1)
            assertTrue(sot.toEither.isRight)
        },
        test("fail on 0.11"){
            val sot: Validation[String, CPLEX.SimplexOptimalityTolerance.Type] = SimplexOptimalityTolerance.make(0.11)
            assertTrue(sot.toEither.isLeft)
        }
      )
    )
  )

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant