Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lyse
libtwtxt
Commits
28e23209
Commit
28e23209
authored
Aug 03, 2021
by
Lysander Trischler
Browse files
Simplify assertions
parent
7e07dabd
Changes
2
Hide whitespace changes
Inline
Side-by-side
libgotwtxt.py
View file @
28e23209
...
...
@@ -23,6 +23,17 @@ class Twter:
twter
.
tagline
=
self
.
tagline
.
encode
(
"utf-8"
)
return
ctypes
.
pointer
(
twter
)
def
__repr__
(
self
):
return
f
"
{
self
.
__class__
.
__name__
}
<nick=
{
self
.
nick
!r}
url=
{
self
.
url
!r}
"
+
\
f
"avatar=
{
self
.
avatar
!r}
tagline=
{
self
.
tagline
!r}
>"
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
self
.
__class__
)
and
\
self
.
nick
==
other
.
nick
and
\
self
.
url
==
other
.
url
and
\
self
.
avatar
==
other
.
avatar
and
\
self
.
tagline
==
other
.
tagline
class
_Twter
(
ctypes
.
Structure
):
_fields_
=
[(
"nick"
,
ctypes
.
c_char_p
),
...
...
@@ -48,6 +59,14 @@ class Link:
self
.
text
=
text
self
.
target
=
target
def
__repr__
(
self
):
return
f
"
{
self
.
__class__
.
__name__
}
<text=
{
self
.
text
!r}
target=
{
self
.
target
!r}
>"
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
self
.
__class__
)
and
\
self
.
text
==
other
.
text
and
\
self
.
target
==
other
.
target
class
_Link
(
ctypes
.
Structure
):
_fields_
=
[(
"text"
,
ctypes
.
c_char_p
),
...
...
@@ -67,6 +86,14 @@ class Subject:
self
.
text
=
text
self
.
tag
=
tag
def
__repr__
(
self
):
return
f
"
{
self
.
__class__
.
__name__
}
<text=
{
self
.
text
!r}
tag=
{
self
.
tag
!r}
>"
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
self
.
__class__
)
and
\
self
.
text
==
other
.
text
and
\
self
.
tag
==
other
.
tag
class
_Subject
(
ctypes
.
Structure
):
_fields_
=
[(
"text"
,
ctypes
.
c_char_p
),
...
...
@@ -90,6 +117,21 @@ class Twt:
self
.
mentions
=
mentions
self
.
links
=
links
def
__repr__
(
self
):
return
f
"
{
self
.
__class__
.
__name__
}
<twter=
{
self
.
twter
!r}
"
+
\
f
"created=
{
self
.
created
!r}
hash=
{
self
.
hash
!r}
"
+
\
f
"subject=
{
self
.
subject
!r}
mentions=
{
self
.
mentions
!r}
"
+
\
f
"links=
{
self
.
links
!r}
>"
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
self
.
__class__
)
and
\
self
.
twter
==
other
.
twter
and
\
self
.
created
==
other
.
created
and
\
self
.
hash
==
other
.
hash
and
\
self
.
subject
==
other
.
subject
and
\
self
.
mentions
==
other
.
mentions
and
\
self
.
links
==
other
.
links
class
_Twt
(
ctypes
.
Structure
):
_fields_
=
[(
"twter"
,
ctypes
.
POINTER
(
_Twter
)),
...
...
@@ -128,6 +170,14 @@ class TwtFile:
self
.
twter
=
twter
self
.
twts
=
twts
def
__repr__
(
self
):
return
f
"
{
self
.
__class__
.
__name__
}
<twter=
{
self
.
twter
!r}
twts=
{
self
.
twts
!r}
>"
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
self
.
__class__
)
and
\
self
.
twter
==
other
.
twter
and
\
self
.
twts
==
other
.
twts
class
_TwtFile
(
ctypes
.
Structure
):
_fields_
=
[(
"twter"
,
ctypes
.
POINTER
(
_Twter
)),
...
...
@@ -145,12 +195,12 @@ class _TwtFile(ctypes.Structure):
return
twtfile
class
ParseFileResult
(
ctypes
.
Structure
):
class
_
ParseFileResult
(
ctypes
.
Structure
):
_fields_
=
[(
"twtfile"
,
ctypes
.
POINTER
(
_TwtFile
)),
(
"error"
,
ctypes
.
c_char_p
)]
_so
.
parse_file
.
argtypes
=
[
ctypes
.
c_char_p
,
ctypes
.
POINTER
(
_Twter
)]
_so
.
parse_file
.
restype
=
ParseFileResult
_so
.
parse_file
.
restype
=
_
ParseFileResult
_so
.
free_twt_file
.
argtypes
=
[
ctypes
.
POINTER
(
_TwtFile
)]
...
...
test_libgotwtxt.py
View file @
28e23209
...
...
@@ -14,16 +14,15 @@ class ParseFileTest(unittest.TestCase):
def
test_single_line
(
self
):
twtfile
=
parse_file
(
"2021-08-02T10:27:42+02:00
\t
Hello world."
,
TWTER
)
self
.
assertIsNotNone
(
twtfile
,
"parse_file returned None"
)
self
.
assert
Twter
Equal
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
1
,
len
(
twtfile
.
twts
),
"number of twts does not match"
)
self
.
assertTwtEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
2
,
10
,
27
,
42
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"slrnx6a"
,
subject
=
Subject
(
text
=
"#slrnx6a"
,
tag
=
Link
(
text
=
"slrnx6a"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
])
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
2
,
10
,
27
,
42
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"slrnx6a"
,
subject
=
Subject
(
text
=
"#slrnx6a"
,
tag
=
Link
(
text
=
"slrnx6a"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
])
def
test_two_lines
(
self
):
twtfile
=
parse_file
(
...
...
@@ -31,23 +30,22 @@ class ParseFileTest(unittest.TestCase):
"2021-08-03T09:28:45+02:00
\t
Foo bar eggs and spam.
\n
"
,
TWTER
)
self
.
assertIsNotNone
(
twtfile
,
"parse_file returned None"
)
self
.
assert
Twter
Equal
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
2
,
len
(
twtfile
.
twts
),
"number of twts does not match"
)
self
.
assertTwtEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
2
,
10
,
27
,
42
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"slrnx6a"
,
subject
=
Subject
(
text
=
"#slrnx6a"
,
tag
=
Link
(
text
=
"slrnx6a"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
],
"first twt does not match"
)
self
.
assertTwtEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
9
,
28
,
45
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"zm7fnka"
,
subject
=
Subject
(
text
=
"#zm7fnka"
,
tag
=
Link
(
text
=
"zm7fnka"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
1
],
"second twt does not match"
)
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
2
,
10
,
27
,
42
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"slrnx6a"
,
subject
=
Subject
(
text
=
"#slrnx6a"
,
tag
=
Link
(
text
=
"slrnx6a"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
],
"first twt does not match"
)
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
9
,
28
,
45
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"zm7fnka"
,
subject
=
Subject
(
text
=
"#zm7fnka"
,
tag
=
Link
(
text
=
"zm7fnka"
,
target
=
None
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
1
],
"second twt does not match"
)
def
test_markdown_links
(
self
):
twtfile
=
parse_file
(
...
...
@@ -55,16 +53,16 @@ class ParseFileTest(unittest.TestCase):
"and [nice](https://example.com/test) world!"
,
TWTER
)
self
.
assertIsNotNone
(
twtfile
,
"parse_file returned None"
)
self
.
assert
Twter
Equal
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
1
,
len
(
twtfile
.
twts
),
"number of twts does not match"
)
self
.
assert
Twt
Equal
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
11
,
16
,
13
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"zv6vujq"
,
subject
=
Subject
(
text
=
"#zv6vujq"
,
tag
=
Link
(
text
=
"zv6vujq"
,
target
=
None
)),
mentions
=
[],
links
=
[
Link
(
text
=
"wonderful"
,
target
=
"https://example.com/"
),
Link
(
text
=
"nice"
,
target
=
"https://example.com/test"
)]),
twtfile
.
twts
[
0
])
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
11
,
16
,
13
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"zv6vujq"
,
subject
=
Subject
(
text
=
"#zv6vujq"
,
tag
=
Link
(
text
=
"zv6vujq"
,
target
=
None
)),
mentions
=
[],
links
=
[
Link
(
text
=
"wonderful"
,
target
=
"https://example.com/"
),
Link
(
text
=
"nice"
,
target
=
"https://example.com/test"
)]),
twtfile
.
twts
[
0
])
def
test_subjects
(
self
):
twtfile
=
parse_file
(
...
...
@@ -72,101 +70,41 @@ class ParseFileTest(unittest.TestCase):
"2021-08-03T12:33:17+02:00
\t
(re: foo) Well, it's quite complicated.
\n
"
,
TWTER
)
self
.
assertIsNotNone
(
twtfile
,
"parse_file returned None"
)
self
.
assert
Twter
Equal
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
2
,
len
(
twtfile
.
twts
),
"number of twts does not match"
)
self
.
assert
Twt
Equal
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
12
,
28
,
26
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"g2xdgsq"
,
subject
=
Subject
(
text
=
"#<1234567 https://example.com/1234567>"
,
tag
=
Link
(
text
=
"1234567"
,
target
=
"https://example.com/1234567"
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
],
"first twt does not match"
)
self
.
assert
Twt
Equal
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
12
,
33
,
17
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"wqw7ipq"
,
subject
=
Subject
(
text
=
"re: foo"
,
tag
=
None
),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
1
],
"second twt does not match"
)
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
12
,
28
,
26
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"g2xdgsq"
,
subject
=
Subject
(
text
=
"#<1234567 https://example.com/1234567>"
,
tag
=
Link
(
text
=
"1234567"
,
target
=
"https://example.com/1234567"
)),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
0
],
"first twt does not match"
)
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
12
,
33
,
17
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"wqw7ipq"
,
subject
=
Subject
(
text
=
"re: foo"
,
tag
=
None
),
mentions
=
[],
links
=
[]),
twtfile
.
twts
[
1
],
"second twt does not match"
)
def
test_mentions
(
self
):
twtfile
=
parse_file
(
"2021-08-03T15:04:09+02:00
\t
Hello @<eugen https://example.org/~eugen/twtxt.txt>!"
,
TWTER
)
self
.
assertIsNotNone
(
twtfile
,
"parse_file returned None"
)
self
.
assert
Twter
Equal
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
TWTER
,
twtfile
.
twter
,
"twter of twt file does not match"
)
self
.
assertEqual
(
1
,
len
(
twtfile
.
twts
),
"number of twts does not match"
)
self
.
assertTwtEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
15
,
4
,
9
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"vu5np6q"
,
subject
=
Subject
(
text
=
"#vu5np6q"
,
tag
=
Link
(
text
=
"vu5np6q"
,
target
=
None
)),
mentions
=
[
Twter
(
nick
=
"eugen"
,
url
=
"https://example.org/~eugen/twtxt.txt"
,
avatar
=
""
,
tagline
=
""
)],
links
=
[]),
twtfile
.
twts
[
0
])
def
msg
(
self
,
message_prefix
):
"""Construct a message factory using the prefix, if present."""
def
inner
(
message
):
if
message_prefix
is
None
:
return
message
return
message_prefix
+
": "
+
message
return
inner
def
assertTwterEqual
(
self
,
expected
,
actual
,
msg
=
None
):
msg
=
self
.
msg
(
msg
)
self
.
assertIsNotNone
(
actual
,
msg
(
"twter is None"
))
self
.
assertEqual
(
expected
.
nick
,
actual
.
nick
,
msg
(
"twter nick does not match"
))
self
.
assertEqual
(
expected
.
url
,
actual
.
url
,
msg
(
"twter URL does not match"
))
self
.
assertEqual
(
expected
.
avatar
,
actual
.
avatar
,
msg
(
"twter avatar does not match"
))
self
.
assertEqual
(
expected
.
tagline
,
actual
.
tagline
,
msg
(
"twter tagline does not match"
))
def
assertTwtEqual
(
self
,
expected
,
actual
,
msg
=
None
):
msg
=
self
.
msg
(
msg
)
self
.
assertIsNotNone
(
actual
,
msg
(
"twt is None"
))
self
.
assertTwterEqual
(
expected
.
twter
,
actual
.
twter
,
msg
(
"twter of twt does not match"
))
self
.
assertEqual
(
expected
.
created
,
actual
.
created
,
msg
(
"twt creation timestamp does not match"
))
self
.
assertEqual
(
expected
.
hash
,
actual
.
hash
,
msg
(
"twt hash does not match"
))
self
.
assertSubjectEqual
(
expected
.
subject
,
actual
.
subject
,
"twt subject does not match"
)
if
expected
.
mentions
is
None
:
self
.
assertIsNone
(
actual
.
mentions
,
"twt mentions do not match"
)
else
:
self
.
assertIsNotNone
(
actual
.
mentions
,
"twt mentions do not match"
)
self
.
assertEqual
(
len
(
expected
.
mentions
),
len
(
actual
.
mentions
),
"number of twt mentions does not match"
)
for
i
in
range
(
len
(
expected
.
mentions
)):
self
.
assertTwterEqual
(
expected
.
mentions
[
i
],
actual
.
mentions
[
i
],
"twt mention at index %d does not match"
%
i
)
if
expected
.
links
is
None
:
self
.
assertIsNone
(
actual
.
links
,
"twt links do not match"
)
else
:
self
.
assertIsNotNone
(
actual
.
links
,
"twt links do not match"
)
self
.
assertEqual
(
len
(
expected
.
links
),
len
(
actual
.
links
),
"number of twt links does not match"
)
for
i
in
range
(
len
(
expected
.
links
)):
self
.
assertLinkEqual
(
expected
.
links
[
i
],
actual
.
links
[
i
],
"twt link at index %d does not match"
%
i
)
def
assertLinkEqual
(
self
,
expected
,
actual
,
msg
=
None
):
msg
=
self
.
msg
(
msg
)
if
expected
is
None
:
self
.
assertIsNone
(
actual
,
msg
(
"link must be None"
))
return
self
.
assertIsNotNone
(
actual
,
msg
(
"link is None"
))
self
.
assertEqual
(
expected
.
text
,
actual
.
text
,
msg
(
"link text does not match"
))
self
.
assertEqual
(
expected
.
target
,
actual
.
target
,
msg
(
"link target does not match"
))
def
assertSubjectEqual
(
self
,
expected
,
actual
,
msg
=
None
):
msg
=
self
.
msg
(
msg
)
if
expected
is
None
:
self
.
assertIsNone
(
actual
,
msg
(
"subject must be None"
))
return
self
.
assertIsNotNone
(
actual
,
msg
(
"subject is None"
))
self
.
assertEqual
(
expected
.
text
,
actual
.
text
,
msg
(
"subject text does not match"
))
self
.
assertLinkEqual
(
expected
.
tag
,
actual
.
tag
,
msg
(
"subject tag does not match"
))
self
.
assertEqual
(
Twt
(
twter
=
TWTER
,
created
=
datetime
.
datetime
(
2021
,
8
,
3
,
15
,
4
,
9
,
tzinfo
=
UTC_PLUS_2
),
hash
=
"vu5np6q"
,
subject
=
Subject
(
text
=
"#vu5np6q"
,
tag
=
Link
(
text
=
"vu5np6q"
,
target
=
None
)),
mentions
=
[
Twter
(
nick
=
"eugen"
,
url
=
"https://example.org/~eugen/twtxt.txt"
,
avatar
=
""
,
tagline
=
""
)],
links
=
[]),
twtfile
.
twts
[
0
])
if
__name__
==
"__main__"
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment