Skip to content

Commit 41792f5

Browse files
committed
Changed test code
1 parent bfa329e commit 41792f5

File tree

4 files changed

+48
-64
lines changed

4 files changed

+48
-64
lines changed

server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private class StringCompiler(
242242
}
243243

244244
def findClass(className: String, classLoader: ClassLoader): Option[Class[_]] =
245-
Try( cache.getOrElseUpdate(className, classLoader.loadClass(className) ) ) match {
245+
Try(cache.getOrElseUpdate(className, classLoader.loadClass(className))) match {
246246
case Success(cls) => Some(cls)
247247
case Failure(_) => None
248248
}
@@ -346,6 +346,10 @@ class Eval(target: Option[File] = None, jars: List[File] = Nil) {
346346
compilerMessageHandler
347347
)
348348

349+
lazy val jarUrls = jars.map(jar => new java.net.URL(s"file://${jar.getAbsolutePath}"))
350+
lazy val urlClassLoader = new URLClassLoader(jarUrls, compiler.getClass.getClassLoader)
351+
lazy val classLoader = new AbstractFileClassLoader(compilerOutputDir, urlClassLoader)
352+
349353
/**
350354
* Will generate a classname of the form Evaluater__<unique>,
351355
* where unique is computed from the jvmID (a random number)
@@ -358,30 +362,23 @@ class Eval(target: Option[File] = None, jars: List[File] = Nil) {
358362
}
359363

360364
def execute[T](className: String, code: String, resetState: Boolean, jars: Seq[File]): T = {
361-
val jarUrls = jars.map(jar => new java.net.URL(s"file://${jar.getAbsolutePath}"))
362-
val urlClassLoader = new URLClassLoader(jarUrls, compiler.getClass.getClassLoader)
363-
val classLoader = new AbstractFileClassLoader(compilerOutputDir, urlClassLoader)
364-
365365
val cls = compiler(
366366
wrapCodeInClass(className, code),
367367
className,
368368
resetState,
369369
classLoader
370370
)
371371

372-
val res = cls
372+
cls
373373
.getConstructor()
374374
.newInstance()
375375
.asInstanceOf[() => T]
376376
.apply()
377377
.asInstanceOf[T]
378-
379-
urlClassLoader.close()
380-
381-
res
382378
}
383379

384380
def clean(): Unit = {
381+
urlClassLoader.close()
385382
compiler.close()
386383
compilerMessageHandler.foreach(_.reset())
387384
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
|"\""{
3+
| "id": "c730433b-082c-4984-9d66-855c243266f0",
4+
| "name": "Foo",
5+
| "counts": [1, 2, 3],
6+
| "values": {
7+
| "bar": true,
8+
| "baz": 100.001,
9+
| "qux": ["a", "b"]
10+
| }
11+
| }
12+
| \\"""
13+
| """.stripMargin

server/src/test/scala/org/scalaexercises/evaluator/EvaluatorSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ Xor.Right(42).toOption.get
9494

9595
val result: EvalResult[Int] = evaluator
9696
.eval(
97-
fetchCode,
97+
circeCode,
9898
remotes = commonResolvers,
99-
dependencies = fetchLibraryDependencies(toScalaVersion(BuildInfo.scalaVersion))
99+
dependencies = circeLibraryDependencies(toScalaVersion(BuildInfo.scalaVersion))
100100
)
101101
.unsafeRunSync()
102102

server/src/test/scala/org/scalaexercises/evaluator/helper.scala

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ object helper {
4040
Dependency("org.scala-lang.modules", s"scala-xml_${scala.version.substring(0, 4)}", "1.2.0")
4141
)
4242

43-
def fetchLibraryDependencies(scala: ScalaVersion): List[Dependency] = {
43+
def circeLibraryDependencies(scala: ScalaVersion): List[Dependency] = {
4444
val sv = scala.version
45+
46+
val circeVersion = "0.12.3"
4547
List(
46-
Dependency("com.47deg", s"fetch_${sv.substring(0, 4)}", "1.2.1")
48+
Dependency("io.circe", s"circe-core_${sv.substring(0, 4)}", circeVersion),
49+
Dependency("io.circe", s"circe-generic_${sv.substring(0, 4)}", circeVersion),
50+
Dependency("io.circe", s"circe-parser_${sv.substring(0, 4)}", circeVersion)
4751
) ++ scalaDependencies(scala)
4852
}
4953

@@ -58,59 +62,29 @@ import stdlib._
5862
Asserts.scalaTestAsserts($assertCheck)
5963
"""
6064

61-
val fetchCode =
62-
"""
63-
import scala.concurrent.ExecutionContext
64-
65-
import cats.data.NonEmptyList
66-
import cats.effect._
67-
68-
import fetch._
69-
import cats.implicits._
70-
71-
val executionContext: ExecutionContext = ExecutionContext.fromExecutor(new java.util.concurrent.ForkJoinPool(2))
72-
73-
implicit val timer: Timer[IO] = IO.timer(executionContext)
74-
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
75-
76-
type UserId = Int
77-
78-
case class User(id: UserId, username: String)
79-
80-
def latency[F[_]: Concurrent](msg: String): F[Unit] =
81-
for {
82-
_ <- Sync[F].delay(println(s"--> [${Thread.currentThread.getId}] $msg"))
83-
_ <- Sync[F].delay(Thread.sleep(100))
84-
_ <- Sync[F].delay(println(s"<-- [${Thread.currentThread.getId}] $msg"))
85-
} yield ()
86-
87-
val userDatabase: Map[UserId, User] = Map(
88-
1 -> User(1, "@one"),
89-
2 -> User(2, "@two"),
90-
3 -> User(3, "@three"),
91-
4 -> User(4, "@four")
92-
)
93-
94-
object Users extends Data[UserId, User] {
95-
def name = "Users"
96-
97-
def source[F[_]: Concurrent]: DataSource[F, UserId, User] = new DataSource[F, UserId, User] {
98-
override def data = Users
99-
100-
def CF = Concurrent[F]
101-
102-
override def fetch(id: UserId): F[Option[User]] =
103-
latency[F](s"One User $id") >> CF.pure(userDatabase.get(id))
65+
val json = """"{
66+
| "id": "c730433b-082c-4984-9d66-855c243266f0",
67+
| "name": "Foo",
68+
| "counts": [1, 2, 3],
69+
| "values": {
70+
| "bar": true,
71+
| "baz": 100.001,
72+
| "qux": ["a", "b"]
73+
| }
74+
| }"""".stripMargin
75+
76+
val circeCode =
77+
s"""
78+
import cats.syntax.either._
79+
import io.circe._
80+
import io.circe.parser._
10481

105-
override def batch(ids: NonEmptyList[UserId]): F[Map[UserId, User]] =
106-
latency[F](s"Batch Users $ids") >> CF.pure(userDatabase.view.filterKeys(ids.toList.toSet).toMap)
107-
}
108-
}
82+
val json: String = ""$json""
10983

110-
def getUser[F[_]: Concurrent](id: UserId): Fetch[F, User] = Fetch[F, UserId, User](id, Users.source)
84+
val doc: Json = parse(json).getOrElse(Json.Null)
11185

112-
def fetchUser[F[_] : Concurrent]: Fetch[F, User] = getUser(1)
86+
val cursor: HCursor = doc.hcursor
11387

114-
Fetch.run[IO](fetchUser).unsafeRunSync()
88+
cursor.downField("values").downField("baz").as[Double]
11589
"""
11690
}

0 commit comments

Comments
 (0)