|
1 | 1 | from abstractions import AdoptablePet, Post |
2 | | -from hypothesis import given, strategies as st |
| 2 | +from hypothesis import given, strategies as st, assume |
3 | 3 | import pytest |
4 | 4 | from social_posters.mastodon import ( |
5 | 5 | PosterMastodon, |
|
23 | 23 | max_size=3, |
24 | 24 | ) |
25 | 25 |
|
| 26 | +caption_text = st.text( |
| 27 | + alphabet=st.characters( |
| 28 | + blacklist_categories=("Cs", "Cc"), |
| 29 | + ), |
| 30 | + min_size=0, |
| 31 | + max_size=5000, |
| 32 | +) |
| 33 | + |
26 | 34 | text_strategy = st.text( |
27 | 35 | alphabet=st.characters(blacklist_categories=("Cs",)), |
28 | 36 | min_size=0, |
@@ -156,6 +164,35 @@ def test_format_post_output_stays_under_mastodon_limit_after_splitting(self, pet |
156 | 164 | assert len(main_caption) <= MASTODON_CHARACTER_LIMIT |
157 | 165 | assert all(len(reply) <= MASTODON_CHARACTER_LIMIT for reply in replies) |
158 | 166 | assert len(replies) <= MAX_REPLIES |
| 167 | + |
| 168 | + @given( |
| 169 | + text=caption_text, |
| 170 | + limit=st.integers(min_value=1, max_value=10), |
| 171 | + ) |
| 172 | + def test_safe_truncate_correctly(self, text, limit): |
| 173 | + fst, snd = self.poster._safe_truncate(text, limit) |
| 174 | + |
| 175 | + assert len(fst) <= limit |
| 176 | + |
| 177 | + if len(text) <= limit: |
| 178 | + assert fst == text |
| 179 | + assert snd == "" |
| 180 | + else: |
| 181 | + assert fst == fst.rstrip() |
| 182 | + assert snd == snd.strip() |
| 183 | + |
| 184 | + @given( |
| 185 | + text=st.text(), |
| 186 | + limit=st.integers(min_value=1, max_value=10), |
| 187 | + ) |
| 188 | + def test_safe_truncate_nothing(self, text, limit): |
| 189 | + assume(len(text) <= limit) |
| 190 | + fst, snd = self.poster._safe_truncate(text, limit) |
| 191 | + |
| 192 | + |
| 193 | + assert fst == text |
| 194 | + assert snd == "" |
| 195 | + |
159 | 196 |
|
160 | 197 |
|
161 | 198 | class TestMastodonCaption: |
|
0 commit comments