8000 Too big zio-test output fix by urbit-pilled · Pull Request #8614 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Too big zio-test output fix #8614

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

Merged
merged 11 commits into from
Jan 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import scala.collection.mutable

trait MutableSetCompat[V] extends mutable.Set[V] {

override def +=(element: V): this.type = {
add(element)
this
}
override def +=(element: V): this.type = {
add(element)
this
}

override def -=(element: V): this.type = {
remove(element)
this
}
override def -=(element: V): this.type = {
remove(element)
this
}

}
}
40 changes: 40 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/AssertionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,42 @@ object AssertionSpec extends ZIOBaseSpec {
)
)
} @@ TestAspect.scala2Only,
test("equalTo must print out shortened value when using simple class") {
val probe: SampleUser = SampleUser("User", 42)
val expected: SampleUser = SampleUser("User", 50)
assert(probe)(equalTo(expected))
} @@ TestAspect.failing[String] {
case TestFailure.Assertion(result, _) => {
val failures = FailureCase.fromTrace(result.failures.get, Chunk.empty)
val errorString: String = failures(0).errorMessage.lines(0).fragments(0).text
errorString == "SampleUser.age : expected '50' got '42'\n"
}
case _ => false
},
test("equalTo must print out shortened error value when using nested class") {
val probe: Person = Person("John", 30, Address("New York", "123 Main St"))
val expected: Person = Person("John", 30, Address("San Francisco", "456 Oak St"))
assert(probe)(equalTo(expected))
} @@ TestAspect.failing[String] {
case TestFailure.Assertion(result, _) => {
val failures = FailureCase.fromTrace(result.failures.get, Chunk.empty)
val errorString: String = failures(0).errorMessage.lines(0).fragments(0).text
errorString == "Person.address.city : expected 'San Francisco' got 'New York'\nPerson.address.street : expected '456 Oak St' got '123 Main St'\n"
}
case _ => false
},
test("equalTo must print out shortened value when using list inside class") {
val probe: Gambler = Gambler("User", List(100, 200))
val expected: Gambler = Gambler("User", List(100, 300, 500))
assert(probe)(equalTo(expected))
} @@ TestAspect.failing[String] {
case TestFailure.Assertion(result, _) => {
val failures = FailureCase.fromTrace(result.failures.get, Chunk.empty)
val errorString: String = failures(0).errorMessage.lines(0).fragments(0).text
errorString == "Gambler.bets[1] : expected '300' got '200'\nGambler.bets[2] : expected '500' got 'null'\n"
}
case _ => false
},
test("equalTo should succeed for same CharSequence value") {
val probe: CharSequence = "test"
assert(probe)(equalTo(probe))
Expand Down Expand Up @@ -621,6 +657,10 @@ object AssertionSpec extends ZIOBaseSpec {
} @@ failing
)

case class Person(name: String, age: Int, address: Address)
case class Address(city: String, street: String)
case class Gambler(name: String, bets: List[Int])

case class SampleUser(name: String, age: Int)
val sampleUser: SampleUser = SampleUser("User", 42)
val sampleException = new Exception
Expand Down
88 changes: 88 additions & 0 deletions test/shared/src/main/scala-2.12/zio/test/AssertionVariants.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zio.test

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.test.Assertion.Arguments.valueArgument
import zio.test.{ErrorMessage => M}

trait AssertionVariants {
private def diffProduct[T](
obj1: T,
obj2: T,
paramNames: List[String] = Nil,
rootClassName: Option[String] = None
): String = {
val currClassName = rootClassName.getOrElse(obj1.getClass.getSimpleName)

(obj1, obj2) match {
case (seq1: Iterable[Any], seq2: Iterable[Any]) => {
val maxSize = math.max(seq1.size, seq2.size)
val paddedSeq1 = seq1.toVector.padTo(maxSize, null)
val paddedSeq2 = seq2.toVector.padTo(maxSize, null)

paddedSeq1
.zip(paddedSeq2)
.zipWithIndex
.flatMap { case ((subObj1, subObj2), index) =>
val newParamName = s"[$index]"

if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product]) {
val paramName = s"${paramNames.reverse.mkString("")}[$index]"
Some(s"$currClassName$paramName : expected '$subObj2' got '$subObj1'\n")
} else {
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
}
.mkString
}
case (obj1: Product, obj2: Product) if obj1.productArity == obj2.productArity =>
obj1.productIterator
.zip(obj2.productIterator)
.zip(obj1.getClass.getDeclaredFields.iterator.map(_.getName))
Copy link
Member

Choose a reason for hiding this comment

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

@urbit-pilled it looks like getDeclaredFields is not working with ScalaJS. Unfortunately the ZIO CI only runs ScalaJS tests with 2.13 so this was not caught before the release but this is showing up in other repos like https://github.com/zio/zio-query/actions/runs/9015988096/job/24771683500?pr=484

Any idea for a workaround?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the late response, I've been offline this week. Looks like #8841 fixes the ScalaJS and Scala Native issue?

.flatMap { case ((subObj1, subObj2), paramName) =>
val newParamName = if (paramName.nonEmpty) s".$paramName" else ""
if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product])
s"$currClassName${paramNames.reverse.mkString("")}$newParamName : expected '$subObj2' got '$subObj1'\n"
else
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
.mkString
case _ => ""
}
}

