aboutsummaryrefslogtreecommitdiff
path: root/docs/source/examples/greeter.py
blob: 32d739fc74faea656badbc53dee0c93607c0b4cf (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
#!/usr/bin/env python3
import hazwaz
import hazwaz.unittest


class World(hazwaz.Command):
    """
    Greet the whole world.
    """

    def main(self):
        print("Hello world!")


class Individual(hazwaz.Command):
    """
    Greet an individual.
    """

    def add_arguments(self, parser):
        parser.add_argument(
            "gretee",
            help="The person to be greeted",
        )

    def main(self):
        print("Hello {}".format(self.args.gretee))


class TestGreeter(hazwaz.unittest.HazwazTestCase):
    def test_greet_world(self):
        cmd = Greet()
        stream = self.run_with_argv(cmd, [
            "./greeter.py",
            "world",
        ])

        self.assertEqual(stream["stdout"].getvalue(), "Hello world!\n")

    def test_greet_individual(self):
        cmd = Greet()
        stream = self.run_with_argv(cmd, [
            "./greeter.py",
            "individual",
            "Bob",
        ])

        self.assertEqual(stream["stdout"].getvalue(), "Hello Bob\n")


class Greet(hazwaz.MainCommand):
    """
    Greet people in different ways.
    """
    commands = (
        World(),
        Individual(),
        hazwaz.unittest.TestCommand([TestGreeter]),
    )


if __name__ == "__main__":
    Greet().run()