Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed May 6, 2024
1 parent 5fb83b5 commit c3984cb
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sphinx_js/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def handle_signature(self, sig: str, signode: desc_signature) -> tuple[str, str]

class JSTypeAlias(JSObject):
doc_field_types = [
GroupedField(
JSGroupedField(
"typeparam",
label="Type parameters",
names=("typeparam",),
Expand Down
10 changes: 10 additions & 0 deletions tests/test_build_ts/source/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export interface I {}
*/
export let interfaceInstance: I = {};

/**
* A super special type alias
* @typeParam T The whatsit
*/
export type TestTypeAlias<T extends A> = { a: T };
export type TestTypeAlias2 = { a: number };

export let t: TestTypeAlias<A>;
export let t2: TestTypeAlias2;

/**
* A function with a type parameter!
*
Expand Down
23 changes: 22 additions & 1 deletion tests/test_build_ts/test_build_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ def test_automodule(self):
Another thing.
module.t
type: "TestTypeAlias"<"A">
module.t2
type: "TestTypeAlias2"
module.zInstance
type: "Z"<"A">
Expand Down Expand Up @@ -353,6 +361,19 @@ class module.Z<T>(a, b)
Documentation for the interface I
*exported from* "module"
module.TestTypeAlias<T>
type: { a: T; }
A super special type alias
Type parameters:
**T** -- The whatsit (extends "A")
module.TestTypeAlias2
type: { a: number; }
"""
),
)
Expand Down Expand Up @@ -434,7 +455,7 @@ def test_autosummary(self):
soup = BeautifulSoup(self._file_contents("autosummary"), "html.parser")
attrs = soup.find(class_="attributes")
rows = list(attrs.find_all("tr"))
assert len(rows) == 5
assert len(rows) == 7

href = rows[0].find("a")
assert href.get_text() == "a"
Expand Down
61 changes: 59 additions & 2 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Interface,
Param,
Return,
TypeAlias,
TypeParam,
TypeXRefExternal,
TypeXRefInternal,
Expand Down Expand Up @@ -133,6 +134,18 @@ def attribute_render(partial_path=None, use_short_name=False, **args):
return attribute_render


@pytest.fixture()
def type_alias_render(attribute_renderer) -> Any:
def type_alias_render(partial_path=None, use_short_name=False, **args):
if not partial_path:
partial_path = ["blah"]
return attribute_renderer.rst(
partial_path, make_type_alias(**args), use_short_name
)

return type_alias_render


top_level_dict = dict(
name="",
path=[],
Expand Down Expand Up @@ -173,6 +186,7 @@ def attribute_render(partial_path=None, use_short_name=False, **args):
)
)
attribute_dict = top_level_dict | member_dict | dict(type="")
type_alias_dict = top_level_dict | dict(type="", type_params=[])


def make_class(**args):
Expand All @@ -191,6 +205,10 @@ def make_attribute(**args):
return Attribute(**(attribute_dict | args))


def make_type_alias(**args):
return TypeAlias(**(type_alias_dict | args))


DEFAULT_RESULT = ".. js:function:: blah()\n"


Expand Down Expand Up @@ -301,6 +319,11 @@ def test_render_xref(function_renderer: AutoFunctionRenderer):
function_renderer.render_type([TypeXRefInternal(name="A", path=["a.", "A"])])
== ":js:class:`A`"
)
function_renderer.objects["A"] = make_type_alias()
assert (
function_renderer.render_type([TypeXRefInternal(name="A", path=["a.", "A"])])
== ":js:typealias:`A`"
)
function_renderer.objects["A"] = make_interface()
assert (
function_renderer.render_type([TypeXRefInternal(name="A", path=["a.", "A"])])
Expand Down Expand Up @@ -341,7 +364,7 @@ def test_func_render_param_type(function_render):
"""
)
assert function_render(
objects={"A": make_interface()},
objects={"A": make_type_alias()},
params=[
Param(
"a",
Expand All @@ -354,7 +377,7 @@ def test_func_render_param_type(function_render):
.. js:function:: blah(a)
:param a: a description
:type a: :js:interface:`A`
:type a: :js:typealias:`A`
"""
)

Expand Down Expand Up @@ -501,3 +524,37 @@ def test_examples(function_render):
Something python
"""
)


def test_type_alias(type_alias_render):
assert type_alias_render() == ".. js:typealias:: blah\n"
assert type_alias_render(
type="number", description="my great type alias!"
) == dedent(
"""\
.. js:typealias:: blah
.. rst-class:: js attribute type
type: **number**
my great type alias!
"""
)
assert type_alias_render(
type="string | T",
type_params=[TypeParam("T", extends="number", description="ABC")],
description="With a type parameter",
) == dedent(
"""\
.. js:typealias:: blah<T>
.. rst-class:: js attribute type
type: **string | T**
With a type parameter
:typeparam T: ABC (extends **number**)
"""
)
6 changes: 6 additions & 0 deletions tests/test_typedoc_analysis/source/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ export class Indexable {

// Test that we don't fail on a reexport
export { Blah } from "./exports";

/**
* A super special type alias
* @typeparam T The whatsit
*/
export type TestTypeAlias<T> = 1 | 2 | T;
13 changes: 11 additions & 2 deletions tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from sphinx_js.ir import (
Attribute,
Class,
Description,
DescriptionCode,
DescriptionText,
Function,
Param,
Pathname,
Return,
Type,
TypeAlias,
TypeParam,
TypeXRef,
TypeXRefExternal,
Expand All @@ -30,12 +32,12 @@ def join_type(t: Type) -> str:
return "".join(e.name if isinstance(e, TypeXRef) else e for e in t)


def join_descri(t: Type) -> str:
def join_description(t: Description) -> str:
if not t:
return ""
if isinstance(t, str):
return t
return "".join(e.name if isinstance(e, TypeXRef) else e for e in t)
return "".join(e.code if isinstance(e, DescriptionCode) else e.text for e in t)


class TestPathSegments(TypeDocTestCase):
Expand Down Expand Up @@ -356,6 +358,13 @@ def test_setter(self):
assert isinstance(setter, Attribute)
assert setter.type == [TypeXRefIntrinsic("string")]

def test_type_alias(self):
alias = self.analyzer.get_object(["TestTypeAlias"])
assert isinstance(alias, TypeAlias)
assert join_description(alias.description) == "A super special type alias"
assert join_type(alias.type) == "1 | 2 | T"
assert alias.type_params == [TypeParam(name="T", extends=None, description=[])]


class TestTypeName(TypeDocAnalyzerTestCase):
"""Make sure our rendering of TypeScript types into text works."""
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def _file_contents(self, filename):

def _file_contents_eq(self, filename, contents):
__tracebackhide__ = True
print(self._file_contents(filename))
assert self._file_contents(filename) == contents


Expand Down

0 comments on commit c3984cb

Please sign in to comment.