def test_no_todo_comment(self): no_todo_comment_config_path = os.path.join(COMMENTS_CONFIG_PATH, 'no-TODO-comment-config.json') bslint.load_config_file(user_filepath=no_todo_comment_config_path, default_filepath=TEST_CONFIG_FILE_PATH) file_name = self.valid_comment_single_quote_no_todo_file_path expected = [error.get_error_msg(err_const.NO_TODOS, [1]), error.get_error_msg(err_const.NO_TODOS, [17])] self.common.lex_file(expected, file_name)
def test_print_and_question_mark(self): bslint.load_config_file(user_filepath=self.trace_free_config_path, default_filepath=TEST_CONFIG_FILE_PATH) print_and_question_mark_file_path = os.path.join( TRACE_TEST_FILES_PATH, 'print-and-question-mark.brs') self.common.lex_file([ error.get_error_msg(err_const.TRACEABLE_CODE, [3]), error.get_error_msg(err_const.TRACEABLE_CODE, [4]) ], print_and_question_mark_file_path)
def test_trailing_white_space_multi_lines(self): file_name = os.path.join(TRAILING_WHITE_SPACE_TEST_FILES_PATH, 'trailing-white-space-multi-lines.brs') expected = [ error.get_error_msg(err_const.TRAILING_WHITE_SPACE, [2]), error.get_error_msg(err_const.TRAILING_WHITE_SPACE, [3]), error.get_error_msg(err_const.TRAILING_WHITE_SPACE, [7]) ] self.common.lex_file(expected, file_name)
def test_skip_file_command_skip_start_inactive(self): inactive_skip_file_config_path = os.path.join( BSLINT_COMMAND_CONFIG_PATH, 'inactive-skip-file-config.json') bslint.load_config_file(user_filepath=inactive_skip_file_config_path, default_filepath=TEST_CONFIG_FILE_PATH) self.common.lex_warnings_match([ msg_handler.get_error_msg(err_const.TYPO_IN_CODE, [2]), msg_handler.get_error_msg(err_const.NO_SPACE_AROUND_OPERATORS, [1, 2]) ], "'BSLint_skip_file\nxgygu =22\ny = 4")
def test_todo_no_comment(self): valid_comment_single_quote_no_todo_config_path = os.path.join(COMMENTS_CONFIG_PATH, 'valid-comment-single-quote-no-TODO.json') bslint.load_config_file(user_filepath=valid_comment_single_quote_no_todo_config_path, default_filepath=TEST_CONFIG_FILE_PATH) file_name = self.valid_comment_single_quote_no_todo_file_path expected = [error.get_error_msg(err_const.NON_CONVENTIONAL_TODO_AND_NO_COMMENTS, [11]), error.get_error_msg(err_const.NON_CONVENTIONAL_TODO_AND_NO_COMMENTS, [12]), error.get_error_msg(err_const.NON_CONVENTIONAL_TODO_AND_NO_COMMENTS, [17])] self.common.lex_file(expected, file_name)
def test_empty_lines_at_start(self): bslint.load_config_file( user_filepath=self.single_empty_lines_config_path, default_filepath=TEST_CONFIG_FILE_PATH) empty_lines_at_start_file_path = os.path.join( EMPTY_LINES_TEST_FILES_PATH, 'empty-lines-at-start.brs') file_name = empty_lines_at_start_file_path expected = [ error.get_error_msg(err_const.CONSECUTIVE_EMPTY_LINES, [1, 2]), error.get_error_msg(err_const.CONSECUTIVE_EMPTY_LINES, [1, 3]) ] self.common.lex_file(expected, file_name)
def test_only_empty_lines_custom_config(self): bslint.load_config_file( user_filepath=self.double_empty_lines_config_path, default_filepath=TEST_CONFIG_FILE_PATH) only_empty_lines_file_path = os.path.join(EMPTY_LINES_TEST_FILES_PATH, 'only-empty-lines.brs') file_name = only_empty_lines_file_path expected = [ error.get_error_msg(err_const.CONSECUTIVE_EMPTY_LINES, [2, 3]), error.get_error_msg(err_const.CONSECUTIVE_EMPTY_LINES, [2, 4]) ] self.common.lex_file(expected, file_name)
def test_multi_line_errors(self): bslint.load_config_file( user_filepath=self.small_max_line_length_config_path, default_filepath=TEST_CONFIG_FILE_PATH) multi_line_file_path = os.path.join(STYLING_TEST_FILES_PATH, 'multiline-assignment.txt') file_name = multi_line_file_path expected = [ error.get_error_msg(err_const.LINE_LENGTH, [11, 1]), error.get_error_msg(err_const.LINE_LENGTH, [11, 2]) ] self.common.lex_file(expected, file_name)
def test_exceed_max_line_length(self): bslint.load_config_file( user_filepath=self.small_max_line_length_config_path, default_filepath=TEST_CONFIG_FILE_PATH) file_name = self.short_line_length_file_path expected = [error.get_error_msg(err_const.LINE_LENGTH, [11, 1])] self.common.lex_file(expected, file_name)
def test_skip_line_command_inactive(self): inactive_skip_line_config_path = os.path.join( BSLINT_COMMAND_CONFIG_PATH, 'inactive-skip-line-config.json') bslint.load_config_file(user_filepath=inactive_skip_line_config_path) self.common.lex_warnings_match( [msg_handler.get_error_msg(err_const.TYPO_IN_CODE, [2])], "'BSLint_skip_line\nxgygu = 22\ny = 4")
def test_many_spaces_around_operator_with_config(self): three_spaces_around_operators_path = \ os.path.join(TESTS_CONFIG_PATH, 'spaces_around_operators/3-spaces-around-operators-config.json') bslint.load_config_file( user_filepath=three_spaces_around_operators_path) expected = [ error.get_error_msg(err_const.NO_SPACE_AROUND_OPERATORS, [3, 1]) ] self.common.lex_string(expected, 'this = "words"')
def test_error_handled_on_last_line_without_return(self): error_file_path = os.path.join(TESTS_RESOURCES_PATH, 'error_handling_files/error-file.brs') chars = open(error_file_path, "r+").read() result = Lexer().lex(chars) expected = [ msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK, ['"error file', 1]) ] self.assertEqual(expected, result["Tokens"])
def test_multiple_tokens_and_empty_lines(self): bslint.load_config_file( user_filepath=self.single_empty_lines_config_path, default_filepath=TEST_CONFIG_FILE_PATH) multiple_tokens_and_empty_lines_file_path = os.path.join( EMPTY_LINES_TEST_FILES_PATH, 'multiple-tokens-and-empty-lines.brs') file_name = multiple_tokens_and_empty_lines_file_path expected = [ error.get_error_msg(err_const.CONSECUTIVE_EMPTY_LINES, [1, 3]) ] self.common.lex_file(expected, file_name)
def test_lex_whole_file_with_multiple_errors(self): skeleton_main_with_errors_file_path = os.path.join( LEXING_TEST_FILES_PATH, 'skeleton-main-with-errors.brs') chars = open(skeleton_main_with_errors_file_path, "r+").read() result = Lexer().lex(chars) expected = [ msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK, ['"roSGScreen)', 2]) ] self.assertEqual(expected, result[const.TOKENS]) self.assertEqual(const.ERROR, result[const.STATUS])
def test_indentation_error(self): bslint.load_config_file(user_filepath=self.indentation_config_path, default_filepath=TEST_CONFIG_FILE_PATH) basic_indentation_file_path = os.path.join(STYLING_TEST_FILES_PATH, 'basic-indentation.txt') file_name = basic_indentation_file_path file = open(file_name, "r+").read() expected = [ error.get_error_msg(err_const.TAB_INDENTATION_ERROR, [4, 2]) ] result = Lexer().lex(file) self.assertEqual(expected, result[const.WARNINGS])
def test_warning_message_printed(self): out = StringIO() warning_file_path = os.path.join( TESTS_RESOURCES_PATH, 'error_handling_files/warning-file.brs') result = bslint.bslint.runner(to_lex=warning_file_path, out=out).printed_output self.assertEqual( msg_handler.get_print_msg(print_const.NO_MANIFEST) + msg_handler.get_print_msg(print_const.NO_BSLINTRC) + msg_handler.get_print_msg(print_const.FILE_NAME, ["file://" + warning_file_path]) + msg_handler.get_print_msg( const.WARNINGS, [msg_handler.get_error_msg(err_const.TAB_AND_SPACES, [1])]) + msg_handler.get_print_msg( const.WARNINGS, [msg_handler.get_error_msg(err_const.TAB_AND_SPACES, [2])]) + msg_handler.get_print_msg(print_const.WARNINGS_IN_FILE, [2]) + msg_handler.get_print_msg(print_const.LINTING_COMPLETE) + msg_handler.get_print_msg(print_const.TOTAL_WARNINGS, [2]) + msg_handler.get_print_msg(print_const.TOTAL_ERRORS, [0]), result)
def test_indent_with_only_tabs_with_error(self): tab_only_indentation_config_path = os.path.join( TESTS_CONFIG_PATH, 'indentation/tab-only-indentation.json') bslint.load_config_file(user_filepath=tab_only_indentation_config_path, default_filepath=TEST_CONFIG_FILE_PATH) indent_with_tabs_only_file_path = os.path.join( STYLING_TEST_FILES_PATH, 'indent-with-tabs-only.txt') file_name = indent_with_tabs_only_file_path file = open(file_name, "r+").read() expected = [error.get_error_msg(err_const.TAB_AND_SPACES, [10])] result = Lexer().lex(file) self.assertEqual(expected, result[const.WARNINGS])
def test_error_message_printed(self): out = StringIO() error_file_path = os.path.join(TESTS_RESOURCES_PATH, 'error_handling_files/error-file.brs') result = bslint.bslint.runner(to_lex=error_file_path, out=out).printed_output print(result) self.assertEqual( msg_handler.get_print_msg(print_const.NO_MANIFEST) + msg_handler.get_print_msg(print_const.NO_BSLINTRC) + msg_handler.get_print_msg(print_const.FILE_NAME, ["file://" + error_file_path]) + msg_handler.get_print_msg(const.ERRORS, [ msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK, ['"error file', 1]) ]) + msg_handler.get_print_msg(print_const.LINTING_COMPLETE) + msg_handler.get_print_msg(print_const.TOTAL_WARNINGS, [0]) + msg_handler.get_print_msg(print_const.TOTAL_ERRORS, [1]), result)
def test_multiple_keys_with_trailing_comma(self): input_str = "testObj = { a:1, b:2, c:3, }" exp_result = [ error.get_error_msg(err_const.TRAILING_COMMA_IN_OBJECTS, [1]) ] self.common.lex_string(exp_result, input_str)
def test_random_key(self): with self.assertRaises(ValueError) as ve: msg_handler.get_error_msg("RANDOM_KEY") self.assertEqual(err_const.NO_SUCH_KEY, ve.exception.args[0])
def handle_parsing_error(self, err_code): self.errors.append( err.get_error_msg(err_code, [self.handle_style.line_number - 1]))
def test_trailing_white_space_first_line(self): file_name = os.path.join(TRAILING_WHITE_SPACE_TEST_FILES_PATH, 'trailing-white-space-first-line.brs') expected = [error.get_error_msg(err_const.TRAILING_WHITE_SPACE, [1])] self.common.lex_file(expected, file_name)
def _warning_filter(self, result): if result: result[const.ERROR_PARAMS].append(str(self.line_number)) warning = msg_handler.get_error_msg(result[const.ERROR_KEY], result[const.ERROR_PARAMS]) self.warnings += [warning]
def handle_unexpected_token(self): end_of_line = self.characters[self.handle_style. current_char_index:].split("\n")[0] self.errors.append( err.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK, [end_of_line, self.handle_style.line_number]))
def test_real_file(self): spell_check_file_path = os.path.join(STYLING_TEST_FILES_PATH, 'spell-check.brs') self.common.lex_file( [error.get_error_msg(err_const.TYPO_IN_CODE, [2])], spell_check_file_path)
def test_no_spaces_around_operator(self): expected = [ error.get_error_msg(err_const.NO_SPACE_AROUND_OPERATORS, [1, 1]) ] self.common.lex_string(expected, 'this="words"')
def test_multiple_keys_invalid(self): input_str = "testObj = { a:1, b:2\n c:3 }" exp_result = [error.get_error_msg(err_const.COMMAS_IN_OBJECTS, [1])] self.common.lex_string(exp_result, input_str)
def test_misspelled_comment_from_file(self): incorrect_comment_spelling_file_path = os.path.join( STYLING_TEST_FILES_PATH, 'incorrect-comment-spelling.brs') self.common.lex_file( [error.get_error_msg(err_const.TYPO_IN_CODE, [1])], incorrect_comment_spelling_file_path)
def test_skip_line_command_with_typo(self): bslint.load_config_file() self.common.lex_warnings_match( [msg_handler.get_error_msg(err_const.TYPO_IN_CODE, [3])], "'BSLint_skip_line\ny=4\nxgygu = 22\n")