Skip to content

Commit

Permalink
Merge pull request #43 from alexarchambault/topic/tidying
Browse files Browse the repository at this point in the history
Tidying / refactoring
  • Loading branch information
alexarchambault committed May 14, 2016
2 parents 06953fe + c56e810 commit 1fdadf4
Show file tree
Hide file tree
Showing 19 changed files with 1,321 additions and 1,358 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scala:
- 2.10.6
jdk:
- oraclejdk8
script: sbt ++${TRAVIS_SCALA_VERSION} test:compile scalacheckShapelessJVM/test $(if false; then echo "mima-report-binary-issues"; fi) $(if [[ "${TRAVIS_PULL_REQUEST}" ==
script: sbt -batch ++${TRAVIS_SCALA_VERSION} test:compile testJVM/test $(if false; then echo "mima-report-binary-issues"; fi) $(if [[ "${TRAVIS_PULL_REQUEST}" ==
"false" && "${TRAVIS_BRANCH}" == "master" ]]; then echo "publish"; fi)
sudo: false
env:
Expand Down
54 changes: 34 additions & 20 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
import com.typesafe.sbt.pgp.PgpKeys

import com.typesafe.tools.mima.plugin.MimaPlugin.mimaDefaultSettings
import com.typesafe.tools.mima.plugin.MimaKeys.previousArtifact

lazy val root = project.in(file("."))
.aggregate(scalacheckShapelessJVM, scalacheckShapelessJS)
lazy val `scalacheck-shapeless` = project.in(file("."))
.aggregate(coreJVM, coreJS, testJVM, testJS)
.settings(commonSettings)
.settings(noPublishSettings)

lazy val scalacheckShapeless = crossProject.in(file("."))
lazy val core = crossProject
.settings(commonSettings: _*)
.settings(mimaSettings: _*)
.settings(
name := coreName,
moduleName := coreName,
libraryDependencies ++= Seq(
"org.scalacheck" %%% "scalacheck" % "1.13.1",
"com.chuusai" %%% "shapeless" % "2.2.5",
"com.github.alexarchambault" %%% "shapeless-compat" % "1.0.0-M4"
)
)
.jsSettings(
postLinkJSEnv := NodeJSEnv().value,
scalaJSUseRhino in Global := false,
scalaJSStage in Test := FastOptStage
)

lazy val scalacheckShapelessJVM = scalacheckShapeless.jvm
lazy val scalacheckShapelessJS = scalacheckShapeless.js
lazy val coreJVM = core.jvm
lazy val coreJS = core.js

lazy val test = crossProject
.dependsOn(core)
.settings(commonSettings: _*)
.settings(noPublishSettings: _*)
.settings(
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test",
testFrameworks += new TestFramework("utest.runner.Framework")
)
.jsSettings(
scalaJSStage in Test := FastOptStage
)

lazy val testJVM = test.jvm
lazy val testJS = test.js

lazy val coreName = "scalacheck-shapeless_1.13"

lazy val commonSettings = Seq(
organization := "com.github.alexarchambault",
name := coreName,
moduleName := coreName
organization := "com.github.alexarchambault"
) ++ compileSettings ++ publishSettings

lazy val compileSettings = Seq(
scalaVersion := "2.11.8",
unmanagedSourceDirectories in Compile += (baseDirectory in Compile).value / ".." / "shared" / "src" / "main" / s"scala-${scalaBinaryVersion.value}",
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test",
testFrameworks += new TestFramework("utest.runner.Framework"),
resolvers ++= Seq(
Resolver.sonatypeRepo("releases"),
Resolver.sonatypeRepo("snapshots")
),
libraryDependencies ++= Seq(
"org.scalacheck" %%% "scalacheck" % "1.13.1",
"com.chuusai" %%% "shapeless" % "2.2.5",
"com.github.alexarchambault" %%% "shapeless-compat" % "1.0.0-M4"
),
libraryDependencies ++= {
if (scalaVersion.value.startsWith("2.10."))
Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.0.1" cross CrossVersion.full))
Expand Down Expand Up @@ -75,12 +89,12 @@ lazy val publishSettings = Seq(
else
"releases" at nexus + "service/local/staging/deploy/maven2"
},
credentials += {
credentials ++= {
Seq("SONATYPE_USER", "SONATYPE_PASS").map(sys.env.get) match {
case Seq(Some(user), Some(pass)) =>
Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", user, pass)
Seq(Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", user, pass))
case _ =>
Credentials(Path.userHome / ".ivy2" / ".credentials")
Seq()
}
}
)
Expand Down
10 changes: 10 additions & 0 deletions core/shared/src/main/scala/org/scalacheck/Shapeless.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalacheck

import derive._

object Shapeless
extends SingletonInstances
with HListInstances
with CoproductInstances
with DerivedInstances
with FieldTypeInstances
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.scalacheck
package org.scalacheck.derive

import shapeless.{ Lazy => _, _ }
import org.scalacheck.{Arbitrary, Cogen, Gen, Shrink}
import shapeless.compat.{Ignoring, LowPriority, Strict}
import shapeless.{Coproduct, HList, Witness}
import shapeless.labelled._
import shapeless.compat._

import derive._
import util._

trait SingletonInstances {

Expand All @@ -16,12 +14,12 @@ trait SingletonInstances {
Arbitrary(Gen.const(w.value))

/**
* Derives `Cogen[T]` instances for `T` a singleton type, like
* `Witness.``"str"``.T` or `Witness.``true``.T` for example.
*
* The generated `Cogen[T]` behaves like `Cogen[Unit]`, as like
* `Unit`, singleton types only have one instance.
*/
* Derives `Cogen[T]` instances for `T` a singleton type, like
* `Witness.``"str"``.T` or `Witness.``true``.T` for example.
*
* The generated `Cogen[T]` behaves like `Cogen[Unit]`, as like
* `Unit`, singleton types only have one instance.
*/
implicit def cogenSingletonType[S]
(implicit
w: Witness.Aux[S]
Expand Down Expand Up @@ -61,76 +59,66 @@ trait FieldTypeInstances {

trait HListInstances {

implicit def mkHListArbitrary[L <: HList]
implicit def hlistArbitrary[L <: HList]
(implicit
arb: MkHListArbitrary[L]
): Arbitrary[L] =
arb.arbitrary

implicit def mkHListCogen[L <: HList]
implicit def hlistCogen[L <: HList]
(implicit
arb: MkHListCogen[L]
): Cogen[L] =
arb.cogen

implicit def mkHListShrink[L <: HList]
implicit def hlistShrink[L <: HList]
(implicit
arb: MkHListShrink[L]
): Shrink[L] =
arb.shrink

}

trait CoproductInstances {

implicit def mkCoproductArbitrary[C <: Coproduct]
implicit def coproductArbitrary[C <: Coproduct]
(implicit
arb: MkCoproductArbitrary[C]
): Arbitrary[C] =
arb.arbitrary

implicit def mkCoproductCogen[C <: Coproduct]
implicit def coproductCogen[C <: Coproduct]
(implicit
arb: MkCoproductCogen[C]
): Cogen[C] =
arb.cogen

implicit def mkCoproductShrink[C <: Coproduct]
implicit def coproductShrink[C <: Coproduct]
(implicit
arb: MkCoproductShrink[C]
): Shrink[C] =
arb.shrink

}

trait DerivedInstances {

implicit def mkArbitrary[T]
implicit def derivedArbitrary[T]
(implicit
ev: Strict[LowPriority[Arbitrary[T]]],
priority: Strict[MkArbitrary[T]]
underlying: Strict[MkArbitrary[T]]
): Arbitrary[T] =
priority.value.arbitrary
underlying.value.arbitrary

implicit def mkShrink[T]
implicit def derivedShrink[T]
(implicit
ev: Strict[LowPriority[Ignoring[Witness.`"Shrink.shrinkAny"`.T, Shrink[T]]]],
priority: Strict[MkShrink[T]]
underlying: Strict[MkShrink[T]]
): Shrink[T] =
priority.value.shrink
underlying.value.shrink

implicit def mkCogen[T]
implicit def derivedCogen[T]
(implicit
ev: Strict[LowPriority[Cogen[T]]],
priority: Strict[MkCogen[T]]
underlying: Strict[MkCogen[T]]
): Cogen[T] =
priority.value.cogen

underlying.value.cogen
}

object Shapeless
extends SingletonInstances
with HListInstances
with CoproductInstances
with DerivedInstances
with FieldTypeInstances
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ trait MkArbitrary[T] {
def arbitrary: Arbitrary[T]
}

object MkArbitrary {
def apply[T](implicit mkArb: MkArbitrary[T]): MkArbitrary[T] = mkArb

def instance[T](arb: => Arbitrary[T]): MkArbitrary[T] =
new MkArbitrary[T] {
def arbitrary = arb
}

implicit def genericProduct[P, L <: HList]
(implicit
gen: Generic.Aux[P, L],
mkArb: Lazy[MkHListArbitrary[L]]
): MkArbitrary[P] =
instance(
Arbitrary(Gen.lzy(mkArb.value.arbitrary.arbitrary).map(gen.from))
)

implicit def genericCoproduct[S, C <: Coproduct]
(implicit
gen: Generic.Aux[S, C],
mkArb: Lazy[MkCoproductArbitrary[C]]
): MkArbitrary[S] =
instance(
Arbitrary(Gen.lzy(mkArb.value.arbitrary.arbitrary).map(gen.from))
)
}


trait MkHListArbitrary[L <: HList] {
/** `Arbitrary[T]` instance built by this `MkArbitraryHList[T]` */
Expand All @@ -28,22 +55,22 @@ trait MkHListArbitrary[L <: HList] {
object MkHListArbitrary {
def apply[L <: HList](implicit mkArb: MkHListArbitrary[L]): MkHListArbitrary[L] = mkArb

def of[L <: HList](arb: => Arbitrary[L]): MkHListArbitrary[L] =
def instance[L <: HList](arb: => Arbitrary[L]): MkHListArbitrary[L] =
new MkHListArbitrary[L] {
def arbitrary = arb
}

implicit val hnilMkArb: MkHListArbitrary[HNil] =
of(Arbitrary(Gen.const(HNil)))
implicit val hnil: MkHListArbitrary[HNil] =
instance(Arbitrary(Gen.const(HNil)))

implicit def hconsMkArb[H, T <: HList, N <: Nat]
implicit def hcons[H, T <: HList, N <: Nat]
(implicit
headArbitrary: Strict[Arbitrary[H]],
tailArbitrary: MkHListArbitrary[T],
length: ops.hlist.Length.Aux[T, N],
n: ops.nat.ToInt[N]
): MkHListArbitrary[H :: T] =
of(
instance(
Arbitrary {
Gen.sized { size =>
val sig = math.signum(size)
Expand Down Expand Up @@ -73,22 +100,22 @@ trait MkCoproductArbitrary[C <: Coproduct] {
object MkCoproductArbitrary {
def apply[C <: Coproduct](implicit mkArb: MkCoproductArbitrary[C]): MkCoproductArbitrary[C] = mkArb

def of[C <: Coproduct](arb: => Arbitrary[C]): MkCoproductArbitrary[C] =
def instance[C <: Coproduct](arb: => Arbitrary[C]): MkCoproductArbitrary[C] =
new MkCoproductArbitrary[C] {
def arbitrary = arb
}

implicit val cnilMkArb: MkCoproductArbitrary[CNil] =
of(Arbitrary(Gen.fail))
implicit val cnil: MkCoproductArbitrary[CNil] =
instance(Arbitrary(Gen.fail))

implicit def cconsMkArb[H, T <: Coproduct, N <: Nat]
implicit def ccons[H, T <: Coproduct, N <: Nat]
(implicit
headArbitrary: Strict[Arbitrary[H]],
tailArbitrary: MkCoproductArbitrary[T],
length: ops.coproduct.Length.Aux[T, N],
n: ops.nat.ToInt[N]
): MkCoproductArbitrary[H :+: T] =
of(
instance(
Arbitrary {
Gen.sized {
case 0 => Gen.fail
Expand All @@ -103,31 +130,3 @@ object MkCoproductArbitrary {
}
)
}


object MkArbitrary {
def apply[T](implicit mkArb: MkArbitrary[T]): MkArbitrary[T] = mkArb

def of[T](arb: => Arbitrary[T]): MkArbitrary[T] =
new MkArbitrary[T] {
def arbitrary = arb
}

implicit def genericProductMkArb[P, L <: HList]
(implicit
gen: Generic.Aux[P, L],
mkArb: Lazy[MkHListArbitrary[L]]
): MkArbitrary[P] =
of(
Arbitrary(Gen.lzy(mkArb.value.arbitrary.arbitrary).map(gen.from))
)

implicit def genericCoproductMkArb[S, C <: Coproduct]
(implicit
gen: Generic.Aux[S, C],
mkArb: Lazy[MkCoproductArbitrary[C]]
): MkArbitrary[S] =
of(
Arbitrary(Gen.lzy(mkArb.value.arbitrary.arbitrary).map(gen.from))
)
}
Loading

0 comments on commit 1fdadf4

Please sign in to comment.