Skip to content

Commit

Permalink
jshelper test enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
aappddeevv committed Apr 12, 2024
1 parent fee90c9 commit d07cb1e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ closely mirror the javascript libraries themselves.
You will most likely also need DOM bindings. Here's a link to the api
for org.scala-js scalajs-dom bindings: [![javadoc](https://javadoc.io/badge2/org.scala-js/scalajs-dom_sjs1_3/javadoc.svg)](https://javadoc.io/doc/org.scala-js/scalajs-dom_sjs1_3).

# jshelpers

Package `jshelpers` is available to help with strange types. Review the docs for extra syntaxt that you can use.

Generally, if you model your data such as `js.UndefOr[A|Null]` then you will want to turn on
`-Xexplicit-nulls` so that the `Null` type becomes explicit and not inferred.

# Documentation

Client:
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lazy val jsSettings = Seq(
("org.scala-js" %%% "scalajs-dom" % "2.8.0"),
),
// testing
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.7.10" % "test",
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.8.1" % "test",
testFrameworks += new TestFramework("utest.runner.Framework")
)

Expand Down
2 changes: 1 addition & 1 deletion jshelpers/src/main/scala/null.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ object jsnull:
/** Swap `js.UndefOr[A|Null]` for `js.UndefOr[A]|Null`. */
@targetName("nullSwap")
inline def swap: js.UndefOr[A | Null] =
if a == null || a == js.undefined then ().asInstanceOf[js.UndefOr[A | Null]]
if a == null || js.isUndefined(a) then ().asInstanceOf[js.UndefOr[A | Null]]
else a.asInstanceOf[js.UndefOr[A | Null]]

extension (a: String | Null)
Expand Down
8 changes: 4 additions & 4 deletions jshelpers/src/main/scala/undefor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ object undefor:
inline private def forceGet: T = a.asInstanceOf[T]

/** Determine if is defined including the value not being null. */
// @targetName("isDefinedUndefOrNull")
// def isDefined: Boolean = if !js.isUndefined(a) && a != null then true else false
@targetName("isDefinedUndefOrNull")
def isDefined: Boolean = if !js.isUndefined(a) && a != null then true else false

// /** Convenience. */
// @targetName("isEmptyUndefOrNull")
// def isEmpty: Boolean = !isDefined
@targetName("isEmptyUndefOrNull")
def isEmpty: Boolean = !isDefined

//def isEmpty: Boolean = !a.asInstanceOf[js.UndefOr[T]].isDefined

Expand Down
14 changes: 13 additions & 1 deletion jshelpers/src/test/scala/NullTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import scala.scalajs.js
import utest.*

import jshelpers.syntax.jsnull.*
//import scala.language.unsafeNulls

object NullTests extends TestSuite:

Expand All @@ -19,6 +18,19 @@ object NullTests extends TestSuite:
val x: String = ""
assert(y.isEmpty && x.nn.isEmpty)
}
test("plain UndefOr[T|Null].isEmpty, no syntax imports") {
val z: js.UndefOr[String | Null] = js.undefined
//println(s"blah: $z")
assert(js.isUndefined(z))
//assert(z.isEmpty)
}
test("absorbNull") {
val y: String | Null = null
val x: String | Null = "blah"
assert(x.absorbNull == "blah")
// will not compile, under -Yexplicit-nulls
//assert(y.absorbNull == null)
}
}

object OrNullStringTests extends TestSuite:
Expand Down
17 changes: 14 additions & 3 deletions jshelpers/src/test/scala/UndefOrTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jshelpers
import scala.scalajs.js
import utest.*

import jshelpers.syntax.*
import jshelpers.syntax.undefor.*

object UndefBooleanTests extends TestSuite:

Expand Down Expand Up @@ -52,8 +52,6 @@ object UndefStringTests extends TestSuite:
}

object UndefTests extends TestSuite:
//import scala.language.unsafeNulls
import jshelpers.syntax.jsnull.*
val tests = Tests {
test("basics") {
val v: js.UndefOr[Int] = null.asInstanceOf[js.UndefOr[Int]]
Expand All @@ -66,6 +64,7 @@ object UndefTests extends TestSuite:
vundef.toTruthy ==> false
}
test("null") {
import jshelpers.syntax.jsnull.*
val v: js.UndefOr[Int] = 10
// just needs to compile
val v2: js.UndefOr[Int|Null] = v.toUndefOrNull
Expand All @@ -88,6 +87,18 @@ object UndefTests extends TestSuite:
(js.defined(0):js.UndefOr[Int]).toTruthy ==> false
(js.defined(1):js.UndefOr[Int]).toTruthy ==> true
}
test("js.Undefor[T|Null].absorbNull, requires import jshelpers.syntax.undefor.*") {
val y: js.UndefOr[String | Null] = null
val x: js.UndefOr[String | Null] = "blah"
val z: js.UndefOr[String | Null] = js.undefined
assert(!z.absorbNull.isDefined && !z.isDefined && z.isEmpty && y.isEmpty && x.absorbNull == "blah")
}
test("toNonNullOption") {
val v: js.UndefOr[Int] = 10
val vundef: js.UndefOr[Int] = js.undefined
v.toNonNullOption ==> Some(10)
vundef.toNonNullOption ==> None
}
}

object UndefMapXTests extends TestSuite:
Expand Down

0 comments on commit d07cb1e

Please sign in to comment.