Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RE-5): remove obsolete tests #7

Merged
merged 2 commits into from
Mar 23, 2024
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
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ lazy val redactedTests = (project in file("tests"))
.settings(scalafixSettings)
.settings(
publish / skip := true,
libraryDependencies ++= Seq("org.scalatest" %% "scalatest" % "3.2.17" % Test),
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"org.scalatestplus" %% "scalacheck-1-17" % "3.2.17.0" % Test
),
scalacOptions ++= {
val jar = (redactedCompilerPlugin / assembly).value
val addPlugin = "-Xplugin:" + jar.getAbsolutePath
Expand Down
170 changes: 83 additions & 87 deletions tests/src/test/scala/io/github/polentino/redacted/RedactedSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,109 @@ package io.github.polentino.redacted

import org.scalatest.Checkpoints.*
import org.scalatest.flatspec.AnyFlatSpec

import io.github.polentino.redacted.RedactionWithNestedCaseClass.Inner
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

import java.util.UUID

class RedactedSpec extends AnyFlatSpec {
class RedactedSpec extends AnyFlatSpec with ScalaCheckPropertyChecks {

behavior of "@redacted"

it should "work with case classes without user-defined companion object" in {
val name: String = "Berfu"
val age = 26
val email: String = "[email protected]"
val expected = s"RedactionWithoutCompanionObj(***,$age,***)"

val testing = RedactionWithoutCompanionObj(name, age, email)
val implicitToString = s"$testing"
val explicitToString = testing.toString
val cp = new Checkpoint

cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name && testing.age == age && testing.email == email) }
cp.reportAll()
it should "work with a redacted case class of just one member" in {
case class OneMember(@redacted name: String)

forAll { (name: String) =>
val expected = "OneMember(***)"
val testing = OneMember(name)
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name) }
cp.reportAll()
}
}

it should "work with case classes with user-defined companion object" in {
val name: String = "Berfu"
val age = 26
val email: String = "[email protected]"
val expected = s"RedactionWithCompanionObj(***,$age,***)"

val testing = RedactionWithCompanionObj(name, age, email)
val implicitToString = s"$testing"
val explicitToString = testing.toString
val cp = new Checkpoint

cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name && testing.age == age && testing.email == email) }
cp { assert(RedactionWithCompanionObj.something == 123) }
cp.reportAll()
it should "work with a redacted case class with many members" in {
case class ManyMembers(field1: String, @redacted field2: String, @redacted field3: String, field4: String)

forAll { (field1: String, field2: String, field3: String, field4: String) =>
val expected = s"ManyMembers($field1,***,***,$field4)"
val testing = ManyMembers(field1, field2, field3, field4)
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp {
assert(testing.field1 == field1 &&
testing.field2 == field2 &&
testing.field3 == field3 &&
testing.field4 == field4)
}
cp.reportAll()
}
}

it should "work with nested case classes in object" in {
val id = "id-1"
val name1 = "Diego"
val age1 = 999
val name2 = "Berfu"
val age2 = 888
val expected = s"RedactionWithNestedCaseClass($id,***,Inner(***,$age2))"

val testing = RedactionWithNestedCaseClass(id, Inner(name1, age1), Inner(name2, age2))
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint

cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp {
assert(testing.id == id &&
testing.inner1.name == name1 &&
testing.inner1.age == age1 &&
testing.inner2.name == name2 &&
testing.inner2.age == age2)
it should "work with case class nested in companion object" in {
forAll { (id: String, name1: String, age1: Int, name2: String, age2: Int) =>
val expected = s"RedactionWithNestedCaseClass($id,***,Inner(***,$age2))"
val testing = RedactionWithNestedCaseClass(id, Inner(name1, age1), Inner(name2, age2))
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp {
assert(testing.id == id &&
testing.inner1.name == name1 &&
testing.inner1.age == age1 &&
testing.inner2.name == name2 &&
testing.inner2.age == age2)
}
cp.reportAll()
}
cp.reportAll()
}

it should "work with nested case classes in case class" in {
case class Inner(userId: String, @redacted balance: Int)
case class Outer(inner: Inner)

val userId = "user-123"
val balance = 123_456_789
val expected = s"Outer(Inner($userId,***))"

val testing = Outer(Inner(userId, balance))
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint

cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp {
assert(
testing.inner.userId == userId &&
testing.inner.balance == balance
)
forAll { (userId: String, balance: Int) =>
val expected = s"Outer(Inner($userId,***))"
val testing = Outer(Inner(userId, balance))
val implicitToString = s"$testing"
val explicitToString = testing.toString

val cp = new Checkpoint
cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp {
assert(
testing.inner.userId == userId &&
testing.inner.balance == balance
)
}
cp.reportAll()
}
cp.reportAll()
}

it should "work with a redacted case class of just one member" in {
case class OneMember(@redacted name: String)
val name = "berfu"
val expected = "OneMember(***)"

val testing = OneMember(name)
val implicitToString = s"$testing"
val explicitToString = testing.toString
it must "not change the behavior of `hashCode`" in {
final case class TestClass(uuid: UUID, name: String, age: Int)
object RedactedTestClass {
final case class TestClass(uuid: UUID, @redacted name: String, @redacted age: Int)
}

val cp = new Checkpoint
forAll { (uuid: UUID, name: String, age: Int) =>
val testClass = TestClass(uuid, name, age)
val redactedTestClass = RedactedTestClass.TestClass(uuid, name, age)

cp { assert(implicitToString == expected) }
cp { assert(explicitToString == expected) }
cp { assert(testing.name == name) }
cp.reportAll()
assert(testClass.hashCode() == redactedTestClass.hashCode())
}
}
}

This file was deleted.

This file was deleted.

Loading