def test_fix_param_dynamic(): r = RSTReader() # in 'machine' module param_in = "(*, trigger, handler=None, wake=machine.IDLE)" param_out = "(*, trigger, handler=None, wake=IDLE)" # in module r.current_module = "machine" result = r.fix_parameters(param_in) assert result == param_out r.current_module = "" result = r.fix_parameters(param_in) assert result != param_out assert result == param_in # ----------------------- param_in = "baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None)" param_out = "baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=MSB, sck=None, mosi=None, miso=None)" # in class r.current_class = "SPI" result = r.fix_parameters(param_in) assert result == param_out # not in class r.current_class = "" result = r.fix_parameters(param_in) assert result != param_out assert result == param_in
def load_rst(r: RSTReader, text: Union[str, List[str]]): "load a string or list of strings" if isinstance(text, List): r.rst_text = text else: r.rst_text = text.splitlines() r.max_line = len(r.rst_text) r.filename = "testmodule"
def test_import_typing(): "always include typing" r = RSTReader() r.prepare_output() lines = r.output # lines = for line in TYPING_IMPORT: assert line.strip() in [l.rstrip() for l in r.output ], f"did not import typing : '{line}'"
def test_parse_class_modulename(line, module): # check if the module name has been removed form the class def r = RSTReader() load_rst(r, CD_ACCEL) r.current_module = module # process r.parse() # check assert len(r.output) > 1 assert line in [l.rstrip() for l in r.output]
def test_parse_class_micro_modulename(line): # check if the module name has been removed form the class def r = RSTReader() load_rst(r, CD_HASHLIB) # r.current_module = module # 'uhashlib' # process r.parse() # check assert len(r.output) > 1 # don't care about indentation, # but one of the lines must start with the class def assert any([l.strip().startswith(line) for l in r.output])
def test_parse_docstr_quoted(): # quoted docstrings from module level constants r = RSTReader() load_rst(r, QUOTED_DOCSTR) r.parse() # check assert len(r.output) > 1 assert len(r.output_dict["constants"]) > 2 assert not any([l.startswith("# :") for l in r.output_dict["constants"] ]), "Some lines were not unquoted" assert not any([l.startswith("# +") for l in r.output_dict["constants"] ]), "Some lines were not unquoted"
def test_sequence_methods(): r = RSTReader() # Plug in test data r.rst_text = CLASS_METHOD_SEQ.splitlines(keepends=True) r.filename = "re.py" r.current_module = "re" r.max_line = len(r.rst_text) - 1 # process r.parse() assert len(r.output) > 1 # a class assert r.output_dict["class regex():"] # make sure all 4 methods are seen assert r.output_dict["class regex():"][ "def match"], "first method in sequence not found" assert r.output_dict["class regex():"][ "def search"], "second method in sequence not found" assert r.output_dict["class regex():"][ "def sub"], "third method in sequence not found" assert r.output_dict["class regex():"][ "def split"], "1st method after sequence not found "
def test_parse_docstr_module(): # check if the module name has been removed form the class def r = RSTReader() load_rst(r, MODULE_DOCSTR) # r.current_module = module # 'uhashlib' # process r.parse() # check assert len(r.output) > 1 assert len(r.output_dict["docstr"]) > 20 # start & end with triple Quote assert r.output_dict["docstr"][0] == '"""' assert r.output_dict["docstr"][-1] == '"""'
def test_module_constants(): # test is module level constants can be processed r = RSTReader() load_rst(r, MACHINE_RST) r.current_module = "machine" # process r.parse() # check assert len(r.output) > 1 list = r.output_dict["constants"] doc_list = [c for c in list if c.startswith("#")] const_list = [c for c in list if not c.startswith("#")] # should have 11 constants assert len(const_list) == 11 # and 11 single line comments for docstrings assert len(doc_list) == 11
def test_rst_parse_function(filename, expected): # testcase = FN_1 r = RSTReader() r.read_file(Path(filename)) # process r.parse() r.prepare_output() # check assert len(r.output) > 1 for fn in expected: assert fn in [l.rstrip() for l in r.output]
def test_rst_parse_class_10(line: str): # testcase = FN_1 r = RSTReader() r.read_file(Path("tests/rst/data/class_10.rst")) # process r.parse() r.prepare_output() # cleanup output # check if each expected line appears in the output # there can be more assert len(r.output) > 1 assert line in [l.rstrip() for l in r.output], f"did not generate : '{line}'"
def test_class_constants(): # check if the module name has been removed form the class def r = RSTReader() load_rst(r, MACHINE_PIN_RST) # r.current_module = module # 'uhashlib' # process r.parse() # check assert len(r.output) > 1 expected = [ "class Pin():", " # Selects the pin mode.", " IN : Any", " OPEN_DRAIN : Any", " # Test wildcard handling.", " # JOKER_* : Any", " def __init__(self, id, mode=-1, pull=-1, *, value=None, drive=-1, alt=-1) -> None:", ] lines = [l.rstrip() for l in r.output] for l in expected: assert l in lines
def test_number_sequence(): testcase = SEQ_NUMBERS r = RSTReader() # Plug in test data # r.rst_text = SEQ_NUMBERS r.rst_text = testcase r.filename = "testdata.py" r.current_module = "testdata" r.max_line = len(r.rst_text) - 1 # process r.parse() # check assert len(r.output) > 1 constants = len( [l for l in r.output_dict["constants"] if not l.startswith("# ")]) assert constants == 8
def test_sequence_3(): r = RSTReader() # Plug in test data # r.rst_text = SEQ_NUMBERS r.rst_text = SEQ_3.splitlines(keepends=True) r.filename = "testdata.py" r.current_module = "testdata" r.max_line = len(r.rst_text) - 1 # process r.parse() assert len(r.output) > 1 c_list = r.output_dict["class framebuf():"]["constants"] constants = len([l for l in c_list if not l.lstrip().startswith("# ")]) assert constants == 7
def test_sequence_functions(): r = RSTReader() # Plug in test data r.rst_text = FUNCTION_SEQ.splitlines(keepends=True) r.filename = "re.py" r.current_module = "re" r.max_line = len(r.rst_text) - 1 # process r.parse() assert len(r.output) > 1 # three function defs assert r.output_dict["def compile"], "first function in sequence not found" assert r.output_dict["def match"], "2nd function in sequence not found" assert r.output_dict[ "def search"], "1st function after sequence not found "
def test_comma_sequence(): SEQ_COMMAS = [ ".. data:: EEXIST, EAGAIN, etc.\n", " Error codes, based on ANSI C/POSIX standard. All error codes start with\n", " 'E'. As mentioned above,\n", ] r = RSTReader() # Plug in test data # r.rst_text = SEQ_NUMBERS r.rst_text = SEQ_COMMAS r.filename = "testdata.py" r.current_module = "testdata" r.max_line = len(r.rst_text) - 1 # process r.parse() # check that etc is not found etc = "etc. : Any\n" in r.output eexist = "EEXIST : Any\n" in r.output eagain = "EAGAIN : Any\n" in r.output assert not etc assert eexist assert eagain
def test_fix_param(param_in, param_out): "validate known parameter typing notation errors" r = RSTReader() result = r.fix_parameters(param_in) assert result == param_out