summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--src/common.py64
-rw-r--r--test/test_common.py72
-rw-r--r--test/test_config.py114
-rw-r--r--test/test_poopt.py14
-rw-r--r--test/test_windows.py76
-rw-r--r--test/test_xhtml.py50
7 files changed, 336 insertions, 60 deletions
diff --git a/Makefile b/Makefile
index 1782fc9c..5b11efa6 100644
--- a/Makefile
+++ b/Makefile
@@ -32,6 +32,10 @@ uninstall:
doc:
make -C doc/ html
+
+test:
+ py.test test/
+
pot:
xgettext src/*.py --from-code=utf-8 --keyword=_ -o locale/poezio.pot
@@ -45,4 +49,4 @@ release:
tar cJf poezio-$(version).tar.xz poezio-$(version) && \
tar czf poezio-$(version).tar.gz poezio-$(version)
-.PHONY : doc
+.PHONY : doc test
diff --git a/src/common.py b/src/common.py
index 20700258..8cb02d4c 100644
--- a/src/common.py
+++ b/src/common.py
@@ -188,17 +188,6 @@ def datetime_tuple(timestamp):
:param str timestamp: The string containing the formatted date.
:return: The date.
:rtype: :py:class:`datetime.datetime`
-
- >>> time.timezone = 0; time.altzone = 0
- >>> datetime_tuple('20130226T06:23:12')
- datetime.datetime(2013, 2, 26, 6, 23, 12)
- >>> datetime_tuple('2013-02-26T06:23:12+02:00')
- datetime.datetime(2013, 2, 26, 4, 23, 12)
- >>> time.timezone = -3600; time.altzone = -3600
- >>> datetime_tuple('20130226T07:23:12')
- datetime.datetime(2013, 2, 26, 8, 23, 12)
- >>> datetime_tuple('2013-02-26T07:23:12+02:00')
- datetime.datetime(2013, 2, 26, 6, 23, 12)
"""
timestamp = timestamp.replace('-', '', 2).replace(':', '')
date = timestamp[:15]
@@ -227,15 +216,10 @@ def datetime_tuple(timestamp):
def get_utc_time(local_time=None):
"""
- Get the current time in UTC
+ Get the current UTC time
:param datetime local_time: The current local time
:return: The current UTC time
- >>> delta = timedelta(seconds=-3600)
- >>> d = datetime.now()
- >>> time.timezone = -3600; time.altzone = -3600
- >>> get_utc_time(local_time=d) == d + delta
- True
"""
if local_time is None:
local_time = datetime.now()
@@ -258,12 +242,6 @@ def get_utc_time(local_time=None):
def get_local_time(utc_time):
"""
Get the local time from an UTC time
-
- >>> delta = timedelta(seconds=-3600)
- >>> d = datetime.now()
- >>> time.timezone = -3600; time.altzone = -3600
- >>> get_local_time(d) == d - delta
- True
"""
if OLD_PYTHON:
isdst = time.localtime(int(utc_time.strftime("%s"))).tm_isdst
@@ -315,16 +293,6 @@ def shell_split(st):
>>> shell_split('"sdf 1" "toto 2"')
['sdf 1', 'toto 2']
- >>> shell_split('toto "titi"')
- ['toto', 'titi']
- >>> shell_split('toto ""')
- ['toto', '']
- >>> shell_split('to"to titi "a" b')
- ['to"to', 'titi', 'a', 'b']
- >>> shell_split('"toto titi" toto ""')
- ['toto titi', 'toto', '']
- >>> shell_split('toto "titi')
- ['toto', 'titi']
"""
sh = shlex.shlex(st)
ret = []
@@ -358,18 +326,8 @@ def find_argument(pos, text, quoted=True):
def find_argument_quoted(pos, text):
"""
- >>> find_argument_quoted(4, 'toto titi tata')
- 3
- >>> find_argument_quoted(4, '"toto titi" tata')
- 0
- >>> find_argument_quoted(8, '"toto" "titi tata"')
- 1
- >>> find_argument_quoted(8, '"toto" "titi tata')
- 1
- >>> find_argument_quoted(3, '"toto" "titi tata')
- 0
- >>> find_argument_quoted(18, '"toto" "titi tata" ')
- 2
+ Get the number of the argument at position pos in
+ a string with possibly quoted text.
"""
sh = shlex.shlex(text)
count = -1
@@ -384,16 +342,8 @@ def find_argument_quoted(pos, text):
def find_argument_unquoted(pos, text):
"""
- >>> find_argument_unquoted(2, 'toto titi tata')
- 0
- >>> find_argument_unquoted(3, 'toto titi tata')
- 0
- >>> find_argument_unquoted(6, 'toto titi tata')
- 1
- >>> find_argument_unquoted(4, 'toto titi tata')
- 3
- >>> find_argument_unquoted(25, 'toto titi tata')
- 3
+ Get the number of the argument at position pos in
+ a string without interpreting quotes.
"""
ret = text.split()
search = 0
@@ -531,7 +481,3 @@ def safeJID(*args, **kwargs):
except InvalidJID:
return JID('')
-
-if __name__ == "__main__":
- import doctest
- doctest.testmod()
diff --git a/test/test_common.py b/test/test_common.py
new file mode 100644
index 00000000..36680328
--- /dev/null
+++ b/test/test_common.py
@@ -0,0 +1,72 @@
+"""
+Test the functions in the `common` module
+"""
+
+import pytest
+import sys
+sys.path.append('src')
+
+from common import (datetime_tuple, get_utc_time, get_local_time, shell_split,
+ find_argument_quoted, find_argument_unquoted,
+ parse_str_to_secs, parse_secs_to_str)
+import time
+from datetime import timedelta
+import datetime
+
+def test_datetime_tuple():
+ time.timezone = 0
+ time.altzone = 0
+
+ assert datetime_tuple('20130226T06:23:12') == datetime.datetime(2013, 2, 26, 6, 23, 12)
+ assert datetime_tuple('2013-02-26T06:23:12+02:00') == datetime.datetime(2013, 2, 26, 4, 23, 12)
+
+ time.timezone = -3600
+ time.altzone = -3600
+
+ assert datetime_tuple('20130226T07:23:12') == datetime.datetime(2013, 2, 26, 8, 23, 12)
+ assert datetime_tuple('2013-02-26T07:23:12+02:00') == datetime.datetime(2013, 2, 26, 6, 23, 12)
+
+def test_utc_time():
+ delta = timedelta(seconds=-3600)
+ d = datetime.datetime.now()
+ time.timezone = -3600; time.altzone = -3600
+ assert get_utc_time(local_time=d) == d + delta
+
+def test_local_time():
+ delta = timedelta(seconds=-3600)
+ d = datetime.datetime.now()
+ time.timezone = -3600
+ time.altzone = -3600
+ assert get_local_time(d) == d - delta
+
+#def find_delayed_tag(message):
+
+def test_shell_split():
+ assert shell_split('"sdf 1" "toto 2"') == ['sdf 1', 'toto 2']
+ assert shell_split('toto "titi"') == ['toto', 'titi']
+ assert shell_split('toto ""') == ['toto', '']
+ assert shell_split('to"to titi "a" b') == ['to"to', 'titi', 'a', 'b']
+ assert shell_split('"toto titi" toto ""') == ['toto titi', 'toto', '']
+ assert shell_split('toto "titi') == ['toto', 'titi']
+
+def test_argument_quoted():
+ assert find_argument_quoted(4, 'toto titi tata') == 3
+ assert find_argument_quoted(4, '"toto titi" tata') == 0
+ assert find_argument_quoted(8, '"toto" "titi tata"') == 1
+ assert find_argument_quoted(8, '"toto" "titi tata') == 1
+ assert find_argument_quoted(3, '"toto" "titi tata') == 0
+ assert find_argument_quoted(18, '"toto" "titi tata" ') == 2
+
+def test_argument_unquoted():
+ assert find_argument_unquoted(2, 'toto titi tata') == 0
+ assert find_argument_unquoted(3, 'toto titi tata') == 0
+ assert find_argument_unquoted(6, 'toto titi tata') == 1
+ assert find_argument_unquoted(4, 'toto titi tata') == 3
+ assert find_argument_unquoted(25, 'toto titi tata') == 3
+
+def test_parse_str_to_secs():
+ assert parse_str_to_secs("1d3m1h") == 90180
+ assert parse_str_to_secs("1d3mfaiiiiil") == 0
+
+def test_parse_secs_to_str():
+ assert parse_secs_to_str(3601) == '1h1s'
diff --git a/test/test_config.py b/test/test_config.py
new file mode 100644
index 00000000..f8d06258
--- /dev/null
+++ b/test/test_config.py
@@ -0,0 +1,114 @@
+"""
+Test the config module
+"""
+
+import tempfile
+import pytest
+import sys
+import os
+
+
+sys.path.append('src')
+
+import config
+
+@pytest.yield_fixture(scope="module")
+def config_obj():
+ file_ = tempfile.NamedTemporaryFile(delete=False)
+ conf = config.Config(file_name=file_.name)
+ yield conf
+ del conf
+ os.unlink(file_.name)
+
+class TestConfigSimple(object):
+ def test_get_set(self, config_obj):
+ config_obj.set_and_save('test', value='coucou')
+ config_obj.set_and_save('test2', value='true')
+ assert config_obj.get('test') == 'coucou'
+ assert config_obj.get('test2') == 'true'
+ assert config_obj.get('toto') == ''
+
+ def test_file_content(self, config_obj):
+ with open(config_obj.file_name, 'r') as fd:
+ data = fd.read()
+ supposed_content = '[Poezio]\ntest = coucou\ntest2 = true\n'
+ assert data == supposed_content
+
+ def test_get_types(self, config_obj):
+
+ config_obj.set_and_save('test_int', '99')
+ config_obj.set_and_save('test_int_neg', '-1')
+ config_obj.set_and_save('test_bool_t', 'true')
+ config_obj.set_and_save('test_bool_f', 'false')
+ config_obj.set_and_save('test_float', '1.5')
+
+ assert config_obj.get('test_int', default=0) == 99
+ assert config_obj.get('test_int_neg', default=0) == -1
+ assert config_obj.get('test_bool_t', default=False) == True
+ assert config_obj.get('test_bool_f', default=True) == False
+ assert config_obj.get('test_float', default=1.0) == 1.5
+
+ def test_remove(self, config_obj):
+ with open(config_obj.file_name, 'r') as fd:
+ data = fd.read()
+
+ supposed_content = ('[Poezio]\ntest = coucou\ntest2 = true\n'
+ 'test_int = 99\ntest_int_neg = -1\ntest_bool_t ='
+ ' true\ntest_bool_f = false\ntest_float = 1.5\n')
+
+ assert data == supposed_content
+
+ config_obj.remove_and_save('test_int')
+ config_obj.remove_and_save('test_int_neg')
+ config_obj.remove_and_save('test_bool_t')
+ config_obj.remove_and_save('test_bool_f')
+ config_obj.remove_and_save('test_float')
+
+ with open(config_obj.file_name, 'r') as fd:
+ data = fd.read()
+
+ supposed_content = '[Poezio]\ntest = coucou\ntest2 = true\n'
+
+ assert data == supposed_content
+
+
+ def test_toggle(self, config_obj):
+ config_obj.set_and_save('test2', value='toggle')
+ assert config_obj.get('test2') == 'false'
+ config_obj.set_and_save('test2', value='toggle')
+ assert config_obj.get('test2') == 'true'
+
+ def test_get_set_default(self, config_obj):
+ assert config_obj.get('doesnotexist', 'toto@tata') == 'toto@tata'
+ assert config_obj.get('doesnotexist2', '1234') == '1234'
+
+class TestConfigSections(object):
+ def test_set_section(self, config_obj):
+ config_obj.set_and_save('option1', 'test', section='NotPoezio')
+ config_obj.set_and_save('option2', 'test2', section='NotPoezio')
+
+ assert config_obj.get('option1', section='NotPoezio') == 'test'
+ assert config_obj.get('option2', section='NotPoezio') == 'test2'
+
+ def test_file_content(self, config_obj):
+ with open(config_obj.file_name, 'r') as fd:
+ data = fd.read()
+ supposed_content = ('[Poezio]\ntest = coucou\ntest2 = true\n'
+ '[NotPoezio]\noption1 = test\noption2 = test2\n')
+ assert data == supposed_content
+
+class TestTabNames(object):
+ def test_get_tabname(self, config_obj):
+ config.post_logging_setup()
+ config_obj.set_and_save('test', value='value.toto@toto.com',
+ section='toto@toto.com')
+ config_obj.set_and_save('test2', value='value2@toto.com',
+ section='@toto.com')
+
+ assert config_obj.get_by_tabname('test', 'toto@toto.com') == 'value.toto@toto.com'
+ assert config_obj.get_by_tabname('test2', 'toto@toto.com') == 'value2@toto.com'
+ assert config_obj.get_by_tabname('test2', 'toto@toto.com', fallback=False) == 'value2@toto.com'
+ assert config_obj.get_by_tabname('test2', 'toto@toto.com', fallback_server=False) == 'true'
+ assert config_obj.get_by_tabname('test_int', 'toto@toto.com', fallback=False) == ''
+
+
diff --git a/test/test_poopt.py b/test/test_poopt.py
new file mode 100644
index 00000000..9b640ff0
--- /dev/null
+++ b/test/test_poopt.py
@@ -0,0 +1,14 @@
+"""
+Test of the poopt module
+"""
+
+import pytest
+import sys
+sys.path.append('src')
+
+from poopt import cut_text
+
+def test_cut_text():
+
+ text = '12345678901234567890'
+ assert cut_text(text, 5) == [(0, 5), (5, 10), (10, 15), (15, 20)]
diff --git a/test/test_windows.py b/test/test_windows.py
new file mode 100644
index 00000000..8fb85295
--- /dev/null
+++ b/test/test_windows.py
@@ -0,0 +1,76 @@
+import pytest
+import sys
+sys.path.append('src')
+
+class ConfigShim(object):
+ def get(self, *args, **kwargs):
+ return ''
+
+import config
+config.config = ConfigShim()
+import core
+
+from windows import Input, HistoryInput, MessageInput, CommandInput
+
+@pytest.fixture
+def input():
+ input = Input()
+ input.rewrite_text = lambda: None
+ return input
+
+class TestInput(object):
+
+ def test_do_command(self, input):
+
+ input.do_command('a')
+ assert input.text == 'a'
+
+ for char in 'coucou':
+ input.do_command(char)
+ assert input.text == 'acoucou'
+
+ def test_empty(self, input):
+ assert input.is_empty() == True
+ input.do_command('a')
+ assert input.is_empty() == False
+
+ def test_key_left(self, input):
+ for char in 'this is a line':
+ input.do_command(char)
+ for i in range(4):
+ input.key_left()
+ for char in 'long ':
+ input.do_command(char)
+
+ assert input.text == 'this is a long line'
+
+ def test_key_right(self, input):
+ for char in 'this is a line':
+ input.do_command(char)
+ for i in range(4):
+ input.key_left()
+ input.key_right()
+
+ for char in 'iii':
+ input.do_command(char)
+
+ assert input.text == 'this is a liiiine'
+
+ def test_key_home(self, input):
+ for char in 'this is a line of text':
+ input.do_command(char)
+ input.do_command('z')
+ input.key_home()
+ input.do_command('a')
+
+ assert input.text == 'athis is a line of textz'
+
+ def test_key_end(self, input):
+ for char in 'this is a line of text':
+ input.do_command(char)
+ input.key_home()
+ input.key_end()
+ input.do_command('z')
+
+ assert input.text == 'this is a line of textz'
+
diff --git a/test/test_xhtml.py b/test/test_xhtml.py
new file mode 100644
index 00000000..58857d67
--- /dev/null
+++ b/test/test_xhtml.py
@@ -0,0 +1,50 @@
+"""
+Test the functions in the `xhtml` module
+"""
+
+import pytest
+import sys
+import xml
+sys.path.append('src')
+
+from xhtml import (poezio_colors_to_html, xhtml_to_poezio_colors,
+ parse_css, clean_text)
+
+def test_clean_text():
+ example_string = '\x191}Toto \x192,-1}titi\x19b Tata'
+ assert clean_text(example_string) == 'Toto titi Tata'
+
+ clean_string = 'toto titi tata'
+ assert clean_text(clean_string) == clean_string
+
+def test_poezio_colors_to_html():
+ base = "<body xmlns='http://www.w3.org/1999/xhtml'><p>"
+ end = "</p></body>"
+ text = '\x191}coucou'
+ assert poezio_colors_to_html(text) == base + '<span style="color: red;">coucou</span>' + end
+
+ text = '\x19bcoucou\x19o toto \x194}titi'
+ assert poezio_colors_to_html(text) == base + '<span style="font-weight: bold;">coucou</span> toto <span style="color: blue;">titi</span>' + end
+
+def test_xhtml_to_poezio_colors():
+ start = b'<body xmlns="http://www.w3.org/1999/xhtml"><p>'
+ end = b'</p></body>'
+ xhtml = start + b'test' + end
+ assert xhtml_to_poezio_colors(xhtml) == 'test'
+
+ xhtml = start + b'<a href="http://perdu.com">salut</a>' + end
+ assert xhtml_to_poezio_colors(xhtml) == '\x19usalut\x19o (http://perdu.com)'
+
+ xhtml = start + b'<a href="http://perdu.com">http://perdu.com</a>' + end
+ assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o'
+
+ with pytest.raises(xml.sax._exceptions.SAXParseException):
+ xhtml_to_poezio_colors(b'<p>Invalid xml')
+
+def test_parse_css():
+ example_css = 'text-decoration: underline; color: red;'
+ assert parse_css(example_css) == '\x19u\x19196}'
+
+ example_css = 'text-decoration: underline coucou color: red;'
+ assert parse_css(example_css) == ''
+