Giter Site home page Giter Site logo

nutri-matic's Introduction

Nutri-Matic

He had found a Nutri-Matic machine which had provided him with a plastic cup filled with a liquid that was almost, but not quite, entirely unlike tea

— Douglas Adams, The Hitchhiker's Guide to the Galaxy

Build Status

Creates arbitrary instances of scala objects using reflection. Currently supports:

  • Primitive types
  • Boxed Java primitives
  • Strings
  • Basic scala collections - Seq, List, Set, Map
  • Arrays
  • Option and Either
  • Sealed traits
  • Scala enums
  • Case classes
  • Case objects
  • Classes that have a constructor

Usage

You can use the default instance:

import com.wix.nutrimatic.NutriMatic

NutriMatic.makeA[Option[String]]

Or you can configure to your hearts content:

import com.wix.nutrimatic.NutriMatic

val nutriMatic = NutriMatic.builder // the arguments here are the default values
  .withCollectionSizes(0, 3) // created collections and arrays will have a size between these numbers (inclusive)
  .withStringLengths(1, 20) // created strings will have a length between these numbers (inclusive)
  .withCustomGenerators() // use these custom generators
  .withSeed(scala.util.Random.nextLong()) // seed used for getting random instances
  .withMaxCacheSize(10000) // this library relies on caching quite a bit, so if you run into memory issues, try reducing this value
  .build

nutriMatic.makeA[Option[String]]

Custom types and overrides

The main concept behind making values in generators, which are just partial functions.

The most basic are generators that match an exact type. Say you wanted to make sure that the ints you get are really random. You find a good code snippet and get to work:

import com.wix.nutrimatic.{Generators, NutriMatic}

val getRandomNumber = Generators.byExactType[Int] { _ => 4}

val nutriMatic = NutriMatic.builder
  .withCustomGenerators(getRandomNumber)
  .build

nutriMatic.makeA[Int] must_== 4

For generics, you can also match by an erasure of a type. Your function gets called with context which allows you to make other values and get randoms from the same seed.

import com.wix.nutrimatic.{Generators, NutriMatic}

val makeSureWeKnow = Generators.byErasure[Option[_]] {
  case (tpe, context) => Some(context.makeComponent(tpe.typeArgs.head))
}

val theAnswerToLifeTheUniverseAndEverything = Generators.byExactType[Int] { _ => 42 }

val nutriMatic = NutriMatic.builder
  .withCustomGenerators(theAnswerToLifeTheUniverseAndEverything, makeSureWeKnow)
  .build

nutriMatic.makeA[Option[Int]] must beSome(42)

Finally, you can provide your own custom generators. They are attempted before any other generators:

import com.wix.nutrimatic.{Generator, NutriMatic}

val typesStartingWithSAreAlwaysNull: Generator[Any] = {
  case (tpe, context) if tpe.toString.startsWith("S") => null
}

val nutriMatic = NutriMatic.builder
  .withCustomGenerators(typesStartingWithSAreAlwaysNull)
  .build

nutriMatic.makeA[String] must beNull
nutriMatic.makeA[Seq[Int]] must beNull
nutriMatic.makeA[Option[Int]] must not(beNull)

nutri-matic's People

Contributors

laurynaslubys avatar dkarlinsky avatar

Stargazers

Amit Peer avatar Linas Medžiūnas avatar Netta Doron avatar Aleksej Kazmin avatar Pavel Migolinets avatar Martynas Mickevičius avatar Andrius Bentkus avatar Povilas Skruibis avatar

Watchers

Ivan Kamenev avatar James Cloos avatar Yoav Abrahami avatar Boris Litvinsky avatar Netta Doron avatar Constantin Farber avatar Yoav Amit avatar Algimantas Krasauskas avatar Mariya Feldman avatar Gilad Artzi avatar  avatar Kristina avatar avgar marciano avatar Rob Rush avatar Noah Dolev avatar JJ Major avatar Jony Vesterman Cohen avatar Andrey Makarov avatar Uri Tagger avatar Olesia Zaichenko avatar Aviva Herman avatar Ariel Wine avatar Audrius Petreikis avatar Ohad Laufer avatar yaniv zadok avatar Dan Koren avatar Tomer Cohen avatar Alena Kapatsyn avatar Dmytro Lukianchuk avatar Gal Morad avatar Victoria Kirushenkova avatar ilana dreizis avatar Itay Gadot avatar Anna Tsibulskaya avatar Adam Shavit avatar Eyal Malron avatar Ron Elrom avatar gershon goland avatar Jonas Kabelka avatar Iftach Yakar avatar Cario Wix avatar Carmel Cohen avatar Guy Benron avatar Aaron Vinestock avatar Amir Uval avatar Kobi Zakut avatar Nir Yaniv avatar Yael Karo avatar Alisa Tischenko avatar Nikolay Kolesnik avatar Robert Woolfson avatar rotem peer avatar Guy Goldman avatar Chen Rosenthal avatar Nadav Abrahami avatar Ido Karnon avatar Itai Shabtai avatar Vitaliy Sobol avatar  avatar Martynas Alenskas avatar Amir Zaccai avatar Dmitriy Gordienko avatar Gustas avatar Dan Noybouer avatar Eran Frumerman avatar Dom avatar Julia Kozyreva avatar Omer Gartzman avatar  avatar Vlada Leykin avatar Doron Bartov avatar Dana avatar Dina Misochnik avatar Aleksander Denga avatar Cheli Bachar avatar  avatar Liron Chakim avatar Itay Steinberg avatar Alex Nairon avatar Aleksandra Pervunina avatar Karina Mitchulevichus avatar Ayelet Gvirsman avatar Assaf Segev avatar Ran Romano avatar Inesa Mike avatar  avatar Nancy avatar  avatar Carmit Baron avatar  avatar Edvinas Jakavičius avatar Wix Academy avatar Yael Sapir avatar  avatar  avatar Andrius Bentkus avatar

nutri-matic's Issues

Classloading problems

Currently, the default classloader is used. This can cause issues in the case of case objects, as they might get loaded twice

java.time types support

Currently getting:

java.time.Instant.<init>()
java.lang.NoSuchMethodException: 

For unknown types randomisation, I'd suggest to implement some magic that finds type constructors or static methods that are accepting primitive values.

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.