Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Add support for weaver-test #446

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import complete.DefaultParsers._

val scala212 = "2.12.16"
val scala213 = "2.13.8"
val scala3 = "3.1.3"
val scala3 = "3.2.2"

val scalaIdeaVersion = scala3 // the version for which to import sources into intellij

Expand Down Expand Up @@ -96,7 +96,7 @@ lazy val core = (projectMatrix in file("core"))
)
.jvmPlatform(
scalaVersions = List(scala212, scala213, scala3),
settings = enableMimaSettings
settings = enableMimaSettings
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scalafmt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should've added it as a CI check.

)
.jsPlatform(
scalaVersions = List(scala212, scala213, scala3),
Expand Down Expand Up @@ -209,6 +209,23 @@ lazy val munit = (projectMatrix in file("munit"))
settings = commonSettings ++ Seq(scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) })
)

lazy val weaver = (projectMatrix in file("weaver"))
.settings(commonSettings)
.settings(
name := "diffx-weaver",
libraryDependencies ++= Seq(
"com.disneystreaming" %%% "weaver-cats" % "0.8.3"
),
testFrameworks += new TestFramework("weaver.framework.CatsEffect")
)
.dependsOn(core)
.jvmPlatform(
scalaVersions = List(scala212, scala213, scala3)
)
.jsPlatform(
scalaVersions = List(scala212, scala213, scala3)
)

lazy val tagging = (projectMatrix in file("tagging"))
.settings(commonSettings)
.settings(
Expand Down Expand Up @@ -280,7 +297,7 @@ lazy val docs = (projectMatrix in file("generated-docs")) // important: it must
),
mdocOut := file("generated-docs/out")
)
.dependsOn(core, scalatestShould, specs2, utest, refined, tagging, cats, munit)
.dependsOn(core, scalatestShould, specs2, utest, refined, tagging, cats, munit, weaver)
.jvmPlatform(scalaVersions = List(scala213))

val testJVM = taskKey[Unit]("Test JVM projects")
Expand All @@ -289,7 +306,7 @@ val testJS = taskKey[Unit]("Test JS projects")
val allAggregates =
core.projectRefs ++ scalatestMust.projectRefs ++ scalatestShould.projectRefs ++ scalatestLegacy.projectRefs ++
specs2.projectRefs ++ utest.projectRefs ++ cats.projectRefs ++
refined.projectRefs ++ tagging.projectRefs ++ docs.projectRefs ++ munit.projectRefs
refined.projectRefs ++ tagging.projectRefs ++ docs.projectRefs ++ munit.projectRefs ++ weaver.projectRefs

def filterProject(p: String => Boolean) =
ScopeFilter(inProjects(allAggregates.filter(pr => p(display(pr.project))): _*))
Expand Down
53 changes: 53 additions & 0 deletions docs-sources/test-frameworks/weaver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# weaver

To use with weaver, add the following dependency:

## sbt

```scala
"com.softwaremill.weaver" %% "diffx-weaver" % "@VERSION@" % Test
```

## mill

```scala
ivy"com.softwaremill.diffx::diffx-weaver::@VERSION@"
```

## Usage

Then, mixin `DiffxExpectations` trait or add `import com.softwaremill.diffx.weaver.DiffxExpectations._` to your test code.
To assert using diffx use `expectEqual` as follows:

```scala mdoc:compile-only
import com.softwaremill.diffx.weaver.DiffxExpectations._
import com.softwaremill.diffx.generic.auto._

sealed trait Parent
case class Bar(s: String, i: Int) extends Parent
case class Foo(bar: Bar, b: List[Int], parent: Option[Parent]) extends Parent

val right: Foo = Foo(
Bar("asdf", 5),
List(123, 1234),
Some(Bar("asdf", 5))
)

val left: Foo = Foo(
Bar("asdf", 66),
List(1234),
Some(right)
)

expectEqual(left, right)
```

## Versions matrix

Below table shows past Diffx releases with the corresponding weaver version they were build with.
For newer versions checkout the release changelog.

| Diffx | weaver |
|-------|:------:|
| 0.8.4 | 0.8.3 |

53 changes: 53 additions & 0 deletions generated-docs/out/test-frameworks/weaver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# weaver

To use with weaver, add the following dependency:

## sbt

```scala
"com.softwaremill.weaver" %% "diffx-weaver" % "0.8.3" % Test
```

## mill

```scala
ivy"com.softwaremill.diffx::diffx-weaver::0.8.3"
```

## Usage

Then, mixin `DiffxExpectations` trait or add `import com.softwaremill.diffx.weaver.DiffxExpectations._` to your test code.
To assert using diffx use `expectEqual` as follows:

```scala
import com.softwaremill.diffx.weaver.DiffxExpectations._
import com.softwaremill.diffx.generic.auto._

sealed trait Parent
case class Bar(s: String, i: Int) extends Parent
case class Foo(bar: Bar, b: List[Int], parent: Option[Parent]) extends Parent

val right: Foo = Foo(
Bar("asdf", 5),
List(123, 1234),
Some(Bar("asdf", 5))
)

val left: Foo = Foo(
Bar("asdf", 66),
List(1234),
Some(right)
)

expectEqual(left, right)
```

