def test_preprocess_should_use_definesdb_when_provided():
    p = Preprocessor()

    content = """\
#define LOCALCONST 42

entry:
    move r1, LOCALCONST
    move r2, DBKEY
"""

    # first try without db
    result = p.preprocess(content)

    assert "move r1, 42" in result
    assert "move r2, DBKEY" in result
    assert "move r2, 99" not in result

    # now try with db
    db = DefinesDB()
    db.clear()
    db.update({'DBKEY': '99'})
    p.use_db(db)

    result = p.preprocess(content)

    assert "move r1, 42" in result
    assert "move r2, 99" in result
    assert "move r2, DBKEY" not in result
def preprocess_should_replace_all_defined_words():
    p = Preprocessor()

    lines = """\
    #define DR_REG_RTCIO_BASE 0x3ff48400
    #define SOME_OFFSET 4

    move r1, DR_REG_RTCIO_BASE
    add r2, r1, SOME_OFFSET"""

    assert "move r1, 0x3ff48400" in p.preprocess(lines)
    assert "add r2, r1, 4" in p.preprocess(lines)
def test_preprocess_should_ensure_the_definesdb_is_properly_closed_after_use():
    content = """\
    #define CONST 42
    move r1, CONST"""

    # remove any existing db
    db = DefinesDB()
    db.open()
    assert db.is_open()

    # now preprocess using db
    p = Preprocessor()
    p.use_db(db)

    p.preprocess(content)

    assert not db.is_open()
def preprocess_should_replace_words_defined():
    p = Preprocessor()

    lines = """\
    #define DR_REG_RTCIO_BASE 0x3ff48400

    move r1, DR_REG_RTCIO_BASE"""

    assert "move r1, 0x3ff48400" in p.preprocess(lines)
def preprocess_should_not_replace_substrings_within_identifiers():
    p = Preprocessor()

    # ie. if AAA is defined don't touch PREFIX_AAA_SUFFIX
    lines = """\
    #define RTCIO 4
    move r1, DR_REG_RTCIO_BASE"""

    assert "DR_REG_4_BASE" not in p.preprocess(lines)

    # ie. if A and AA are defined, don't replace AA as two A's but with AA
    lines = """\
    #define A 4
    #define AA 8
    move r1, A
    move r2, AA"""

    assert "move r1, 4" in p.preprocess(lines)
    assert "move r2, 8" in p.preprocess(lines)
def preprocess_should_replace_defines_used_in_defines():
    p = Preprocessor()

    lines = """\
    #define BITS (BASE << 4)
    #define BASE 0x1234

    move r1, BITS
    move r2, BASE"""

    assert "move r1, (0x1234 << 4)" in p.preprocess(lines)
def preprocess_should_replace_words_defined_multiple_times():
    p = Preprocessor()

    lines = """\
    #define DR_REG_RTCIO_BASE 0x3ff48400

    move r1, DR_REG_RTCIO_BASE  #once
    move r2, DR_REG_RTCIO_BASE  #second time"""

    assert "move r1, 0x3ff48400" in p.preprocess(lines)
    assert "move r2, 0x3ff48400" in p.preprocess(lines)
def preprocess_should_remove_comments_and_defines_but_keep_the_lines_as_empty_lines(
):
    p = Preprocessor()

    lines = """\
    // copyright
    #define A 1

    move r1, r2"""

    assert p.preprocess(lines) == "\n\n\n\tmove r1, r2"
def test_preprocess_should_ensure_no_definesdb_is_created_when_only_reading_from_it(
):
    content = """\
    #define CONST 42
    move r1, CONST"""

    # remove any existing db
    db = DefinesDB()
    db.clear()
    assert not file_exists(DBNAME)

    # now preprocess using db
    p = Preprocessor()
    p.use_db(db)

    result = p.preprocess(content)

    assert "move r1, 42" in result

    assert not file_exists(DBNAME)
Example #10
0
def replace_defines_should_return_remove_comments():
    p = Preprocessor()

    line = "// some comment"
    expected = ""
    assert p.preprocess(line) == expected
Example #11
0
def test_replace_defines_should_return_empty_line_given_empty_string():
    p = Preprocessor()

    assert p.preprocess("") == ""