Пример #1
0
class CompletionTest(unittest.TestCase):
    """ tests the completion generator """
    def init1(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_description = {"create": '', "command can": ''}
        commands = _Commands(command_tree=com_tree3,
                             descrip=command_description)
        self.completer = AzCompleter(commands,
                                     global_params=False,
                                     outstream=six.StringIO())

    def init2(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["-funtime"],
        }
        completable_param = ["-helloworld", "-funtime"]
        param_descript = {
            "create -funtime":
            "There is no work life balance, it's just your life"
        }
        command_description = {"create": '', "command can": ''}
        commands = _Commands(command_tree=com_tree3,
                             command_param=command_param,
                             completable_param=completable_param,
                             param_descript=param_descript,
                             descrip=command_description)
        self.completer = AzCompleter(commands,
                                     global_params=False,
                                     outstream=six.StringIO())

    def init3(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["--funtimes", "-f", '--fun', "--helloworld"],
        }
        completable_param = ["--helloworld", "--funtimes", "-f", '--fun']
        param_descript = {
            "create -f": "There is no work life balance, it's just your life",
            "create --funtimes":
            "There is no work life balance, it's just your life",
            "create --fun":
            "There is no work life balance, it's just your life"
        }
        same_param_doubles = {
            "create":
            [set(["-f", "--funtimes", '--fun']),
             set(['--unhap', '-u'])]
        }
        command_description = {"create": '', "command can": ''}
        commands = _Commands(command_tree=com_tree3,
                             command_param=command_param,
                             completable_param=completable_param,
                             param_descript=param_descript,
                             same_param_doubles=same_param_doubles,
                             descrip=command_description)
        self.completer = AzCompleter(commands,
                                     global_params=False,
                                     outstream=six.StringIO())

    def init4(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("createmore can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["--funtimes", "-f", "--helloworld"],
        }
        completable_param = ["--helloworld", "--funtimes", "-f"]
        param_descript = {
            "create -f":
            "There is no work life balance, it's just your life",
            "create --funtimes":
            "There is no work life balance, it's just your life"
        }
        same_param_doubles = {"create": [set(["-f", "--funtimes", '--fun'])]}
        command_description = {"create": '', "createmore can": ''}
        commands = _Commands(command_tree=com_tree3,
                             command_param=command_param,
                             completable_param=completable_param,
                             param_descript=param_descript,
                             same_param_doubles=same_param_doubles,
                             descrip=command_description)
        self.completer = AzCompleter(commands,
                                     global_params=False,
                                     outstream=six.StringIO())

    def test_command_completion(self):
        # tests general command completion
        self.init1()

        doc = Document(u'')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("command"))
        self.assertEqual(six.next(gen), Completion("create"))

        doc = Document(u'c')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("command", -1))
        self.assertEqual(six.next(gen), Completion("create", -1))

        doc = Document(u'cr')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("create", -2))

        doc = Document(u'command ')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("can"))

        doc = Document(u'create ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_completion(self):
        # tests param completion
        self.init2()
        doc = Document(u'create -')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(
            six.next(gen),
            Completion("-funtime",
                       -1,
                       display_meta=
                       "There is no work life balance, it's just your life"))

        doc = Document(u'command can -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_double(self):
        # tests not generating doubles for parameters
        self.init3()
        doc = Document(u'create -f --')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("--helloworld", -2))

        doc = Document(u'create -f -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --fun -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --funtimes -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_second_completion(self):
        self.init3()
        doc = Document(u'crea ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --fun ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'command d ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --funtimes "life" --hello')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("--helloworld", -7))

    def test_substring_completion(self):
        self.init4()
        doc = Document(u'create')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("createmore", -6))
Пример #2
0
class CompletionTest(unittest.TestCase):
    """ tests the completion generator """
    def init1(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_description = {
            "create": '',
            "command can": ''
        }
        commands = _Commands(
            command_tree=com_tree3,
            descrip=command_description
        )
        self.completer = AzCompleter(commands, global_params=False)

    def init2(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["-funtime"],
        }
        completable_param = [
            "-helloworld",
            "-funtime"
        ]
        param_descript = {
            "create -funtime": "There is no work life balance, it's just your life"
        }
        command_description = {
            "create": '',
            "command can": ''
        }
        commands = _Commands(
            command_tree=com_tree3,
            command_param=command_param,
            completable_param=completable_param,
            param_descript=param_descript,
            descrip=command_description
        )
        self.completer = AzCompleter(commands, global_params=False)

    def init3(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["--funtimes", "-f", '--fun', "--helloworld"],
        }
        completable_param = [
            "--helloworld",
            "--funtimes",
            "-f",
            '--fun'
        ]
        param_descript = {
            "create -f": "There is no work life balance, it's just your life",
            "create --funtimes": "There is no work life balance, it's just your life",
            "create --fun": "There is no work life balance, it's just your life"
        }
        same_param_doubles = {
            "create": [set(["-f", "--funtimes", '--fun']), set(['--unhap', '-u'])]
        }
        command_description = {
            "create": '',
            "command can": ''
        }
        commands = _Commands(
            command_tree=com_tree3,
            command_param=command_param,
            completable_param=completable_param,
            param_descript=param_descript,
            same_param_doubles=same_param_doubles,
            descrip=command_description
        )
        self.completer = AzCompleter(commands, global_params=False)

    def init4(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("createmore can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["--funtimes", "-f", "--helloworld"],
        }
        completable_param = [
            "--helloworld",
            "--funtimes",
            "-f"
        ]
        param_descript = {
            "create -f": "There is no work life balance, it's just your life",
            "create --funtimes": "There is no work life balance, it's just your life"
        }
        same_param_doubles = {
            "create": [set(["-f", "--funtimes", '--fun'])]
        }
        command_description = {
            "create": '',
            "createmore can": ''
        }
        commands = _Commands(
            command_tree=com_tree3,
            command_param=command_param,
            completable_param=completable_param,
            param_descript=param_descript,
            same_param_doubles=same_param_doubles,
            descrip=command_description
        )
        self.completer = AzCompleter(commands, global_params=False)

    def test_command_completion(self):
        # tests general command completion
        self.init1()

        doc = Document(u'')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("command"))
        self.assertEqual(six.next(gen), Completion("create"))

        doc = Document(u'c')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("command", -1))
        self.assertEqual(six.next(gen), Completion("create", -1))

        doc = Document(u'cr')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("create", -2))

        doc = Document(u'command ')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("can"))

        doc = Document(u'create ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_completion(self):
        # tests param completion
        self.init2()
        doc = Document(u'create -')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion(
            "-funtime", -1, display_meta="There is no work life balance, it's just your life"))

        doc = Document(u'command can -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_double(self):
        # tests not generating doubles for parameters
        self.init3()
        doc = Document(u'create -f --')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion(
            "--helloworld", -2))

        doc = Document(u'create -f -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --fun -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --funtimes -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_second_completion(self):
        self.init3()
        doc = Document(u'crea ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --fun ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'command d ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

        doc = Document(u'create --funtimes "life" --hello')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion(
            "--helloworld", -7))

    def test_substring_completion(self):
        self.init4()
        doc = Document(u'create')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion(
            "createmore", -6))
