项目作者: jacobBaumbach

项目描述 :
Macros to make using Freestyle easier
高级语言: Scala
项目地址: git://github.com/jacobBaumbach/freestyle-macro.git
创建时间: 2017-09-02T22:46:41Z
项目社区:https://github.com/jacobBaumbach/freestyle-macro

开源协议:

下载


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:

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

Then you create a Handler as such:

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

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:

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

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

  1. scala> AlgebraHandler.f("A")
  2. "a"
  3. scala> AlgebraHandler.g(1)
  4. 0
  5. scala> AlgebraHandler.h(0.0)("A")
  6. (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.