tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_util.py (2387B)


      1 #!/usr/bin/env python
      2 
      3 """
      4 Test how our utility functions are working.
      5 """
      6 
      7 from io import StringIO
      8 from textwrap import dedent
      9 
     10 import mozunit
     11 import pytest
     12 from manifestparser import read_ini
     13 from manifestparser.util import evaluate_list_from_string
     14 
     15 
     16 @pytest.fixture(scope="module")
     17 def parse_manifest():
     18    def inner(string, **kwargs):
     19        buf = StringIO()
     20        buf.write(dedent(string))
     21        buf.seek(0)
     22        return read_ini(buf, **kwargs)[0]
     23 
     24    return inner
     25 
     26 
     27 @pytest.mark.parametrize(
     28    "test_manifest, expected_list",
     29    [
     30        [
     31            """
     32            [test_felinicity.py]
     33            kittens = true
     34            cats =
     35                "I",
     36                "Am",
     37                "A",
     38                "Cat",
     39            """,
     40            ["I", "Am", "A", "Cat"],
     41        ],
     42        [
     43            """
     44            [test_felinicity.py]
     45            kittens = true
     46            cats =
     47                ["I", 1],
     48                ["Am", 2],
     49                ["A", 3],
     50                ["Cat", 4],
     51            """,
     52            [
     53                ["I", 1],
     54                ["Am", 2],
     55                ["A", 3],
     56                ["Cat", 4],
     57            ],
     58        ],
     59    ],
     60 )
     61 def test_string_to_list_conversion(test_manifest, expected_list, parse_manifest):
     62    parsed_tests = parse_manifest(test_manifest)
     63    assert evaluate_list_from_string(parsed_tests[0][1]["cats"]) == expected_list
     64 
     65 
     66 @pytest.mark.parametrize(
     67    "test_manifest, failure",
     68    [
     69        [
     70            """
     71            # This will fail since the elements are not enlosed in quotes
     72            [test_felinicity.py]
     73            kittens = true
     74            cats =
     75                I,
     76                Am,
     77                A,
     78                Cat,
     79            """,
     80            ValueError,
     81        ],
     82        [
     83            """
     84            # This will fail since the syntax is incorrect
     85            [test_felinicity.py]
     86            kittens = true
     87            cats =
     88                ["I", 1,
     89                ["Am", 2,
     90                ["A", 3],
     91                ["Cat", 4],
     92            """,
     93            SyntaxError,
     94        ],
     95    ],
     96 )
     97 def test_string_to_list_conversion_failures(test_manifest, failure, parse_manifest):
     98    parsed_tests = parse_manifest(test_manifest)
     99    with pytest.raises(failure):
    100        evaluate_list_from_string(parsed_tests[0][1]["cats"])
    101 
    102 
    103 if __name__ == "__main__":
    104    mozunit.main()