Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macwire DI #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.mattmoore.scala.playground.di

import com.softwaremill.macwire.wire
import com.softwaremill.tagging._
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

class MacwireTaggingSpec extends AnyFunSpec with Matchers {
describe("without macwire") {
case class Milk()

case class CoffeeBean() {
def description = "not implemented"
}

trait DefaultBean extends CoffeeBean {
override def description = "default coffee bean"
}

trait MonkeyBean extends CoffeeBean {
override def description = "monkey bean"
}

case class Coffee(milk: Milk, defaultBean: CoffeeBean, monkeyBean: CoffeeBean)

val milk = new Milk
val defaultBean = new CoffeeBean with DefaultBean
val monkeyBean = new CoffeeBean with MonkeyBean
val coffee = new Coffee(milk, defaultBean, monkeyBean)

it("should initialize correctly") {
coffee should not be (null)
}
}

describe("with macwire") {
case class Milk()

// trait DefaultBean {
// def description = "default coffee bean"
// }

trait MonkeyBean {
def description = "monkey bean"
}

case class CoffeeBean() {
def description = "not implemented"
}

case class Coffee(milk: Milk, bean: CoffeeBean)

val milk = wire[Milk]
val bean = wire[CoffeeBean].taggedWith[MonkeyBean]
val coffee = wire[Coffee]

it("should inject things") {
coffee should not be (null)
coffee.bean.description shouldBe "monkey bean"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.mattmoore.scala.playground.di

import com.softwaremill.macwire.wire
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

class SimpleMacwireSpec extends AnyFunSpec with Matchers {
case class Milk()
case class CoffeeBean() {
def beanType: String = "monkey poop"
}
case class Coffee(milk: Milk, coffeeBean: CoffeeBean)

describe("without macwire") {
val milk = new Milk
val coffeeBean = new CoffeeBean
val coffee = new Coffee(milk, coffeeBean)
coffee should not be (null)
coffee.coffeeBean.beanType shouldBe "monkey poop"
}

describe("with macwire") {
val milk = wire[Milk]
val coffeeBean = wire[CoffeeBean]
val coffee = wire[Coffee]
coffee should not be (null)
coffee.coffeeBean.beanType shouldBe "monkey poop"
}
}