Skip to content

Commit

Permalink
Fix tests/doctests (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelogladding authored Dec 1, 2023
1 parent 50b5d57 commit bfee398
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
39 changes: 11 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Import the parser using:

```

### Parse an HTML File
### Parse an HTML Document from a string

```pycon
>>> with open("test/examples/eras.html", "r") as file:
... mf2json = mf2py.parse(doc=file)
>>> with open("test/examples/eras.html") as fp:
... mf2json = mf2py.parse(doc=fp)
>>> mf2json
{'items': [{'type': ['h-entry'],
'properties': {'name': ['Excited for the Taylor Swift Eras Tour'],
Expand All @@ -57,19 +57,7 @@ Import the parser using:

```

### Parse an HTML String

```pycon
>>> html = '''<article class="h-entry"><p class="p-content">The best time
... to plant a tree was 30 years ago, and the second best time to plant a
... tree is now.</p></article>'''
>>> mf2py.parse(doc=html)["items"]
[{'type': ['h-entry'], 'properties': {'content': ['The best time to plant
a tree was 30 years ago, and the second best time to plant a tree is now.']}}]

```

### Parse an HTML Document Retrieved from a URL
### Parse an HTML Document from a URL

```pycon
>>> mf2json = mf2py.parse(url="https://events.indieweb.org")
Expand All @@ -95,17 +83,12 @@ found.

## Advanced Usage

`parse` is a convenience method that delegates to `Parser`. More sophisticated
behaviors are available by invoking the parser object directly.
`parse` is a convenience function for `Parser`. More sophisticated behaviors are
available by invoking the parser object directly.

```pycon
>>> html = '''<h1><span class=h-card>Frank</span> and <span class=h-card>Cosmo</span></h1>
... <p class=h-entry><q class=p-content>It's time for the Festivus feats of
... strength.</q></p>
... <p class=h-entry><q class=p-content>It's a Festivus miracle!</q></p>
... <p class=h-entry><q class=p-content>The tradition of Festivus begins with
... the airing of grievances.</q></p>'''
>>> mf2parser = mf2py.Parser(doc=html)
>>> with open("test/examples/festivus.html") as fp:
... mf2parser = mf2py.Parser(doc=fp)

```

Expand All @@ -114,11 +97,11 @@ behaviors are available by invoking the parser object directly.
```pycon
>>> mf2json = mf2parser.to_dict()
>>> len(mf2json["items"])
5
7
>>> len(mf2parser.to_dict(filter_by_type="h-card"))
2
>>> len(mf2parser.to_dict(filter_by_type="h-entry"))
3
>>> len(mf2parser.to_dict(filter_by_type="h-entry"))
4

```

Expand Down
10 changes: 10 additions & 0 deletions test/examples/festivus.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<h2 class=h-card>Jerry</h2>
<p class=h-entry><q class=p-content>Happy Festivus!</q></p>
<h2 class=h-card>Frank</h2>
<p class=h-entry><q class=p-content>It's time for the Festivus feats of
strength.</q></p>
<p class=h-entry><q class=p-content>The tradition of Festivus begins with
the airing of grievances.</q></p>
<h2 class=h-card>Cosmo</h2>
<p class=h-entry><q class=p-content>It's a Festivus miracle!</q></p>
13 changes: 13 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,3 +1198,16 @@ def test_language():
assert result["items"][2]["lang"] == "sv"
assert result["items"][2]["properties"]["content"][0]["lang"] == "en"
assert result["items"][2]["properties"]["content"][1]["lang"] == "sv"


def test_parser_object():
with open(os.path.join(TEST_DIR, "festivus.html")) as f:
p = Parser(doc=f)
assert len(p.to_dict(filter_by_type="h-card")) == 3
assert len(p.to_dict(filter_by_type="h-entry")) == 4
assert (
p.to_json(filter_by_type="h-card")
== '[{"type": ["h-card"], "properties": {"name": ["Jerry"]}}, {"type": '
'["h-card"], "properties": {"name": ["Frank"]}}, {"type": ["h-card"], '
'"properties": {"name": ["Cosmo"]}}]'
)

0 comments on commit bfee398

Please sign in to comment.