Skip to content

Commit

Permalink
Added tests for DuplicatedImportError
Browse files Browse the repository at this point in the history
  • Loading branch information
Genarito committed Nov 19, 2021
1 parent e65e322 commit 2231d76
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 2 additions & 3 deletions gura/GuraParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,11 @@ def __compute_imports(self, parent_dir_path: Optional[str]):
file_to_import = os.path.join(origin_file_path, file_to_import)

# Files can be imported only once. This prevents circular reference
# TODO: check how to report well the position and line
if file_to_import in self.imported_files:
raise DuplicatedImportError(
self.pos + 1,
self.pos - len(file_to_import) - 1, # -1 for the quotes (")
self.line,
f'The file {file_to_import} has been already imported'
f'The file "{file_to_import}" has been already imported'
)

with open(file_to_import, 'r') as f:
Expand Down
10 changes: 7 additions & 3 deletions tests/exception_report/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ def test_missing_variable_6(self):
"""Tests error position and line when user uses a non defined variable inside an import statement"""
self.__test_fail('missing_variable_error_6.ura', VariableNotDefinedError, error_pos=21, error_line=0)

# def test_line_and_pos_6(self):
# """Tests error position and line when imported files are duplicated"""
# self.__test_fail('importing_error_1.ura', DuplicatedImportError, error_pos=20, error_line=2)
def test_duplicated_import_1(self):
"""Tests error position and line when imported files are duplicated"""
self.__test_fail('importing_error_1.ura', DuplicatedImportError, error_pos=74, error_line=1)

def test_duplicated_import_2(self):
"""Tests error position and line when imported files are duplicated but in other line than 0"""
self.__test_fail('importing_error_2.ura', DuplicatedImportError, error_pos=86, error_line=4)


if __name__ == '__main__':
Expand Down
5 changes: 5 additions & 0 deletions tests/exception_report/tests-files/importing_error_2.ura
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# COMMENT


import "tests/importing/tests-files/duplicated_variable_aux_1.ura"
import "tests/importing/tests-files/duplicated_variable_aux_1.ura"

0 comments on commit 2231d76

Please sign in to comment.