summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_ui_console.py
blob: eacf170da5d7ae25fe92af069c5e07b290ac578e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

import argparse

from deluge.ui.console.cmdline.commands.add import Command
from deluge.ui.console.cmdline.commands.config import json_eval
from deluge.ui.console.widgets.fields import TextInput

from .basetest import BaseTestCase


class MockParent(object):
    def __init__(self):
        self.border_off_x = 1
        self.pane_width = 20
        self.encoding = 'utf8'


class UIConsoleFieldTestCase(BaseTestCase):
    def setUp(self):  # NOQA: N803
        self.parent = MockParent()

    def tearDown(self):  # NOQA: N803
        pass

    def test_text_input(self):
        def move_func(self, r, c):
            self._cursor_row = r
            self._cursor_col = c

        t = TextInput(
            self.parent,
            'name',
            'message',
            move_func,
            20,
            '/text/field/file/path',
            complete=False,
        )
        self.assertTrue(t)
        self.assertTrue(t.handle_read(33))


class UIConsoleCommandsTestCase(BaseTestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_add_move_completed(self):
        completed_path = 'completed_path'
        parser = argparse.ArgumentParser()
        cmd = Command()
        cmd.add_arguments(parser)
        args = parser.parse_args(['torrent', '-m', completed_path])
        self.assertEqual(args.move_completed_path, completed_path)
        args = parser.parse_args(['torrent', '--move-path', completed_path])
        self.assertEqual(args.move_completed_path, completed_path)

    def test_config_json_eval(self):
        self.assertEqual(json_eval('/downloads'), '/downloads')
        self.assertEqual(json_eval('/dir/with space'), '/dir/with space')
        self.assertEqual(json_eval('c:\\\\downloads'), 'c:\\\\downloads')
        self.assertEqual(json_eval('c:/downloads'), 'c:/downloads')
        # Ensure newlines are split and only first setting is used.
        self.assertEqual(json_eval('setting\nwithneline'), 'setting')
        # Allow both parentheses and square brackets.
        self.assertEqual(json_eval('(8000, 8001)'), [8000, 8001])
        self.assertEqual(json_eval('[8000, 8001]'), [8000, 8001])
        self.assertEqual(json_eval('["abc", "def"]'), ['abc', 'def'])
        self.assertEqual(json_eval('{"foo": "bar"}'), {'foo': 'bar'})
        self.assertEqual(json_eval('{"number": 1234}'), {'number': 1234})
        # Hex string for peer_tos.
        self.assertEqual(json_eval('0x00'), '0x00')
        self.assertEqual(json_eval('1000'), 1000)
        self.assertEqual(json_eval('-6'), -6)
        self.assertEqual(json_eval('10.5'), 10.5)
        self.assertEqual(json_eval('True'), True)
        self.assertEqual(json_eval('false'), False)
        self.assertEqual(json_eval('none'), None)
        # Empty values to clear config key.
        self.assertEqual(json_eval('[]'), [])
        self.assertEqual(json_eval(''), '')