Пример #1
0
 def test_sequence_nothing(self):
     lst = List([
         Just(1),
         Nothing,
         Just(3)
     ])
     sequenced = lst.sequence(Option)
     self.assertEqual(sequenced, Nothing)
Пример #2
0
 def test_sequence(self):
     lst = List([
         Just(1),
         Just(2),
         Just(3)
     ])
     sequenced = lst.sequence(Option)
     self.assertEqual(sequenced.extract(), List([1, 2, 3]))
Пример #3
0
 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)
Пример #4
0
 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")])
Пример #5
0
 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))
Пример #6
0
 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))
Пример #7
0
 def test_getitem(self):
     value = {"address": {"street": "Ferndale", "number": 123}}
     lens = Prism["address"]["street"]
     self.assertEqual(lens.get(value), Just("Ferndale"))
Пример #8
0
 def test_getattr(self):
     value = Address(Street("Ferndale"), Number(123))
     self.assertEqual(Prism.street.name.get(value), Just("Ferndale"))
Пример #9
0
 def test_identity(self):
     value = {"id": 1}
     self.assertEqual(Prism.get(value), Just({"id": 1}))
Пример #10
0
 def effect(a):
     if a >= 0:
         return Just(a*2)
     return Nothing
Пример #11
0
 def test_something(self):
     something = Just("cat")
     self.assertEqual(something.extract(), "cat")
Пример #12
0
    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")
Пример #13
0
    def test_otherwise(self):
        something = Just("cat")
        self.assertEqual(something.otherwise(lambda: "dog"), "cat")

        nothing = Nothing
        self.assertEqual(nothing.otherwise(lambda: "dog"), "dog")