Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Georg Brandl | 8393843 | 2010-10-22 06:28:01 +0000 | [diff] [blame] | 3 | from io import StringIO |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 4 | import pstats |
| 5 | |
| 6 | |
| 7 | |
| 8 | class AddCallersTestCase(unittest.TestCase): |
| 9 | """Tests for pstats.add_callers helper.""" |
| 10 | |
| 11 | def test_combine_results(self): |
Georg Brandl | 8393843 | 2010-10-22 06:28:01 +0000 | [diff] [blame] | 12 | # pstats.add_callers should combine the call results of both target |
| 13 | # and source by adding the call time. See issue1269. |
Georg Brandl | 2d3c4e7 | 2010-08-02 17:24:49 +0000 | [diff] [blame] | 14 | # new format: used by the cProfile module |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 15 | 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 Brandl | 2d3c4e7 | 2010-08-02 17:24:49 +0000 | [diff] [blame] | 19 | # 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 Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 24 | |
| 25 | |
Georg Brandl | 8393843 | 2010-10-22 06:28:01 +0000 | [diff] [blame] | 26 | class 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 Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 37 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 38 | support.run_unittest( |
Georg Brandl | 8393843 | 2010-10-22 06:28:01 +0000 | [diff] [blame] | 39 | AddCallersTestCase, |
| 40 | StatsTestCase, |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | |
| 44 | if __name__ == "__main__": |
| 45 | test_main() |