GithubHelp home page GithubHelp logo

chocpanda / scalacheck-magnolia Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 6.0 207 KB

Scalacheck Arbitrary type class derivation using magnolia

License: Apache License 2.0

Scala 99.13% Shell 0.87%
scala scalacheck magnolia tests test property-based-testing

scalacheck-magnolia's Introduction

ScalaCheck Magnolia

Codacy Badge Build Status 2.12 - Maven Central 2.13 - Maven Central

This library will derive instances of the Arbitrary type class from scalacheck using Magnolia. The functionality would be very similar to scalacheck-shapeless but hopefully with the compile time benefits that magnolia provides over shapeless.

It's very simple to use simply add to your build.sbt:

    libraryDependencies += "com.github.chocpanda" %% "scalacheck-magnolia" % "0.5.1"

and import:

   import org.scalacheck.magnolia._

into the relevant test files for instances of Arbitrary for all your testing needs

Examples

It can derive arbitrary instances of product or sum types, and because magnolia does so recursively it can also derive instances of nested data types.

package org.scalacheck.magnolia.example

import org.scalacheck.magnolia._
import org.scalacheck.Prop._
import utest._

object ExampleTest extends TestSuite {
    
    sealed trait Entity
    
    final case class Company(name: String)                      extends Entity
    final case class Human(name: String, age: Int)              extends Entity
    final case class Address(line1: String, occupant: Human)    extends Entity

    val tests = Tests {
        "My first test description" - forAll {
            company: Company =>
            // Test for an arbitrary company...
        }.check()
        
        "My second test description" - forAll {
            entity: Entity =>
            // Test for an arbitrary Entity... Could be any Company, Human or Address
        }.check()
    }
}

It works with recursive types:

package org.scalacheck.magnolia.example

import org.scalacheck.magnolia._
import org.scalacheck.Prop._
import utest._

object ExampleTest extends TestSuite {

    sealed trait Tree
    final case class Leaf(value: String)             extends Tree
    final case class Branch(left: Tree, right: Tree) extends Tree

    val tests = Tests {
        "My test description" - forAll {
            tree: Tree =>
            // Test for an arbitrary tree...
        }.check()
    }
}

It even works with Generics:

package org.scalacheck.magnolia.example

import org.scalacheck.magnolia._
import org.scalacheck.Prop._
import utest._

object ExampleTest extends TestSuite {
    
    sealed trait GTree[+T]
    final case class GLeaf[+T](value: T)                          extends GTree[T]
    final case class GBranch[+T](left: GTree[T], right: GTree[T]) extends GTree[T]

    val tests = Tests {
        "My first test description" - forAll {
            gTree: GTree[Int] =>
            // Test for an arbitrary IntTree...
        }.check()
    }
}

Contribution policy

Contributions via GitHub pull requests are gladly accepted from their original author. Along with any pull requests, please state that the contribution is your original work and that you license the work to the project under the project's open source license. Whether or not you state this explicitly, by submitting any copyrighted material via pull request, email, or other means you agree to license the material under the project's open source license and warrant that you have the legal authority to do so.

License

This code is open source software licensed under the Apache-2.0 license.

scalacheck-magnolia's People

Contributors

asakaev avatar chocpanda avatar codacy-badger avatar howyp avatar kubukoz avatar scala-steward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

scalacheck-magnolia's Issues

Possible deprecation?

Hi @ChocPanda,

Personally I have been using https://github.com/spotify/magnolify's scalacheck module for a while instead of this project. They have both Arbitrary and Cogen derivation, and the project is maintained under a company's umbrella, which makes it look like it'll be there for a while.

Given that scalacheck-magnolia hasn't had much development recently, and that there's a new Scala version coming, I would consider deprecating the project and suggesting that new users use Magnolify instead.

What do you think?

Request to separate semi-auto vs auto derivation

Would you consider providing a non-implicit gen for semiauto derivation, e.g.

// Traditional behavior, auto-derivation
import org.scalacheck.magnolia.auto._

and

// Semi-auto derivation (gen not implicit and renamed)
import org.scalacheck.magnolia.semiauto._

case class Foo(...)
object Foo {
  // deriveArbitrary is just a non-implicit version of gen
  // like circe, caches result to save compile time and give predictable behavior
  implicit val arbFoo: Arbitrary[Foo] = deriveArbitrary[Foo]  
}

I think this is just a couple lines of change. This solves two problems I've seen:

  • Bloated compile times due to needlessly repeated macro generation for the same type
    • This is really bad for some reason with protobuf generated enum classes
  • Differences in how an arbitrary Foo is populated, among different tests, depending on what's in scope

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.