diff --git a/docs/developer-guides/check-date-leap.py.exp b/docs/developer-guides/check-date-leap.py.exp new file mode 100644 index 0000000..2a38e5f --- /dev/null +++ b/docs/developer-guides/check-date-leap.py.exp @@ -0,0 +1 @@ +2016-12-31 has a leap second! diff --git a/docs/developer-guides/check-date-leap.py.exp.license b/docs/developer-guides/check-date-leap.py.exp.license new file mode 100644 index 0000000..10512f1 --- /dev/null +++ b/docs/developer-guides/check-date-leap.py.exp.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2024 Thomas Touhey +# SPDX-License-Identifier: Unlicense +# Placed in a separate file so that it does not appear in the produced docs. diff --git a/docs/developer-guides/checking-if-date-is-leap.rst b/docs/developer-guides/checking-if-date-is-leap.rst index 665d7aa..6695c3b 100644 --- a/docs/developer-guides/checking-if-date-is-leap.rst +++ b/docs/developer-guides/checking-if-date-is-leap.rst @@ -16,6 +16,4 @@ second, you can use the following code: The output of this program is the following: -.. code-block:: text - - 2016-12-31 has a leap second! +.. literalinclude:: check-date-leap.py.exp diff --git a/docs/developer-guides/convert-tai-to-utc.py.exp b/docs/developer-guides/convert-tai-to-utc.py.exp new file mode 100644 index 0000000..752c79e --- /dev/null +++ b/docs/developer-guides/convert-tai-to-utc.py.exp @@ -0,0 +1 @@ +2024-07-18T22:00:00+00:00 diff --git a/docs/developer-guides/convert-tai-to-utc.py.exp.license b/docs/developer-guides/convert-tai-to-utc.py.exp.license new file mode 100644 index 0000000..10512f1 --- /dev/null +++ b/docs/developer-guides/convert-tai-to-utc.py.exp.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2024 Thomas Touhey +# SPDX-License-Identifier: Unlicense +# Placed in a separate file so that it does not appear in the produced docs. diff --git a/docs/developer-guides/convert-utc-to-tai.py b/docs/developer-guides/convert-utc-to-tai.py index 2cac6da..04dbbed 100644 --- a/docs/developer-guides/convert-utc-to-tai.py +++ b/docs/developer-guides/convert-utc-to-tai.py @@ -1,8 +1,8 @@ -from datetime import UTC, datetime +from datetime import timezone, datetime from leapseconddata import LeapSecondData -my_date = datetime(2024, 7, 18, 22, 0, 0, tzinfo=UTC) +my_date = datetime(2024, 7, 18, 22, 0, 0, tzinfo=timezone.utc) data = LeapSecondData.from_standard_source() my_tai_date = data.to_tai(my_date) diff --git a/docs/developer-guides/convert-utc-to-tai.py.exp b/docs/developer-guides/convert-utc-to-tai.py.exp new file mode 100644 index 0000000..41f6d7c --- /dev/null +++ b/docs/developer-guides/convert-utc-to-tai.py.exp @@ -0,0 +1 @@ +2024-07-18T22:00:37+00:00 diff --git a/docs/developer-guides/convert-utc-to-tai.py.exp.license b/docs/developer-guides/convert-utc-to-tai.py.exp.license new file mode 100644 index 0000000..10512f1 --- /dev/null +++ b/docs/developer-guides/convert-utc-to-tai.py.exp.license @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2024 Thomas Touhey +# SPDX-License-Identifier: Unlicense +# Placed in a separate file so that it does not appear in the produced docs. diff --git a/docs/developer-guides/converting-tai-to-utc.rst b/docs/developer-guides/converting-tai-to-utc.rst index c81ec12..224db3f 100644 --- a/docs/developer-guides/converting-tai-to-utc.rst +++ b/docs/developer-guides/converting-tai-to-utc.rst @@ -17,6 +17,4 @@ For example: This program will provide you with the following output: -.. code-block:: text - - 2024-07-18T22:00:00g+00:00 +.. literalinclude:: convert-tai-to-utc.py.exp diff --git a/docs/developer-guides/converting-utc-to-tai.rst b/docs/developer-guides/converting-utc-to-tai.rst index 4049b3a..bd622bd 100644 --- a/docs/developer-guides/converting-utc-to-tai.rst +++ b/docs/developer-guides/converting-utc-to-tai.rst @@ -17,6 +17,4 @@ For example: This program will provide you with the following output: -.. code-block:: text - - 2024-07-18T22:00:37+00:00 +.. literalinclude:: convert-utc-to-tai.py.exp diff --git a/testleapseconddata.py b/testleapseconddata.py index 580ac26..1bf8315 100644 --- a/testleapseconddata.py +++ b/testleapseconddata.py @@ -9,8 +9,10 @@ """Test most leapseconddata functionality""" -# pylint: disable=missing-class-docstring,missing-function-docstring +import contextlib import datetime +import io +import pathlib import unittest import leapseconddata @@ -23,10 +25,12 @@ class LeapSecondDataTest(unittest.TestCase): def run_main(self, *args: str) -> None: - try: - leapseconddata.__main__.cli(args) - except SystemExit as e: - self.assertEqual(e.code, 0) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + try: + leapseconddata.__main__.cli(args) + except SystemExit as e: + self.assertEqual(e.code, 0) def test_main(self) -> None: self.run_main("info") @@ -132,6 +136,18 @@ def test_to_tai(self) -> None: assert when_tai.tzinfo is leapseconddata.tai assert when_tai2.tzinfo is leapseconddata.tai + def assertPrints(self, code: str, expected: str) -> None: # noqa: N802 + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + exec(code, {}, {}) + self.assertEqual(expected, buf.getvalue()) + + def test_doc(self) -> None: + docs = pathlib.Path(__file__).parent / "docs" + for expected in docs.rglob("**/*.py.exp"): + py = expected.with_suffix("") # Pop off the ".exp" suffix + self.assertPrints(py.read_text(encoding="utf-8"), expected.read_text(encoding="utf-8")) + if __name__ == "__main__": # pragma: no cover unittest.main()