Last active
June 11, 2019 17:25
-
-
Save enzief/ca659430e85595028d343f36f9a8ec21 to your computer and use it in GitHub Desktop.
fake lens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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