-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
format_signatures: Fix whitespace issues and add tests (#381)
* format_signatures: Fix whitespace issues and add tests * Update sphinx_immaterial/apidoc/format_signatures.py Co-authored-by: Brendan <[email protected]> * Exclude pformat snapshots since they aren't consistent across Sphinx/docutils versions --------- Co-authored-by: Brendan <[email protected]>
- Loading branch information
Showing
8 changed files
with
177 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import sphinx.addnodes | ||
|
||
|
||
TEST_SIGNATURES = { | ||
"cpp_function": "cpp:function:: void foo(int something, int something_else, bool third_param, bool fourth_param, int fifth_param)", | ||
"cpp_function_long": r"cpp:function:: template <typename T, \ | ||
typename U = void, \ | ||
int AnotherParameter = 42> \ | ||
requires std::is_const_v<T> \ | ||
const MyType LongFunctionSignatureExample(\ | ||
const MyType bar, \ | ||
uint8_t* arr, \ | ||
unsigned int len = DEFAULT_LENGTH, \ | ||
bool baz = false)", | ||
"cpp_function_long_return_type": r"cpp:function:: std::integral_constant<ptrdiff_t, N> tensorstore::GetStaticOrDynamicExtent(span<X, N>);", | ||
"py_function": r"py:function:: some_module.method_name( \ | ||
some_parameter_with_a_long_name: \ | ||
collections.abc.MutableMapping[\ | ||
tuple[str, float, numbers.Real], \ | ||
dict[int, tuple[list[frozenset[int]]]]], \ | ||
) -> collections.abc.MutableMapping[\ | ||
tuple[str, float, numbers.Real], \ | ||
dict[int, tuple[list[frozenset[int]]]]]", | ||
} | ||
|
||
|
||
def test_format_signatures(immaterial_make_app, snapshot): | ||
app = immaterial_make_app( | ||
extra_conf=""" | ||
extensions.append("sphinx_immaterial.apidoc.format_signatures") | ||
object_description_options = [ | ||
("cpp:.*", dict(clang_format_style={"BasedOnStyle": "LLVM"})), | ||
("py:.*", dict(black_format_style={})), | ||
] | ||
""", | ||
files={ | ||
"index.rst": "\n\n".join( | ||
f""" | ||
.. {directive} | ||
Synopsis goes here. | ||
""" | ||
for directive in TEST_SIGNATURES.values() | ||
) | ||
}, | ||
) | ||
|
||
app.build() | ||
|
||
assert not app._warning.getvalue() | ||
|
||
doc = app.env.get_and_resolve_doctree("index", app.builder) | ||
|
||
formatted_signatures = { | ||
identifier: signature | ||
for identifier, signature in zip( | ||
TEST_SIGNATURES.keys(), | ||
doc.findall(condition=sphinx.addnodes.desc_signature), | ||
) | ||
} | ||
for identifier in TEST_SIGNATURES.keys(): | ||
node = formatted_signatures[identifier] | ||
snapshot.assert_match(node.astext(), f"{identifier}_astext.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/snapshots/format_signatures_test/test_format_signatures/cpp_function_astext.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
void foo(int something, int something_else, bool third_param, | ||
bool fourth_param, int fifth_param); |
6 changes: 6 additions & 0 deletions
6
tests/snapshots/format_signatures_test/test_format_signatures/cpp_function_long_astext.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
template <typename T, typename U = void, int AnotherParameter = 42> | ||
requires std::is_const_v<T> | ||
const MyType | ||
LongFunctionSignatureExample(const MyType bar, uint8_t *arr, | ||
unsigned int len = DEFAULT_LENGTH, | ||
bool baz = false); |
2 changes: 2 additions & 0 deletions
2
...ts/format_signatures_test/test_format_signatures/cpp_function_long_return_type_astext.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
std::integral_constant<ptrdiff_t, N> | ||
tensorstore::GetStaticOrDynamicExtent(span<X, N>); |
9 changes: 9 additions & 0 deletions
9
tests/snapshots/format_signatures_test/test_format_signatures/py_function_astext.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
some_module.method_name( | ||
some_parameter_with_a_long_name: collections.abc.MutableMapping[ | ||
tuple[str, float, numbers.Real], | ||
dict[int, tuple[list[frozenset[int]]]], | ||
] | ||
) -> collections.abc.MutableMapping[ | ||
tuple[str, float, numbers.Real], | ||
dict[int, tuple[list[frozenset[int]]]], | ||
] |