diff -r 652605cdd3d6 -r 68b09220c104 Lib/test/test_pstats.py --- a/Lib/test/test_pstats.py Sat Dec 17 12:36:34 2011 -0800 +++ b/Lib/test/test_pstats.py Mon Dec 26 19:18:17 2011 +0000 @@ -1,8 +1,12 @@ import unittest +import pstats +import tempfile +import cProfile as profile + from test import support from io import StringIO -import pstats - +from os import remove, path +from functools import cmp_to_key class AddCallersTestCase(unittest.TestCase): @@ -27,17 +31,99 @@ class StatsTestCase(unittest.TestCase): def setUp(self): stats_file = support.findfile('pstats.pck') self.stats = pstats.Stats(stats_file) + #TODO: add more complicated tests, which might almost compile + to_compile = 'import os' + # temp_storage = tempfile.NamedTemporaryFile() + self.temp_storage = tempfile.mktemp() + profiled = compile(to_compile, '', 'exec') + profile.run(profiled, filename=self.temp_storage) + + def tearDown(self): + remove(self.temp_storage) def test_add(self): + """Add stats object to a stats created from an empty stream + """ stream = StringIO() stats = pstats.Stats(stream=stream) stats.add(self.stats, self.stats) + def test_dump_and_load_works_correctly(self): + """Dump a statistics on file and load the same file produces + the same statistic + """ + self.stats.dump_stats(filename=self.temp_storage) + tmp_stats = pstats.Stats(self.temp_storage) + self.assertEqual(self.stats.stats, tmp_stats.stats) + # tmp_out = StringIO() + # stats.dump_stats(tmp_out) + + def test_load_equivalent_to_init(self): + """Check that we can create a pstats file with different + objects in input + """ + empty = pstats.Stats() + empty.load_stats(self.temp_storage) + created = pstats.Stats(self.temp_storage) + self.assertEqual(empty.stats, created.stats) + + def test_loading_wrong_types(self): + empty = pstats.Stats() + with self.assertRaises(TypeError): + empty.load_stats(42) + + def test_sorting_produce_correct_order(self): + # can create a fake stats maybe to show the order + self.stats.sort_stats("stdname") + + +class TupleCompTestCase(unittest.TestCase): + + def test_tuple_comp_compare_is_correct(self): + """Test the internal class TupleComp, to see if the order + generated on the tuples is correct + """ + comp_list = [(0, 1), (1, -1), (2, 1)] + tup = pstats.TupleComp(comp_list) + to_sort = [(1, 3, 4), (2, 3, 1), (5, 4, 3)] + desired = [(1, 3, 4), (2, 3, 1), (5, 4, 3)] + to_sort.sort(key=cmp_to_key(tup.compare)) + self.assertEqual(to_sort, desired) + #TODO: add more tests and make some fail too + + +class UtilsTestCase(unittest.TestCase): + def test_count_calls(self): + dic = { + 1: 2, 3: 5 + } + dic_null = { + 1: 0, 2: 0 + } + self.assertEqual(pstats.count_calls(dic), 7) + self.assertEqual(pstats.count_calls(dic_null), 0) + + def test_f8(self): + self.assertEqual(pstats.f8(2.3232), ' 2.323') + self.assertEqual(pstats.f8(0), ' 0.000') + + def test_func_name(self): + func = ('file', 10, 'name') + self.assertEqual(pstats.func_get_function_name(func), 'name') + + def test_strip_path(self): + # we should not test for the implementation but sometimes is good to do so + func = (path.join('long', 'path'), 10, 'name') + desired = ('path', 10, 'name') + self.assertEqual(pstats.func_strip_path(func), desired) + def test_main(): support.run_unittest( AddCallersTestCase, StatsTestCase, + TupleCompTestCase, + UtilsTestCase )