File: test_completion.py

package info (click to toggle)
knack 0.8.0~rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 688 kB
  • sloc: python: 6,202; sh: 8; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download
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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import unittest
try:
    import mock
except ImportError:
    from unittest import mock

from knack.completion import CLICompletion, CaseInsensitiveChoicesCompleter, ARGCOMPLETE_ENV_NAME
from tests.util import MockContext


class TestCompletion(unittest.TestCase):

    def setUp(self):
        self.mock_ctx = MockContext()

    def test_cli_ctx_type_error(self):
        with self.assertRaises(TypeError):
            CLICompletion(cli_ctx=object())

    @mock.patch.dict(os.environ, {})
    def test_completer_not_active(self):
        CLICompletion(cli_ctx=self.mock_ctx)
        self.assertFalse(self.mock_ctx.data['completer_active'])

    @mock.patch.dict(os.environ, {ARGCOMPLETE_ENV_NAME: '1'})
    def test_completer_active(self):
        CLICompletion(cli_ctx=self.mock_ctx)
        self.assertTrue(self.mock_ctx.data['completer_active'])

    def test_case_insensitive_choices_empty(self):
        choices = []
        prefix = ''
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        expected_result = []
        self.assertListEqual(actual_result, expected_result)

    def test_case_insensitive_choices_empty_with_prefix(self):
        choices = []
        prefix = 'abc'
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        expected_result = []
        self.assertListEqual(actual_result, expected_result)

    def test_case_insensitive_choices_list_same_case(self):
        choices = ['abc', 'xyz']
        prefix = ''
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        expected_result = ['abc', 'xyz']
        self.assertListEqual(actual_result, expected_result)

    def test_case_insensitive_choices_list_diff_case(self):
        choices = ['ABC', 'xyz']
        prefix = ''
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        expected_result = ['ABC', 'xyz']
        self.assertListEqual(actual_result, expected_result)

    def test_case_insensitive_choices_list_diff_case_with_prefix(self):
        choices = ['ABC', 'xyz']
        prefix = 'ab'
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        # Casing is returned the same as the original choice list
        expected_result = ['ABC']
        self.assertListEqual(actual_result, expected_result)

    def test_case_insensitive_choices_list_multi_case_with_prefix(self):
        choices = ['red', 'blue', 'YelLoW']
        prefix = 'y'
        actual_result = list(CaseInsensitiveChoicesCompleter(choices)(prefix))
        # Casing is returned the same as the original choice list
        expected_result = ['YelLoW']
        self.assertListEqual(actual_result, expected_result)


if __name__ == '__main__':
    unittest.main()