test_text.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- test-case-name: twisted.conch.test.test_text -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. from twisted.trial import unittest
  5. from twisted.conch.insults import text
  6. from twisted.conch.insults.text import attributes as A
  7. class FormattedTextTests(unittest.TestCase):
  8. """
  9. Tests for assembling formatted text.
  10. """
  11. def test_trivial(self):
  12. """
  13. Using no formatting attributes produces no VT102 control sequences in
  14. the flattened output.
  15. """
  16. self.assertEqual(
  17. text.assembleFormattedText(A.normal['Hello, world.']),
  18. 'Hello, world.')
  19. def test_bold(self):
  20. """
  21. The bold formatting attribute, L{A.bold}, emits the VT102 control
  22. sequence to enable bold when flattened.
  23. """
  24. self.assertEqual(
  25. text.assembleFormattedText(A.bold['Hello, world.']),
  26. '\x1b[1mHello, world.')
  27. def test_underline(self):
  28. """
  29. The underline formatting attribute, L{A.underline}, emits the VT102
  30. control sequence to enable underlining when flattened.
  31. """
  32. self.assertEqual(
  33. text.assembleFormattedText(A.underline['Hello, world.']),
  34. '\x1b[4mHello, world.')
  35. def test_blink(self):
  36. """
  37. The blink formatting attribute, L{A.blink}, emits the VT102 control
  38. sequence to enable blinking when flattened.
  39. """
  40. self.assertEqual(
  41. text.assembleFormattedText(A.blink['Hello, world.']),
  42. '\x1b[5mHello, world.')
  43. def test_reverseVideo(self):
  44. """
  45. The reverse-video formatting attribute, L{A.reverseVideo}, emits the
  46. VT102 control sequence to enable reversed video when flattened.
  47. """
  48. self.assertEqual(
  49. text.assembleFormattedText(A.reverseVideo['Hello, world.']),
  50. '\x1b[7mHello, world.')
  51. def test_minus(self):
  52. """
  53. Formatting attributes prefixed with a minus (C{-}) temporarily disable
  54. the prefixed attribute, emitting no VT102 control sequence to enable
  55. it in the flattened output.
  56. """
  57. self.assertEqual(
  58. text.assembleFormattedText(
  59. A.bold[A.blink['Hello', -A.bold[' world'], '.']]),
  60. '\x1b[1;5mHello\x1b[0;5m world\x1b[1;5m.')
  61. def test_foreground(self):
  62. """
  63. The foreground color formatting attribute, L{A.fg}, emits the VT102
  64. control sequence to set the selected foreground color when flattened.
  65. """
  66. self.assertEqual(
  67. text.assembleFormattedText(
  68. A.normal[A.fg.red['Hello, '], A.fg.green['world!']]),
  69. '\x1b[31mHello, \x1b[32mworld!')
  70. def test_background(self):
  71. """
  72. The background color formatting attribute, L{A.bg}, emits the VT102
  73. control sequence to set the selected background color when flattened.
  74. """
  75. self.assertEqual(
  76. text.assembleFormattedText(
  77. A.normal[A.bg.red['Hello, '], A.bg.green['world!']]),
  78. '\x1b[41mHello, \x1b[42mworld!')
  79. def test_flattenDeprecated(self):
  80. """
  81. L{twisted.conch.insults.text.flatten} emits a deprecation warning when
  82. imported or accessed.
  83. """
  84. warningsShown = self.flushWarnings([self.test_flattenDeprecated])
  85. self.assertEqual(len(warningsShown), 0)
  86. # Trigger the deprecation warning.
  87. text.flatten
  88. warningsShown = self.flushWarnings([self.test_flattenDeprecated])
  89. self.assertEqual(len(warningsShown), 1)
  90. self.assertEqual(warningsShown[0]['category'], DeprecationWarning)
  91. self.assertEqual(
  92. warningsShown[0]['message'],
  93. 'twisted.conch.insults.text.flatten was deprecated in Twisted '
  94. '13.1.0: Use twisted.conch.insults.text.assembleFormattedText '
  95. 'instead.')