def test_sequence_nothing(self): lst = List([ Just(1), Nothing, Just(3) ]) sequenced = lst.sequence(Option) self.assertEqual(sequenced, Nothing)
def test_sequence(self): lst = List([ Just(1), Just(2), Just(3) ]) sequenced = lst.sequence(Option) self.assertEqual(sequenced.extract(), List([1, 2, 3]))
def test_set_for_all(self): value = { "array": [{ "id": 0, "color": "red" }, { "id": 1, "color": "blue" }, { "id": 2, "color": "yellow" }] } expected = Just({ "array": [{ "id": 0, "color": "magenta" }, { "id": 1, "color": "magenta" }, { "id": 2, "color": "magenta" }] }) lens = Prism["array"].for_all["color"] self.assertEqual(lens.set(value, "magenta"), expected)
def test_get_for_all(self): value = [{ "id": 0, "color": "red" }, { "id": 1, "color": "blue" }, { "id": 2, "color": "yellow" }] lens = Prism.for_all["color"] self.assertEqual( lens.get(value), [Just("red"), Just("blue"), Just("yellow")])
def test_setitem(self): value = {"address": {"street": "Ferndale", "number": 123}} expected = {"address": {"street": "Ferndale", "number": 456}} lens = Prism["address"]["number"] self.assertEqual(lens.set(value, 456), Just(expected))
def test_setattr(self): value = Address(Street("Ferndale"), Number(123)) expected = Address(Street("Nanaimo"), Number(123)) lens = Prism.street.name self.assertEqual(lens.set(value, "Nanaimo"), Just(expected))
def test_getitem(self): value = {"address": {"street": "Ferndale", "number": 123}} lens = Prism["address"]["street"] self.assertEqual(lens.get(value), Just("Ferndale"))
def test_getattr(self): value = Address(Street("Ferndale"), Number(123)) self.assertEqual(Prism.street.name.get(value), Just("Ferndale"))
def test_identity(self): value = {"id": 1} self.assertEqual(Prism.get(value), Just({"id": 1}))
def effect(a): if a >= 0: return Just(a*2) return Nothing
def test_something(self): something = Just("cat") self.assertEqual(something.extract(), "cat")
def test_or_else(self): something = Just("cat") self.assertEqual(something.extract_or_else("dog"), "cat") nothing = Nothing self.assertEqual(nothing.extract_or_else("dog"), "dog")
def test_otherwise(self): something = Just("cat") self.assertEqual(something.otherwise(lambda: "dog"), "cat") nothing = Nothing self.assertEqual(nothing.otherwise(lambda: "dog"), "dog")