xref: /aosp_15_r20/external/autotest/client/common_lib/test_utils/mock_demo.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2
3from __future__ import division
4from __future__ import print_function
5__author__ = "[email protected] (Travis Miller)"
6
7import mock, mock_demo_MUT
8
9class MyError(Exception):
10    pass
11
12
13class A(object):
14    var = 8
15
16    def __init__(self):
17        self.x = 0
18
19    def method1(self):
20        self.x += 1
21        return self.x
22
23    def method2(self, y):
24        return y * self.x
25
26class B(A):
27    def method3(self, z):
28        return self.x + z
29
30    def method4(self, z, w):
31        return self.x * z + w
32
33
34class C(B):
35    def method5(self):
36        self.method1()
37        t = self.method2(4)
38        u = self.method3(t)
39        return u
40
41
42class D(C):
43    def method6(self, error):
44        if error:
45            raise MyError("woops")
46        else:
47            return 10
48
49class E(D):
50    def __init__(self, val):
51        self.val = val
52
53
54# say we want to test that do_stuff is doing what we think it is doing
55def do_stuff(a, b, func):
56    print(b.method1())
57    print(b.method3(10))
58    print(func("how many"))
59    print(a.method2(5))
60    print(b.method1())
61    print(b.method4(1, 4))
62    print(b.method2(3))
63    print(b.method2("hello"))
64
65
66def do_more_stuff(d):
67    print(d.method6(False))
68    try:
69        d.method6(True)
70    except:
71        print("caught error")
72
73
74def main():
75    god = mock.mock_god()
76
77    m1 = god.create_mock_class(A, "A")
78    print(m1.var)
79    m2 = god.create_mock_class(B, "B")
80    f = god.create_mock_function("func")
81
82    print(dir(m1))
83    print(dir(m2))
84
85    # sets up the "recording"
86    m2.method1.expect_call().and_return(1)
87    m2.method3.expect_call(10).and_return(10)
88    f.expect_call("how many").and_return(42)
89    m1.method2.expect_call(5).and_return(0)
90    m2.method1.expect_call().and_return(2)
91    m2.method4.expect_call(1, 4).and_return(6)
92    m2.method2.expect_call(3).and_return(6)
93    m2.method2.expect_call(mock.is_string_comparator()).and_return("foo")
94
95    # check the recording order
96    for func_call in god.recording:
97        print(func_call)
98
99    # once we start making calls into the methods we are in
100    # playback mode
101    do_stuff(m1, m2, f)
102
103    # we can now check that playback succeeded
104    god.check_playback()
105
106    # now test the ability to mock out all methods of an object
107    # except those under test
108    c = C()
109    god.mock_up(c, "c")
110
111    # setup recording
112    c.method1.expect_call()
113    c.method2.expect_call(4).and_return(4)
114    c.method3.expect_call(4).and_return(5)
115
116    # perform the test
117    answer = c.method5.run_original_function()
118
119    # check playback
120    print("answer = %s" % (answer))
121    god.check_playback()
122
123    # check exception returns too
124    m3 = god.create_mock_class(D, "D")
125    m3.method6.expect_call(False).and_return(10)
126    m3.method6.expect_call(True).and_raises(MyError("woops"))
127
128    do_more_stuff(m3)
129    god.check_playback()
130
131    # now check we can mock out a whole class (rather than just an instance)
132    mockE = god.create_mock_class_obj(E, "E")
133    oldE = mock_demo_MUT.E
134    mock_demo_MUT.E = mockE
135
136    m4 = mockE.expect_new(val=7)
137    m4.method1.expect_call().and_return(1)
138
139    mock_demo_MUT.do_create_stuff()
140    god.check_playback()
141
142    mock_demo_MUT.E = oldE
143
144
145if __name__ == "__main__":
146    main()
147