Skip to content

Instantly share code, notes, and snippets.

@enzief
Last active June 11, 2019 17:25
Show Gist options
  • Save enzief/ca659430e85595028d343f36f9a8ec21 to your computer and use it in GitHub Desktop.
Save enzief/ca659430e85595028d343f36f9a8ec21 to your computer and use it in GitHub Desktop.
fake lens
case class Config(one: Config_1)
case class Config_1(two: Config_2)
case class Config_2(three: Config_3)
case class Config_3(value: Int)
class GetIntService {
def fromConfig(c: Config): Int
}
def main(config: Config)(service: GetIntService): Int = service.fromConfig(config) + 1
def test: Boolean = {
val i = 100
val config = Config(Config_1(Config_2(Config_3(i))))
main(config)(prod_service) == i + 1
}
// I don't have to make config value here
def test_with_lens: Boolean = {
val i = 10
val mock_service: GetIntService = (_: Config) => 10
main(null)(mock_service) == i + 1
}
case class Config(one: Config_1)
case class Config_1(two: Config_2)
case class Config_2(three: Config_3)
case class Config_3(value: Int)
def real_lens: Lens[Config, Int] = Lens(_.one.two.three.value)
def main(config: Config)(l: Lens[Config, Int]): Int = l.get(config) + 1
def test: Boolean = {
val i = 100
val config = Config(Config_1(Config_2(Config_3(i))))
main(config)(real_lens) == i + 1
}
// I don't have to make config value here
def test_with_lens: Boolean = {
val i = 10
val fake_lens: Lens[Config, Int] = Lens(_ => 10)
main(null)(fake_lens) == i + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment