From 29942d38bb88e4654879bcaff0a004c646e1a315 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 27 Oct 2014 20:01:22 +0100 Subject: Add some unit tests using py.test - we need to have more --- test/test_common.py | 72 ++++++++++++++++++++++++++++++++ test/test_config.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_poopt.py | 14 +++++++ test/test_windows.py | 76 ++++++++++++++++++++++++++++++++++ test/test_xhtml.py | 50 ++++++++++++++++++++++ 5 files changed, 326 insertions(+) create mode 100644 test/test_common.py create mode 100644 test/test_config.py create mode 100644 test/test_poopt.py create mode 100644 test/test_windows.py create mode 100644 test/test_xhtml.py (limited to 'test') 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 = "

" + end = "

" + text = '\x191}coucou' + assert poezio_colors_to_html(text) == base + 'coucou' + end + + text = '\x19bcoucou\x19o toto \x194}titi' + assert poezio_colors_to_html(text) == base + 'coucou toto titi' + end + +def test_xhtml_to_poezio_colors(): + start = b'

' + end = b'

' + xhtml = start + b'test' + end + assert xhtml_to_poezio_colors(xhtml) == 'test' + + xhtml = start + b'salut' + end + assert xhtml_to_poezio_colors(xhtml) == '\x19usalut\x19o (http://perdu.com)' + + xhtml = start + b'http://perdu.com' + end + assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o' + + with pytest.raises(xml.sax._exceptions.SAXParseException): + xhtml_to_poezio_colors(b'

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) == '' + -- cgit v1.2.3