aports/main/py3-iniconfig/unittest_tests.patch
2024-03-21 15:55:05 +00:00

477 lines
15 KiB
Diff

--- a/test_iniconfig.py
+++ b/test_iniconfig.py
@@ -1,5 +1,5 @@
from __future__ import annotations
-import pytest
+import unittest
from iniconfig import IniConfig, ParseError, __all__ as ALL
from iniconfig._parse import _ParsedLine as PL
from iniconfig import iscommentline
@@ -42,264 +42,270 @@
}
-@pytest.fixture(params=sorted(check_tokens))
-def input_expected(request: pytest.FixtureRequest) -> tuple[str, list[PL]]:
+class TestIniConfig(unittest.TestCase):
- return check_tokens[request.param]
+ def input_expected(self, key: str) -> tuple[str, list[PL]]:
+ # Sample usage: self.input_expected("value")
+ # Sample output: ('value = 1', [_ParsedLine(lineno=0, section=None,
+ # name='value', value='1')])
+ return check_tokens[key]
-@pytest.fixture
-def input(input_expected: tuple[str, list[PL]]) -> str:
- return input_expected[0]
+ def input(self, input_expected: tuple[str, list[PL]]) -> str:
+ # Sample usage: self.input(self.input_expected("value"))
+ return input_expected[0]
-@pytest.fixture
-def expected(input_expected: tuple[str, list[PL]]) -> list[PL]:
- return input_expected[1]
+ def expected(self, input_expected: tuple[str, list[PL]]) -> list[PL]:
+ # Sample usage: self.expected(self.input_expected("value"))
+ return input_expected[1]
-def parse(input: str) -> list[PL]:
- from iniconfig._parse import parse_lines
- return parse_lines("sample", input.splitlines(True))
+ def parse(self, input: str) -> list[PL]:
+ # Sample usage: self.parse(self.input(self.input_expected("value")))
+ from iniconfig._parse import parse_lines
+ return parse_lines("sample", input.splitlines(True))
-def parse_a_error(input: str) -> ParseError:
- try:
- parse(input)
- except ParseError as e:
- return e
- else:
- raise ValueError(input)
+ def parse_a_error(self, input: str) -> ParseError:
+ try:
+ self.parse(input)
+ except ParseError as e:
+ return e
+ else:
+ raise ValueError(input)
-def test_tokenize(input: str, expected: list[PL]) -> None:
- parsed = parse(input)
- assert parsed == expected
+ def test_tokenize(self) -> None:
+ for i in sorted(check_tokens):
+ with self.subTest(i=i):
+ input_expected = self.input_expected(i)
+ parsed = self.parse(self.input(input_expected))
+ expected = self.expected(input_expected)
+ self.assertEqual(parsed, expected)
-def test_parse_empty() -> None:
- parsed = parse("")
- assert not parsed
- ini = IniConfig("sample", "")
- assert not ini.sections
+ def test_parse_empty(self) -> None:
+ parsed = self.parse("")
+ self.assertFalse(parsed)
+ ini = IniConfig("sample", "")
+ self.assertFalse(ini.sections)
-def test_ParseError() -> None:
- e = ParseError("filename", 0, "hello")
- assert str(e) == "filename:1: hello"
+ def test_ParseError(self) -> None:
+ e = ParseError("filename", 0, "hello")
+ self.assertEqual(str(e), "filename:1: hello")
-def test_continuation_needs_perceeding_token() -> None:
- err = parse_a_error(" Foo")
- assert err.lineno == 0
+ def test_continuation_needs_perceeding_token(self) -> None:
+ err = self.parse_a_error(" Foo")
+ self.assertEqual(err.lineno, 0)
-def test_continuation_cant_be_after_section() -> None:
- err = parse_a_error("[section]\n Foo")
- assert err.lineno == 1
+ def test_continuation_cant_be_after_section(self) -> None:
+ err = self.parse_a_error("[section]\n Foo")
+ self.assertEqual(err.lineno, 1)
-def test_section_cant_be_empty() -> None:
- err = parse_a_error("[]")
- assert err.lineno == 0
+ def test_section_cant_be_empty(self) -> None:
+ err = self.parse_a_error("[]")
+ self.assertEqual(err.lineno, 0)
-@pytest.mark.parametrize(
- "line",
- [
- "!!",
- ],
-)
-def test_error_on_weird_lines(line: str) -> None:
- parse_a_error(line)
+ def test_error_on_weird_lines(self) -> None:
+ line = [ "!!", ]
+ for l in line:
+ with self.subTest(l=l):
+ self.parse_a_error(l)
-def test_iniconfig_from_file(tmp_path: Path) -> None:
- path = tmp_path / "test.txt"
- path.write_text("[metadata]\nname=1")
+ def test_iniconfig_from_file(self) -> None:
+ path = Path("test.txt")
+ path.write_text("[metadata]\nname=1")
- config = IniConfig(path=str(path))
- assert list(config.sections) == ["metadata"]
- config = IniConfig(str(path), "[diff]")
- assert list(config.sections) == ["diff"]
- with pytest.raises(TypeError):
- IniConfig(data=path.read_text()) # type: ignore
+ config = IniConfig(path=str(path))
+ self.assertEqual(list(config.sections), ["metadata"])
+ config = IniConfig(str(path), "[diff]")
+ self.assertEqual(list(config.sections), ["diff"])
+ with self.assertRaises(TypeError):
+ IniConfig(data=path.read_text()) # type: ignore
-def test_iniconfig_section_first() -> None:
- with pytest.raises(ParseError) as excinfo:
- IniConfig("x", data="name=1")
- assert excinfo.value.msg == "no section header defined"
+ def test_iniconfig_section_first(self) -> None:
+ with self.assertRaisesRegex(ParseError, "no section header defined"):
+ IniConfig("x", data="name=1")
-def test_iniconig_section_duplicate_fails() -> None:
- with pytest.raises(ParseError) as excinfo:
- IniConfig("x", data="[section]\n[section]")
- assert "duplicate section" in str(excinfo.value)
+ def test_iniconfig_section_duplicate_fails(self) -> None:
+ with self.assertRaisesRegex(ParseError, "duplicate section"):
+ IniConfig("x", data="[section]\n[section]")
-def test_iniconfig_duplicate_key_fails() -> None:
- with pytest.raises(ParseError) as excinfo:
- IniConfig("x", data="[section]\nname = Alice\nname = bob")
+ def test_iniconfig_duplicate_key_fails(self) -> None:
+ with self.assertRaisesRegex(ParseError, "duplicate name"):
+ IniConfig("x", data="[section]\nname = Alice\nname = bob")
- assert "duplicate name" in str(excinfo.value)
+ def test_iniconfig_lineof(self) -> None:
+ config = IniConfig(
+ "x.ini",
+ data=("[section]\nvalue = 1\n[section2]\n# comment\nvalue =2"),
+ )
-def test_iniconfig_lineof() -> None:
- config = IniConfig(
- "x.ini",
- data=("[section]\nvalue = 1\n[section2]\n# comment\nvalue =2"),
- )
+ self.assertIs(config.lineof("missing"), None)
+ self.assertEqual(config.lineof("section"), 1)
+ self.assertEqual(config.lineof("section2"), 3)
+ self.assertEqual(config.lineof("section", "value"), 2)
+ self.assertEqual(config.lineof("section2", "value"), 5)
- assert config.lineof("missing") is None
- assert config.lineof("section") == 1
- assert config.lineof("section2") == 3
- assert config.lineof("section", "value") == 2
- assert config.lineof("section2", "value") == 5
+ self.assertEqual(config["section"].lineof("value"), 2)
+ self.assertEqual(config["section2"].lineof("value"), 5)
- assert config["section"].lineof("value") == 2
- assert config["section2"].lineof("value") == 5
+ def test_iniconfig_get_convert(self) -> None:
+ config = IniConfig("x", data="[section]\nint = 1\nfloat = 1.1")
+ self.assertEqual(config.get("section", "int"), "1")
+ self.assertEqual(config.get("section", "int", convert=int), 1)
-def test_iniconfig_get_convert() -> None:
- config = IniConfig("x", data="[section]\nint = 1\nfloat = 1.1")
- assert config.get("section", "int") == "1"
- assert config.get("section", "int", convert=int) == 1
+ def test_iniconfig_get_missing(self) -> None:
+ config = IniConfig("x", data="[section]\nint = 1\nfloat = 1.1")
+ self.assertEqual(config.get("section", "missing", default=1), 1)
+ self.assertIs(config.get("section", "missing"), None)
-def test_iniconfig_get_missing() -> None:
- config = IniConfig("x", data="[section]\nint = 1\nfloat = 1.1")
- assert config.get("section", "missing", default=1) == 1
- assert config.get("section", "missing") is None
+ def test_section_get(self) -> None:
+ config = IniConfig("x", data="[section]\nvalue=1")
+ section = config["section"]
+ self.assertEqual(section.get("value", convert=int), 1)
+ self.assertEqual(section.get("value", 1), "1")
+ self.assertEqual(section.get("missing", 2), 2)
-def test_section_get() -> None:
- config = IniConfig("x", data="[section]\nvalue=1")
- section = config["section"]
- assert section.get("value", convert=int) == 1
- assert section.get("value", 1) == "1"
- assert section.get("missing", 2) == 2
+ def test_missing_section(self) -> None:
+ config = IniConfig("x", data="[section]\nvalue=1")
+ with self.assertRaises(KeyError):
+ config["other"]
-def test_missing_section() -> None:
- config = IniConfig("x", data="[section]\nvalue=1")
- with pytest.raises(KeyError):
- config["other"]
+ def test_section_getitem(self) -> None:
+ config = IniConfig("x", data="[section]\nvalue=1")
+ self.assertEqual(config["section"]["value"], "1")
+ self.assertEqual(config["section"]["value"], "1")
-def test_section_getitem() -> None:
- config = IniConfig("x", data="[section]\nvalue=1")
- assert config["section"]["value"] == "1"
- assert config["section"]["value"] == "1"
+ def test_section_iter(self) -> None:
+ config = IniConfig("x", data="[section]\nvalue=1")
+ names = list(config["section"])
+ self.assertEqual(names, ["value"])
+ items = list(config["section"].items())
+ self.assertEqual(items, [("value", "1")])
-def test_section_iter() -> None:
- config = IniConfig("x", data="[section]\nvalue=1")
- names = list(config["section"])
- assert names == ["value"]
- items = list(config["section"].items())
- assert items == [("value", "1")]
+ def test_config_iter(self) -> None:
+ config = IniConfig(
+ "x.ini",
+ data=dedent(
+ """
+ [section1]
+ value=1
+ [section2]
+ value=2
+ """
+ ),
+ )
+ l = list(config)
+ self.assertEqual(len(l), 2)
+ self.assertEqual(l[0].name, "section1")
+ self.assertEqual(l[0]["value"], "1")
+ self.assertEqual(l[1].name, "section2")
+ self.assertEqual(l[1]["value"], "2")
-def test_config_iter() -> None:
- config = IniConfig(
- "x.ini",
- data=dedent(
- """
- [section1]
- value=1
- [section2]
- value=2
- """
- ),
- )
- l = list(config)
- assert len(l) == 2
- assert l[0].name == "section1"
- assert l[0]["value"] == "1"
- assert l[1].name == "section2"
- assert l[1]["value"] == "2"
+ def test_config_contains(self) -> None:
+ config = IniConfig(
+ "x.ini",
+ data=dedent(
+ """
+ [section1]
+ value=1
+ [section2]
+ value=2
+ """
+ ),
+ )
+ self.assertNotIn("xyz", config)
+ self.assertIn("section1", config)
+ self.assertIn("section2", config)
-def test_config_contains() -> None:
- config = IniConfig(
- "x.ini",
- data=dedent(
- """
- [section1]
- value=1
- [section2]
- value=2
- """
- ),
- )
- assert "xyz" not in config
- assert "section1" in config
- assert "section2" in config
+ def test_iter_file_order(self) -> None:
+ config = IniConfig(
+ "x.ini",
+ data=dedent("""
+ [section2] #cpython dict ordered before section
+ value = 1
+ value2 = 2 # dict ordered before value
+ [section]
+ a = 1
+ b = 2
+ """
+ ),
+ )
+ l = list(config)
+ secnames = [x.name for x in l]
+ self.assertEqual(secnames, ["section2", "section"])
+ self.assertEqual(list(config["section2"]), ["value", "value2"])
+ self.assertEqual(list(config["section"]), ["a", "b"])
-def test_iter_file_order() -> None:
- config = IniConfig(
- "x.ini",
- data="""
-[section2] #cpython dict ordered before section
-value = 1
-value2 = 2 # dict ordered before value
-[section]
-a = 1
-b = 2
-""",
- )
- l = list(config)
- secnames = [x.name for x in l]
- assert secnames == ["section2", "section"]
- assert list(config["section2"]) == ["value", "value2"]
- assert list(config["section"]) == ["a", "b"]
+ def test_example_pypirc(self) -> None:
+ config = IniConfig(
+ "pypirc",
+ data=dedent(
+ """
+ [distutils]
+ index-servers =
+ pypi
+ other
-def test_example_pypirc() -> None:
- config = IniConfig(
- "pypirc",
- data=dedent(
- """
- [distutils]
- index-servers =
- pypi
- other
+ [pypi]
+ repository: <repository-url>
+ username: <username>
+ password: <password>
- [pypi]
- repository: <repository-url>
- username: <username>
- password: <password>
+ [other]
+ repository: http://example.com/pypi
+ username: <username>
+ password: <password>
+ """
+ ),
+ )
+ distutils, pypi, other = list(config)
+ self.assertEqual(distutils["index-servers"], "pypi\nother")
+ self.assertEqual(pypi["repository"], "<repository-url>")
+ self.assertEqual(pypi["username"], "<username>")
+ self.assertEqual(pypi["password"], "<password>")
+ self.assertEqual(["repository", "username", "password"], list(other))
- [other]
- repository: http://example.com/pypi
- username: <username>
- password: <password>
- """
- ),
- )
- distutils, pypi, other = list(config)
- assert distutils["index-servers"] == "pypi\nother"
- assert pypi["repository"] == "<repository-url>"
- assert pypi["username"] == "<username>"
- assert pypi["password"] == "<password>"
- assert ["repository", "username", "password"] == list(other)
+ def test_api_import(self) -> None:
+ self.assertEqual(ALL, ["IniConfig", "ParseError", "COMMENTCHARS",
+ "iscommentline"])
-def test_api_import() -> None:
- assert ALL == ["IniConfig", "ParseError", "COMMENTCHARS", "iscommentline"]
+ def test_iscommentline_true(self) -> None:
+ line = [
+ "#qwe",
+ " #qwe",
+ ";qwe",
+ " ;qwe",
+ ]
+ for l in line:
+ with self.subTest(l=l):
+ self.assertTrue(iscommentline(l))
-@pytest.mark.parametrize(
- "line",
- [
- "#qwe",
- " #qwe",
- ";qwe",
- " ;qwe",
- ],
-)
-def test_iscommentline_true(line: str) -> None:
- assert iscommentline(line)
+
+if __name__ == "__main__":
+ unittest.main()