Skip to content

Commit e199087

Browse files
authored
Add __str__ and __repr__ methods (#7)
* Add __str__ and __repr__ methods * Fix newlines
1 parent 60fa3f0 commit e199087

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

ada_url/ada_adapter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ def __setattr__(self, attr, value):
124124

125125
return super().__setattr__(attr, value)
126126

127+
def __str__(self):
128+
return self.href
129+
130+
def __repr__(self):
131+
return f'<URL "{self.href}">'
132+
127133
@staticmethod
128134
def can_parse(url, base=None):
129135
try:

tests/test_ada_url.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ def test_class_dir(self):
9191
actual = set(dir(urlobj))
9292
self.assertTrue(actual.issuperset(GET_ATTRIBUTES))
9393

94+
def test_to_str(self):
95+
urlobj = URL('https://example.org/../something.txt')
96+
actual = str(urlobj)
97+
expected = 'https://example.org/something.txt'
98+
self.assertEqual(actual, expected)
99+
100+
def test_to_repr(self):
101+
urlobj = URL('https://example.org/../something.txt')
102+
actual = repr(urlobj)
103+
expected = '<URL "https://example.org/something.txt">'
104+
self.assertEqual(actual, expected)
105+
94106
def test_check_url(self):
95107
for s, expected in (
96108
('https:example.org', True),

0 commit comments

Comments
 (0)