|
1 | 1 | import os
|
| 2 | +from queue import Queue |
| 3 | + |
| 4 | +from coalib.results.Result import Result |
| 5 | +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
| 6 | +from coalib.settings.Section import Section |
| 7 | +from coalib.testing.BearTestHelper import generate_skip_decorator |
| 8 | +from coalib.testing.LocalBearTestHelper import LocalBearTestHelper |
2 | 9 |
|
3 | 10 | from bears.ruby.RuboCopBear import RuboCopBear
|
4 |
| -from coalib.testing.LocalBearTestHelper import verify_local_bear |
5 | 11 |
|
6 |
| -good_file = """def good_name |
7 |
| - test if something |
8 |
| -end |
9 |
| -""" |
10 | 12 |
|
11 |
| -bad_file = """def badName |
12 |
| - test if something |
13 |
| -end |
14 |
| -""" |
| 13 | +def get_testfile_path(name): |
| 14 | + return os.path.join(os.path.dirname(__file__), |
| 15 | + 'test_files', |
| 16 | + name) |
| 17 | + |
| 18 | + |
| 19 | +def load_testfile(name): |
| 20 | + with open(get_testfile_path(name)) as f: |
| 21 | + return f.readlines() |
| 22 | + |
15 | 23 |
|
| 24 | +@generate_skip_decorator(RuboCopBear) |
| 25 | +class RuboCopBearTest(LocalBearTestHelper): |
16 | 26 |
|
17 |
| -RuboCopBearTest = verify_local_bear(RuboCopBear, |
18 |
| - invalid_files=(bad_file,), |
19 |
| - valid_files=(good_file,)) |
| 27 | + def setUp(self): |
| 28 | + self.uut = RuboCopBear(Section('name'), Queue()) |
20 | 29 |
|
21 |
| -# Testing Config |
22 |
| -rubocop_config = os.path.join(os.path.dirname(__file__), |
23 |
| - 'test_files', |
24 |
| - 'rubocop_config.yaml') |
| 30 | + def test_good_file(self): |
| 31 | + filename = 'good_file.rb' |
| 32 | + file_contents = load_testfile(filename) |
| 33 | + self.check_results( |
| 34 | + self.uut, |
| 35 | + file_contents, |
| 36 | + [], |
| 37 | + filename=get_testfile_path(filename)) |
| 38 | + |
| 39 | + def test_bad_file(self): |
| 40 | + filename = 'bad_file.rb' |
| 41 | + file_contents = load_testfile(filename) |
| 42 | + self.check_results( |
| 43 | + self.uut, |
| 44 | + file_contents, |
| 45 | + [Result.from_values('RuboCopBear (Style/MethodName)', |
| 46 | + message='Use snake_case for method names.', |
| 47 | + file=get_testfile_path(filename), |
| 48 | + line=1, |
| 49 | + column=5, |
| 50 | + end_line=1, |
| 51 | + end_column=12, |
| 52 | + severity=RESULT_SEVERITY.INFO)], |
| 53 | + filename=get_testfile_path(filename)) |
25 | 54 |
|
26 | 55 |
|
27 | 56 | # bad file becomes good and vice-versa
|
28 |
| -RuboCopBearConfigFileTest = verify_local_bear( |
29 |
| - RuboCopBear, |
30 |
| - valid_files=(bad_file,), |
31 |
| - invalid_files=(good_file,), |
32 |
| - settings={'rubocop_config': rubocop_config}) |
33 |
| - |
34 |
| -# Testing settings |
35 |
| -another_good_file = """ |
36 |
| -def goodindent |
37 |
| - # 1 space indent |
38 |
| -end |
39 |
| -""" |
40 |
| - |
41 |
| -another_bad_file = """ |
42 |
| -def badindent |
43 |
| - # 2 spaces indent |
44 |
| -end |
45 |
| -""" |
46 |
| - |
47 |
| -RuboCopBearSettingsTest = verify_local_bear( |
48 |
| - RuboCopBear, |
49 |
| - valid_files=(another_good_file,), |
50 |
| - invalid_files=(another_bad_file,), |
51 |
| - settings={'indent_size': 1}) |
52 |
| - |
53 |
| -RuboCopBearSettingsTest = verify_local_bear( |
54 |
| - RuboCopBear, |
55 |
| - valid_files=(bad_file,), |
56 |
| - invalid_files=(good_file,), |
57 |
| - settings={'method_name_case': 'camel'}) |
| 57 | + def test_bad_config_file(self): |
| 58 | + filename = 'good_file.rb' |
| 59 | + file_contents = load_testfile(filename) |
| 60 | + self.check_results( |
| 61 | + self.uut, |
| 62 | + file_contents, |
| 63 | + [Result.from_values('RuboCopBear (Style/MethodName)', |
| 64 | + message='Use camelCase for method names.', |
| 65 | + file=get_testfile_path(filename), |
| 66 | + line=1, |
| 67 | + column=5, |
| 68 | + end_line=1, |
| 69 | + end_column=14, |
| 70 | + severity=RESULT_SEVERITY.INFO)], |
| 71 | + filename=get_testfile_path(filename), |
| 72 | + settings={'rubocop_config': get_testfile_path( |
| 73 | + 'rubocop_config.yaml')}) |
| 74 | + |
| 75 | + def test_good_config_file(self): |
| 76 | + filename = 'bad_file.rb' |
| 77 | + file_contents = load_testfile(filename) |
| 78 | + self.check_results( |
| 79 | + self.uut, |
| 80 | + file_contents, |
| 81 | + [], |
| 82 | + filename=get_testfile_path(filename), |
| 83 | + settings={'rubocop_config': get_testfile_path( |
| 84 | + 'rubocop_config.yaml')}) |
| 85 | + |
| 86 | + def test_bad_indent_size(self): |
| 87 | + filename = 'indent_file.rb' |
| 88 | + file_contents = load_testfile(filename) |
| 89 | + self.check_results( |
| 90 | + self.uut, |
| 91 | + file_contents, |
| 92 | + [Result.from_values('RuboCopBear (Layout/CommentIndentation)', |
| 93 | + message='Incorrect indentation detected ' |
| 94 | + '(column 2 instead of 1).', |
| 95 | + file=get_testfile_path(filename), |
| 96 | + line=2, |
| 97 | + column=3, |
| 98 | + end_line=2, |
| 99 | + end_column=20, |
| 100 | + severity=RESULT_SEVERITY.INFO)], |
| 101 | + filename=get_testfile_path(filename), |
| 102 | + settings={'indent_size': 1}) |
| 103 | + |
| 104 | + def test_good_indent_size(self): |
| 105 | + filename = 'indent_file.rb' |
| 106 | + file_contents = load_testfile(filename) |
| 107 | + self.check_results( |
| 108 | + self.uut, |
| 109 | + file_contents, |
| 110 | + [], |
| 111 | + filename=get_testfile_path(filename)) |
0 commit comments