class CompletionTest(unittest.TestCase):
    """ tests the completion generator """
    def init1(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)

        commands = _Commands(command_tree=com_tree3)
        self.completer = AzCompleter(commands)

    def init2(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["-funtime"],
        }
        completable_param = ["-helloworld", "-funtime"]
        param_descript = {
            "create -funtime":
            "There is no work life balance, it's just your life"
        }
        commands = _Commands(command_tree=com_tree3,
                             command_param=command_param,
                             completable_param=completable_param,
                             param_descript=param_descript)
        self.completer = AzCompleter(commands)

    def init3(self):
        """ a variation of initializing """
        com_tree1 = tree.generate_tree("command can")
        com_tree2 = tree.generate_tree("create")
        com_tree3 = tree.CommandHead()
        com_tree3.add_child(com_tree2)
        com_tree3.add_child(com_tree1)
        command_param = {
            "create": ["--funtimes", "-f", "--helloworld"],
        }
        completable_param = ["--helloworld", "--funtimes", "-f"]
        param_descript = {
            "create -f":
            "There is no work life balance, it's just your life",
            "create --funtimes":
            "There is no work life balance, it's just your life"
        }
        same_param_doubles = {"-f": "--funtimes", "--funtimes": '-f'}
        commands = _Commands(command_tree=com_tree3,
                             command_param=command_param,
                             completable_param=completable_param,
                             param_descript=param_descript,
                             same_param_doubles=same_param_doubles)
        self.completer = AzCompleter(commands)

    def test_command_completion(self):
        """ tests general command completion """
        self.init1()

        doc = Document(u'')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("create"))
        self.assertEqual(six.next(gen), Completion("command"))

        doc = Document(u'c')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("create", -1))
        self.assertEqual(six.next(gen), Completion("command", -1))

        doc = Document(u'cr')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("create", -2))

        doc = Document(u'command ')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("can"))

        doc = Document(u'create ')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_completion(self):
        """ tests param completion """
        self.init2()
        doc = Document(u'create -')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(
            six.next(gen),
            Completion("-funtime",
                       -1,
                       display_meta=
                       "There is no work life balance, it's just your life"))

        doc = Document(u'command can -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)

    def test_param_double(self):
        """ tests not generating doubles for parameters """
        self.init3()
        doc = Document(u'create -f --')
        gen = self.completer.get_completions(doc, None)
        self.assertEqual(six.next(gen), Completion("--helloworld", -2))

        doc = Document(u'create -f -')
        gen = self.completer.get_completions(doc, None)
        with self.assertRaises(StopIteration):
            six.next(gen)