test_style.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. from __future__ import annotations
  2. from prompt_toolkit.styles import Attrs, Style, SwapLightAndDarkStyleTransformation
  3. def test_style_from_dict():
  4. style = Style.from_dict(
  5. {
  6. "a": "#ff0000 bold underline strike italic",
  7. "b": "bg:#00ff00 blink reverse",
  8. }
  9. )
  10. # Lookup of class:a.
  11. expected = Attrs(
  12. color="ff0000",
  13. bgcolor="",
  14. bold=True,
  15. underline=True,
  16. strike=True,
  17. italic=True,
  18. blink=False,
  19. reverse=False,
  20. hidden=False,
  21. )
  22. assert style.get_attrs_for_style_str("class:a") == expected
  23. # Lookup of class:b.
  24. expected = Attrs(
  25. color="",
  26. bgcolor="00ff00",
  27. bold=False,
  28. underline=False,
  29. strike=False,
  30. italic=False,
  31. blink=True,
  32. reverse=True,
  33. hidden=False,
  34. )
  35. assert style.get_attrs_for_style_str("class:b") == expected
  36. # Test inline style.
  37. expected = Attrs(
  38. color="ff0000",
  39. bgcolor="",
  40. bold=False,
  41. underline=False,
  42. strike=False,
  43. italic=False,
  44. blink=False,
  45. reverse=False,
  46. hidden=False,
  47. )
  48. assert style.get_attrs_for_style_str("#ff0000") == expected
  49. # Combine class name and inline style (Whatever is defined later gets priority.)
  50. expected = Attrs(
  51. color="00ff00",
  52. bgcolor="",
  53. bold=True,
  54. underline=True,
  55. strike=True,
  56. italic=True,
  57. blink=False,
  58. reverse=False,
  59. hidden=False,
  60. )
  61. assert style.get_attrs_for_style_str("class:a #00ff00") == expected
  62. expected = Attrs(
  63. color="ff0000",
  64. bgcolor="",
  65. bold=True,
  66. underline=True,
  67. strike=True,
  68. italic=True,
  69. blink=False,
  70. reverse=False,
  71. hidden=False,
  72. )
  73. assert style.get_attrs_for_style_str("#00ff00 class:a") == expected
  74. def test_class_combinations_1():
  75. # In this case, our style has both class 'a' and 'b'.
  76. # Given that the style for 'a b' is defined at the end, that one is used.
  77. style = Style(
  78. [
  79. ("a", "#0000ff"),
  80. ("b", "#00ff00"),
  81. ("a b", "#ff0000"),
  82. ]
  83. )
  84. expected = Attrs(
  85. color="ff0000",
  86. bgcolor="",
  87. bold=False,
  88. underline=False,
  89. strike=False,
  90. italic=False,
  91. blink=False,
  92. reverse=False,
  93. hidden=False,
  94. )
  95. assert style.get_attrs_for_style_str("class:a class:b") == expected
  96. assert style.get_attrs_for_style_str("class:a,b") == expected
  97. assert style.get_attrs_for_style_str("class:a,b,c") == expected
  98. # Changing the order shouldn't matter.
  99. assert style.get_attrs_for_style_str("class:b class:a") == expected
  100. assert style.get_attrs_for_style_str("class:b,a") == expected
  101. def test_class_combinations_2():
  102. # In this case, our style has both class 'a' and 'b'.
  103. # The style that is defined the latest get priority.
  104. style = Style(
  105. [
  106. ("a b", "#ff0000"),
  107. ("b", "#00ff00"),
  108. ("a", "#0000ff"),
  109. ]
  110. )
  111. expected = Attrs(
  112. color="00ff00",
  113. bgcolor="",
  114. bold=False,
  115. underline=False,
  116. strike=False,
  117. italic=False,
  118. blink=False,
  119. reverse=False,
  120. hidden=False,
  121. )
  122. assert style.get_attrs_for_style_str("class:a class:b") == expected
  123. assert style.get_attrs_for_style_str("class:a,b") == expected
  124. assert style.get_attrs_for_style_str("class:a,b,c") == expected
  125. # Defining 'a' latest should give priority to 'a'.
  126. expected = Attrs(
  127. color="0000ff",
  128. bgcolor="",
  129. bold=False,
  130. underline=False,
  131. strike=False,
  132. italic=False,
  133. blink=False,
  134. reverse=False,
  135. hidden=False,
  136. )
  137. assert style.get_attrs_for_style_str("class:b class:a") == expected
  138. assert style.get_attrs_for_style_str("class:b,a") == expected
  139. def test_substyles():
  140. style = Style(
  141. [
  142. ("a.b", "#ff0000 bold"),
  143. ("a", "#0000ff"),
  144. ("b", "#00ff00"),
  145. ("b.c", "#0000ff italic"),
  146. ]
  147. )
  148. # Starting with a.*
  149. expected = Attrs(
  150. color="0000ff",
  151. bgcolor="",
  152. bold=False,
  153. underline=False,
  154. strike=False,
  155. italic=False,
  156. blink=False,
  157. reverse=False,
  158. hidden=False,
  159. )
  160. assert style.get_attrs_for_style_str("class:a") == expected
  161. expected = Attrs(
  162. color="ff0000",
  163. bgcolor="",
  164. bold=True,
  165. underline=False,
  166. strike=False,
  167. italic=False,
  168. blink=False,
  169. reverse=False,
  170. hidden=False,
  171. )
  172. assert style.get_attrs_for_style_str("class:a.b") == expected
  173. assert style.get_attrs_for_style_str("class:a.b.c") == expected
  174. # Starting with b.*
  175. expected = Attrs(
  176. color="00ff00",
  177. bgcolor="",
  178. bold=False,
  179. underline=False,
  180. strike=False,
  181. italic=False,
  182. blink=False,
  183. reverse=False,
  184. hidden=False,
  185. )
  186. assert style.get_attrs_for_style_str("class:b") == expected
  187. assert style.get_attrs_for_style_str("class:b.a") == expected
  188. expected = Attrs(
  189. color="0000ff",
  190. bgcolor="",
  191. bold=False,
  192. underline=False,
  193. strike=False,
  194. italic=True,
  195. blink=False,
  196. reverse=False,
  197. hidden=False,
  198. )
  199. assert style.get_attrs_for_style_str("class:b.c") == expected
  200. assert style.get_attrs_for_style_str("class:b.c.d") == expected
  201. def test_swap_light_and_dark_style_transformation():
  202. transformation = SwapLightAndDarkStyleTransformation()
  203. # Test with 6 digit hex colors.
  204. before = Attrs(
  205. color="440000",
  206. bgcolor="888844",
  207. bold=True,
  208. underline=True,
  209. strike=True,
  210. italic=True,
  211. blink=False,
  212. reverse=False,
  213. hidden=False,
  214. )
  215. after = Attrs(
  216. color="ffbbbb",
  217. bgcolor="bbbb76",
  218. bold=True,
  219. underline=True,
  220. strike=True,
  221. italic=True,
  222. blink=False,
  223. reverse=False,
  224. hidden=False,
  225. )
  226. assert transformation.transform_attrs(before) == after
  227. # Test with ANSI colors.
  228. before = Attrs(
  229. color="ansired",
  230. bgcolor="ansiblack",
  231. bold=True,
  232. underline=True,
  233. strike=True,
  234. italic=True,
  235. blink=False,
  236. reverse=False,
  237. hidden=False,
  238. )
  239. after = Attrs(
  240. color="ansibrightred",
  241. bgcolor="ansiwhite",
  242. bold=True,
  243. underline=True,
  244. strike=True,
  245. italic=True,
  246. blink=False,
  247. reverse=False,
  248. hidden=False,
  249. )
  250. assert transformation.transform_attrs(before) == after