Skip to content

Commit

Permalink
Reformat and check formatting in CI build (#65)
Browse files Browse the repository at this point in the history
* Reformat and check formatting in CI build

* scalafmtAll
  • Loading branch information
fthomas authored Sep 10, 2020
1 parent 929d4d5 commit 0808da5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ install:
- pip install --user codecov

script:
- sbt ++$TRAVIS_SCALA_VERSION clean coverage test coverageReport scalastyle scalafmt test:scalafmt scalafmtSbt && codecov
- sbt ++$TRAVIS_SCALA_VERSION clean coverage test coverageReport scalastyle scalafmtCheckAll scalafmtSbtCheck && codecov

# See http://www.scala-sbt.org/0.13/docs/Travis-CI-with-sbt.html
# Tricks to avoid unnecessary cache updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ object Bar {
)

val decodeBar: Decoder[Bar] = Decoder.forProduct2("i", "s")(Bar.apply)
val encodeBar: Encoder[Bar] = Encoder.forProduct2("i", "s") {
case Bar(i, s) => (i, s)
val encodeBar: Encoder[Bar] = Encoder.forProduct2("i", "s") { case Bar(i, s) =>
(i, s)
}
}

Expand All @@ -43,8 +43,8 @@ object Baz {
)

implicit val decodeBaz: Decoder[Baz] = Decoder[List[String]].map(Baz(_))
implicit val encodeBaz: Encoder[Baz] = Encoder.instance {
case Baz(xs) => Json.fromValues(xs.map(Json.fromString))
implicit val encodeBaz: Encoder[Baz] = Encoder.instance { case Baz(xs) =>
Json.fromValues(xs.map(Json.fromString))
}
}

Expand All @@ -58,8 +58,8 @@ object Bam {
)

