Best Python code snippet using playwright-python
test_contentmanager.py
Source:test_contentmanager.py
...71 cm = ContentManager()72 cm.add_set_handler(key, foo_setter)73 m = self._make_message()74 msg_obj = self.Thing()75 cm.set_content(m, msg_obj, foo='bar')76 self.assertEqual(m['X-Foo-Header'], 'bar')77 self.assertEqual(m.get_payload(), msg_obj)78 def set_key_as_set_content_key_order(self, order, key):79 def foo_setter(msg, obj):80 msg['X-FooBar-Header'] = 'foo'81 msg.set_payload(obj)82 def bar_setter(msg, obj):83 msg['X-FooBar-Header'] = 'bar'84 cm = ContentManager()85 cm.add_set_handler(key, foo_setter)86 for precedence, key in self.get_key_params.values():87 if precedence > order:88 cm.add_set_handler(key, bar_setter)89 m = self._make_message()90 msg_obj = self.Thing()91 cm.set_content(m, msg_obj)92 self.assertEqual(m['X-FooBar-Header'], 'foo')93 self.assertEqual(m.get_payload(), msg_obj)94 def test_set_content_raises_if_unknown_type_and_no_default(self):95 cm = ContentManager()96 m = self._make_message()97 msg_obj = self.Thing()98 with self.assertRaisesRegex(KeyError, self.testobject_full_path):99 cm.set_content(m, msg_obj)100 def test_set_content_raises_if_called_on_multipart(self):101 cm = ContentManager()102 m = self._make_message()103 m['Content-Type'] = 'multipart/foo'104 with self.assertRaises(TypeError):105 cm.set_content(m, 'test')106 def test_set_content_calls_clear_content(self):107 m = self._make_message()108 m['Content-Foo'] = 'bar'109 m['Content-Type'] = 'text/html'110 m['To'] = 'test'111 m.set_payload('abc')112 cm = ContentManager()113 cm.add_set_handler(str, lambda *args, **kw: None)114 m.set_content('xyz', content_manager=cm)115 self.assertIsNone(m['Content-Foo'])116 self.assertIsNone(m['Content-Type'])117 self.assertEqual(m['To'], 'test')118 self.assertIsNone(m.get_payload())119@parameterize120class TestRawDataManager(TestEmailBase):121 # Note: these tests are dependent on the order in which headers are added122 # to the message objects by the code. There's no defined ordering in123 # RFC5322/MIME, so this makes the tests more fragile than the standards124 # require. However, if the header order changes it is best to understand125 # *why*, and make sure it isn't a subtle bug in whatever change was126 # applied.127 policy = policy.default.clone(max_line_length=60,128 content_manager=raw_data_manager)129 message = EmailMessage130 def test_get_text_plain(self):131 m = self._str_msg(textwrap.dedent("""\132 Content-Type: text/plain133 Basic text.134 """))135 self.assertEqual(raw_data_manager.get_content(m), "Basic text.\n")136 def test_get_text_html(self):137 m = self._str_msg(textwrap.dedent("""\138 Content-Type: text/html139 <p>Basic text.</p>140 """))141 self.assertEqual(raw_data_manager.get_content(m),142 "<p>Basic text.</p>\n")143 def test_get_text_plain_latin1(self):144 m = self._bytes_msg(textwrap.dedent("""\145 Content-Type: text/plain; charset=latin1146 Basìc tëxt.147 """).encode('latin1'))148 self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n")149 def test_get_text_plain_latin1_quoted_printable(self):150 m = self._str_msg(textwrap.dedent("""\151 Content-Type: text/plain; charset="latin-1"152 Content-Transfer-Encoding: quoted-printable153 Bas=ECc t=EBxt.154 """))155 self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n")156 def test_get_text_plain_utf8_base64(self):157 m = self._str_msg(textwrap.dedent("""\158 Content-Type: text/plain; charset="utf8"159 Content-Transfer-Encoding: base64160 QmFzw6xjIHTDq3h0Lgo=161 """))162 self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt.\n")163 def test_get_text_plain_bad_utf8_quoted_printable(self):164 m = self._str_msg(textwrap.dedent("""\165 Content-Type: text/plain; charset="utf8"166 Content-Transfer-Encoding: quoted-printable167 Bas=c3=acc t=c3=abxt=fd.168 """))169 self.assertEqual(raw_data_manager.get_content(m), "Basìc tëxt�.\n")170 def test_get_text_plain_bad_utf8_quoted_printable_ignore_errors(self):171 m = self._str_msg(textwrap.dedent("""\172 Content-Type: text/plain; charset="utf8"173 Content-Transfer-Encoding: quoted-printable174 Bas=c3=acc t=c3=abxt=fd.175 """))176 self.assertEqual(raw_data_manager.get_content(m, errors='ignore'),177 "Basìc tëxt.\n")178 def test_get_text_plain_utf8_base64_recoverable_bad_CTE_data(self):179 m = self._str_msg(textwrap.dedent("""\180 Content-Type: text/plain; charset="utf8"181 Content-Transfer-Encoding: base64182 QmFzw6xjIHTDq3h0Lgo\xFF=183 """))184 self.assertEqual(raw_data_manager.get_content(m, errors='ignore'),185 "Basìc tëxt.\n")186 def test_get_text_invalid_keyword(self):187 m = self._str_msg(textwrap.dedent("""\188 Content-Type: text/plain189 Basic text.190 """))191 with self.assertRaises(TypeError):192 raw_data_manager.get_content(m, foo='ignore')193 def test_get_non_text(self):194 template = textwrap.dedent("""\195 Content-Type: {}196 Content-Transfer-Encoding: base64197 Ym9ndXMgZGF0YQ==198 """)199 for maintype in 'audio image video application'.split():200 with self.subTest(maintype=maintype):201 m = self._str_msg(template.format(maintype+'/foo'))202 self.assertEqual(raw_data_manager.get_content(m), b"bogus data")203 def test_get_non_text_invalid_keyword(self):204 m = self._str_msg(textwrap.dedent("""\205 Content-Type: image/jpg206 Content-Transfer-Encoding: base64207 Ym9ndXMgZGF0YQ==208 """))209 with self.assertRaises(TypeError):210 raw_data_manager.get_content(m, errors='ignore')211 def test_get_raises_on_multipart(self):212 m = self._str_msg(textwrap.dedent("""\213 Content-Type: multipart/mixed; boundary="==="214 --===215 --===--216 """))217 with self.assertRaises(KeyError):218 raw_data_manager.get_content(m)219 def test_get_message_rfc822_and_external_body(self):220 template = textwrap.dedent("""\221 Content-Type: message/{}222 To: foo@example.com223 From: bar@example.com224 Subject: example225 an example message226 """)227 for subtype in 'rfc822 external-body'.split():228 with self.subTest(subtype=subtype):229 m = self._str_msg(template.format(subtype))230 sub_msg = raw_data_manager.get_content(m)231 self.assertIsInstance(sub_msg, self.message)232 self.assertEqual(raw_data_manager.get_content(sub_msg),233 "an example message\n")234 self.assertEqual(sub_msg['to'], 'foo@example.com')235 self.assertEqual(sub_msg['from'].addresses[0].username, 'bar')236 def test_get_message_non_rfc822_or_external_body_yields_bytes(self):237 m = self._str_msg(textwrap.dedent("""\238 Content-Type: message/partial239 To: foo@example.com240 From: bar@example.com241 Subject: example242 The real body is in another message.243 """))244 self.assertEqual(raw_data_manager.get_content(m)[:10], b'To: foo@ex')245 def test_set_text_plain(self):246 m = self._make_message()247 content = "Simple message.\n"248 raw_data_manager.set_content(m, content)249 self.assertEqual(str(m), textwrap.dedent("""\250 Content-Type: text/plain; charset="utf-8"251 Content-Transfer-Encoding: 7bit252 Simple message.253 """))254 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)255 self.assertEqual(m.get_content(), content)256 def test_set_text_plain_null(self):257 m = self._make_message()258 content = ''259 raw_data_manager.set_content(m, content)260 self.assertEqual(str(m), textwrap.dedent("""\261 Content-Type: text/plain; charset="utf-8"262 Content-Transfer-Encoding: 7bit263 """))264 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), '\n')265 self.assertEqual(m.get_content(), '\n')266 def test_set_text_html(self):267 m = self._make_message()268 content = "<p>Simple message.</p>\n"269 raw_data_manager.set_content(m, content, subtype='html')270 self.assertEqual(str(m), textwrap.dedent("""\271 Content-Type: text/html; charset="utf-8"272 Content-Transfer-Encoding: 7bit273 <p>Simple message.</p>274 """))275 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)276 self.assertEqual(m.get_content(), content)277 def test_set_text_charset_latin_1(self):278 m = self._make_message()279 content = "Simple message.\n"280 raw_data_manager.set_content(m, content, charset='latin-1')281 self.assertEqual(str(m), textwrap.dedent("""\282 Content-Type: text/plain; charset="iso-8859-1"283 Content-Transfer-Encoding: 7bit284 Simple message.285 """))286 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)287 self.assertEqual(m.get_content(), content)288 def test_set_text_plain_long_line_heuristics(self):289 m = self._make_message()290 content = ("Simple but long message that is over 78 characters"291 " long to force transfer encoding.\n")292 raw_data_manager.set_content(m, content)293 self.assertEqual(str(m), textwrap.dedent("""\294 Content-Type: text/plain; charset="utf-8"295 Content-Transfer-Encoding: quoted-printable296 Simple but long message that is over 78 characters long to =297 force transfer encoding.298 """))299 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)300 self.assertEqual(m.get_content(), content)301 def test_set_text_short_line_minimal_non_ascii_heuristics(self):302 m = self._make_message()303 content = "et là il est monté sur moi et il commence à m'éto.\n"304 raw_data_manager.set_content(m, content)305 self.assertEqual(bytes(m), textwrap.dedent("""\306 Content-Type: text/plain; charset="utf-8"307 Content-Transfer-Encoding: 8bit308 et là il est monté sur moi et il commence à m'éto.309 """).encode('utf-8'))310 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)311 self.assertEqual(m.get_content(), content)312 def test_set_text_long_line_minimal_non_ascii_heuristics(self):313 m = self._make_message()314 content = ("j'ai un problème de python. il est sorti de son"315 " vivarium. et là il est monté sur moi et il commence"316 " à m'éto.\n")317 raw_data_manager.set_content(m, content)318 self.assertEqual(bytes(m), textwrap.dedent("""\319 Content-Type: text/plain; charset="utf-8"320 Content-Transfer-Encoding: quoted-printable321 j'ai un probl=C3=A8me de python. il est sorti de son vivari=322 um. et l=C3=A0 il est mont=C3=A9 sur moi et il commence =323 =C3=A0 m'=C3=A9to.324 """).encode('utf-8'))325 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)326 self.assertEqual(m.get_content(), content)327 def test_set_text_11_lines_long_line_minimal_non_ascii_heuristics(self):328 m = self._make_message()329 content = '\n'*10 + (330 "j'ai un problème de python. il est sorti de son"331 " vivarium. et là il est monté sur moi et il commence"332 " à m'éto.\n")333 raw_data_manager.set_content(m, content)334 self.assertEqual(bytes(m), textwrap.dedent("""\335 Content-Type: text/plain; charset="utf-8"336 Content-Transfer-Encoding: quoted-printable337 """ + '\n'*10 + """338 j'ai un probl=C3=A8me de python. il est sorti de son vivari=339 um. et l=C3=A0 il est mont=C3=A9 sur moi et il commence =340 =C3=A0 m'=C3=A9to.341 """).encode('utf-8'))342 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)343 self.assertEqual(m.get_content(), content)344 def test_set_text_maximal_non_ascii_heuristics(self):345 m = self._make_message()346 content = "áà äéèÄöÅ.\n"347 raw_data_manager.set_content(m, content)348 self.assertEqual(bytes(m), textwrap.dedent("""\349 Content-Type: text/plain; charset="utf-8"350 Content-Transfer-Encoding: 8bit351 áà äéèÄöÅ.352 """).encode('utf-8'))353 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)354 self.assertEqual(m.get_content(), content)355 def test_set_text_11_lines_maximal_non_ascii_heuristics(self):356 m = self._make_message()357 content = '\n'*10 + "áà äéèÄöÅ.\n"358 raw_data_manager.set_content(m, content)359 self.assertEqual(bytes(m), textwrap.dedent("""\360 Content-Type: text/plain; charset="utf-8"361 Content-Transfer-Encoding: 8bit362 """ + '\n'*10 + """363 áà äéèÄöÅ.364 """).encode('utf-8'))365 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)366 self.assertEqual(m.get_content(), content)367 def test_set_text_long_line_maximal_non_ascii_heuristics(self):368 m = self._make_message()369 content = ("áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"370 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"371 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n")372 raw_data_manager.set_content(m, content)373 self.assertEqual(bytes(m), textwrap.dedent("""\374 Content-Type: text/plain; charset="utf-8"375 Content-Transfer-Encoding: base64376 w6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOoxJnD377 tsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOo378 xJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TD379 qcOoxJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOg380 w6TDqcOoxJnDtsWRLgo=381 """).encode('utf-8'))382 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)383 self.assertEqual(m.get_content(), content)384 def test_set_text_11_lines_long_line_maximal_non_ascii_heuristics(self):385 # Yes, it chooses "wrong" here. It's a heuristic. So this result386 # could change if we come up with a better heuristic.387 m = self._make_message()388 content = ('\n'*10 +389 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"390 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"391 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n")392 raw_data_manager.set_content(m, "\n"*10 +393 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"394 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ"395 "áà äéèÄöÅáà äéèÄöÅáà äéèÄöÅáà äéèÄöÅ.\n")396 self.assertEqual(bytes(m), textwrap.dedent("""\397 Content-Type: text/plain; charset="utf-8"398 Content-Transfer-Encoding: quoted-printable399 """ + '\n'*10 + """400 =C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=401 =A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=402 =C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=403 =A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=404 =C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=405 =91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=406 =C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=407 =A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=408 =C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=409 =99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=410 =C5=91.411 """).encode('utf-8'))412 self.assertEqual(m.get_payload(decode=True).decode('utf-8'), content)413 self.assertEqual(m.get_content(), content)414 def test_set_text_non_ascii_with_cte_7bit_raises(self):415 m = self._make_message()416 with self.assertRaises(UnicodeError):417 raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", cte='7bit')418 def test_set_text_non_ascii_with_charset_ascii_raises(self):419 m = self._make_message()420 with self.assertRaises(UnicodeError):421 raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", charset='ascii')422 def test_set_text_non_ascii_with_cte_7bit_and_charset_ascii_raises(self):423 m = self._make_message()424 with self.assertRaises(UnicodeError):425 raw_data_manager.set_content(m,"áà äéèÄöÅ.\n", cte='7bit', charset='ascii')426 def test_set_message(self):427 m = self._make_message()428 m['Subject'] = "Forwarded message"429 content = self._make_message()430 content['To'] = 'python@vivarium.org'431 content['From'] = 'police@monty.org'432 content['Subject'] = "get back in your box"433 content.set_content("Or face the comfy chair.")434 raw_data_manager.set_content(m, content)435 self.assertEqual(str(m), textwrap.dedent("""\436 Subject: Forwarded message437 Content-Type: message/rfc822438 Content-Transfer-Encoding: 8bit439 To: python@vivarium.org440 From: police@monty.org441 Subject: get back in your box442 Content-Type: text/plain; charset="utf-8"443 Content-Transfer-Encoding: 7bit444 MIME-Version: 1.0445 Or face the comfy chair.446 """))447 payload = m.get_payload(0)448 self.assertIsInstance(payload, self.message)449 self.assertEqual(str(payload), str(content))450 self.assertIsInstance(m.get_content(), self.message)451 self.assertEqual(str(m.get_content()), str(content))452 def test_set_message_with_non_ascii_and_coercion_to_7bit(self):453 m = self._make_message()454 m['Subject'] = "Escape report"455 content = self._make_message()456 content['To'] = 'police@monty.org'457 content['From'] = 'victim@monty.org'458 content['Subject'] = "Help"459 content.set_content("j'ai un problème de python. il est sorti de son"460 " vivarium.")461 raw_data_manager.set_content(m, content)462 self.assertEqual(bytes(m), textwrap.dedent("""\463 Subject: Escape report464 Content-Type: message/rfc822465 Content-Transfer-Encoding: 8bit466 To: police@monty.org467 From: victim@monty.org468 Subject: Help469 Content-Type: text/plain; charset="utf-8"470 Content-Transfer-Encoding: 8bit471 MIME-Version: 1.0472 j'ai un problème de python. il est sorti de son vivarium.473 """).encode('utf-8'))474 # The choice of base64 for the body encoding is because generator475 # doesn't bother with heuristics and uses it unconditionally for utf-8476 # text.477 # XXX: the first cte should be 7bit, too...that's a generator bug.478 # XXX: the line length in the body also looks like a generator bug.479 self.assertEqual(m.as_string(maxheaderlen=self.policy.max_line_length),480 textwrap.dedent("""\481 Subject: Escape report482 Content-Type: message/rfc822483 Content-Transfer-Encoding: 8bit484 To: police@monty.org485 From: victim@monty.org486 Subject: Help487 Content-Type: text/plain; charset="utf-8"488 Content-Transfer-Encoding: base64489 MIME-Version: 1.0490 aidhaSB1biBwcm9ibMOobWUgZGUgcHl0aG9uLiBpbCBlc3Qgc29ydGkgZGUgc29uIHZpdmFyaXVt491 Lgo=492 """))493 self.assertIsInstance(m.get_content(), self.message)494 self.assertEqual(str(m.get_content()), str(content))495 def test_set_message_invalid_cte_raises(self):496 m = self._make_message()497 content = self._make_message()498 for cte in 'quoted-printable base64'.split():499 for subtype in 'rfc822 external-body'.split():500 with self.subTest(cte=cte, subtype=subtype):501 with self.assertRaises(ValueError) as ar:502 m.set_content(content, subtype, cte=cte)503 exc = str(ar.exception)504 self.assertIn(cte, exc)505 self.assertIn(subtype, exc)506 subtype = 'external-body'507 for cte in '8bit binary'.split():508 with self.subTest(cte=cte, subtype=subtype):509 with self.assertRaises(ValueError) as ar:510 m.set_content(content, subtype, cte=cte)511 exc = str(ar.exception)512 self.assertIn(cte, exc)513 self.assertIn(subtype, exc)514 def test_set_image_jpg(self):515 for content in (b"bogus content",516 bytearray(b"bogus content"),517 memoryview(b"bogus content")):518 with self.subTest(content=content):519 m = self._make_message()520 raw_data_manager.set_content(m, content, 'image', 'jpeg')521 self.assertEqual(str(m), textwrap.dedent("""\522 Content-Type: image/jpeg523 Content-Transfer-Encoding: base64524 Ym9ndXMgY29udGVudA==525 """))526 self.assertEqual(m.get_payload(decode=True), content)527 self.assertEqual(m.get_content(), content)528 def test_set_audio_aif_with_quoted_printable_cte(self):529 # Why you would use qp, I don't know, but it is technically supported.530 # XXX: the incorrect line length is because binascii.b2a_qp doesn't531 # support a line length parameter, but we must use it to get newline532 # encoding.533 # XXX: what about that lack of tailing newline? Do we actually handle534 # that correctly in all cases? That is, if the *source* has an535 # unencoded newline, do we add an extra newline to the returned payload536 # or not? And can that actually be disambiguated based on the RFC?537 m = self._make_message()538 content = b'b\xFFgus\tcon\nt\rent ' + b'z'*100539 m.set_content(content, 'audio', 'aif', cte='quoted-printable')540 self.assertEqual(bytes(m), textwrap.dedent("""\541 Content-Type: audio/aif542 Content-Transfer-Encoding: quoted-printable543 MIME-Version: 1.0544 b=FFgus=09con=0At=0Dent=20zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=545 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz""").encode('latin-1'))546 self.assertEqual(m.get_payload(decode=True), content)547 self.assertEqual(m.get_content(), content)548 def test_set_video_mpeg_with_binary_cte(self):549 m = self._make_message()550 content = b'b\xFFgus\tcon\nt\rent ' + b'z'*100551 m.set_content(content, 'video', 'mpeg', cte='binary')552 self.assertEqual(bytes(m), textwrap.dedent("""\553 Content-Type: video/mpeg554 Content-Transfer-Encoding: binary555 MIME-Version: 1.0556 """).encode('ascii') +557 # XXX: the second \n ought to be a \r, but generator gets it wrong.558 # THIS MEANS WE DON'T ACTUALLY SUPPORT THE 'binary' CTE.559 b'b\xFFgus\tcon\nt\nent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' +560 b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')561 self.assertEqual(m.get_payload(decode=True), content)562 self.assertEqual(m.get_content(), content)563 def test_set_application_octet_stream_with_8bit_cte(self):564 # In 8bit mode, universal line end logic applies. It is up to the565 # application to make sure the lines are short enough; we don't check.566 m = self._make_message()567 content = b'b\xFFgus\tcon\nt\rent\n' + b'z'*60 + b'\n'568 m.set_content(content, 'application', 'octet-stream', cte='8bit')569 self.assertEqual(bytes(m), textwrap.dedent("""\570 Content-Type: application/octet-stream571 Content-Transfer-Encoding: 8bit572 MIME-Version: 1.0573 """).encode('ascii') +574 b'b\xFFgus\tcon\nt\nent\n' +575 b'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\n')576 self.assertEqual(m.get_payload(decode=True), content)577 self.assertEqual(m.get_content(), content)578 def test_set_headers_from_header_objects(self):579 m = self._make_message()580 content = "Simple message.\n"581 header_factory = self.policy.header_factory582 raw_data_manager.set_content(m, content, headers=(583 header_factory("To", "foo@example.com"),584 header_factory("From", "foo@example.com"),585 header_factory("Subject", "I'm talking to myself.")))586 self.assertEqual(str(m), textwrap.dedent("""\587 Content-Type: text/plain; charset="utf-8"588 To: foo@example.com589 From: foo@example.com590 Subject: I'm talking to myself.591 Content-Transfer-Encoding: 7bit592 Simple message.593 """))594 def test_set_headers_from_strings(self):595 m = self._make_message()596 content = "Simple message.\n"597 raw_data_manager.set_content(m, content, headers=(598 "X-Foo-Header: foo",599 "X-Bar-Header: bar",))600 self.assertEqual(str(m), textwrap.dedent("""\601 Content-Type: text/plain; charset="utf-8"602 X-Foo-Header: foo603 X-Bar-Header: bar604 Content-Transfer-Encoding: 7bit605 Simple message.606 """))607 def test_set_headers_with_invalid_duplicate_string_header_raises(self):608 m = self._make_message()609 content = "Simple message.\n"610 with self.assertRaisesRegex(ValueError, 'Content-Type'):611 raw_data_manager.set_content(m, content, headers=(612 "Content-Type: foo/bar",)613 )614 def test_set_headers_with_invalid_duplicate_header_header_raises(self):615 m = self._make_message()616 content = "Simple message.\n"617 header_factory = self.policy.header_factory618 with self.assertRaisesRegex(ValueError, 'Content-Type'):619 raw_data_manager.set_content(m, content, headers=(620 header_factory("Content-Type", " foo/bar"),)621 )622 def test_set_headers_with_defective_string_header_raises(self):623 m = self._make_message()624 content = "Simple message.\n"625 with self.assertRaisesRegex(ValueError, 'a@fairly@@invalid@address'):626 raw_data_manager.set_content(m, content, headers=(627 'To: a@fairly@@invalid@address',)628 )629 print(m['To'].defects)630 def test_set_headers_with_defective_header_header_raises(self):631 m = self._make_message()632 content = "Simple message.\n"633 header_factory = self.policy.header_factory634 with self.assertRaisesRegex(ValueError, 'a@fairly@@invalid@address'):635 raw_data_manager.set_content(m, content, headers=(636 header_factory('To', 'a@fairly@@invalid@address'),)637 )638 print(m['To'].defects)639 def test_set_disposition_inline(self):640 m = self._make_message()641 m.set_content('foo', disposition='inline')642 self.assertEqual(m['Content-Disposition'], 'inline')643 def test_set_disposition_attachment(self):644 m = self._make_message()645 m.set_content('foo', disposition='attachment')646 self.assertEqual(m['Content-Disposition'], 'attachment')647 def test_set_disposition_foo(self):648 m = self._make_message()649 m.set_content('foo', disposition='foo')650 self.assertEqual(m['Content-Disposition'], 'foo')651 # XXX: we should have a 'strict' policy mode (beyond raise_on_defect) that652 # would cause 'foo' above to raise.653 def test_set_filename(self):654 m = self._make_message()655 m.set_content('foo', filename='bar.txt')656 self.assertEqual(m['Content-Disposition'],657 'attachment; filename="bar.txt"')658 def test_set_filename_and_disposition_inline(self):659 m = self._make_message()660 m.set_content('foo', disposition='inline', filename='bar.txt')661 self.assertEqual(m['Content-Disposition'], 'inline; filename="bar.txt"')662 def test_set_non_ascii_filename(self):663 m = self._make_message()664 m.set_content('foo', filename='ábárî.txt')665 self.assertEqual(bytes(m), textwrap.dedent("""\666 Content-Type: text/plain; charset="utf-8"667 Content-Transfer-Encoding: 7bit668 Content-Disposition: attachment;669 filename*=utf-8''%C3%A1b%C3%A1r%C3%AE.txt670 MIME-Version: 1.0671 foo672 """).encode('ascii'))673 def test_set_content_bytes_cte_7bit(self):674 m = self._make_message()675 m.set_content(b'ASCII-only message.\n',676 maintype='application', subtype='octet-stream', cte='7bit')677 self.assertEqual(str(m), textwrap.dedent("""\678 Content-Type: application/octet-stream679 Content-Transfer-Encoding: 7bit680 MIME-Version: 1.0681 ASCII-only message.682 """))683 content_object_params = {684 'text_plain': ('content', ()),685 'text_html': ('content', ('html',)),686 'application_octet_stream': (b'content',687 ('application', 'octet_stream')),688 'image_jpeg': (b'content', ('image', 'jpeg')),689 'message_rfc822': (message(), ()),690 'message_external_body': (message(), ('external-body',)),691 }692 def content_object_as_header_receiver(self, obj, mimetype):693 m = self._make_message()694 m.set_content(obj, *mimetype, headers=(695 'To: foo@example.com',696 'From: bar@simple.net'))697 self.assertEqual(m['to'], 'foo@example.com')698 self.assertEqual(m['from'], 'bar@simple.net')699 def content_object_as_disposition_inline_receiver(self, obj, mimetype):700 m = self._make_message()701 m.set_content(obj, *mimetype, disposition='inline')702 self.assertEqual(m['Content-Disposition'], 'inline')703 def content_object_as_non_ascii_filename_receiver(self, obj, mimetype):704 m = self._make_message()705 m.set_content(obj, *mimetype, disposition='inline', filename='bár.txt')706 self.assertEqual(m['Content-Disposition'], 'inline; filename="bár.txt"')707 self.assertEqual(m.get_filename(), "bár.txt")708 self.assertEqual(m['Content-Disposition'].params['filename'], "bár.txt")709 def content_object_as_cid_receiver(self, obj, mimetype):710 m = self._make_message()711 m.set_content(obj, *mimetype, cid='some_random_stuff')712 self.assertEqual(m['Content-ID'], 'some_random_stuff')713 def content_object_as_params_receiver(self, obj, mimetype):714 m = self._make_message()715 params = {'foo': 'bár', 'abc': 'xyz'}716 m.set_content(obj, *mimetype, params=params)717 if isinstance(obj, str):718 params['charset'] = 'utf-8'719 self.assertEqual(m['Content-Type'].params, params)720if __name__ == '__main__':...
sokoban.py
Source:sokoban.py
...7172 def get_content(self, x, y):73 return self.matrix[y][x]7475 def set_content(self, x, y, content):76 if self.is_valid_value(content):77 self.matrix[y][x] = content78 else:79 print("ERROR: Value '" + content + "' to be added is not valid")8081 def worker(self):82 x = 083 y = 084 for row in self.matrix:85 for pos in row:86 if pos == '@' or pos == '+':87 return (x, y, pos)88 else:89 x = x + 190 y = y + 191 x = 09293 def can_move(self, x, y):94 return self.get_content(self.worker()[0] + x, self.worker()[1] + y) not in ['#', '*', '$']9596 def next(self, x, y):97 return self.get_content(self.worker()[0] + x, self.worker()[1] + y)9899 def can_push(self, x, y):100 return (self.next(x, y) in ['*', '$'] and self.next(x + x, y + y) in [' ', '.'])101102 def is_completed(self):103 for row in self.matrix:104 for cell in row:105 if cell == '$':106 return False107 return True108109 def move_box(self, x, y, a, b):110 # (x,y) -> move to do111 # (a,b) -> box to move112 current_box = self.get_content(x, y)113 future_box = self.get_content(x + a, y + b)114 if current_box == '$' and future_box == ' ':115 self.set_content(x + a, y + b, '$')116 self.set_content(x, y, ' ')117 elif current_box == '$' and future_box == '.':118 self.set_content(x + a, y + b, '*')119 self.set_content(x, y, ' ')120 elif current_box == '*' and future_box == ' ':121 self.set_content(x + a, y + b, '$')122 self.set_content(x, y, '.')123 elif current_box == '*' and future_box == '.':124 self.set_content(x + a, y + b, '*')125 self.set_content(x, y, '.')126127 def unmove(self):128 if not self.queue.empty():129 movement = self.queue.get()130 if movement[2]:131 current = self.worker()132 self.move(movement[0] * -1, movement[1] * -1, False)133 self.move_box(current[0] + movement[0], current[1] + movement[1], movement[0] * -1, movement[1] * -1)134 else:135 self.move(movement[0] * -1, movement[1] * -1, False)136137 def move(self, x, y, save):138 if self.can_move(x, y):139 current = self.worker()140 future = self.next(x, y)141 if current[2] == '@' and future == ' ':142 self.set_content(current[0] + x, current[1] + y, '@')143 self.set_content(current[0], current[1], ' ')144 if save: self.queue.put((x, y, False))145 elif current[2] == '@' and future == '.':146 self.set_content(current[0] + x, current[1] + y, '+')147 self.set_content(current[0], current[1], ' ')148 if save: self.queue.put((x, y, False))149 elif current[2] == '+' and future == ' ':150 self.set_content(current[0] + x, current[1] + y, '@')151 self.set_content(current[0], current[1], '.')152 if save: self.queue.put((x, y, False))153 elif current[2] == '+' and future == '.':154 self.set_content(current[0] + x, current[1] + y, '+')155 self.set_content(current[0], current[1], '.')156 if save: self.queue.put((x, y, False))157 elif self.can_push(x, y):158 current = self.worker()159 future = self.next(x, y)160 future_box = self.next(x + x, y + y)161 if current[2] == '@' and future == '$' and future_box == ' ':162 self.move_box(current[0] + x, current[1] + y, x, y)163 self.set_content(current[0], current[1], ' ')164 self.set_content(current[0] + x, current[1] + y, '@')165 if save: self.queue.put((x, y, True))166 elif current[2] == '@' and future == '$' and future_box == '.':167 self.move_box(current[0] + x, current[1] + y, x, y)168 self.set_content(current[0], current[1], ' ')169 self.set_content(current[0] + x, current[1] + y, '@')170 if save: self.queue.put((x, y, True))171 elif current[2] == '@' and future == '*' and future_box == ' ':172 self.move_box(current[0] + x, current[1] + y, x, y)173 self.set_content(current[0], current[1], ' ')174 self.set_content(current[0] + x, current[1] + y, '+')175 if save: self.queue.put((x, y, True))176 elif current[2] == '@' and future == '*' and future_box == '.':177 self.move_box(current[0] + x, current[1] + y, x, y)178 self.set_content(current[0], current[1], ' ')179 self.set_content(current[0] + x, current[1] + y, '+')180 if save: self.queue.put((x, y, True))181 if current[2] == '+' and future == '$' and future_box == ' ':182 self.move_box(current[0] + x, current[1] + y, x, y)183 self.set_content(current[0], current[1], '.')184 self.set_content(current[0] + x, current[1] + y, '@')185 if save: self.queue.put((x, y, True))186 elif current[2] == '+' and future == '$' and future_box == '.':187 self.move_box(current[0] + x, current[1] + y, x, y)188 self.set_content(current[0], current[1], '.')189 self.set_content(current[0] + x, current[1] + y, '+')190 if save: self.queue.put((x, y, True))191 elif current[2] == '+' and future == '*' and future_box == ' ':192 self.move_box(current[0] + x, current[1] + y, x, y)193 self.set_content(current[0], current[1], '.')194 self.set_content(current[0] + x, current[1] + y, '+')195 if save: self.queue.put((x, y, True))196 elif current[2] == '+' and future == '*' and future_box == '.':197 self.move_box(current[0] + x, current[1] + y, x, y)198 self.set_content(current[0], current[1], '.')199 self.set_content(current[0] + x, current[1] + y, '+')200 if save: self.queue.put((x, y, True))201202203def print_game(matrix, screen):204 screen.fill(background)205 x = 0206 y = 0207 for row in matrix:208 for char in row:209 if char == ' ': # floor210 screen.blit(floor, (x, y))211 elif char == '#': # wall212 screen.blit(wall, (x, y))213 elif char == '@': # worker on floor
...
logic.py
Source:logic.py
...64 sys.stdout.flush()65 sys.stdout.write('\n')66 def get_content(self,x,y):67 return self.matrix[y][x]68 def set_content(self,x,y,content):69 if self.is_valid_value(content):70 self.matrix[y][x] = content71 else:72 print("ERROR: Value '"+content+"' to be added is not valid")73 def worker(self):74 x = 075 y = 076 for row in self.matrix:77 for pos in row:78 if pos == '@' or pos == '+':79 return (x, y, pos)80 else:81 x = x + 182 y = y + 183 x = 084 def can_move(self,x,y):85 return self.get_content(self.worker()[0]+x,self.worker()[1]+y) not in ['#','*','$']86 def next(self,x,y):87 return self.get_content(self.worker()[0]+x,self.worker()[1]+y)88 def can_push(self,x,y):89 return (self.next(x,y) in ['*','$'] and self.next(x+x,y+y) in [' ','.'])90 def is_completed(self):91 for row in self.matrix:92 for cell in row:93 if cell == '$':94 return False95 return True96 def move_box(self,x,y,a,b):97# (x,y) -> move to do98# (a,b) -> box to move99 current_box = self.get_content(x,y)100 future_box = self.get_content(x+a,y+b)101 if current_box == '$' and future_box == ' ':102 self.set_content(x+a,y+b,'$')103 self.set_content(x,y,' ')104 elif current_box == '$' and future_box == '.':105 self.set_content(x+a,y+b,'*')106 self.set_content(x,y,' ')107 elif current_box == '*' and future_box == ' ':108 self.set_content(x+a,y+b,'$')109 self.set_content(x,y,'.')110 elif current_box == '*' and future_box == '.':111 self.set_content(x+a,y+b,'*')112 self.set_content(x,y,'.')113 def unmove(self):114 if not self.queue.empty():115 movement = self.queue.get()116 if movement[2]:117 current = self.worker()118 self.move(movement[0] * -1,movement[1] * -1, False)119 self.move_box(current[0]+movement[0],current[1]+movement[1],movement[0] * -1,movement[1] * -1)120 else:121 self.move(movement[0] * -1,movement[1] * -1, False)122 def move(self,x,y,save):123 if self.can_move(x,y):124 current = self.worker()125 future = self.next(x,y)126 if current[2] == '@' and future == ' ':127 self.set_content(current[0]+x,current[1]+y,'@')128 self.set_content(current[0],current[1],' ')129 if save: self.queue.put((x,y,False))130 elif current[2] == '@' and future == '.':131 self.set_content(current[0]+x,current[1]+y,'+')132 self.set_content(current[0],current[1],' ')133 if save: self.queue.put((x,y,False))134 elif current[2] == '+' and future == ' ':135 self.set_content(current[0]+x,current[1]+y,'@')136 self.set_content(current[0],current[1],'.')137 if save: self.queue.put((x,y,False))138 elif current[2] == '+' and future == '.':139 self.set_content(current[0]+x,current[1]+y,'+')140 self.set_content(current[0],current[1],'.')141 if save: self.queue.put((x,y,False))142 elif self.can_push(x,y):143 current = self.worker()144 future = self.next(x,y)145 future_box = self.next(x+x,y+y)146 if current[2] == '@' and future == '$' and future_box == ' ':147 self.move_box(current[0]+x,current[1]+y,x,y)148 self.set_content(current[0],current[1],' ')149 self.set_content(current[0]+x,current[1]+y,'@')150 if save: self.queue.put((x,y,True))151 elif current[2] == '@' and future == '$' and future_box == '.':152 self.move_box(current[0]+x,current[1]+y,x,y)153 self.set_content(current[0],current[1],' ')154 self.set_content(current[0]+x,current[1]+y,'@')155 if save: self.queue.put((x,y,True))156 elif current[2] == '@' and future == '*' and future_box == ' ':157 self.move_box(current[0]+x,current[1]+y,x,y)158 self.set_content(current[0],current[1],' ')159 self.set_content(current[0]+x,current[1]+y,'+')160 if save: self.queue.put((x,y,True))161 elif current[2] == '@' and future == '*' and future_box == '.':162 self.move_box(current[0]+x,current[1]+y,x,y)163 self.set_content(current[0],current[1],' ')164 self.set_content(current[0]+x,current[1]+y,'+')165 if save: self.queue.put((x,y,True))166 if current[2] == '+' and future == '$' and future_box == ' ':167 self.move_box(current[0]+x,current[1]+y,x,y)168 self.set_content(current[0],current[1],'.')169 self.set_content(current[0]+x,current[1]+y,'@')170 if save: self.queue.put((x,y,True))171 elif current[2] == '+' and future == '$' and future_box == '.':172 self.move_box(current[0]+x,current[1]+y,x,y)173 self.set_content(current[0],current[1],'.')174 self.set_content(current[0]+x,current[1]+y,'@')175 if save: self.queue.put((x,y,True))176 elif current[2] == '+' and future == '*' and future_box == ' ':177 self.move_box(current[0]+x,current[1]+y,x,y)178 self.set_content(current[0],current[1],'.')179 self.set_content(current[0]+x,current[1]+y,'+')180 if save: self.queue.put((x,y,True))181 elif current[2] == '+' and future == '*' and future_box == '.':182 self.move_box(current[0]+x,current[1]+y,x,y)183 self.set_content(current[0],current[1],'.')184 self.set_content(current[0]+x,current[1]+y,'+')...
test_spacing.py
Source:test_spacing.py
...3class TestSpacing(unittest.TestCase):4 def setUp(self):5 self.converter = Converter(classes_only=True)6 def test_padding_it_converts_on_all_sides(self):7 self.assertEqual("p-1", self.converter.set_content("p-1").convert())8 self.assertEqual(9 "sm:p-1", self.converter.set_content("p-sm-1").convert(),10 )11 self.assertEqual(12 "md:p-2", self.converter.set_content("p-md-2").convert(),13 )14 self.assertEqual(15 "lg:p-4", self.converter.set_content("p-lg-3").convert(),16 )17 self.assertEqual(18 "xl:p-6", self.converter.set_content("p-xl-4").convert(),19 )20 self.assertEqual(21 "xl:p-12", self.converter.set_content("p-xl-5").convert(),22 )23 # @test */24 def test_padding_it_converts_on_y(self):25 self.assertEqual("py-1", self.converter.set_content("py-1").convert())26 self.assertEqual(27 "sm:py-1", self.converter.set_content("py-sm-1").convert(),28 )29 self.assertEqual(30 "md:py-2", self.converter.set_content("py-md-2").convert(),31 )32 self.assertEqual(33 "lg:py-4", self.converter.set_content("py-lg-3").convert(),34 )35 self.assertEqual(36 "xl:py-6", self.converter.set_content("py-xl-4").convert(),37 )38 self.assertEqual(39 "xl:py-12", self.converter.set_content("py-xl-5").convert(),40 )41 # @test */42 def test_padding_it_converts_on_x(self):43 self.assertEqual("px-1", self.converter.set_content("px-1").convert())44 self.assertEqual(45 "sm:px-1", self.converter.set_content("px-sm-1").convert(),46 )47 self.assertEqual(48 "md:px-2", self.converter.set_content("px-md-2").convert(),49 )50 self.assertEqual(51 "lg:px-4", self.converter.set_content("px-lg-3").convert(),52 )53 self.assertEqual(54 "xl:px-6", self.converter.set_content("px-xl-4").convert(),55 )56 self.assertEqual(57 "xl:px-12", self.converter.set_content("px-xl-5").convert(),58 )59 # @test */60 def test_padding_it_converts_0_on_all_sides(self):61 self.assertEqual("p-0", self.converter.set_content("p-0").convert())62 self.assertEqual(63 "lg:py-0", self.converter.set_content("py-lg-0").convert(),64 )65 #66 # * @group Margin67 # */68 # @test */69 def test_margin_it_converts_on_all_sides(self):70 self.assertEqual("m-1", self.converter.set_content("m-1").convert())71 self.assertEqual(72 "sm:m-1", self.converter.set_content("m-sm-1").convert(),73 )74 self.assertEqual(75 "md:m-2", self.converter.set_content("m-md-2").convert(),76 )77 self.assertEqual(78 "lg:m-4", self.converter.set_content("m-lg-3").convert(),79 )80 self.assertEqual(81 "xl:m-6", self.converter.set_content("m-xl-4").convert(),82 )83 self.assertEqual(84 "xl:m-12", self.converter.set_content("m-xl-5").convert(),85 )86 # @test */87 def test_margin_it_converts_on_y(self):88 self.assertEqual("my-1", self.converter.set_content("my-1").convert())89 self.assertEqual(90 "sm:my-1", self.converter.set_content("my-sm-1").convert(),91 )92 self.assertEqual(93 "md:my-2", self.converter.set_content("my-md-2").convert(),94 )95 self.assertEqual(96 "lg:my-4", self.converter.set_content("my-lg-3").convert(),97 )98 self.assertEqual(99 "xl:my-6", self.converter.set_content("my-xl-4").convert(),100 )101 self.assertEqual(102 "xl:my-12", self.converter.set_content("my-xl-5").convert(),103 )104 # @test */105 def test_margin_it_converts_on_x(self):106 self.assertEqual("mx-1", self.converter.set_content("mx-1").convert())107 self.assertEqual(108 "sm:mx-1", self.converter.set_content("mx-sm-1").convert(),109 )110 self.assertEqual(111 "md:mx-2", self.converter.set_content("mx-md-2").convert(),112 )113 self.assertEqual(114 "lg:mx-4", self.converter.set_content("mx-lg-3").convert(),115 )116 self.assertEqual(117 "xl:mx-6", self.converter.set_content("mx-xl-4").convert(),118 )119 self.assertEqual(120 "xl:mx-12", self.converter.set_content("mx-xl-5").convert(),121 )122 # @test */123 def test_margin_it_converts_0_on_all_sides(self):124 self.assertEqual("m-0", self.converter.set_content("m-0").convert())125 self.assertEqual(126 "lg:my-0", self.converter.set_content("my-lg-0").convert(),...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!