Skip to content

Commit

Permalink
test: add a test for firstIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Jan 2, 2024
1 parent f60c2e3 commit 67a907f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
scala
alias(libs.plugins.scalatest)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.qa)
Expand All @@ -14,6 +15,8 @@ repositories {
dependencies {
implementation(libs.kotlin.stdlib)
implementation(libs.scala.stdlib)
testRuntimeOnly(libs.flexmark) // needed to make it works scalatest
testImplementation(libs.scalatest)
testImplementation(libs.bundles.kotlin.testing)
}

Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mockito = "5.8.0"

[libraries]
scala-stdlib = { module = "org.scala-lang:scala3-library_3", version.ref = "scala" }
scalatest = "org.scalatest:scalatest_3:3.2.17"
flexmark = "com.vladsch.flexmark:flexmark-all:0.64.8"
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotest-assertions-core-jvm = { module = "io.kotest:kotest-assertions-core-jvm", version.ref = "kotest" }
kotlinx-coroutines-core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
Expand All @@ -16,6 +18,7 @@ mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }
kotlin-testing = [ "kotest-junit5-jvm", "kotest-assertions-core-jvm", "mockito-core" ]

[plugins]
scalatest = "com.github.maiflai.scalatest:0.32"
dokka = "org.jetbrains.dokka:1.9.10"
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-qa = "org.danilopianini.gradle-kotlin-qa:0.57.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object optional:

@main def useBOptional(): Unit =
import optional.*

def firstColumn[T](xss: List[List[T]]): Option[List[T]] =
optional:
xss.map(_.headOption.?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package io.github.tassiLuca.boundaries
import scala.util.boundary
import scala.util.boundary.break

object BoundaryTests extends App:
object BoundaryExamples extends App:

def firstIndex[T](xs: List[T], elem: T): Int =
boundary:
for (x, i) <- xs.zipWithIndex do if x == elem then break(i)
-1

println(firstIndex(List(1, 2, 3, 4), 4))
println(firstIndex(List(1, 2, 3, 4), 0))
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.tassiLuca.boundaries

import io.github.tassiLuca.boundaries.BoundaryExamples.firstIndex
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class BoundaryExamplesTest extends AnyFlatSpec:
"BoundaryExamples.firstIndex" should "return the first index of an item in the list or -1 if not present" in {
firstIndex(List(1, 2, 3, 4), 4) shouldBe 3
firstIndex(List(1, 2, 3, 4), 0) shouldBe -1
}

0 comments on commit 67a907f

Please sign in to comment.