val decodeBam: Decoder[Bam] = Decoder.forProduct2("w", "d")(Bam.apply)(Wub.decodeWub, implicitly)
val encodeBam: Encoder[Bam] = Encoder.forProduct2[Bam, Wub, Double]("w", "d") {
case Bam(w, d) => (w, d)
val encodeBam: Encoder[Bam] = Encoder.forProduct2[Bam, Wub, Double]("w", "d") { case Bam(w, d) =>
(w, d)
}(Wub.encodeWub, implicitly)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ trait GoldenCodecLaws[A] extends CodecLaws[A] {
protected def goldenExamples: Try[List[(A, String)]]

final def goldenDecoding: Try[List[IsEq[A]]] = goldenExamples.flatMap {
_.traverse {
case (value, encoded) =>
io.circe.parser.decode[A](encoded)(decode) match {
case Left(error) => Failure(error)
case Right(decoded) => Success(decoded <-> value)
}
_.traverse { case (value, encoded) =>
io.circe.parser.decode[A](encoded)(decode) match {
case Left(error) => Failure(error)
case Right(decoded) => Success(decoded <-> value)
}
}
}

final def goldenEncoding: Try[List[IsEq[String]]] = goldenExamples.map {
_.map {
case (value, encoded) =>
printJson(encode(value)) <-> encoded
_.map { case (value, encoded) =>
printJson(encode(value)) <-> encoded
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ trait GoldenCodecTests[A] extends CodecTests[A] {
case Success(equalities) => Prop.all(equalities.map(catsLawsIsEqToProp(_)): _*)
}

def goldenCodec(
implicit
def goldenCodec(implicit
arbitraryA: Arbitrary[A],
shrinkA: Shrink[A],
eqA: Eq[A],
Expand All @@ -32,8 +31,7 @@ trait GoldenCodecTests[A] extends CodecTests[A] {
"encoding golden files" -> tryListToProp(laws.goldenEncoding)
)

def unserializableGoldenCodec(
implicit
def unserializableGoldenCodec(implicit
arbitraryA: Arbitrary[A],
shrinkA: Shrink[A],
eqA: Eq[A],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ abstract class ResourceFileGoldenCodecLaws[A](
with ExampleGeneration[A] {

private[this] val resourceRootPath: String = "/" + resourcePackage.mkString("/") + "/"
private[this] val resourceDir: File = resourcePackage.foldLeft(resourceRootDir) {
case (acc, p) => new File(acc, p)
private[this] val resourceDir: File = resourcePackage.foldLeft(resourceRootDir) { case (acc, p) =>
new File(acc, p)
}
private[this] val GoldenFilePattern: Regex = "^-(.{44})\\.json$".r

Expand All @@ -34,14 +34,13 @@ abstract class ResourceFileGoldenCodecLaws[A](
case _ => None
}
case _ => None
}.toList.traverse[Try, (A, String)] {
case (seed, name) =>
val contents = Resources.open(resourceRootPath + name).map { source =>
val lines = source.getLines.mkString("\n")
source.close()
lines
}
(getValueFromBase64Seed(seed), contents).tupled
}.toList.traverse[Try, (A, String)] { case (seed, name) =>
val contents = Resources.open(resourceRootPath + name).map { source =>
val lines = source.getLines.mkString("\n")
source.close()
lines
}
(getValueFromBase64Seed(seed), contents).tupled
}

dirSource.close()
Expand All @@ -54,18 +53,17 @@ abstract class ResourceFileGoldenCodecLaws[A](
}

private[this] def generateGoldenFiles: Try[List[(A, String)]] =
generateRandomGoldenExamples(count).traverse {
case (seed, value, encoded) =>
Try {
resourceDir.mkdirs()
val file = new File(resourceDir, s"$name-${seed.toBase64}.json")
generateRandomGoldenExamples(count).traverse { case (seed, value, encoded) =>
Try {
resourceDir.mkdirs()
val file = new File(resourceDir, s"$name-${seed.toBase64}.json")

val writer = new PrintWriter(file)
writer.print(encoded)
writer.close()
val writer = new PrintWriter(file)
writer.print(encoded)
writer.close()

(value, encoded)
}
(value, encoded)
}
}

protected lazy val goldenExamples: Try[List[(A, String)]] =
Expand All @@ -79,7 +77,7 @@ object ResourceFileGoldenCodecLaws {
resourcePackage: List[String],
size: Int,
count: Int,
printer: Printer
printer: Printer
)(implicit decodeA: Decoder[A], encodeA: Encoder[A], arbitraryA: Arbitrary[A]): GoldenCodecLaws[A] =
new ResourceFileGoldenCodecLaws[A](name, resourceRootDir, resourcePackage, size, count, printer) {
val decode: Decoder[A] = decodeA
Expand All @@ -91,8 +89,8 @@ object ResourceFileGoldenCodecLaws {
size: Int = 100,
count: Int = 1,
printer: Printer = Printer.spaces2
)(
implicit decodeA: Decoder[A],
)(implicit
decodeA: Decoder[A],
encodeA: Encoder[A],
arbitraryA: Arbitrary[A],
typeTagA: TypeTag[A]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ object Resources {
lazy val inferRootDir: File = {
var current = new File(getClass.getResource("/").toURI)

while (current.ne(null) && current.getName != "target") {
while (current.ne(null) && current.getName != "target")
current = current.getParentFile
}

val resourceDir = new File(new File(new File(current.getParentFile, "src"), "test"), "resources")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ trait VisitRepositoryTestInstances extends VisitTestInstances with ArbitraryInst
)
}

class OldVisitRepositorySuite extends AnyFlatSpec with FlatSpecDiscipline with Configuration with VisitRepositoryTestInstances {
class OldVisitRepositorySuite
extends AnyFlatSpec
with FlatSpecDiscipline
with Configuration
with VisitRepositoryTestInstances {
checkAll("Codec[VisitRepository]", CodecTests[VisitRepository].codec)

val good = """{"visits":{"1":{"id":12345,"page":"/index.html","ts":"2019-10-22T14:54:13Z"}}}"""
Expand All @@ -44,6 +48,10 @@ class OldVisitRepositorySuite extends AnyFlatSpec with FlatSpecDiscipline with C

import io.circe.testing.golden.GoldenCodecTests

class VisitRepositorySuite extends AnyFlatSpec with FlatSpecDiscipline with Configuration with VisitRepositoryTestInstances {
class VisitRepositorySuite
extends AnyFlatSpec
with FlatSpecDiscipline
with Configuration
with VisitRepositoryTestInstances {
checkAll("GoldenCodec[VisitRepository]", GoldenCodecTests[VisitRepository](Printer.spaces2SortKeys).goldenCodec)
}

0 comments on commit 0808da5

Please sign in to comment.