-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test newly added cats.data (except NonEmptyLazyList, yet)
- Loading branch information
1 parent
85c859c
commit b905b9e
Showing
6 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
chimney-cats/src/main/scala-2.12/io/scalaland/chimney/cats/CatsDataImplicitsCompat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.scalaland.chimney.cats | ||
|
||
/** @since 1.0.0 */ | ||
private[cats] trait CatsDataImplicitsCompat {} |
29 changes: 29 additions & 0 deletions
29
chimney-cats/src/main/scala-2.13+/io/scalaland/chimney/cats/CatsDataImplicitsCompat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.scalaland.chimney.cats | ||
|
||
import cats.data.NonEmptyLazyList | ||
import io.scalaland.chimney.integrations.* | ||
import io.scalaland.chimney.partial | ||
|
||
import scala.collection.compat.Factory | ||
import scala.collection.mutable | ||
|
||
/** @since 1.0.0 */ | ||
private[cats] trait CatsDataImplicitsCompat { | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptyLazyListIsPartiallyBuildIterable[A]: PartiallyBuildIterable[NonEmptyLazyList[A], A] = | ||
new PartiallyBuildIterable[NonEmptyLazyList[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptyLazyList[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptyLazyList[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptyLazyList[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptyLazyList[A]]] { | ||
private val impl = mutable.ListBuffer.empty[A] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptyLazyList[A]] = | ||
partial.Result.fromOption(NonEmptyLazyList.fromSeq(impl.result())) | ||
def addOne(elem: A): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptyLazyList[A]): Iterator[A] = collection.iterator | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
chimney-cats/src/main/scala/io/scalaland/chimney/cats/CatsDataImplicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package io.scalaland.chimney.cats | ||
|
||
import cats.data.{Chain, NonEmptyChain, NonEmptyList, NonEmptyMap, NonEmptySeq, NonEmptySet, NonEmptyVector} | ||
import io.scalaland.chimney.integrations.* | ||
import io.scalaland.chimney.partial | ||
|
||
import scala.collection.compat.Factory | ||
import scala.collection.mutable | ||
|
||
/** @since 1.0.0 */ | ||
trait CatsDataImplicits extends CatsDataImplicitsCompat { | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsChainIsTotallyBuildIterable[A]: TotallyBuildIterable[Chain[A], A] = | ||
new TotallyBuildIterable[Chain[A], A] { | ||
def totalFactory: Factory[A, Chain[A]] = new FactoryCompat[A, Chain[A]] { | ||
def newBuilder: mutable.Builder[A, Chain[A]] = new FactoryCompat.Builder[A, Chain[A]] { | ||
private var impl = Chain.empty[A] | ||
def clear(): Unit = impl = Chain.empty[A] | ||
def result(): Chain[A] = impl | ||
def addOne(elem: A): this.type = { impl = impl.append(elem); this } | ||
} | ||
} | ||
def iterator(collection: Chain[A]): Iterator[A] = collection.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptyChainIsPartiallyBuildIterable[A]: PartiallyBuildIterable[NonEmptyChain[A], A] = | ||
new PartiallyBuildIterable[NonEmptyChain[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptyChain[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptyChain[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptyChain[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptyChain[A]]] { | ||
private var impl = Chain.empty[A] | ||
def clear(): Unit = impl = Chain.empty[A] | ||
def result(): partial.Result[NonEmptyChain[A]] = partial.Result.fromOption(NonEmptyChain.fromChain(impl)) | ||
def addOne(elem: A): this.type = { impl = impl.append(elem); this } | ||
} | ||
} | ||
def iterator(collection: NonEmptyChain[A]): Iterator[A] = collection.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptyListIsPartiallyBuildIterable[A]: PartiallyBuildIterable[NonEmptyList[A], A] = | ||
new PartiallyBuildIterable[NonEmptyList[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptyList[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptyList[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptyList[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptyList[A]]] { | ||
private val impl = List.newBuilder[A] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptyList[A]] = | ||
partial.Result.fromOption(NonEmptyList.fromList(impl.result())) | ||
def addOne(elem: A): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptyList[A]): Iterator[A] = collection.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptyMapIsPartiallyBuildMap[K: Ordering, V]: PartiallyBuildMap[NonEmptyMap[K, V], K, V] = | ||
new PartiallyBuildMap[NonEmptyMap[K, V], K, V] { | ||
def partialFactory: Factory[(K, V), partial.Result[NonEmptyMap[K, V]]] = | ||
new FactoryCompat[(K, V), partial.Result[NonEmptyMap[K, V]]] { | ||
def newBuilder: mutable.Builder[(K, V), partial.Result[NonEmptyMap[K, V]]] = | ||
new FactoryCompat.Builder[(K, V), partial.Result[NonEmptyMap[K, V]]] { | ||
private val impl = scala.collection.immutable.SortedMap.newBuilder[K, V] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptyMap[K, V]] = | ||
partial.Result.fromOption(NonEmptyMap.fromMap(impl.result())) | ||
def addOne(elem: (K, V)): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptyMap[K, V]): Iterator[(K, V)] = collection.toSortedMap.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptySeqIsPartiallyBuildIterable[A]: PartiallyBuildIterable[NonEmptySeq[A], A] = | ||
new PartiallyBuildIterable[NonEmptySeq[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptySeq[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptySeq[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptySeq[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptySeq[A]]] { | ||
private val impl = mutable.ListBuffer.empty[A] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptySeq[A]] = | ||
partial.Result.fromOption(NonEmptySeq.fromSeq(impl.result())) | ||
def addOne(elem: A): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptySeq[A]): Iterator[A] = collection.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptySetIsPartiallyBuildIterable[A: Ordering]: PartiallyBuildIterable[NonEmptySet[A], A] = | ||
new PartiallyBuildIterable[NonEmptySet[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptySet[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptySet[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptySet[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptySet[A]]] { | ||
private val impl = scala.collection.immutable.SortedSet.newBuilder[A] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptySet[A]] = | ||
partial.Result.fromOption(NonEmptySet.fromSet(impl.result())) | ||
def addOne(elem: A): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptySet[A]): Iterator[A] = collection.toNonEmptyList.iterator | ||
} | ||
|
||
/** @since 1.0.0 */ | ||
implicit def catsNonEmptyVectorIsPartiallyBuildIterable[A]: PartiallyBuildIterable[NonEmptyVector[A], A] = | ||
new PartiallyBuildIterable[NonEmptyVector[A], A] { | ||
def partialFactory: Factory[A, partial.Result[NonEmptyVector[A]]] = | ||
new FactoryCompat[A, partial.Result[NonEmptyVector[A]]] { | ||
def newBuilder: mutable.Builder[A, partial.Result[NonEmptyVector[A]]] = | ||
new FactoryCompat.Builder[A, partial.Result[NonEmptyVector[A]]] { | ||
private val impl = Vector.newBuilder[A] | ||
def clear(): Unit = impl.clear() | ||
def result(): partial.Result[NonEmptyVector[A]] = | ||
partial.Result.fromOption(NonEmptyVector.fromVector(impl.result())) | ||
def addOne(elem: A): this.type = { impl += elem; this } | ||
} | ||
} | ||
def iterator(collection: NonEmptyVector[A]): Iterator[A] = collection.iterator | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
chimney-cats/src/test/scala/io/scalaland/chimney/cats/CatsDataSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.scalaland.chimney.cats | ||
|
||
import cats.data.{Chain, NonEmptyChain, NonEmptyList, NonEmptyMap, NonEmptySeq, NonEmptySet, NonEmptyVector} | ||
import io.scalaland.chimney.ChimneySpec | ||
import io.scalaland.chimney.dsl.* | ||
|
||
class CatsDataSpec extends ChimneySpec { | ||
|
||
test("DSL should handle transformation to and from cats.data.Chain") { | ||
List("test").transformInto[Chain[String]] ==> Chain("test") | ||
|
||
Chain("test").transformInto[List[String]] ==> List("test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptyChain") { | ||
List("test").transformIntoPartial[NonEmptyChain[String]].asOption ==> Some(NonEmptyChain.one("test")) | ||
List.empty[String].transformIntoPartial[NonEmptyChain[String]].asOption ==> None | ||
|
||
NonEmptyChain.one("test").transformInto[List[String]] ==> List("test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptyList") { | ||
List("test").transformIntoPartial[NonEmptyList[String]].asOption ==> Some(NonEmptyList.one("test")) | ||
List.empty[String].transformIntoPartial[NonEmptyList[String]].asOption ==> None | ||
|
||
NonEmptyList.one("test").transformInto[List[String]] ==> List("test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptyMap") { | ||
List("test" -> "test").transformIntoPartial[NonEmptyMap[String, String]].asOption ==> Some( | ||
NonEmptyMap.one("test", "test") | ||
) | ||
List.empty[(String, String)].transformIntoPartial[NonEmptyMap[String, String]].asOption ==> None | ||
|
||
NonEmptyMap.one("test", "test").transformInto[List[(String, String)]] ==> List("test" -> "test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptySeq") { | ||
List("test").transformIntoPartial[NonEmptySeq[String]].asOption ==> Some(NonEmptySeq.one("test")) | ||
List.empty[String].transformIntoPartial[NonEmptySeq[String]].asOption ==> None | ||
|
||
NonEmptySeq.one("test").transformInto[List[String]] ==> List("test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptySet") { | ||
List("test").transformIntoPartial[NonEmptySet[String]].asOption ==> Some(NonEmptySet.one("test")) | ||
List.empty[String].transformIntoPartial[NonEmptySet[String]].asOption ==> None | ||
|
||
NonEmptySet.one("test").transformInto[List[String]] ==> List("test") | ||
} | ||
|
||
test("DSL should handle transformation to and from cats.data.NonEmptyVector") { | ||
List("test").transformIntoPartial[NonEmptyVector[String]].asOption ==> Some(NonEmptyVector.one("test")) | ||
List.empty[String].transformIntoPartial[NonEmptyVector[String]].asOption ==> None | ||
|
||
NonEmptyVector.one("test").transformInto[List[String]] ==> List("test") | ||
} | ||
} |