-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework
motd
field, and upgrade parser
This is the base, and draft. Just would like to hear feedback
- Loading branch information
1 parent
223d6f2
commit 43db85e
Showing
6 changed files
with
248 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import pytest | ||
|
||
from mcstatus.motd import Formatting, MinecraftColor, Motd, RawHTMLColor | ||
|
||
|
||
class TestRawHTMLColor: | ||
@pytest.mark.parametrize( | ||
"hex,rgb", | ||
[ | ||
("#bfff00", (191, 255, 0)), | ||
("#00ff80", (0, 255, 128)), | ||
("#4000ff", (64, 0, 255)), | ||
], | ||
) | ||
def test_hex_to_rgb_correct(self, hex, rgb): | ||
assert RawHTMLColor(hex).rgb == rgb | ||
|
||
def test_hex_in_output_has_number_sign(self): | ||
assert RawHTMLColor("#bfff00").hex.startswith("#") | ||
|
||
def test_fail_on_incorrect_hex(self): | ||
with pytest.raises(ValueError): | ||
RawHTMLColor("abcd") | ||
|
||
|
||
class TestMotdParse: | ||
@pytest.fixture | ||
def source(self): | ||
return { | ||
"extra": [ | ||
{"text": " "}, | ||
{"strikethrough": True, "color": "#b3eeff", "text": "="}, | ||
{"strikethrough": True, "color": "#b9ecff", "text": "="}, | ||
{"strikethrough": True, "color": "#c0eaff", "text": "="}, | ||
{"strikethrough": True, "color": "#c7e8ff", "text": "="}, | ||
{"strikethrough": True, "color": "#cee6ff", "text": "="}, | ||
{"strikethrough": True, "color": "#d5e4ff", "text": "="}, | ||
{"strikethrough": True, "color": "#dce2ff", "text": "="}, | ||
{"strikethrough": True, "color": "#e3e0ff", "text": "="}, | ||
{"strikethrough": True, "color": "#eadeff", "text": "="}, | ||
{"strikethrough": True, "color": "#f1dcff", "text": "="}, | ||
{"strikethrough": True, "color": "#f8daff", "text": "="}, | ||
{"strikethrough": True, "color": "#ffd9ff", "text": "="}, | ||
{"strikethrough": True, "color": "#f4dcff", "text": "="}, | ||
{"strikethrough": True, "color": "#f9daff", "text": "="}, | ||
{"strikethrough": True, "color": "#ffd9ff", "text": "="}, | ||
{"color": "white", "text": " "}, | ||
{"bold": True, "color": "#66ff99", "text": "C"}, | ||
{"bold": True, "color": "#75f5a2", "text": "r"}, | ||
{"bold": True, "color": "#84ebab", "text": "e"}, | ||
{"bold": True, "color": "#93e2b4", "text": "a"}, | ||
{"bold": True, "color": "#a3d8bd", "text": "t"}, | ||
{"bold": True, "color": "#b2cfc6", "text": "i"}, | ||
{"bold": True, "color": "#c1c5cf", "text": "v"}, | ||
{"bold": True, "color": "#d1bbd8", "text": "e"}, | ||
{"bold": True, "color": "#e0b2e1", "text": "F"}, | ||
{"bold": True, "color": "#efa8ea", "text": "u"}, | ||
{"bold": True, "color": "#ff9ff4", "text": "n "}, | ||
{"strikethrough": True, "color": "#b3eeff", "text": "="}, | ||
{"strikethrough": True, "color": "#b9ecff", "text": "="}, | ||
{"strikethrough": True, "color": "#c0eaff", "text": "="}, | ||
{"strikethrough": True, "color": "#c7e8ff", "text": "="}, | ||
{"strikethrough": True, "color": "#cee6ff", "text": "="}, | ||
{"strikethrough": True, "color": "#d5e4ff", "text": "="}, | ||
{"strikethrough": True, "color": "#dce2ff", "text": "="}, | ||
{"strikethrough": True, "color": "#e3e0ff", "text": "="}, | ||
{"strikethrough": True, "color": "#eadeff", "text": "="}, | ||
{"strikethrough": True, "color": "#f1dcff", "text": "="}, | ||
{"strikethrough": True, "color": "#f8daff", "text": "="}, | ||
{"strikethrough": True, "color": "#ffd9ff", "text": "="}, | ||
{"strikethrough": True, "color": "#f4dcff", "text": "="}, | ||
{"strikethrough": True, "color": "#f9daff", "text": "="}, | ||
{"strikethrough": True, "color": "#ffd9ff", "text": "="}, | ||
{"color": "white", "text": " \n "}, | ||
{"bold": True, "color": "#E5E5E5", "text": "The server has been updated to "}, | ||
{"bold": True, "color": "#97ABFF", "text": "1.17.1"}, | ||
], | ||
"text": "", | ||
} | ||
|
||
def test_correct_result(self, source): | ||
assert Motd.parse(source) == Motd( | ||
[ | ||
# fmt: off | ||
" ", | ||
RawHTMLColor(hex="#b3eeff", rgb=(179, 238, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#b9ecff", rgb=(185, 236, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#c0eaff", rgb=(192, 234, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#c7e8ff", rgb=(199, 232, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#cee6ff", rgb=(206, 230, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#d5e4ff", rgb=(213, 228, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#dce2ff", rgb=(220, 226, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#e3e0ff", rgb=(227, 224, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#eadeff", rgb=(234, 222, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f1dcff", rgb=(241, 220, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f8daff", rgb=(248, 218, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#ffd9ff", rgb=(255, 217, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f4dcff", rgb=(244, 220, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f9daff", rgb=(249, 218, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#ffd9ff", rgb=(255, 217, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
MinecraftColor.WHITE, " ", Formatting.RESET, | ||
RawHTMLColor(hex="#66ff99", rgb=(102, 255, 153)), Formatting.BOLD, "C", Formatting.RESET, | ||
RawHTMLColor(hex="#75f5a2", rgb=(117, 245, 162)), Formatting.BOLD, "r", Formatting.RESET, | ||
RawHTMLColor(hex="#84ebab", rgb=(132, 235, 171)), Formatting.BOLD, "e", Formatting.RESET, | ||
RawHTMLColor(hex="#93e2b4", rgb=(147, 226, 180)), Formatting.BOLD, "a", Formatting.RESET, | ||
RawHTMLColor(hex="#a3d8bd", rgb=(163, 216, 189)), Formatting.BOLD, "t", Formatting.RESET, | ||
RawHTMLColor(hex="#b2cfc6", rgb=(178, 207, 198)), Formatting.BOLD, "i", Formatting.RESET, | ||
RawHTMLColor(hex="#c1c5cf", rgb=(193, 197, 207)), Formatting.BOLD, "v", Formatting.RESET, | ||
RawHTMLColor(hex="#d1bbd8", rgb=(209, 187, 216)), Formatting.BOLD, "e", Formatting.RESET, | ||
RawHTMLColor(hex="#e0b2e1", rgb=(224, 178, 225)), Formatting.BOLD, "F", Formatting.RESET, | ||
RawHTMLColor(hex="#efa8ea", rgb=(239, 168, 234)), Formatting.BOLD, "u", Formatting.RESET, | ||
RawHTMLColor(hex="#ff9ff4", rgb=(255, 159, 244)), Formatting.BOLD, "n ", Formatting.RESET, | ||
RawHTMLColor(hex="#b3eeff", rgb=(179, 238, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#b9ecff", rgb=(185, 236, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#c0eaff", rgb=(192, 234, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#c7e8ff", rgb=(199, 232, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#cee6ff", rgb=(206, 230, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#d5e4ff", rgb=(213, 228, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#dce2ff", rgb=(220, 226, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#e3e0ff", rgb=(227, 224, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#eadeff", rgb=(234, 222, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f1dcff", rgb=(241, 220, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f8daff", rgb=(248, 218, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#ffd9ff", rgb=(255, 217, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f4dcff", rgb=(244, 220, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#f9daff", rgb=(249, 218, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
RawHTMLColor(hex="#ffd9ff", rgb=(255, 217, 255)), Formatting.STRIKETHROUGH, "=", Formatting.RESET, | ||
MinecraftColor.WHITE, " \n ", Formatting.RESET, | ||
RawHTMLColor(hex="#E5E5E5", rgb=(229, 229, 229)), Formatting.BOLD, | ||
"The server has been updated to ", Formatting.RESET, | ||
RawHTMLColor(hex="#97ABFF", rgb=(151, 171, 255)), Formatting.BOLD, "1.17.1", Formatting.RESET, | ||
"", | ||
# fmt: on | ||
] | ||
) | ||
|
||
@pytest.mark.parametrize("bedrock", (True, False)) | ||
def test_bedrock_parameter_nothing_changes(self, bedrock: bool): | ||
assert Motd.parse([{"color": "minecoin_gold", "text": " "}], bedrock=bedrock).raw == [ | ||
MinecraftColor.MINECOIN_GOLD, | ||
" ", | ||
Formatting.RESET, | ||
"", | ||
] | ||
|
||
@pytest.mark.parametrize("bedrock", (True, False)) | ||
def test_parse_as_str_ignore_minecoin_gold_on_java(self, bedrock: bool): | ||
assert Motd.parse("&g", bedrock=bedrock).raw == [MinecraftColor.MINECOIN_GOLD if bedrock else "&g"] | ||
|
||
@pytest.mark.parametrize("input", ["", [], {"extra": [], "text": ""}]) | ||
def test_empty_input_also_empty_raw(self, input): | ||
assert Motd.parse(input).raw == [] if isinstance(input, str) else [""] | ||
|
||
@pytest.mark.parametrize("plain_motd", ["&1&2&3", "§123§5bc", "§1§2§3"]) | ||
def test_plain_return_the_same(self, plain_motd: str): | ||
assert Motd.parse(plain_motd).plain == plain_motd.replace("&", "§") | ||
|
||
def test_plain_skip_html_colors(self): | ||
assert Motd.parse({"extra": [{"color": "#4000ff", "text": "colored text"}], "text": ""}).plain == "colored text§r" | ||
|
||
@pytest.mark.parametrize("bedrock", (True, False)) | ||
def test_html_return_correct(self, bedrock: bool): | ||
assert Motd.parse( | ||
{ | ||
"extra": [ | ||
{"text": "1"}, | ||
{"color": "#b3eeff", "text": "2"}, | ||
{"obfuscated": True, "color": "black", "text": "3"}, | ||
{"bold": True, "strikethrough": True, "color": "dark_blue", "text": "4"}, | ||
{"italic": True, "color": "dark_green", "text": "5"}, | ||
{"underlined": True, "color": "dark_aqua", "text": "6"}, | ||
{"color": "dark_aqua", "text": "7"}, | ||
{"color": "dark_red", "text": "8"}, | ||
{"color": "dark_purple", "text": "9"}, | ||
{"color": "gold", "text": "10"}, | ||
{"color": "gray", "text": "11"}, | ||
{"color": "dark_gray", "text": "12"}, | ||
{"color": "blue", "text": "13"}, | ||
{"color": "green", "text": "14"}, | ||
{"color": "aqua", "text": "15"}, | ||
{"color": "red", "text": "16"}, | ||
{"color": "light_purple", "text": "17"}, | ||
{"color": "yellow", "text": "18"}, | ||
{"color": "white", "text": "19"}, | ||
{"color": "minecoin_gold", "text": "20"}, | ||
], | ||
"text": "", | ||
}, | ||
bedrock=bedrock, | ||
).html == ( | ||
"<p>1" | ||
"<span style='color:#b3eeff'>2</span>" | ||
"<span style='color:#000000;text-shadow:0 0 1px #000000'><span class=obfuscated>3</span></span>" | ||
"<span style='color:#0000AA;text-shadow:0 0 1px #00002A'><b><s>4</span></b></s>" | ||
"<span style='color:#00AA00;text-shadow:0 0 1px #002A00'><i>5</span></i>" | ||
"<span style='color:#00AAAA;text-shadow:0 0 1px #002A2A'><u>6</span></u>" | ||
"<span style='color:#00AAAA;text-shadow:0 0 1px #002A2A'>7</span>" | ||
"<span style='color:#AA0000;text-shadow:0 0 1px #AA0000'>8</span>" | ||
"<span style='color:#AA00AA;text-shadow:0 0 1px #2A002A'>9</span>" | ||
"<span style='color:#FFAA00;text-shadow:0 0 1px #" + ("402A00" if bedrock else "2A2A00") + "'>10</span>" | ||
"<span style='color:#AAAAAA;text-shadow:0 0 1px #2A2A2A'>11</span>" | ||
"<span style='color:#555555;text-shadow:0 0 1px #151515'>12</span>" | ||
"<span style='color:#5555FF;text-shadow:0 0 1px #15153F'>13</span>" | ||
"<span style='color:#55FF55;text-shadow:0 0 1px #153F15'>14</span>" | ||
"<span style='color:#55FFFF;text-shadow:0 0 1px #153F3F'>15</span>" | ||
"<span style='color:#FF5555;text-shadow:0 0 1px #3F1515'>16</span>" | ||
"<span style='color:#FF55FF;text-shadow:0 0 1px #3F153F'>17</span>" | ||
"<span style='color:#FFFF55;text-shadow:0 0 1px #3F3F15'>18</span>" | ||
"<span style='color:#FFFFFF;text-shadow:0 0 1px #3F3F3F'>19</span>" | ||
"<span style='color:#DDD605;text-shadow:0 0 1px #373501'>20</span>" | ||
"</p>" | ||
) |
Oops, something went wrong.