Skip to content

Commit

Permalink
Add a simple repr.
Browse files Browse the repository at this point in the history
Closes: #2
  • Loading branch information
Julian committed Nov 3, 2023
1 parent cd34017 commit c91797f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
41 changes: 24 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "url-py"
version = "0.7.1"
version = "0.8.0"
edition = "2021"

[lib]
Expand All @@ -11,5 +11,5 @@ crate-type = ["cdylib"]
url = "2"

[dependencies.pyo3]
version = "0.19.2"
version = "0.20.0"
features = ["extension-module"]
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fn from_result(input: Result<url::Url, ParseError>) -> PyResult<UrlPy> {

#[pymethods]
impl UrlPy {
fn __str__(&self) -> &str {
self.inner.as_str()
fn __repr__(&self) -> String {
format!("<URL {}>", self.inner.as_str())
}

fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyObject {
Expand All @@ -57,6 +57,10 @@ impl UrlPy {
}
}

fn __str__(&self) -> &str {
self.inner.as_str()
}

fn __truediv__(&self, other: &str) -> PyResult<Self> {
// .join()'s behavior depends on whether the URL has a trailing slash or not --
// which is good! But here for division we follow the convention of e.g. yarl that someone
Expand Down
52 changes: 29 additions & 23 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ def test_join():
assert str(css_url) == "http://servo.github.io/rust-url/main.css"


@pytest.mark.parametrize(
"base_url",
["http://foo.com/bar", "http://foo.com/bar/"],
)
def test_slash(base_url):
"""
Support the / operator as many Python types have decided to.
Whether the base URL ends in a slash or not, e.g. yarl.URL adds one, so we
follow that behavior for this (and not for .join()).
"""
joined = URL.parse(base_url) / "baz"
assert str(joined) == "http://foo.com/bar/baz"


def test_invalid_ipv6_address():
with pytest.raises(url.InvalidIPv6Address):
URL.parse("http://[:::1]")
Expand All @@ -78,14 +63,6 @@ def test_invalid_junk():
URL.parse("https:/12949a;df;;@@@")


def test_eq():
"""
Support the / operator as many Python types have decided to.
"""
assert URL.parse("http://example.com") == URL.parse("http://example.com")
assert URL.parse("http://foo.com") != URL.parse("http://bar.com")


def test_parse_with_params():
url = URL.parse_with_params(
"https://example.net?dont=clobberme",
Expand All @@ -101,3 +78,32 @@ def test_make_relative():
url = URL.parse("https://example.net/a/c.png")
relative = base.make_relative(url)
assert relative == "c.png"


@pytest.mark.parametrize(
"base_url",
["http://foo.com/bar", "http://foo.com/bar/"],
)
def test_slash(base_url):
"""
Support the / operator as many Python types have decided to.
Whether the base URL ends in a slash or not, e.g. yarl.URL adds one, so we
follow that behavior for this (and not for .join()).
"""
joined = URL.parse(base_url) / "baz"
assert str(joined) == "http://foo.com/bar/baz"


def test_str():
example = "http://example.com/"
assert str(URL.parse(example)) == example


def test_repr():
assert repr(URL.parse("http://example.com")) == "<URL http://example.com/>"


def test_eq():
assert URL.parse("http://example.com") == URL.parse("http://example.com")
assert URL.parse("http://foo.com") != URL.parse("http://bar.com")

0 comments on commit c91797f

Please sign in to comment.