testformatter.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from unittest import TestCase, main
  3. from mypy.util import split_words, trim_source_line
  4. class FancyErrorFormattingTestCases(TestCase):
  5. def test_trim_source(self) -> None:
  6. assert trim_source_line("0123456789abcdef", max_len=16, col=5, min_width=2) == (
  7. "0123456789abcdef",
  8. 0,
  9. )
  10. # Locations near start.
  11. assert trim_source_line("0123456789abcdef", max_len=7, col=0, min_width=2) == (
  12. "0123456...",
  13. 0,
  14. )
  15. assert trim_source_line("0123456789abcdef", max_len=7, col=4, min_width=2) == (
  16. "0123456...",
  17. 0,
  18. )
  19. # Middle locations.
  20. assert trim_source_line("0123456789abcdef", max_len=7, col=5, min_width=2) == (
  21. "...1234567...",
  22. -2,
  23. )
  24. assert trim_source_line("0123456789abcdef", max_len=7, col=6, min_width=2) == (
  25. "...2345678...",
  26. -1,
  27. )
  28. assert trim_source_line("0123456789abcdef", max_len=7, col=8, min_width=2) == (
  29. "...456789a...",
  30. 1,
  31. )
  32. # Locations near the end.
  33. assert trim_source_line("0123456789abcdef", max_len=7, col=11, min_width=2) == (
  34. "...789abcd...",
  35. 4,
  36. )
  37. assert trim_source_line("0123456789abcdef", max_len=7, col=13, min_width=2) == (
  38. "...9abcdef",
  39. 6,
  40. )
  41. assert trim_source_line("0123456789abcdef", max_len=7, col=15, min_width=2) == (
  42. "...9abcdef",
  43. 6,
  44. )
  45. def test_split_words(self) -> None:
  46. assert split_words("Simple message") == ["Simple", "message"]
  47. assert split_words('Message with "Some[Long, Types]"' " in it") == [
  48. "Message",
  49. "with",
  50. '"Some[Long, Types]"',
  51. "in",
  52. "it",
  53. ]
  54. assert split_words('Message with "Some[Long, Types]"' " and [error-code]") == [
  55. "Message",
  56. "with",
  57. '"Some[Long, Types]"',
  58. "and",
  59. "[error-code]",
  60. ]
  61. assert split_words('"Type[Stands, First]" then words') == [
  62. '"Type[Stands, First]"',
  63. "then",
  64. "words",
  65. ]
  66. assert split_words('First words "Then[Stands, Type]"') == [
  67. "First",
  68. "words",
  69. '"Then[Stands, Type]"',
  70. ]
  71. assert split_words('"Type[Only, Here]"') == ['"Type[Only, Here]"']
  72. assert split_words("OneWord") == ["OneWord"]
  73. assert split_words(" ") == ["", ""]
  74. if __name__ == "__main__":
  75. main()