test_style.py 6.6 KB

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