File: supercommands_test.py

package info (click to toggle)
python-nubia 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: python: 4,182; makefile: 9; sh: 1
file content (116 lines) | stat: -rw-r--r-- 3,123 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#

import unittest

from later.unittest import TestCase

from nubia import command
from tests.util import TestShell


class SuperCommandSpecTest(TestCase):
    async def test_super_basics(self):
        this = self

        @command
        class SuperCommand:
            "SuperHelp"

            @command
            async def sub_command(self, arg1: str, arg2: int):
                "SubHelp"
                this.assertEqual(arg1, "giza")
                this.assertEqual(arg2, 22)
                return 45

            @command
            async def another_command(self, arg1: str):
                "AnotherHelp"
                return 22

        shell = TestShell(commands=[SuperCommand])
        self.assertEqual(
            45,
            await shell.run_cli_line(
                "test_shell super-command sub-command --arg1=giza --arg2=22"
            ),
        )
        self.assertEqual(
            22,
            await shell.run_cli_line(
                "test_shell super-command another-command --arg1=giza"
            ),
        )

    async def test_super_common_arguments(self):
        this = self

        @command
        class SuperCommand:
            "SuperHelp"

            def __init__(self, shared: int = 10) -> None:
                self.shared = shared

            @command
            async def sub_command(self, arg1: str, arg2: int):
                "SubHelp"
                this.assertEqual(self.shared, 15)
                this.assertEqual(arg1, "giza")
                this.assertEqual(arg2, 22)
                return 45

        shell = TestShell(commands=[SuperCommand])
        self.assertEqual(
            45,
            await shell.run_cli_line(
                "test_shell super-command --shared=15 "
                "sub-command --arg1=giza --arg2=22"
            ),
        )
        self.assertEqual(
            45,
            await shell.run_cli_line(
                "test_shell super-command sub-command "
                "--arg1=giza --arg2=22 --shared=15"
            ),
        )

    async def test_super_no_docstring(self):
        @command
        class SuperCommand:
            "SuperHelp"

            @command
            async def sub_command(self, arg1: str):
                return f"Hi {arg1}"

        shell = TestShell(commands=[SuperCommand])

        with self.assertRaises(SystemExit):
            await shell.run_cli_line(
                "test_shell super-command sub-command --arg1=human"
            )

    async def test_sync_sub_command(self):
        @command
        class SuperCommand:
            "SuperHelp"

            @command
            def sub_command(self):
                "SubHelp"
                return 45

        shell = TestShell(commands=[SuperCommand])
        self.assertEqual(
            45,
            await shell.run_cli_line("test_shell super-command sub-command"),
        )