tests: Implement mock weechat module and add some color tests.
This commit is contained in:
parent
404f785f21
commit
fc6a6e9da9
5 changed files with 165 additions and 35 deletions
6
Makefile
6
Makefile
|
@ -1,7 +1,8 @@
|
||||||
.PHONY: install install-lib phony
|
.PHONY: install install-lib phony test
|
||||||
|
|
||||||
WEECHAT_HOME ?= $(HOME)/.weechat
|
WEECHAT_HOME ?= $(HOME)/.weechat
|
||||||
PREFIX ?= $(WEECHAT_HOME)
|
PREFIX ?= $(WEECHAT_HOME)
|
||||||
|
PYTHON ?= python
|
||||||
|
|
||||||
lib := $(patsubst matrix/%.py, $(DESTDIR)$(PREFIX)/python/matrix/%.py, \
|
lib := $(patsubst matrix/%.py, $(DESTDIR)$(PREFIX)/python/matrix/%.py, \
|
||||||
$(wildcard matrix/*.py))
|
$(wildcard matrix/*.py))
|
||||||
|
@ -15,3 +16,6 @@ phony:
|
||||||
|
|
||||||
$(DESTDIR)$(PREFIX)/python/matrix/%.py: matrix/%.py phony
|
$(DESTDIR)$(PREFIX)/python/matrix/%.py: matrix/%.py phony
|
||||||
install -Dm644 $< $@
|
install -Dm644 $< $@
|
||||||
|
|
||||||
|
test:
|
||||||
|
$(PYTHON) -m pytest
|
||||||
|
|
93
matrix/_weechat.py
Normal file
93
matrix/_weechat.py
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
def color(color_name):
|
||||||
|
# type: (str) -> str
|
||||||
|
# yapf: disable
|
||||||
|
weechat_base_colors = {
|
||||||
|
"black": "0",
|
||||||
|
"red": "1",
|
||||||
|
"green": "2",
|
||||||
|
"brown": "3",
|
||||||
|
"blue": "4",
|
||||||
|
"magenta": "5",
|
||||||
|
"cyan": "6",
|
||||||
|
"default": "7",
|
||||||
|
"gray": "8",
|
||||||
|
"lightred": "9",
|
||||||
|
"lightgreen": "10",
|
||||||
|
"yellow": "11",
|
||||||
|
"lightblue": "12",
|
||||||
|
"lightmagenta": "13",
|
||||||
|
"lightcyan": "14",
|
||||||
|
"white": "15"
|
||||||
|
}
|
||||||
|
|
||||||
|
escape_codes = []
|
||||||
|
reset_code = "0"
|
||||||
|
|
||||||
|
def make_fg_color(color):
|
||||||
|
return "38;5;{}".format(color)
|
||||||
|
|
||||||
|
def make_bg_color(color):
|
||||||
|
return "48;5;{}".format(color)
|
||||||
|
|
||||||
|
attributes = {
|
||||||
|
"bold": "1",
|
||||||
|
"-bold": "21",
|
||||||
|
"reverse": "27",
|
||||||
|
"-reverse": "21",
|
||||||
|
"italic": "3",
|
||||||
|
"-italic": "23",
|
||||||
|
"underline": "4",
|
||||||
|
"-underline": "24",
|
||||||
|
"reset": "0",
|
||||||
|
"resetcolor": "39"
|
||||||
|
}
|
||||||
|
|
||||||
|
short_attributes = {
|
||||||
|
"*": "1",
|
||||||
|
"!": "27",
|
||||||
|
"/": "3",
|
||||||
|
"_": "4"
|
||||||
|
}
|
||||||
|
|
||||||
|
colors = color_name.split(",", 2)
|
||||||
|
|
||||||
|
fg_color = colors.pop(0)
|
||||||
|
|
||||||
|
bg_color = colors.pop(0) if colors else ""
|
||||||
|
|
||||||
|
if fg_color in attributes:
|
||||||
|
escape_codes.append(attributes[fg_color])
|
||||||
|
else:
|
||||||
|
chars = list(fg_color)
|
||||||
|
|
||||||
|
for char in chars:
|
||||||
|
if char in short_attributes:
|
||||||
|
escape_codes.append(short_attributes[char])
|
||||||
|
elif char == "|":
|
||||||
|
reset_code = ""
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
stripped_color = fg_color.lstrip("*_|/!")
|
||||||
|
|
||||||
|
if stripped_color in weechat_base_colors:
|
||||||
|
escape_codes.append(
|
||||||
|
make_fg_color(weechat_base_colors[stripped_color]))
|
||||||
|
|
||||||
|
elif stripped_color.isdecimal():
|
||||||
|
num_color = int(stripped_color)
|
||||||
|
if num_color >= 0 and num_color < 256:
|
||||||
|
escape_codes.append(make_fg_color(stripped_color))
|
||||||
|
|
||||||
|
|
||||||
|
if bg_color in weechat_base_colors:
|
||||||
|
escape_codes.append(make_bg_color(weechat_base_colors[bg_color]))
|
||||||
|
else:
|
||||||
|
if bg_color.isdecimal():
|
||||||
|
num_color = int(bg_color)
|
||||||
|
if num_color >= 0 and num_color < 256:
|
||||||
|
escape_codes.append(make_bg_color(bg_color))
|
||||||
|
|
||||||
|
escape_string = "\033[{}{}m".format(reset_code, ";".join(escape_codes))
|
||||||
|
|
||||||
|
return escape_string
|
|
@ -542,6 +542,7 @@ def colour_find_rgb(r, g, b):
|
||||||
|
|
||||||
def color_html_to_weechat(color):
|
def color_html_to_weechat(color):
|
||||||
# type: (str) -> str
|
# type: (str) -> str
|
||||||
|
# yapf: disable
|
||||||
first_16 = {
|
first_16 = {
|
||||||
(0, 0, 0): "black", # 0
|
(0, 0, 0): "black", # 0
|
||||||
(128, 0, 0): "red", # 1
|
(128, 0, 0): "red", # 1
|
||||||
|
@ -553,12 +554,14 @@ def color_html_to_weechat(color):
|
||||||
(192, 192, 192): "default", # 7
|
(192, 192, 192): "default", # 7
|
||||||
(128, 128, 128): "gray", # 8
|
(128, 128, 128): "gray", # 8
|
||||||
(255, 0, 0): "lightred", # 9
|
(255, 0, 0): "lightred", # 9
|
||||||
(0, 255, 0): "lightgreen", # 11
|
(0, 255, 0): "lightgreen", # 10
|
||||||
(255, 255, 0): "yellow", # 12
|
(255, 255, 0): "yellow", # 11
|
||||||
(0, 0, 255): "lightblue", # 13
|
(0, 0, 255): "lightblue", # 12
|
||||||
(255, 0, 255): "lightmagenta", # 14
|
(255, 0, 255): "lightmagenta", # 13
|
||||||
(0, 255, 255): "lightcyan", # 15
|
(0, 255, 255): "lightcyan", # 14
|
||||||
|
(255, 255, 255): "white", # 15
|
||||||
}
|
}
|
||||||
|
# yapf: enable
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rgb_color = webcolors.html5_parse_legacy_color(color)
|
rgb_color = webcolors.html5_parse_legacy_color(color)
|
||||||
|
@ -573,6 +576,7 @@ def color_html_to_weechat(color):
|
||||||
|
|
||||||
def color_weechat_to_html(color):
|
def color_weechat_to_html(color):
|
||||||
# type: (str) -> str
|
# type: (str) -> str
|
||||||
|
# yapf: disable
|
||||||
first_16 = {
|
first_16 = {
|
||||||
"black": "black", # 0
|
"black": "black", # 0
|
||||||
"red": "maroon", # 1
|
"red": "maroon", # 1
|
||||||
|
@ -582,15 +586,15 @@ def color_weechat_to_html(color):
|
||||||
"magenta": "purple", # 5
|
"magenta": "purple", # 5
|
||||||
"cyan": "teal", # 6
|
"cyan": "teal", # 6
|
||||||
"default": "silver", # 7
|
"default": "silver", # 7
|
||||||
"gray": "grey", # 8
|
"gray": "gray", # 8
|
||||||
"lightred": "red", # 9
|
"lightred": "red", # 9
|
||||||
"lightgreen": "lime", # 11
|
"lightgreen": "lime", # 11
|
||||||
"yellow": "yellow", # 12
|
"yellow": "yellow", # 12
|
||||||
"lightblue": "fuchsia", # 13
|
"lightblue": "blue", # 13
|
||||||
"lightmagenta": "aqua", # 14
|
"lightmagenta": "fuchsia", # 14
|
||||||
"lightcyan": "white", # 15
|
"lightcyan": "aqua", # 15
|
||||||
|
"white": "white", # 15
|
||||||
}
|
}
|
||||||
#yapf: disable
|
|
||||||
hex_colors = {
|
hex_colors = {
|
||||||
"0": "#000000",
|
"0": "#000000",
|
||||||
"1": "#800000",
|
"1": "#800000",
|
||||||
|
|
|
@ -21,9 +21,12 @@ import sys
|
||||||
from matrix.utf import WeechatWrapper
|
from matrix.utf import WeechatWrapper
|
||||||
from matrix.plugin_options import PluginOptions
|
from matrix.plugin_options import PluginOptions
|
||||||
|
|
||||||
import weechat
|
try:
|
||||||
|
import weechat
|
||||||
W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat)
|
W = weechat if sys.hexversion >= 0x3000000 else WeechatWrapper(weechat)
|
||||||
|
except ImportError:
|
||||||
|
import matrix._weechat as weechat
|
||||||
|
W = weechat
|
||||||
|
|
||||||
OPTIONS = PluginOptions() # type: PluginOptions
|
OPTIONS = PluginOptions() # type: PluginOptions
|
||||||
SERVERS = dict() # type: Dict[str, MatrixServer]
|
SERVERS = dict() # type: Dict[str, MatrixServer]
|
||||||
|
|
26
tests/color_test.py
Normal file
26
tests/color_test.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import webcolors
|
||||||
|
from hypothesis import given
|
||||||
|
from hypothesis.strategies import sampled_from
|
||||||
|
|
||||||
|
from matrix.colors import (Formatted, color_html_to_weechat,
|
||||||
|
color_weechat_to_html)
|
||||||
|
|
||||||
|
html_prism = ("<font color=maroon>T</font><font color=red>e</font><font "
|
||||||
|
"color=olive>s</font><font color=yellow>t</font>")
|
||||||
|
|
||||||
|
weechat_prism = (
|
||||||
|
u"\x1b[038;5;1mT\x1b[039m\x1b[038;5;9me\x1b[039m\x1b[038;5;3ms\x1b[039m\x1b[038;5;11mt\x1b[039m"
|
||||||
|
)
|
||||||
|
|
||||||
|
first_16_html_colors = list(webcolors.HTML4_HEX_TO_NAMES.values())
|
||||||
|
|
||||||
|
|
||||||
|
def test_prism():
|
||||||
|
formatted = Formatted.from_html(html_prism)
|
||||||
|
assert formatted.to_weechat() == weechat_prism
|
||||||
|
|
||||||
|
|
||||||
|
@given(sampled_from(first_16_html_colors))
|
||||||
|
def test_color_conversion(color_name):
|
||||||
|
assert color_weechat_to_html(
|
||||||
|
color_html_to_weechat(color_name)) == color_name
|
Loading…
Reference in a new issue