Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

94 wiersze
2.6 KiB

  1. import re
  2. import sys
  3. import traceback
  4. import pytest
  5. from .._util import *
  6. def test_ProtocolError():
  7. with pytest.raises(TypeError):
  8. ProtocolError("abstract base class")
  9. def test_LocalProtocolError():
  10. try:
  11. raise LocalProtocolError("foo")
  12. except LocalProtocolError as e:
  13. assert str(e) == "foo"
  14. assert e.error_status_hint == 400
  15. try:
  16. raise LocalProtocolError("foo", error_status_hint=418)
  17. except LocalProtocolError as e:
  18. assert str(e) == "foo"
  19. assert e.error_status_hint == 418
  20. def thunk():
  21. raise LocalProtocolError("a", error_status_hint=420)
  22. try:
  23. try:
  24. thunk()
  25. except LocalProtocolError as exc1:
  26. orig_traceback = "".join(traceback.format_tb(sys.exc_info()[2]))
  27. exc1._reraise_as_remote_protocol_error()
  28. except RemoteProtocolError as exc2:
  29. assert type(exc2) is RemoteProtocolError
  30. assert exc2.args == ("a",)
  31. assert exc2.error_status_hint == 420
  32. new_traceback = "".join(traceback.format_tb(sys.exc_info()[2]))
  33. assert new_traceback.endswith(orig_traceback)
  34. def test_validate():
  35. my_re = re.compile(br"(?P<group1>[0-9]+)\.(?P<group2>[0-9]+)")
  36. with pytest.raises(LocalProtocolError):
  37. validate(my_re, b"0.")
  38. groups = validate(my_re, b"0.1")
  39. assert groups == {"group1": b"0", "group2": b"1"}
  40. # successful partial matches are an error - must match whole string
  41. with pytest.raises(LocalProtocolError):
  42. validate(my_re, b"0.1xx")
  43. with pytest.raises(LocalProtocolError):
  44. validate(my_re, b"0.1\n")
  45. def test_validate_formatting():
  46. my_re = re.compile(br"foo")
  47. with pytest.raises(LocalProtocolError) as excinfo:
  48. validate(my_re, b"", "oops")
  49. assert "oops" in str(excinfo.value)
  50. with pytest.raises(LocalProtocolError) as excinfo:
  51. validate(my_re, b"", "oops {}")
  52. assert "oops {}" in str(excinfo.value)
  53. with pytest.raises(LocalProtocolError) as excinfo:
  54. validate(my_re, b"", "oops {} xx", 10)
  55. assert "oops 10 xx" in str(excinfo.value)
  56. def test_make_sentinel():
  57. S = make_sentinel("S")
  58. assert repr(S) == "S"
  59. assert S == S
  60. assert type(S).__name__ == "S"
  61. assert S in {S}
  62. assert type(S) is S
  63. S2 = make_sentinel("S2")
  64. assert repr(S2) == "S2"
  65. assert S != S2
  66. assert S not in {S2}
  67. assert type(S) is not type(S2)
  68. def test_bytesify():
  69. assert bytesify(b"123") == b"123"
  70. assert bytesify(bytearray(b"123")) == b"123"
  71. assert bytesify("123") == b"123"
  72. with pytest.raises(UnicodeEncodeError):
  73. bytesify(u"\u1234")
  74. with pytest.raises(TypeError):
  75. bytesify(10)