## Versions matrix

Below table shows past Diffx releases with the corresponding weaver version they were build with.
For newer versions checkout the release changelog.

| Diffx | weaver |
|-------|:------:|
| 0.8.4 | 0.8.3 |

2 changes: 1 addition & 1 deletion generated-docs/out/usage/ignoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ implicit val conf: DiffConfiguration = DiffConfiguration(makeIgnored =
)
// conf: DiffConfiguration = DiffConfiguration(makeIgnored = <function1>)
val d = Diff[Person].ignore(_.age)
// d: Diff[Person] = com.softwaremill.diffx.Diff$$anon$1@3c4f66a2
// d: Diff[Person] = com.softwaremill.diffx.Diff$$anon$1@2b53172e
d(Person("bob", 25), Person("bob", 30))
// res2: DiffResult = DiffResultObject(
// name = "Person",
Expand Down
4 changes: 2 additions & 2 deletions generated-docs/out/usage/modifying.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ with the `Diff.ignore` instance.
## collection support

Specify how objects within particular collection within particular diff instance should be matched.
We distinguish free main types of collections:
We distinguish three main types of collections:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks 😅

- seqLike collections where elements are indexed collections
- setLike collections where elements aren't indexed
- mapLike collections where elements(values) are indexed by some keys
Expand All @@ -61,4 +61,4 @@ implicit val diffOrg: Diff[Organization] = Diff.summon[Organization]
// mapLike methods:
.modify(_.peopleMap).matchByValue(_.age)
.modify(_.peopleMap).matchByKey(_.weight)
```
```
14 changes: 7 additions & 7 deletions generated-docs/out/usage/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ By default, it is set to an identical function.
```scala
implicit val showConfig = ShowConfig.default.copy(transformer = DiffResultTransformer.skipIdentical)
// showConfig: ShowConfig = ShowConfig(
// left = com.softwaremill.diffx.ShowConfig$$$Lambda$10463/0x0000000802afe890@2988c7f6,
// right = com.softwaremill.diffx.ShowConfig$$$Lambda$10463/0x0000000802afe890@5f94affc,
// missing = com.softwaremill.diffx.ShowConfig$$$Lambda$10463/0x0000000802afe890@2b1e794e,
// additional = com.softwaremill.diffx.ShowConfig$$$Lambda$10463/0x0000000802afe890@65ca52ad,
// default = com.softwaremill.diffx.ShowConfig$$$Lambda$10466/0x0000000802aff058@a929e95,
// arrow = com.softwaremill.diffx.ShowConfig$$$Lambda$10463/0x0000000802afe890@70032b74,
// transformer = com.softwaremill.diffx.DiffResultTransformer$$$Lambda$10455/0x0000000802afc958@280fe833
// left = com.softwaremill.diffx.ShowConfig$$$Lambda$12994/0x0000000802fcd840@44247db5,
// right = com.softwaremill.diffx.ShowConfig$$$Lambda$12994/0x0000000802fcd840@66c2460c,
// missing = com.softwaremill.diffx.ShowConfig$$$Lambda$12994/0x0000000802fcd840@4f023172,
// additional = com.softwaremill.diffx.ShowConfig$$$Lambda$12994/0x0000000802fcd840@3b0ad140,
// default = com.softwaremill.diffx.ShowConfig$$$Lambda$12997/0x0000000802fca040@5c24da41,
// arrow = com.softwaremill.diffx.ShowConfig$$$Lambda$12994/0x0000000802fcd840@28a205ef,
// transformer = com.softwaremill.diffx.DiffResultTransformer$$$Lambda$12986/0x0000000802d76840@76752828
// )
case class Person(name:String, age:Int)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.softwaremill.diffx.weaver

import com.softwaremill.diffx.{Diff, ShowConfig}
import weaver.Expectations.Helpers.{failure, success}
import weaver.{Expectations, SourceLocation}

trait DiffxExpectations {
def expectEqual[T: Diff](t1: T, t2: T)(implicit c: ShowConfig, loc: SourceLocation): Expectations = {
val result = Diff.compare(t1, t2)
if (result.isIdentical)
success
else
failure(result.show())(loc)
}
}

object DiffxExpectations extends DiffxExpectations
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softwaremill.diffx.weaver

import cats.data.Validated
import com.softwaremill.diffx.generic.AutoDerivation
import weaver.FunSuite

object DiffxExpectationsTest extends FunSuite with DiffxExpectations with AutoDerivation {
test("expectEqual should fail when there are differences") {
val n = Person("n1", 11)
expectEqual(n, Person("n2", 12)).run match {
case Validated.Valid(_) => failure(s"Expected a failure but succeeded")
case Validated.Invalid(_) => success
}
}

test("expectEqual should succeed when there are no differences") {
val n = Person("n1", 11)
expectEqual(n, Person("n1", 11))
}
}

case class Person(name: String, age: Int)