Example #1
0
 def test_resolve_multiple_truthy(self):
     truthy = MagicMock(return_value=True)
     switch = Switch(
         [Case(truthy) >> sentinel.exact1,
          Case(truthy) >> sentinel.exact2])
     self.assertEqual(switch(sentinel.any), sentinel.exact1)
     truthy.assert_called_once_with(sentinel.any)
Example #2
0
 def test_resolve_multiple_falsy(self):
     falsy = MagicMock(return_value=False)
     switch = Switch(
         [Case(falsy) >> sentinel.exact1,
          Case(falsy) >> sentinel.exact2])
     with self.assertRaises(ValueError):
         switch(sentinel.any)
     falsy.assert_has_calls([call(sentinel.any), call(sentinel.any)])
Example #3
0
    def test_immutable_expr(self):
        truthy1 = MagicMock(return_value=True)
        truthy2 = MagicMock(return_value=True)
        switch1 = inst(sentinel.expr1) | Case(truthy1) >> sentinel.exact1
        switch2 = inst(sentinel.expr2) | Case(truthy2) >> sentinel.exact2

        self.assertEqual(switch1(), sentinel.exact1)
        truthy1.assert_called_once_with(sentinel.expr1)

        self.assertEqual(switch2(), sentinel.exact2)
        truthy2.assert_called_once_with(sentinel.expr2)
Example #4
0
 def test_resolve_multiple_truthy_falsy(self):
     truthy = MagicMock(return_value=True)
     falsy = MagicMock(return_value=False)
     switch = Switch([
         Case(falsy) >> sentinel.exact1,
         Case(falsy) >> sentinel.exact2,
         Case(truthy) >> sentinel.exact3,
         Case(truthy) >> sentinel.exact4
     ])
     self.assertEqual(switch(sentinel.any), sentinel.exact3)
     falsy.assert_has_calls([call(sentinel.any), call(sentinel.any)])
     truthy.assert_called_once_with(sentinel.any)
    def test_resolve(self):
        mock = MagicMock(return_value=True)
        case = Case(predicate=mock)

        self.assertEqual(case >> sentinel.val, case)
        self.assertEqual(case.resolve(), sentinel.val)
        mock.assert_not_called()
Example #6
0
    def test_immutable(self):
        switch1 = inst | Case(lambda x: True) >> sentinel.exact1
        switch2 = inst | Case(lambda x: True) >> sentinel.exact2

        self.assertEqual(switch1(sentinel.any), sentinel.exact1)
        self.assertEqual(switch2(sentinel.any), sentinel.exact2)
Example #7
0
 def test_switch_resolve(self):
     truthy = MagicMock(return_value=True)
     switch = inst(sentinel.expr) | Case(truthy) >> sentinel.smth
     self.assertEqual(~switch, sentinel.smth)
     truthy.assert_called_once_with(sentinel.expr)
Example #8
0
 def test_switch_without_expr(self):
     switch = Switch([Case(lambda x: True) >> sentinel.smth])
     with self.assertRaises(ValueError):
         switch()
Example #9
0
 def test_switch_with_2_expr(self):
     switch = Switch()(
         sentinel.expr1) | Case(lambda x: True) >> sentinel.smth
     with self.assertRaises(ValueError):
         switch(sentinel.expr2)
Example #10
0
 def test_switch_with_expr(self):
     truthy = MagicMock(return_value=True)
     switch = Switch()(sentinel.expr) | Case(truthy) >> sentinel.smth
     self.assertEqual(switch(), sentinel.smth)
     truthy.assert_called_once_with(sentinel.expr)
Example #11
0
 def test_switch_multiple_cases(self):
     switch = (Switch()
               | Case(lambda x: False) >> sentinel.exact1
               | Case(lambda x: True) >> sentinel.exact2)
     self.assertEqual(switch(sentinel.any), sentinel.exact2)
Example #12
0
 def test_switch_single_case(self):
     switch = Switch() | Case(lambda x: True) >> sentinel.exact
     self.assertEqual(switch(sentinel.any), sentinel.exact)
Example #13
0
 def test_resolve_single_falsy(self):
     falsy = MagicMock(return_value=False)
     switch = Switch([Case(falsy) >> sentinel.exact])
     with self.assertRaises(ValueError):
         switch(sentinel.any)
     falsy.assert_called_once_with(sentinel.any)
 def test_no_args(self):
     with self.assertRaises(TypeError):
         case = Case()
    def test_match(self):
        mock = MagicMock(return_value=True)
        case = Case(predicate=mock)

        self.assertTrue(case.match(sentinel.val))
        mock.assert_called_once_with(sentinel.val)
        with self.assertRaises(TypeError):
            case = Case()

    def test_match(self):
        mock = MagicMock(return_value=True)
        case = Case(predicate=mock)

        self.assertTrue(case.match(sentinel.val))
        mock.assert_called_once_with(sentinel.val)

    def test_resolve(self):
        mock = MagicMock(return_value=True)
        case = Case(predicate=mock)

        self.assertEqual(case >> sentinel.val, case)
        self.assertEqual(case.resolve(), sentinel.val)
        mock.assert_not_called()

    def test_resolve_without_value(self):
        case = Case(lambda x: True)
        with self.assertRaises(ValueError):
            case.resolve()

    def test_factory(self):
        mock = MagicMock(return_value=True)
        case = case_factory(mock)

        self.assertIsInstance(case, Case)
        self.assertTrue(case.match(sentinel.val))
        mock.assert_called_once_with(sentinel.val)