Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit 23c6633

Browse files
committed
fix calc_expected_status_length for urls not at beginning on string
1 parent ae88240 commit 23c6633

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

tests/test_twitter_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import twitter
66

77
from twitter.twitter_utils import (
8+
calc_expected_status_length,
89
parse_media_file
910
)
1011

@@ -58,3 +59,18 @@ def test_utils_error_checking(self):
5859
self.assertRaises(
5960
twitter.TwitterError,
6061
lambda: twitter.twitter_utils.enf_type('test', int, 'hi'))
62+
63+
def test_calc_expected_status_length(self):
64+
status = 'hi a tweet there'
65+
len_status = calc_expected_status_length(status)
66+
self.assertEqual(len_status, 16)
67+
68+
def test_calc_expected_status_length_with_url(self):
69+
status = 'hi a tweet there example.com'
70+
len_status = calc_expected_status_length(status)
71+
self.assertEqual(len_status, 40)
72+
73+
def test_calc_expected_status_length_with_url_and_extra_spaces(self):
74+
status = 'hi a tweet there example.com'
75+
len_status = calc_expected_status_length(status)
76+
self.assertEqual(len_status, 63)

twitter/twitter_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,13 @@ def calc_expected_status_length(status, short_url_length=23):
161161
Expected length of the status message as an integer.
162162
163163
"""
164-
replaced_chars = 0
165-
status_length = len(status)
166-
match = re.findall(URL_REGEXP, status)
167-
if len(match) >= 1:
168-
replaced_chars = len(''.join(match))
169-
status_length = status_length - replaced_chars + (short_url_length * len(match))
164+
status_length = 0
165+
for word in re.split(r'\s', status):
166+
if is_url(word):
167+
status_length += short_url_length
168+
else:
169+
status_length += len(word)
170+
status_length += len(re.findall(r'\s', status))
170171
return status_length
171172

172173

0 commit comments

Comments
 (0)