Exemplo n.º 1
0
    def test_dump_and_load(self):
        fndb, ret = trace(main1, [], trace_all=True)
        self.assertEqual(len(fndb), main1_fn_count)

        store = freeze_db(fndb)
        dump = format_db(store)
        dump_direct = format_db(fndb)
        self.assertEqual(dump, dump_direct)

        loaded = load_db(dump)
        self.assertDictEqual(loaded, store)
Exemplo n.º 2
0
 def test_exception(self):
     fndbe, ret = trace(main_exc, [], trace_all=True)
     dumpe = format_db(fndbe)
     print(dumpe)
     self.assertEqual(len(fndbe), main_exc_fn_count)
     self.assertTrue(
         any('ValueError' in line and 'Catch this error' in line
             for line in dumpe.splitlines()))
Exemplo n.º 3
0
 def test_exception(self):
     fndbe, ret = trace(main_exc, [], trace_all=True)
     dumpe = format_db(fndbe)
     print(dumpe)
     self.assertEqual(len(fndbe), main_exc_fn_count)
     self.assertTrue(any(
         'ValueError' in line and 'Catch this error' in line
         for line in dumpe.splitlines()))
Exemplo n.º 4
0
 def test_dump(self):
     fndb1, ret = trace(main1, [], trace_all=True)
     dump1 = format_db(fndb1)
     print(dump1)
     self.assertEqual(len(fndb1), main1_fn_count)
Exemplo n.º 5
0
    def test_annotate(self):

        # Run a trace
        with tracing(trace_all=True) as fndb:
            f('Traced code')
        # Dump it and load it back
        dump = format_db(fndb)
        store = load_db(dump)
        self.assertTrue(len(store), 5)

        afile = '{}-traced'.format(__file__)
        restored_file = '{}-restored'.format(__file__)
        rm(afile)
        rm(restored_file)

        # Annotated the function at the top of this very file
        annotate(store, __file__, afile)
        self.assertTrue(os.path.exists(afile))

        # Doing it again yields the same output
        ''' XXX Doesn't work because of different filenames
        with open(afile) as fd:
            saved_afile = fd.read()
        annotate(store, afile, afile)
        with open(afile) as fd:
            self.assertEqual(fd.read(), saved_afile)
        '''

        # Check the annotations
        n_annotations = 0
        found_f = False
        found_inner = False
        found_method = False
        found_method_inner = False
        found_local_change = False
        # Do not erase regular comments
        found_random_comment = False
        polluted = False
        with open(afile) as fd:
            for line in fd:
                if line.strip().startswith('#?'):
                    n_annotations += 1
                if "x=" + "Traced code" in line:
                    found_f = True
                if "y=" + "Yyy" in line:
                    found_inner = True
                if (
                    "self" + "=" in line and
                    "a=" + "X" in line
                ):
                    found_method = True
                if "what=" + "Aaa" in line:
                    found_method_inner = True
                if r"self=" in line and r".C {attr: X}" in line:
                    found_local_change = True
                if '# Do not' + ' erase regular comments' == line.strip():
                    found_random_comment = True
                if "!!" + "python" in line:
                    polluted = True
        self.assertTrue(found_f)
        self.assertTrue(found_inner)
        self.assertTrue(found_method)
        self.assertTrue(found_method_inner)
        self.assertTrue(found_local_change)
        self.assertTrue(found_random_comment)
        self.assertEqual(n_annotations, 4)
        self.assertTrue(not polluted)

        # Remove annotations from the file
        deannotate(afile, restored_file)
        # It must be identical to the original file
        with open(__file__) as f_orig, open(restored_file) as f_restored:
            restored = f_restored.read()
            self.assertTrue(not any(
                line.strip().startswith('#?')
                for line in restored.splitlines()
            ))
            self.assertEqual(f_orig.read(), restored)

        rm(afile)
        rm(restored_file)
Exemplo n.º 6
0
 def just_tracing(self):
     ' Record a trace in a file '
     with tracing(trace_all=True) as fndb:
         f('Traced code')
     with open('some_entry_point.yml', 'w') as fd:
         fd.write(format_db(fndb))
Exemplo n.º 7
0
    def test_annotate(self):

        # Run a trace
        with tracing(trace_all=True) as fndb:
            f('Traced code')
        # Dump it and load it back
        dump = format_db(fndb)
        store = load_db(dump)
        self.assertTrue(len(store), 5)

        afile = '{}-traced'.format(__file__)
        restored_file = '{}-restored'.format(__file__)
        rm(afile)
        rm(restored_file)

        # Annotated the function at the top of this very file
        annotate(store, __file__, afile)
        self.assertTrue(os.path.exists(afile))

        # Doing it again yields the same output
        ''' XXX Doesn't work because of different filenames
        with open(afile) as fd:
            saved_afile = fd.read()
        annotate(store, afile, afile)
        with open(afile) as fd:
            self.assertEqual(fd.read(), saved_afile)
        '''

        # Check the annotations
        n_annotations = 0
        found_f = False
        found_inner = False
        found_method = False
        found_method_inner = False
        found_local_change = False
        # Do not erase regular comments
        found_random_comment = False
        polluted = False
        with open(afile) as fd:
            for line in fd:
                if line.strip().startswith('#?'):
                    n_annotations += 1
                if "x=" + "Traced code" in line:
                    found_f = True
                if "y=" + "Yyy" in line:
                    found_inner = True
                if ("self" + "=" in line and "a=" + "X" in line):
                    found_method = True
                if "what=" + "Aaa" in line:
                    found_method_inner = True
                if r"self=" in line and r".C {attr: X}" in line:
                    found_local_change = True
                if '# Do not' + ' erase regular comments' == line.strip():
                    found_random_comment = True
                if "!!" + "python" in line:
                    polluted = True
        self.assertTrue(found_f)
        self.assertTrue(found_inner)
        self.assertTrue(found_method)
        self.assertTrue(found_method_inner)
        self.assertTrue(found_local_change)
        self.assertTrue(found_random_comment)
        self.assertEqual(n_annotations, 4)
        self.assertTrue(not polluted)

        # Remove annotations from the file
        deannotate(afile, restored_file)
        # It must be identical to the original file
        with open(__file__) as f_orig, open(restored_file) as f_restored:
            restored = f_restored.read()
            self.assertTrue(not any(line.strip().startswith('#?')
                                    for line in restored.splitlines()))
            self.assertEqual(f_orig.read(), restored)

        rm(afile)
        rm(restored_file)
Exemplo n.º 8
0
 def just_tracing(self):
     ' Record a trace in a file '
     with tracing(trace_all=True) as fndb:
         f('Traced code')
     with open('some_entry_point.yml', 'w') as fd:
         fd.write(format_db(fndb))
Exemplo n.º 9
0
 def test_dump(self):
     fndb1, ret = trace(main1, [], trace_all=True)
     dump1 = format_db(fndb1)
     print(dump1)
     self.assertEqual(len(fndb1), main1_fn_count)