summaryrefslogtreecommitdiff
path: root/test/test_common.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2014-10-31 19:15:57 +0100
committermathieui <mathieui@mathieui.net>2014-10-31 19:16:44 +0100
commit1c1ab3cb839e5509db52770e10c7190f844eb2e5 (patch)
treeb331060b3dc42651e61e4ccb2dfe6af5e2e97752 /test/test_common.py
parentcedc5a6ec80a46437f42464415fd1806049c593d (diff)
parentea2b703bfd07d293ba9fdd85ac657275d43da2a7 (diff)
downloadpoezio-1c1ab3cb839e5509db52770e10c7190f844eb2e5.tar.gz
poezio-1c1ab3cb839e5509db52770e10c7190f844eb2e5.tar.bz2
poezio-1c1ab3cb839e5509db52770e10c7190f844eb2e5.tar.xz
poezio-1c1ab3cb839e5509db52770e10c7190f844eb2e5.zip
Merge branch 'master' of git.poez.io:poezio into slix
Conflicts: src/bookmark.py src/config.py src/connection.py src/core/commands.py src/core/core.py src/core/handlers.py src/windows/info_bar.py src/windows/muc.py src/windows/roster_win.py src/windows/text_win.py src/xhtml.py
Diffstat (limited to 'test/test_common.py')
-rw-r--r--test/test_common.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/test_common.py b/test/test_common.py
new file mode 100644
index 00000000..315318bd
--- /dev/null
+++ b/test/test_common.py
@@ -0,0 +1,79 @@
+"""
+Test the functions in the `common` module
+"""
+
+import sys
+sys.path.append('src')
+
+import time
+import pytest
+import datetime
+from sleekxmpp import JID
+from datetime import timedelta
+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, safeJID)
+
+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 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'
+ assert parse_secs_to_str(0) == '0s'
+
+ with pytest.raises(TypeError):
+ parse_secs_to_str('toto')
+
+def test_safeJID():
+ assert safeJID('toto@titi/tata') == JID('toto@titi/tata')
+ assert safeJID('é_è') == JID('')