blob: 9ebeebbfee678d440fbf9f2d4c980a14647b48d1 [file] [log] [blame]
Christian Heimese1c98112008-01-21 11:20:28 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Georg Brandl83938432010-10-22 06:28:01 +00003from io import StringIO
Christian Heimese1c98112008-01-21 11:20:28 +00004import pstats
5
6
7
8class AddCallersTestCase(unittest.TestCase):
9 """Tests for pstats.add_callers helper."""
10
11 def test_combine_results(self):
Georg Brandl83938432010-10-22 06:28:01 +000012 # pstats.add_callers should combine the call results of both target
13 # and source by adding the call time. See issue1269.
Georg Brandl2d3c4e72010-08-02 17:24:49 +000014 # new format: used by the cProfile module
Christian Heimese1c98112008-01-21 11:20:28 +000015 target = {"a": (1, 2, 3, 4)}
16 source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
17 new_callers = pstats.add_callers(target, source)
18 self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)})
Georg Brandl2d3c4e72010-08-02 17:24:49 +000019 # old format: used by the profile module
20 target = {"a": 1}
21 source = {"a": 1, "b": 5}
22 new_callers = pstats.add_callers(target, source)
23 self.assertEqual(new_callers, {'a': 2, 'b': 5})
Christian Heimese1c98112008-01-21 11:20:28 +000024
25
Georg Brandl83938432010-10-22 06:28:01 +000026class StatsTestCase(unittest.TestCase):
27 def setUp(self):
28 stats_file = support.findfile('pstats.pck')
29 self.stats = pstats.Stats(stats_file)
30
31 def test_add(self):
32 stream = StringIO()
33 stats = pstats.Stats(stream=stream)
34 stats.add(self.stats, self.stats)
35
36
Christian Heimese1c98112008-01-21 11:20:28 +000037def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000038 support.run_unittest(
Georg Brandl83938432010-10-22 06:28:01 +000039 AddCallersTestCase,
40 StatsTestCase,
Christian Heimese1c98112008-01-21 11:20:28 +000041 )
42
43
44if __name__ == "__main__":
45 test_main()