test2doc 0.0.3__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: test2doc
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: Generate documentation from tests
5
5
  Project-URL: Homepage, https://github.com/i2mint/test2doc
6
6
  Author: Thor Whalen
@@ -16,7 +16,6 @@ Classifier: Topic :: Software Development :: Documentation
16
16
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
17
  Requires-Python: >=3.10
18
18
  Requires-Dist: dol
19
- Requires-Dist: i2
20
19
  Provides-Extra: dev
21
20
  Requires-Dist: pytest-cov>=4.0; extra == 'dev'
22
21
  Requires-Dist: pytest>=7.0; extra == 'dev'
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "test2doc"
9
- version = "0.0.3"
9
+ version = "0.0.5"
10
10
  description = "Generate documentation from tests"
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.10"
@@ -32,10 +32,7 @@ classifiers = [
32
32
  "Topic :: Software Development :: Documentation",
33
33
  "Topic :: Software Development :: Libraries :: Python Modules",
34
34
  ]
35
- dependencies = [
36
- "i2",
37
- "dol",
38
- ]
35
+ dependencies = ["dol"]
39
36
 
40
37
  [project.urls]
41
38
  Homepage = "https://github.com/i2mint/test2doc"
@@ -1,6 +1,5 @@
1
1
  """Base functionality for test2doc"""
2
2
 
3
- from i2.tests.test_wrapper import test_mk_ingress_from_name_mapper
4
3
  import inspect
5
4
  import textwrap
6
5
  from typing import Union
@@ -1,5 +1,6 @@
1
1
  """Utils for jupyter (ipython) notebooks."""
2
2
 
3
+ from itertools import islice
3
4
  import json
4
5
  from pathlib import Path
5
6
  from typing import Union, List, Tuple
@@ -73,7 +74,7 @@ def cells_with_largest_output(notebook, n=5, cell_egress=_output_size_stats):
73
74
  sorted_cells = sort_notebook_cells(
74
75
  notebook, key=get_output_size, reverse=True, cell_egress=cell_egress
75
76
  )
76
- return [next(sorted_cells) for _ in range(n)]
77
+ return list(islice(sorted_cells, n))
77
78
 
78
79
 
79
80
  def clear_outputs_of_cell_ids(notebook, cell_ids):
@@ -0,0 +1,104 @@
1
+ """Tests for base conversion helpers, doctest_utils and notebook_utils."""
2
+
3
+ import json
4
+
5
+ import pytest
6
+
7
+ from test2doc import doctest_utils as du
8
+ from test2doc import notebook_utils as nu
9
+ from test2doc.base import (
10
+ are_all_valid_python_code,
11
+ blocks_to_docs,
12
+ code_to_docs,
13
+ is_proper_partition,
14
+ is_valid_blocks_of_src_string,
15
+ is_valid_python_code,
16
+ src_string_to_blocks,
17
+ )
18
+
19
+
20
+ def test_validity_helpers():
21
+ assert is_valid_python_code("x = 1") and not is_valid_python_code("x =")
22
+ assert are_all_valid_python_code(["a = 1", "b = 2"])
23
+ assert not are_all_valid_python_code(["a = 1", "b ="])
24
+ assert is_proper_partition(["a = 1", "b = 2"], "a = 1\nb = 2")
25
+ assert not is_proper_partition(["a = 1"], "a = 1\nb = 2")
26
+ assert is_valid_blocks_of_src_string(["a = 1", "b = 2"], "a = 1\nb = 2")
27
+
28
+
29
+ def test_blocks_to_docs_alternates_text_and_code():
30
+ docs = blocks_to_docs(["# a comment", "x = 1", "y = 2", "# more"])
31
+ assert docs.startswith(" a comment\n")
32
+ assert "```python\nx = 1\ny = 2\n```" in docs
33
+ assert docs.rstrip().endswith("more")
34
+
35
+
36
+ def test_code_to_docs_from_string_and_function():
37
+ src = "# intro\nx = 1\n# next\nprint(x)"
38
+ docs = code_to_docs(src)
39
+ assert "intro" in docs and "```python\nx = 1\n```" in docs
40
+ assert src_string_to_blocks(src) # non-empty
41
+
42
+ def demo():
43
+ # say hi
44
+ greeting = "hi"
45
+ return greeting
46
+
47
+ assert "greeting" in code_to_docs(demo)
48
+
49
+
50
+ def test_doctest_utils():
51
+ assert du.remove_assertion("assert x == 1\nplain") == "x\nplain"
52
+ assert (
53
+ du.assertions_to_doctest(">>> assert f(1) == 2\nother")
54
+ == ">>> f(1)\n2\nother\n"
55
+ )
56
+ out = du.txt_to_doctest_txt("a = 1\n b = 2\n")
57
+ assert out.splitlines()[0] == ">>> a = 1"
58
+ assert out.splitlines()[1].startswith("...\t")
59
+ assert out.splitlines()[-1] == ">>> "
60
+
61
+
62
+ NB = {
63
+ "cells": [
64
+ {"id": "a", "outputs": [{"text": ["12345"]}]},
65
+ {"id": "b", "outputs": [{"data": {"text/plain": "123", "image/png": ["ab", "c"]}}]},
66
+ {"id": "c"},
67
+ ]
68
+ }
69
+
70
+
71
+ def test_notebook_dict_and_sizes(tmp_path):
72
+ assert nu.ensure_notebook_dict(NB) is NB
73
+ p = tmp_path / "n.ipynb"
74
+ p.write_text(json.dumps(NB))
75
+ assert nu.ensure_notebook_dict(str(p)) == NB
76
+ with pytest.raises(FileNotFoundError):
77
+ nu.ensure_notebook_dict(str(tmp_path / "missing.ipynb"))
78
+ assert [nu.get_output_size(c) for c in NB["cells"]] == [5, 6, 0]
79
+
80
+
81
+ def test_sort_and_largest():
82
+ ids = [c["id"] for c in nu.sort_notebook_cells(NB, key=nu.get_output_size, reverse=True)]
83
+ assert ids == ["b", "a", "c"]
84
+ assert nu.cells_with_largest_output(NB, n=2) == [
85
+ {"id": "b", "output_size": 6},
86
+ {"id": "a", "output_size": 5},
87
+ ]
88
+ with pytest.raises(ValueError, match="Invalid notebook"):
89
+ list(nu.sort_notebook_cells({"nope": 1}))
90
+
91
+
92
+ def test_largest_default_n_on_small_notebook():
93
+ assert len(nu.cells_with_largest_output(NB)) == 3
94
+
95
+
96
+ def test_clear_outputs():
97
+ import copy
98
+
99
+ nb = copy.deepcopy(NB)
100
+ out = nu.clear_outputs_of_cell_ids(nb, {"a"})
101
+ assert out["cells"][0]["outputs"] == [] and out["cells"][1]["outputs"]
102
+ nb2 = copy.deepcopy(NB)
103
+ nu.clear_outputs_of_largest_output_cells(nb2, n=1)
104
+ assert nb2["cells"][1]["outputs"] == []
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes