Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Spurious ellipsis in YAML examples #151

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix Spurious ellipsis in YAML examples
  • Loading branch information
dblanchette committed Aug 11, 2022
commit ba8885f391ff0c474fe6cf649e3a867cb5d76bbf
7 changes: 5 additions & 2 deletions json_schema_for_humans/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ def highlight_json_example(example_text: str) -> str:

def yaml_example(example_text: str) -> str:
"""Filter. Return a YAML version of the provided JSON text"""
yaml_text = yaml.dump(json.loads(example_text), allow_unicode=True, sort_keys=False)
return yaml_text
loaded_example = json.loads(example_text)
if not isinstance(loaded_example, dict):
# YAML dump does not like things that are not object
return str(loaded_example)
return yaml.dump(loaded_example, allow_unicode=True, sort_keys=False)


def highlight_yaml_example(example_text: str) -> str:
Expand Down
57 changes: 29 additions & 28 deletions tests/generate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,41 +411,42 @@ def test_with_examples() -> None:
}\n""",
]


def test_with_urlencoded_anchor() -> None:
soup = generate_case("with_urlencoded_anchor")
property_names = soup.find_all("span", class_=["property-name"])
property_names_text = [pn.text for pn in property_names]
assert "lowerBound" in property_names_text
assert "upperBound" in property_names_text


def test_with_yaml_examples() -> None:
soup = generate_case("with_examples", GenerationConfiguration(examples_as_yaml=True))
def test_with_examples_as_yaml() -> None:
soup = generate_case("with_examples", config=GenerationConfiguration(examples_as_yaml=True))

examples_label = soup.find_all("div", class_=["badge", "badge-secondary"])
examples_label_text = [ex.text for ex in examples_label]
assert examples_label_text == ["Examples:", "Example:", "Example:", "Example:"]

examples_content = soup.find_all("div", class_="examples")
examples_content_text = [ex.findChildren()[0].text for ex in examples_content]
assert examples_content_text == [
"Guido\n...\n",
"BDFL\n...\n",
"Van Rossum\n...\n",
"64\n...\n",
"birthplace: Haarlem, Netherlands\nfavorite_emoji: 🐍\n"
"motto: Beautiful is better than ugly.\\nExplicit is better than implicit.\\nSimple is\n"
" better than complex.\\nComplex is better than complicated.\\nFlat is better than nested.\\nSparse\n"
" is better than dense.\\nReadability counts.\\nSpecial cases aren't special enough\n"
" to break the rules.\\nAlthough practicality beats purity.\\nErrors should never pass\n"
" silently.\\nUnless explicitly silenced.\\nIn the face of ambiguity, refuse the temptation\n"
" to guess.\\nThere should be one-- and preferably only one --obvious way to do it.\\nAlthough\n"
" that way may not be obvious at first unless you're Dutch.\\nNow is better than never.\\nAlthough\n"
" never is often better than *right* now.\\nIf the implementation is hard to explain,\n"
" it's a bad idea.\\nIf the implementation is easy to explain, it may be a good idea.\\nNamespaces\n"
" are one honking great idea -- let's do more of those!\n",
]
assert [
'Guido\n',
'BDFL\n',
'Van Rossum\n',
"64\n",
"""birthplace: Haarlem, Netherlands
favorite_emoji: 🐍
motto: Beautiful is better than ugly.\\nExplicit is better than implicit.\\nSimple is
better than complex.\\nComplex is better than complicated.\\nFlat is better than nested.\\nSparse
is better than dense.\\nReadability counts.\\nSpecial cases aren't special enough
to break the rules.\\nAlthough practicality beats purity.\\nErrors should never pass
silently.\\nUnless explicitly silenced.\\nIn the face of ambiguity, refuse the temptation
to guess.\\nThere should be one-- and preferably only one --obvious way to do it.\\nAlthough
that way may not be obvious at first unless you're Dutch.\\nNow is better than never.\\nAlthough
never is often better than *right* now.\\nIf the implementation is hard to explain,
it's a bad idea.\\nIf the implementation is easy to explain, it may be a good idea.\\nNamespaces
are one honking great idea -- let's do more of those!
""",
] == examples_content_text


def test_with_urlencoded_anchor() -> None:
soup = generate_case("with_urlencoded_anchor")
property_names = soup.find_all("span", class_=["property-name"])
property_names_text = [pn.text for pn in property_names]
assert "lowerBound" in property_names_text
assert "upperBound" in property_names_text


def test_pattern_properties() -> None:
Expand Down