summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_common.py13
-rw-r--r--test/test_completion.py167
-rw-r--r--test/test_xhtml.py8
3 files changed, 175 insertions, 13 deletions
diff --git a/test/test_common.py b/test/test_common.py
index 50643733..0b2bd279 100644
--- a/test/test_common.py
+++ b/test/test_common.py
@@ -14,19 +14,6 @@ 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()
diff --git a/test/test_completion.py b/test/test_completion.py
new file mode 100644
index 00000000..0e6e0492
--- /dev/null
+++ b/test/test_completion.py
@@ -0,0 +1,167 @@
+"""
+Test the completions methods on an altered input object.
+"""
+
+import string
+import pytest
+import random
+import sys
+import os
+
+sys.path.append('src')
+
+class ConfigShim(object):
+ def get(self, *args, **kwargs):
+ return ''
+
+import config
+config.config = ConfigShim()
+
+from windows import Input
+
+@pytest.fixture(scope="function")
+def input_obj():
+ obj = Input()
+ obj.reset_completion()
+ obj.resize = lambda: None
+ obj.rewrite_text = lambda: None
+ obj.refresh = lambda: None
+ return obj
+
+@pytest.fixture(scope="module")
+def random_unquoted_words():
+ letters = string.ascii_lowercase + ((len(string.ascii_lowercase)//4)*' ')
+ acc = [random.choice(letters) for _ in range(200)]
+ words = ''.join(acc).split()
+ return words
+
+@pytest.fixture(scope="module")
+def quoted_words():
+ words = []
+ letters = string.ascii_lowercase + ((len(string.ascii_lowercase)//4)*' ')
+ words_by_letter = {}
+ for start_letter in string.ascii_lowercase:
+ words_by_letter[start_letter] = []
+ for _ in range(5):
+ size = random.randint(0, 15)
+ word = start_letter + ''.join(random.choice(letters) for i in range(size))
+ words.append(word)
+ words_by_letter[start_letter].append(word)
+ return (words, words_by_letter)
+
+
+def test_new_completion_1_unquoted(input_obj):
+
+ input_obj.text = '/example '
+ input_obj.pos = len(input_obj.text) - 1
+
+ input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
+ assert input_obj.text == '/example toto'
+
+ input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
+ assert input_obj.text == '/example titi'
+
+ input_obj.new_completion(['toto', 'titi'], 1, quotify=False)
+ assert input_obj.text == '/example toto'
+
+
+def test_new_completion_1_quoted_spaces(input_obj):
+ input_obj.text = '/example '
+ input_obj.pos = len(input_obj.text) - 1
+
+ input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example "toto toto"'
+
+ input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example "titi titi"'
+
+ input_obj.new_completion(['toto toto', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example "toto toto"'
+
+ input_obj.text = '/example '
+ input_obj.pos = len(input_obj.text) - 1
+ input_obj.reset_completion()
+
+ input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example "toto toto"'
+
+ input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example tata'
+
+ input_obj.new_completion(['toto toto', 'tata', 'titi titi'], 1, quotify=True)
+ assert input_obj.text == '/example "titi titi"'
+
+def test_new_completion_unquoted_random_override(input_obj, random_unquoted_words):
+ """
+ Complete completely random words and ensure that the input is
+ changed adequately.
+ """
+ words = random_unquoted_words
+
+ # try the completion on the middle element without affecting the others
+ input_obj.text = '/example %s %s %s' % (words[0], words[1], words[2])
+ base = len(input_obj.text) - len(words[2]) - 1
+ input_obj.pos = base
+ def f(n):
+ return '/example %s %s' % (words[0], words[n])
+
+ for i in range(len(words)):
+ pos = input_obj.get_argument_position(False)
+ input_obj.new_completion(words[:], pos, quotify=False, override=True)
+ assert f(i) + " " + words[2] == input_obj.text
+ assert len(f(i)) == input_obj.pos
+
+ assert input_obj.text == '/example %s %s %s' % (words[0], words[-1], words[2])
+
+ pos = input_obj.get_argument_position(False)
+ input_obj.new_completion(words[:], pos, quotify=False, override=True)
+ assert input_obj.text == '/example %s %s %s' % (words[0], words[0], words[2])
+
+ input_obj.reset_completion()
+
+ # try the completion on the final element without affecting the others
+ input_obj.text = '/example %s %s %s' % (words[0], words[1], words[2])
+ base = len(input_obj.text)
+ input_obj.pos = base
+ def f2(n):
+ return '/example %s %s %s' % (words[0], words[1], words[n])
+
+ print(words)
+ for i in range(len(words)):
+ pos = input_obj.get_argument_position(False)
+ input_obj.new_completion(words[:], pos, quotify=False, override=True)
+ assert f2(i) == input_obj.text
+ assert len(f2(i)) == input_obj.pos
+
+ assert input_obj.text == '/example %s %s %s' % (words[0], words[1], words[-1])
+
+
+def test_new_completion_quoted_random(input_obj, quoted_words):
+ """
+ Complete (possibly) quoted words starting with a specific letter.
+ And make sure that the quotes only appear when necessary.
+ """
+ words = quoted_words[0]
+ words_l = quoted_words[1]
+
+ letters = ('', 'a', 'b', 'c')
+
+ # generate the text which is supposed to be present in the input
+ def f(p, i):
+ rep = words_l[letters[p]][i] if not ' ' in words_l[letters[p]][i] else '"'+words_l[letters[p]][i]+'"'
+ fst = letters[1] if p != 1 else rep
+ snd = letters[2] if p != 2 else rep
+ trd = letters[3] if p != 3 else rep
+ return '/example %s %s %s' % (fst, snd, trd)
+
+ for pos in range(1, 4):
+ input_obj.text = '/example a b c'
+ input_obj.reset_completion()
+ input_obj.pos = len('/example') + pos * 2
+
+ extra = (3 - pos) * 2
+ for i in range(5):
+ input_obj.new_completion(words[:], pos, quotify=True)
+ assert f(pos, i) == input_obj.text
+ assert len(f(pos, i)) - extra == input_obj.pos
+
diff --git a/test/test_xhtml.py b/test/test_xhtml.py
index 58857d67..5afd08ff 100644
--- a/test/test_xhtml.py
+++ b/test/test_xhtml.py
@@ -38,6 +38,14 @@ def test_xhtml_to_poezio_colors():
xhtml = start + b'<a href="http://perdu.com">http://perdu.com</a>' + end
assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o'
+ xhtml = b'<div style="font-weight:bold">Allo <div style="color:red">test <div style="color: blue">test2</div></div></div>'
+ assert xhtml_to_poezio_colors(xhtml, force=True) == '\x19bAllo \x19196}test \x1921}test2\x19o'
+
+ xhtml = (b'<div style="color:blue"><div style="color:yellow">'
+ b'<div style="color:blue">Allo <div style="color:red">'
+ b'test <div style="color: blue">test2</div></div></div></div></div>')
+ assert xhtml_to_poezio_colors(xhtml, force=True) == '\x1921}Allo \x19196}test \x1921}test2\x19o'
+
with pytest.raises(xml.sax._exceptions.SAXParseException):
xhtml_to_poezio_colors(b'<p>Invalid xml')