summaryrefslogtreecommitdiff
path: root/plugins/template/tests/test_myplugin.py
blob: 06ca25670892314ca030552432a9f682cb5a8040 (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
#
# coding=utf-8

import cmd2_myplugin

from cmd2 import (
    cmd2,
)

######
#
# define a class which uses our plugin and some convenience functions
#
######


class MyApp(cmd2_myplugin.MyPluginMixin, cmd2.Cmd):
    """Simple subclass of cmd2.Cmd with our SayMixin plugin included."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @cmd2_myplugin.empty_decorator
    def do_empty(self, args):
        self.poutput("running the empty command")


#
# You can't use a fixture to instantiate your app if you want to use
# to use the capsys fixture to capture the output. cmd2.Cmd sets
# internal variables to sys.stdout and sys.stderr on initialization
# and then uses those internal variables instead of sys.stdout. It does
# this so you can redirect output from within the app. The capsys fixture
# can't capture the output properly in this scenario.
#
# If you have extensive initialization needs, create a function
# to initialize your cmd2 application.


def init_app():
    app = MyApp()
    return app


#####
#
# unit tests
#
#####


def test_say(capsys):
    # call our initialization function instead of using a fixture
    app = init_app()
    # run our mixed in command
    app.onecmd_plus_hooks('say hello')
    # use the capsys fixture to retrieve the output on stdout and stderr
    out, err = capsys.readouterr()
    # make our assertions
    assert out == 'in postparsing hook\nhello\n'
    assert not err


def test_decorator(capsys):
    # call our initialization function instead of using a fixture
    app = init_app()
    # run one command in the app
    app.onecmd_plus_hooks('empty')
    # use the capsys fixture to retrieve the output on stdout and stderr
    out, err = capsys.readouterr()
    # make our assertions
    assert out == 'in postparsing hook\nin the empty decorator\nrunning the empty command\n'
    assert not err