File: ordered.py

package info (click to toggle)
python-flexmock 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: python: 3,802; makefile: 17; sh: 14
file content (51 lines) | stat: -rw-r--r-- 1,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Tests for flexmock ordered feature."""

# pylint: disable=missing-docstring,no-member
import re

from flexmock import exceptions, flexmock
from tests.utils import assert_raises


class OrderedTestCase:
    def test_ordered_on_different_methods(self):
        class String(str):
            pass

        string = String("abc")
        flexmock(string)
        string.should_call("startswith").with_args("asdf", 0, 4).ordered()
        string.should_call("endswith").ordered()
        with assert_raises(
            exceptions.CallOrderError,
            re.compile(
                r'endswith\("c"\) called before startswith'
                # Argument names are displayed in PyPy
                r'\((prefix=)?"asdf", (start=)?0, (end=)?4\)'
            ),
        ):
            string.endswith("c")

    def test_flexmock_ordered_worked_after_default_stub(self):
        mocked = flexmock()
        mocked.should_receive("bar")
        mocked.should_receive("bar").with_args("a").ordered()
        mocked.should_receive("bar").with_args("b").ordered()
        with assert_raises(exceptions.CallOrderError, 'bar("b") called before bar("a")'):
            mocked.bar("b")

    def test_flexmock_ordered_works_with_same_args(self):
        mocked = flexmock()
        mocked.should_receive("bar").ordered().and_return(1)
        mocked.should_receive("bar").ordered().and_return(2)
        assert mocked.bar() == 1
        assert mocked.bar() == 2

    def test_flexmock_ordered_works_with_same_args_after_default_stub(self):
        mocked = flexmock()
        mocked.should_receive("bar").and_return(9)
        mocked.should_receive("bar").ordered().and_return(1)
        mocked.should_receive("bar").ordered().and_return(2)
        assert mocked.bar() == 1
        assert mocked.bar() == 2
        assert mocked.bar() == 9