Giter VIP home page Giter VIP logo

freestyle-macro's Introduction

freestyle-macro

Macros to make using Freestyle easier

Macros

  • Unit

Unit

@Unit is a macro that allows you to access methods in your handler to perform unit tests on the overriden functions in your given handler.

Imagine you created an algebra called Algebra:

  @free trait Algebra{
    def f(a: String): FS[String]
    def g(b: Int): FS[Int]
    def h(c: Double)(d: String): FS[(Double, String)]
  }

Then you create a Handler as such:

  implicit val algebraHandler: Algebra.Handler[Id] = new Algebra.Handler[Id]{
    override def f(a: String): Id[String] = a.toLowerCase
    override def g(b: Int): Id[Int] = b - 1
    override def h(c: Double)(d: String): Id[(Double, String)] = (c, d)
  }

The only way to test the functions you overrode in your handler are correct is by integration test.

If you would like to unit test your overriden handler functions, simply use the @Unit macro as such:

  @Unit implicit val algebraHandler: Algebra.Handler[Id] = new Algebra.Handler[Id]{
    override def f(a: String): Id[String] = a.toLowerCase
    override def g(b: Int): Id[Int] = b - 1
    override def h(c: Double)(d: String): Id[(Double, String)] = (c, d)
  }

Now you can access f, g, or h by simply calling the function via AlgebraHandler.f, AlgebraHandler.g, or AlgebraHandler.h

  scala> AlgebraHandler.f("A")
  "a"
  scala> AlgebraHandler.g(1)
  0
  scala> AlgebraHandler.h(0.0)("A")
  (0.0, "A")

@Unit allows you to access these overriden funcitons and now you may perform unit tests instead of solely relying on integration tests, improving the validity of you freestyle applications.

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.