test_errors.py (3339B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import sys 6 7 from marionette_driver import errors 8 9 from marionette_harness import marionette_test 10 11 12 def fake_cause(): 13 try: 14 raise ValueError("bar") 15 except ValueError: 16 return sys.exc_info() 17 18 19 message = "foo" 20 unicode_message = "\u201Cfoo" 21 cause = fake_cause() 22 stacktrace = "first\nsecond" 23 24 25 class TestErrors(marionette_test.MarionetteTestCase): 26 def test_defaults(self): 27 exc = errors.MarionetteException() 28 self.assertEqual(str(exc), "None") 29 self.assertIsNone(exc.cause) 30 self.assertIsNone(exc.stacktrace) 31 32 def test_construction(self): 33 exc = errors.MarionetteException( 34 message=message, cause=cause, stacktrace=stacktrace 35 ) 36 self.assertEqual(exc.message, message) 37 self.assertEqual(exc.cause, cause) 38 self.assertEqual(exc.stacktrace, stacktrace) 39 40 def test_str_message(self): 41 exc = errors.MarionetteException( 42 message=message, cause=cause, stacktrace=stacktrace 43 ) 44 r = str(exc) 45 self.assertIn(message, r) 46 self.assertIn(", caused by {0!r}".format(cause[0]), r) 47 self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r) 48 49 def test_unicode_message(self): 50 exc = errors.MarionetteException( 51 message=unicode_message, cause=cause, stacktrace=stacktrace 52 ) 53 r = str(exc) 54 self.assertIn(unicode_message, r) 55 self.assertIn(", caused by {0!r}".format(cause[0]), r) 56 self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r) 57 58 def test_unicode_message_as_str(self): 59 exc = errors.MarionetteException( 60 message=unicode_message, cause=cause, stacktrace=stacktrace 61 ) 62 r = str(exc) 63 self.assertIn(unicode_message, r) 64 self.assertIn(", caused by {0!r}".format(cause[0]), r) 65 self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r) 66 67 def test_cause_string(self): 68 exc = errors.MarionetteException(cause="foo") 69 self.assertEqual(exc.cause, "foo") 70 r = str(exc) 71 self.assertIn(", caused by foo", r) 72 73 def test_cause_tuple(self): 74 exc = errors.MarionetteException(cause=cause) 75 self.assertEqual(exc.cause, cause) 76 r = str(exc) 77 self.assertIn(", caused by {0!r}".format(cause[0]), r) 78 79 80 class TestLookup(marionette_test.MarionetteTestCase): 81 def test_by_unknown_number(self): 82 self.assertEqual(errors.MarionetteException, errors.lookup(123456)) 83 84 def test_by_known_string(self): 85 self.assertEqual( 86 errors.NoSuchElementException, errors.lookup("no such element") 87 ) 88 89 def test_by_unknown_string(self): 90 self.assertEqual(errors.MarionetteException, errors.lookup("barbera")) 91 92 def test_by_known_unicode_string(self): 93 self.assertEqual( 94 errors.NoSuchElementException, errors.lookup("no such element") 95 ) 96 97 98 class TestAllErrors(marionette_test.MarionetteTestCase): 99 def test_properties(self): 100 for exc in errors.es_: 101 self.assertTrue( 102 hasattr(exc, "status"), "expected exception to have attribute `status'" 103 )