Skip to content

Commit

Permalink
Refactored project structure & HarnessSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalin-Rudnicki committed Jun 29, 2024
1 parent d54cd8d commit 3e7eb85
Show file tree
Hide file tree
Showing 58 changed files with 882 additions and 380 deletions.
232 changes: 182 additions & 50 deletions build.sbt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object QueuedSenderSpec extends DefaultHarnessSpec {
}
} yield (ref, sender)

override def spec: TestSpec =
override def testSpec: TestSpec =
suite("QueuedSenderSpec")(
test("Case 1") {
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import template.domain.impl.storage.inMemory.*
import template.domain.storage.*
import zio.*

object InMemoryPaymentMethodStorageSpec
extends DefaultHarnessSpec.ForContract[UserStorage & PaymentMethodStorage]("InMemoryPaymentMethodStorage", PaymentMethodStorageContract)(
ZLayer.make[UserStorage & PaymentMethodStorage](
object InMemoryPaymentMethodStorageSpec extends ContractHarnessSpec[UserStorage & PaymentMethodStorage]("InMemoryPaymentMethodStorage", PaymentMethodStorageContract) {

override def layerProvider: LayerProvider[R] =
LayerProvider
.providePerTest(
DbState.layer,
InMemoryUserStorage.layer,
InMemoryPaymentMethodStorage.layer,
),
)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import template.domain.impl.storage.inMemory.*
import template.domain.storage.*
import zio.*

object InMemorySessionStorageSpec
extends DefaultHarnessSpec.ForContract[UserStorage & SessionStorage]("InMemorySessionStorage", SessionStorageContract)(
ZLayer.make[UserStorage & SessionStorage](
object InMemorySessionStorageSpec extends ContractHarnessSpec[UserStorage & SessionStorage]("InMemorySessionStorage", SessionStorageContract) {

override def layerProvider: LayerProvider[R] =
LayerProvider
.providePerTest(
DbState.layer,
InMemoryUserStorage.layer,
InMemorySessionStorage.layer,
),
)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import template.domain.impl.storage.inMemory.*
import template.domain.storage.*
import zio.*

object InMemoryUserStorageSpec
extends DefaultHarnessSpec.ForContract[UserStorage]("InMemoryUserStorage", UserStorageContract)(
ZLayer.make[UserStorage](
object InMemoryUserStorageSpec extends ContractHarnessSpec[UserStorage]("InMemoryUserStorage", UserStorageContract) {

override def layerProvider: LayerProvider[R] =
LayerProvider
.providePerTest(
DbState.layer,
InMemoryUserStorage.layer,
),
)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import zio.test.Assertion.*

object PaymentMethodStorageContract extends Contract[UserStorage & PaymentMethodStorage] {

val contract: TestSpec =
override def testSpec: TestSpec =
suite("PaymentMethodStorageContract")(
suite("insert")(
test("works") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import zio.test.Assertion.*

object SessionStorageContract extends Contract[UserStorage & SessionStorage] {

val contract: TestSpec =
override def testSpec: TestSpec =
suite("SessionStorageContract")(
suite("insert")(
test("works") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import zio.test.Assertion.*

object UserStorageContract extends Contract[UserStorage] {

override val contract: TestSpec =
override def testSpec: TestSpec =
suite("UserStorageContract")(
suite("insert")(
test("works") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ object SqlISpec extends DefaultHarnessSpec {
),
)

override def spec: TestSpec =
override def testSpec: TestSpec =
innerSpec
.provideSomeLayer[HarnessEnv & JDBCConnectionPool & Scope](
JDBCConnection.poolLayer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object PostgresTestContainerISpec extends DefaultHarnessSpec {
},
)

override def spec: TestSpec =
override def testSpec: TestSpec =
innerSpec
.provideSome[HarnessEnv & Scope & JDBCConnectionPool](
JDBCConnection.poolLayer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package harness.cli

import harness.test.*
import harness.zio.test.*
import zio.test.*
import zio.test.Assertion.*

object ArgSpec extends PlainHarnessSpec {
object ArgSpec extends DefaultHarnessSpec {

private val parseSucceeds: TestSpec = {
def makeTest(input: String)(expArgs: Arg*): TestSpec =
Expand Down Expand Up @@ -63,7 +63,7 @@ object ArgSpec extends PlainHarnessSpec {
)
}

override def spec: TestSpec =
override def testSpec: TestSpec =
suite("ArgSpec")(
parseSucceeds,
parseFails,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package harness.cli

import harness.test.AssertionHelpers.*
import harness.test.PlainHarnessSpec
import harness.zio.test.*
import harness.zio.test.AssertionHelpers.*
import scala.reflect.ClassTag
import zio.*
import zio.test.*
import zio.test.Assertion.*

object NameSpec extends PlainHarnessSpec {
object NameSpec extends DefaultHarnessSpec {

private val longName: TestSpec = {
val applySucceeds: TestSpec = {
Expand Down Expand Up @@ -126,7 +126,7 @@ object NameSpec extends PlainHarnessSpec {
)
}

override def spec: TestSpec =
override def testSpec: TestSpec =
suite("NameSpec")(
longName,
shortName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package harness.cli

import cats.data.{Ior, NonEmptyList}
import cats.syntax.option.*
import harness.test.AssertionHelpers.*
import harness.test.PlainHarnessSpec
import harness.zio.test.*
import harness.zio.test.AssertionHelpers.*
import scala.annotation.unused
import zio.test.*
import zio.test.Assertion.*

object ParserSpec extends PlainHarnessSpec {
object ParserSpec extends DefaultHarnessSpec {

private def parserSuite[T](name: String)(parser: Parser[T])(
tests: (FinalizedParser[T] ?=> TestSpec)*,
Expand Down Expand Up @@ -302,7 +302,7 @@ object ParserSpec extends PlainHarnessSpec {
makeTest()(isSuccess(equalTo(SumType.Case2(None)))),
)

override def spec: TestSpec =
override def testSpec: TestSpec =
suite("ParserSpec")(
help,
suite("simple")(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package harness.core

import harness.test.PlainHarnessSpec
import harness.zio.test.*
import zio.test.*
import zio.test.Assertion.*

object StringOpsSpec extends PlainHarnessSpec {
object StringOpsSpec extends DefaultHarnessSpec {

private def makeTest(input: String, convert: String => String, exp: => String): TestSpec =
test(s"'$input' -> '$exp'")(assert(convert(input))(equalTo(exp)))
Expand All @@ -30,7 +30,7 @@ object StringOpsSpec extends PlainHarnessSpec {
*/

override def spec: TestSpec =
override def testSpec: TestSpec =
suite("StringOps")(
suite("camelToSnake")(
makeTest("aBC", _.camelToSnake, "a_bc"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package harness.core

import cats.data.NonEmptyList
import cats.syntax.option.*
import harness.zio.test.*
import zio.internal.stacktracer.SourceLocation
import zio.test.*
import zio.test.Assertion.*

object VersionSpec extends DefaultHarnessSpec {

private object versionSpec {

private def passingParseTest(string: String)(hasVPrefix: Boolean, suffix: Option[Version.Suffix.Repr], num0: Int, numN: Int*)(implicit loc: SourceLocation): TestSpec =
test(string) {
val version = Version.parse(string)
assert(version)(isSome(equalTo(Version(hasVPrefix, NonEmptyList(num0, numN.toList), suffix.map(_.toSuffix))))) &&
assert(version.map(_.toString))(isSome(equalTo(string)))
}

private def failingParseTest(string: String)(implicit loc: SourceLocation): TestSpec =
test(string) {
assert(Version.parse(string))(isNone)
}

private def orderingTest(smaller: String, greater: String)(implicit loc: SourceLocation): TestSpec =
test(s"$smaller < $greater") {
val smallerVersion = Version.parseUnsafe(smaller)
val greaterVersion = Version.parseUnsafe(greater)
assertTrue(
Version.ordering.lt(smallerVersion, greaterVersion),
Version.ordering.gt(greaterVersion, smallerVersion),
)
}

private def equalsTest(a: String, b: String)(implicit loc: SourceLocation): TestSpec =
test(s"$a == $b") {
assertTrue(Version.parseUnsafe(a) == Version.parseUnsafe(b))
}

val spec: TestSpec =
suite("version")(
suite("parse")(
suite("passes")(
suite("basic")(
passingParseTest("1")(false, None, 1),
passingParseTest("1.2")(false, None, 1, 2),
passingParseTest("1.2.3")(false, None, 1, 2, 3),
passingParseTest("1.2.3.4")(false, None, 1, 2, 3, 4),
),
suite("with v prefix")(
passingParseTest("v1")(true, None, 1),
passingParseTest("v1.2")(true, None, 1, 2),
passingParseTest("v1.2.3")(true, None, 1, 2, 3),
passingParseTest("v1.2.3.4")(true, None, 1, 2, 3, 4),
),
suite("with suffix")(
passingParseTest("1-SNAPSHOT")(false, Version.Suffix.Snapshot(None).some, 1),
passingParseTest("1.2-SNAPSHOT")(false, Version.Suffix.Snapshot(None).some, 1, 2),
passingParseTest("1.2.3-SNAPSHOT")(false, Version.Suffix.Snapshot(None).some, 1, 2, 3),
passingParseTest("1.2.3.4-SNAPSHOT")(false, Version.Suffix.Snapshot(None).some, 1, 2, 3, 4),
),
suite("with v prefix and suffix")(
passingParseTest("v1-SNAPSHOT")(true, Version.Suffix.Snapshot(None).some, 1),
passingParseTest("v1.2-SNAPSHOT")(true, Version.Suffix.Snapshot(None).some, 1, 2),
passingParseTest("v1.2.3-SNAPSHOT")(true, Version.Suffix.Snapshot(None).some, 1, 2, 3),
passingParseTest("v1.2.3.4-SNAPSHOT")(true, Version.Suffix.Snapshot(None).some, 1, 2, 3, 4),
),
passingParseTest("v1.2.3.4---SNAPSHOT")(true, Version.Suffix.Other("--SNAPSHOT").some, 1, 2, 3, 4),
),
suite("fails")(
failingParseTest(" 1.2.3"),
failingParseTest("1.2.3 "),
failingParseTest("v 1.2.3"),
failingParseTest("1.2.3 SNAPSHOT"),
),
),
suite("ordering")(
// without suffix
orderingTest("0", "1"),
orderingTest("2", "11"),
orderingTest("0", "0.1"),
orderingTest("0.1", "0.1.2"),
orderingTest("0.1.2", "0.1.2.3"),
orderingTest("0.0.1", "0.0.2"),
// with suffix
orderingTest("0-SNAPSHOT-2", "0-SNAPSHOT-11"),
orderingTest("0-SNAPSHOT-2", "0-RC1"),
orderingTest("0-RC2", "0-RC11"),
),
suite("equals")(
equalsTest("0", "0"),
equalsTest("3", "3.0.0"),
equalsTest("v3", "3.0.0"),
),
)

}

private object suffixSpec {

private def passingParseTest(string: String, suffix: Version.Suffix.Repr)(implicit loc: SourceLocation): TestSpec =
test(string) {
assertTrue(Version.Suffix.parse(string).repr == suffix)
}

private def orderingTest(smaller: String, greater: String)(implicit loc: SourceLocation): TestSpec =
test(s"$smaller < $greater") {
val smallerSuffix = Version.Suffix.parse(smaller)
val greaterSuffix = Version.Suffix.parse(greater)
assertTrue(
Version.Suffix.ordering.lt(smallerSuffix, greaterSuffix),
Version.Suffix.ordering.gt(greaterSuffix, smallerSuffix),
)
}

private def equalsTest(a: String, b: String)(implicit loc: SourceLocation): TestSpec =
test(s"$a == $b") {
assertTrue(Version.Suffix.parse(a) == Version.Suffix.parse(b))
}

val spec: TestSpec =
suite("suffix")(
suite("parse")(
// snapshot
passingParseTest("snap", Version.Suffix.Snapshot(None)),
passingParseTest("snap1", Version.Suffix.Snapshot("1".some)),
passingParseTest("snap-1", Version.Suffix.Snapshot("-1".some)),
passingParseTest("snapshot", Version.Suffix.Snapshot(None)),
passingParseTest("snapshot1", Version.Suffix.Snapshot("1".some)),
passingParseTest("snapshot-1", Version.Suffix.Snapshot("-1".some)),
// rc
passingParseTest("RC1", Version.Suffix.RC(1)),
passingParseTest("rc2", Version.Suffix.RC(2)),
passingParseTest("RC-3", Version.Suffix.RC(3)),
// other
passingParseTest("snappy", Version.Suffix.Other("SNAPPY")),
passingParseTest("other", Version.Suffix.Other("OTHER")),
passingParseTest("RC--3", Version.Suffix.Other("RC--3")),
),
suite("ordering")(
// same type
orderingTest("RC1", "RC2"),
orderingTest("RC2", "RC11"),
orderingTest("SNAPSHOT", "SNAPSHOT-1"),
orderingTest("SNAPSHOT-2", "SNAPSHOT-11"),
orderingTest("SNAPSHOT-2", "SNAPSHOT-11"),
orderingTest("snappy11", "snappy2"),
// different type
orderingTest("SNAPSHOT", "RC1"),
orderingTest("SNAPSHOT", "snappy"),
orderingTest("RC1", "snappy"),
),
suite("equals")(
// snapshot
equalsTest("snapshot2", "SNAP-2"),
equalsTest("snapshot-2", "SNAP-2"),
equalsTest("snapshot2", "SNAP2"),
equalsTest("snapshot-2", "SNAP2"),
// rc
equalsTest("RC1", "rc1"),
equalsTest("RC1", "rc-1"),
// other
equalsTest("snappy", "SNAPPY"),
),
)

}

override def testSpec: TestSpec =
suite("VersionSpec")(
versionSpec.spec,
suffixSpec.spec,
)

}
Loading

0 comments on commit 3e7eb85

Please sign in to comment.