def equalTo[A, B](expected: A)(implicit eql: Eql[A, B]): Assertion[B] =
Assertion[B](
TestArrow
.make[B, Boolean] { actual =>
val result = (actual, expected) match {
case (left: Array[_], right: Array[_]) => left.sameElements[Any](right)
case (left: CharSequence, right: CharSequence) => left.toString == right.toString
case (left, right) => left == right
}
TestTrace.boolean(result) {
if (expected.isInstanceOf[Product]) {
M.text(diffProduct(actual, expected))
} else {
M.pretty(actual) + M.equals + M.pretty(expected)
}
}
}
.withCode("equalTo", valueArgument(expected))
)
}
87 changes: 87 additions & 0 deletions test/shared/src/main/scala-2.13/zio/test/AssertionVariants.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zio.test

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.test.Assertion.Arguments.valueArgument
import zio.test.{ErrorMessage => M}

trait AssertionVariants {
private def diffProduct[T](
obj1: T,
obj2: T,
paramNames: List[String] = Nil,
rootClassName: Option[String] = None
): String = {
val currClassName = rootClassName.getOrElse(obj1.getClass.getSimpleName)

(obj1, obj2) match {
case (seq1: Iterable[Any], seq2: Iterable[Any]) => {
val maxSize = math.max(seq1.size, seq2.size)
val paddedSeq1 = seq1.toVector.padTo(maxSize, null)
val paddedSeq2 = seq2.toVector.padTo(maxSize, null)

paddedSeq1
.zip(paddedSeq2)
.zipWithIndex
.flatMap { case ((subObj1, subObj2), index) =>
val newParamName = s"[$index]"
if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product]) {
val paramName = s"${paramNames.reverse.mkString("")}[$index]"
Some(s"$currClassName$paramName : expected '$subObj2' got '$subObj1'\n")
} else {
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
}
.mkString
}
case (obj1: Product, obj2: Product) if obj1.productArity == obj2.productArity =>
obj1.productIterator
.zip(obj2.productIterator)
.zip(obj1.productElementNames)
.flatMap { case ((subObj1, subObj2), paramName) =>
val newParamName = if (paramName.nonEmpty) s".$paramName" else ""
if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product])
s"$currClassName${paramNames.reverse.mkString("")}$newParamName : expected '$subObj2' got '$subObj1'\n"
else
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
.mkString
case _ => ""
}
}

def equalTo[A, B](expected: A)(implicit eql: Eql[A, B]): Assertion[B] =
Assertion[B](
TestArrow
.make[B, Boolean] { actual =>
val result = (actual, expected) match {
case (left: Array[_], right: Array[_]) => left.sameElements[Any](right)
case (left: CharSequence, right: CharSequence) => left.toString == right.toString
case (left, right) => left == right
}
TestTrace.boolean(result) {
if (expected.isInstanceOf[Product]) {
M.text(diffProduct(actual, expected))
} else {
M.pretty(actual) + M.equals + M.pretty(expected)
}
}
}
.withCode("equalTo", valueArgument(expected))
)
}
40 changes: 0 additions & 40 deletions test/shared/src/main/scala-2/zio/test/AssertionVariants.scala

This file was deleted.

49 changes: 48 additions & 1 deletion test/shared/src/main/scala-3/zio/test/AssertionVariants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,49 @@ import zio.test.{ErrorMessage => M}
import zio.test.Assertion.Arguments.valueArgument

trait AssertionVariants {
private def diffProduct[T](
obj1: T,
obj2: T,
paramNames: List[String] = Nil,
rootClassName: Option[String] = None
): String = {
val currClassName = rootClassName.getOrElse(obj1.getClass.getSimpleName)

(obj1, obj2) match {
case (seq1: Iterable[Any], seq2: Iterable[Any]) =>{
val maxSize = math.max(seq1.size, seq2.size)
val paddedSeq1 = seq1.toVector.padTo(maxSize, null)
val paddedSeq2 = seq2.toVector.padTo(maxSize, null)

paddedSeq1
.zip(paddedSeq2)
.zipWithIndex
.flatMap { case ((subObj1, subObj2), index) =>
val newParamName = s"[$index]"
if(subObj1 != subObj2 && !subObj1.isInstanceOf[Product]){
val paramName = s"${paramNames.reverse.mkString("")}[$index]"
Some(s"$currClassName$paramName : expected '$subObj2' got '$subObj1'\n")
}else{
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
}
.mkString
}
case (obj1: Product, obj2: Product) if obj1.productArity == obj2.productArity =>
obj1.productIterator
.zip(obj2.productIterator)
.zip(obj1.productElementNames)
.flatMap { case ((subObj1, subObj2), paramName) =>
val newParamName = if (paramName.nonEmpty) s".$paramName" else ""
if (subObj1 != subObj2 && !subObj1.isInstanceOf[Product])
s"$currClassName${paramNames.reverse.mkString("")}$newParamName : expected '$subObj2' got '$subObj1'\n"
else
diffProduct(subObj1, subObj2, newParamName :: paramNames, Some(currClassName))
}
.mkString
case _ => ""
}
}

/**
* Makes a new assertion that requires a value equal the specified value.
Expand All @@ -35,7 +78,11 @@ trait AssertionVariants {
case (left, right) => left == right
}
TestTrace.boolean(result) {
M.pretty(actual) + M.equals + M.pretty(expected)
if(expected.isInstanceOf[Product]){
M.text(diffProduct(actual, expected))
}else{
M.pretty(actual) + M.equals + M.pretty(expected)
}
}
}
.withCode("equalTo", valueArgument(expected))
Expand Down
0