From d8db75610b516583851a17feeb3b91fad5e423e1 Mon Sep 17 00:00:00 2001 From: surister Date: Sat, 21 Dec 2019 19:24:34 +0100 Subject: [PATCH] Add syntax checker suite --- tests/syntax_checker.py | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/syntax_checker.py diff --git a/tests/syntax_checker.py b/tests/syntax_checker.py new file mode 100644 index 0000000..6c7d30b --- /dev/null +++ b/tests/syntax_checker.py @@ -0,0 +1,112 @@ +""" +Testing suite for https://github.com/bregman-arie/devops-interview-questions written by surister + +Even though both check_details_tag and check_summary_tags are practically the same, due to readability and functionality +it was decided to be split like that. + +Usage: +$ python tests/syntax_checker.py + +""" + +import pathlib + +p = pathlib.Path(__file__).parent.parent.joinpath('README.md') + +with open(p, 'rb') as f: + file_list = [line.rstrip() for line in f.readlines()] + +errors = [] + + +def count_details(file_list): + """ + Counts the total amount of
and
+ + Used for debugging purpose, not meant to be used in actual tests + """ + details_final_count = 0 + details_count = 0 + + for line_number, line in enumerate(file_list): + if b'
' in line: + details_count += 1 + if b'
' in line: + details_final_count += 1 + + return details_count, details_final_count + + +def check_details_tag(file_list): + """ + Check whether the structure: +
+ ... +
+ + Is correctly followed, if not generates an error. + + """ + + after_detail = False + error = False + err_message = '' + for line_number, line in enumerate(file_list): + if b'
' in line and b'
' in line: + pass + else: + if b'
' in line and after_detail: + error = True + if b'
' in line and not after_detail: + err_message = 'Missing opening detail tag' + error = True + + if b'
' in line: + after_detail = True + + if b'
' in line and after_detail: + err_message = 'Missing closing detail tag' + after_detail = False + + if error: + raise Exception(f'{err_message} at line {line_number -1}') + + +def check_summary_tag(file_list): + """ + Check whether the structure: + + ... + + + Is correctly followed, if not generates an error. + + """ + + after_detail = False + error = False + err_message = '' + for line_number, line in enumerate(file_list): + if b'' in line and b'' in line: + pass + else: + if b'' in line and after_detail: + error = True + if b'' in line and not after_detail: + err_message = 'Missing opening detail tag' + error = True + + if b'' in line: + after_detail = True + + if b'' in line and after_detail: + err_message = 'Missing closing detail tag' + after_detail = False + + if error: + raise Exception(f'{err_message} at line {line_number -3}') + + +if __name__ == '__main__': + check_details_tag(file_list) + check_summary_tag(file_list)