Skip to content

Commit

Permalink
Merge pull request #27 from jepler/test-docs
Browse files Browse the repository at this point in the history
test & doc: Check that the doc examples' output matches
  • Loading branch information
jepler authored Jul 20, 2024
2 parents d2329db + 2ffe5c2 commit 37418fe
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/developer-guides/check-date-leap.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2016-12-31 has a leap second!
3 changes: 3 additions & 0 deletions docs/developer-guides/check-date-leap.py.exp.license
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 1 addition & 3 deletions docs/developer-guides/checking-if-date-is-leap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions docs/developer-guides/convert-tai-to-utc.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-07-18T22:00:00+00:00
3 changes: 3 additions & 0 deletions docs/developer-guides/convert-tai-to-utc.py.exp.license
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions docs/developer-guides/convert-utc-to-tai.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/developer-guides/convert-utc-to-tai.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-07-18T22:00:37+00:00
3 changes: 3 additions & 0 deletions docs/developer-guides/convert-utc-to-tai.py.exp.license
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 1 addition & 3 deletions docs/developer-guides/converting-tai-to-utc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions docs/developer-guides/converting-utc-to-tai.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 21 additions & 5 deletions testleapseconddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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()

0 comments on commit 37418fe

Please sign in to comment.