def test_EVERYTHING(self): result = s.critic(self.test_code, forbid_semicolons=True, max_nesting=3, methods_per_class=2, max_arity=2, max_lines_per_function=4 ) expected = {2: ['line too long (89 > 50)'], 6: ['too many methods in class(3 > 2)'], 7: ['indentation is 5 instead of 4', 'too many arguments(3 > 2)', 'method with too many lines (5 > 4)'], 15: ['trailing whitespace'], 11: ['nesting too deep (4 > 3)'], 17: ['method with too many lines (5 > 4)'], 20: ['nesting too deep (4 > 3)'], 21: ['line too long (81 > 79)', 'nesting too deep (5 > 3)'], 22: ['nesting too deep (5 > 3)'], 24: ['too many arguments(4 > 2)'], 25: ['indentation is 3 instead of 4'], 26: ['indentation is 7 instead of 8'], 27: ['indentation is 3 instead of 4'], 28: ['indentation is 3 instead of 4'], 30: ['too many methods in class(3 > 2)'], 32: ['indentation is 9 instead of 8', 'trailing whitespace'], 35: ['line too long (121 > 79)'], 43: ['trailing whitespace'], 46: ['too many arguments(3 > 2)', 'method with too many lines (8 > 4)'], 47: ['multiple expressions on the same line'], 48: ['multiple expressions on the same line', 'indentation is 5 instead of 4'], } self.assertEqual(expected, result)
def test_forbid_semicolons(self): result = s.critic(self.test_code) self.assertIn('multiple expressions on the same line', result[42]) self.assertIn('multiple expressions on the same line', result[43]) self.assertNotIn('multiple expressions on the same line', result[44]) self.assertNotIn('multiple expressions on the same line', result[45]) self.assertNotIn('multiple expressions on the same line', result[46])
def test_too_deep_nesting(self): code = ("def some_func():\n" " for char in a_variable:\n" " if char != 'a':\n" " for _ in range(10):\n" " print('SOOOO MUUUCH INDENTATION')\n") issues = solution.critic(code, max_nesting=3) print(issues) self.assertSetEqual(set(issues[5]), {'nesting too deep (4 > 3)'})
def test_line_length(self): result = s.critic(self.test_code) self.assertIn('line too long (89 > 79)', result[2]) self.assertIn('line too long (81 > 79)', result[21]) self.assertIn('line too long (121 > 79)', result[30]) result = s.critic(self.test_code, line_length=50) self.assertIn('line too long (89 > 50)', result[2]) self.assertIn('line too long (81 > 50)', result[21]) self.assertIn('line too long (121 > 50)', result[30]) self.assertIn('line too long (51 > 50)', result[19]) self.assertIn('line too long (53 > 50)', result[20]) self.assertEqual(len(result[22]), 0) result = s.critic('''def some_func(): a_variable = 'some text'; another_variable = 'some more text'; even_moar_variables = 'just for to pass the time' ''') self.assertIn('line too long (116 > 79)', result[2])
def test_long_line_with_several_statements(self): code = ("def some_func():\n" " a_variable = 'some text';" " another_variable = 'some more text';" " even_moar_variables = 'just for to pass the time'") issues = solution.critic(code) self.assertSetEqual(set(issues[2]), { 'line too long (116 > 79)', 'multiple expressions on the same line' })
def test_indentation(self): code = ('def ugly(indent):\n' ' return indent') issues = solution.critic(code) self.assertSetEqual(set(issues[2]), {'indentation is 5 instead of 4'})
def test_two_statements_on_one_line(self): code = 'a = 5; b = 6' issues = solution.critic(code) self.assertSetEqual(set(issues[1]), {'multiple expressions on the same line'})
def test_forbid_trailing_whitespaces(self): result = s.critic(self.test_code) self.assertIn('trailing whitespace', result[15]) self.assertIn('trailing whitespace', result[27]) self.assertIn('trailing whitespace', result[38])