Ejemplo n.º 1
0
 def test_type_comparisons(self):
     tifa = pedal.tifa.Tifa()
     tifa.process_code('my_int=0\nmy_str="A"\nmy_list=[1,2,3]')
     variables = tifa.report['tifa']['top_level_variables']
     self.assertEqual(len(variables), 3)
     # Integer variable
     my_int = variables['my_int'].type
     self.assertTrue(my_int.is_equal('num'))
     self.assertTrue(my_int.is_equal('NumType'))
     self.assertTrue(my_int.is_equal(int))
     self.assertTrue(my_int.is_equal(float))
     self.assertFalse(my_int.is_equal(str))
     # String variable
     my_str = variables['my_str'].type
     self.assertTrue(my_str.is_equal('str'))
     self.assertTrue(my_str.is_equal('StrType'))
     self.assertTrue(my_str.is_equal(str))
     # List variable
     my_list = variables['my_list'].type
     self.assertTrue(my_list.is_equal('list'))
     self.assertTrue(my_list.is_equal('ListType'))
     self.assertTrue(my_list.is_equal(list))
     # List subtype
     self.assertTrue(my_list.subtype.is_equal(int))
     self.assertTrue(my_list.subtype.is_equal('num'))
     self.assertTrue(my_list.subtype.is_equal(float))
     self.assertTrue(my_list.subtype.is_equal('NumType'))
Ejemplo n.º 2
0
 def test_variable_def_use(self):
     tifa = pedal.tifa.Tifa()
     tifa.process_code('a=0\nb=0\nb\nc')
     issues = tifa.report['tifa']['issues']
     # Unused variables
     self.assertEqual(len(issues['Unused Variable']), 1)
     unused_variables = [i['name'] for i in issues['Unused Variable']]
     self.assertIn('a', unused_variables)
     self.assertNotIn('b', unused_variables)
     self.assertNotIn('c', unused_variables)
     # Uninitalized variables
     self.assertEqual(len(issues['Initialization Problem']), 1)
     unset_variables = [i['name'] for i in issues['Initialization Problem']]
     self.assertNotIn('a', unset_variables)
     self.assertNotIn('b', unset_variables)
     self.assertIn('c', unset_variables)
Ejemplo n.º 3
0
 def test_code(self):
     tifa = pedal.tifa.Tifa()
     try:
         tifa.process_code(code)
     except Exception as e:
         raise type(e)(str(e) + ' in code:\n%s' % code)
     if not tifa.report['tifa']['success']:
         self.fail("Error message in\n" + code + "\n" +
                   str(tifa.report['tifa']['error']))
     for none in nones:
         if tifa.report['tifa']['issues'].get(none, []):
             print("")
             pprint(tifa.report['tifa']['variables'])
             self.fail("Incorrectly detected " + none + "\n" + code + "\n")
     for some in somes:
         if not tifa.report['tifa']['issues'].get(some, []):
             self.fail("Failed to detect " + some + "\n" + code + "\n")
Ejemplo n.º 4
0
    def test_get_types(self):
        tifa = pedal.tifa.Tifa()
        tifa.process_code('a=0\na="Hello"\na=[1,2,3]\na=1')
        a = tifa.report['tifa']['top_level_variables']['a']
        self.assertTrue(a.was_type('num'))
        self.assertTrue(a.was_type(int))
        self.assertTrue(a.was_type('NumType'))
        self.assertTrue(a.was_type(defs.NumType))
        self.assertFalse(a.was_type(bool))
        self.assertFalse(a.was_type(defs.DictType))
        self.assertTrue(a.was_type('ListType'))

        tifa = pedal.tifa.Tifa()
        tifa.process_code(
            'credits=[1,2,3,4]\nfor credit in credits:\n  print(credit)')
        credit = tifa.report['tifa']['top_level_variables']['credit']
        self.assertFalse(credit.was_type(list))
        self.assertTrue(credit.was_type(int))
Ejemplo n.º 5
0
    def test_weirdness(self):
        tifa = pedal.tifa.Tifa()
        tifa.process_code('def a(x):\n    return x\na(1)')
        issues = tifa.report['tifa']['issues']
        self.assertTrue(tifa.report['tifa']['success'])

        tifa = pedal.tifa.Tifa()
        tifa.process_code('def a(x):\n    return x\n')
        issues = tifa.report['tifa']['issues']
        self.assertEqual(
            issues['Unused Variable'][0]['message'],
            "The function <code>a</code> was given a definition, "
            "but was never used after that.")

        tifa = pedal.tifa.Tifa()
        tifa.process_code(
            dedent('''
        import tate
        art = tate.get_artwork()
        h = [a['dimensions']['height'] for a in art if a['artist']['birth']['year'] > 1000]
        b = [a['artist']['birth']['year'] for a in art 
             if a['artist']['birth']['year'] > 1000]
        import matplotlib.pyplot as plt
        plt.scatter(b, h, alpha=.1)
        plt.show()'''))
        issues = tifa.report['tifa']['issues']
        self.assertTrue(tifa.report['tifa']['success'])
Ejemplo n.º 6
0
 def test_custom_module_import(self):
     tifa = pedal.tifa.Tifa()
     tifa.process_code('import weather\n' + 'rs = weather.get_weather()\n' +
                       'for r in rs:\n' + '  0+r["Data"]["Precipitation"]')
     issues = tifa.report['tifa']['issues']
     self.assertTrue(tifa.report['tifa']['success'])