replace irctokens.format() with irctokens.build().format()

This commit is contained in:
jesopo 2020-03-12 13:53:10 +00:00
parent 6370dbb90d
commit 89a2a6c131
4 changed files with 24 additions and 24 deletions

View File

@ -35,7 +35,7 @@ Hostmask(nickname='jess', username='~jess', hostname='hostname')
### formatting
```python
>>> irctokens.format("USER", ["user", "0", "*", "real name"])
>>> irctokens.build("USER", ["user", "0", "*", "real name"]).format()
'USER user 0 * :real name'
```
@ -63,8 +63,8 @@ def _send(line):
while e.pending():
e.pop(s.send(e.pending()))
_send(irctokens.format("USER", ["username", "0", "*", "real name"]))
_send(irctokens.format("NICK", [NICK]))
_send(irctokens.build("USER", ["username", "0", "*", "real name"]))
_send(irctokens.build("NICK", [NICK]))
while True:
lines = d.push(s.recv(1024))
@ -75,10 +75,10 @@ while True:
for line in lines:
print(f"< {line.format()}")
if line.command == "PING":
to_send = irctokens.format("PONG", [line.params[0]])
to_send = irctokens.build("PONG", [line.params[0]])
_send(to_send)
elif line.command == "001":
to_send = irctokens.format("JOIN", [CHAN])
to_send = irctokens.build("JOIN", [CHAN])
_send(to_send)
```

View File

@ -1,2 +1,2 @@
from .protocol import Line, tokenise, format, Hostmask
from .protocol import Line, tokenise, build, Hostmask
from .stateful import StatefulDecoder, StatefulEncoder

View File

@ -121,10 +121,10 @@ def tokenise(line: str) -> Line:
return line_obj
def format(
def build(
command: str,
params: List[str]=[],
source: Optional[str]=None,
tags: Optional[Dict[str, Optional[str]]]=None
) -> str:
return Line(tags, source, command, params).format()
) -> Line:
return Line(tags, source, command, params)

View File

@ -3,55 +3,55 @@ import irctokens
class FormatTestTags(unittest.TestCase):
def test(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello"],
tags={"id": "\\" + " " + ";" + "\r\n"})
line = irctokens.build("PRIVMSG", ["#channel", "hello"],
tags={"id": "\\" + " " + ";" + "\r\n"}).format()
self.assertEqual(line, "@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello")
def test_missing(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello"])
line = irctokens.build("PRIVMSG", ["#channel", "hello"]).format()
self.assertEqual(line, "PRIVMSG #channel hello")
def test_none_value(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello"],
tags={"a": None})
line = irctokens.build("PRIVMSG", ["#channel", "hello"],
tags={"a": None}).format()
self.assertEqual(line, "@a PRIVMSG #channel hello")
def test_empty_value(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello"],
tags={"a": ""})
line = irctokens.build("PRIVMSG", ["#channel", "hello"], tags={"a": ""}
).format()
self.assertEqual(line, "@a PRIVMSG #channel hello")
class FormatTestSource(unittest.TestCase):
def test(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello"],
source="nick!user@host")
line = irctokens.build("PRIVMSG", ["#channel", "hello"],
source="nick!user@host").format()
self.assertEqual(line, ":nick!user@host PRIVMSG #channel hello")
class FormatTestCommand(unittest.TestCase):
def test_lowercase(self):
line = irctokens.format("privmsg")
line = irctokens.build("privmsg").format()
self.assertEqual(line, "PRIVMSG")
class FormatTestTrailing(unittest.TestCase):
def test_space(self):
line = irctokens.format("PRIVMSG", ["#channel", "hello world"])
line = irctokens.build("PRIVMSG", ["#channel", "hello world"]).format()
self.assertEqual(line, "PRIVMSG #channel :hello world")
def test_no_space(self):
line = irctokens.format("PRIVMSG", ["#channel", "helloworld"])
line = irctokens.build("PRIVMSG", ["#channel", "helloworld"]).format()
self.assertEqual(line, "PRIVMSG #channel helloworld")
def test_double_colon(self):
line = irctokens.format("PRIVMSG", ["#channel", ":helloworld"])
line = irctokens.build("PRIVMSG", ["#channel", ":helloworld"]).format()
self.assertEqual(line, "PRIVMSG #channel ::helloworld")
class FormatTestInvalidParam(unittest.TestCase):
def test_non_last_space(self):
def _inner():
irctokens.format("USER", ["user", "0 *", "real name"])
irctokens.build("USER", ["user", "0 *", "real name"]).format()
self.assertRaises(ValueError, _inner)
def test_non_last_colon(self):
def _inner():
irctokens.format("PRIVMSG", [":#channel", "hello"])
irctokens.build("PRIVMSG", [":#channel", "hello"]).format()
self.assertRaises(ValueError, _inner)