1# -*- coding: utf-8 -*-
2from __future__ import division, print_function
3
4from plumbum import cli
5
6
7class TestValidator:
8    def test_named(self):
9        class Try(object):
10            @cli.positional(x=abs, y=str)
11            def main(selfy, x, y):
12                pass
13
14        assert Try.main.positional == [abs, str]
15        assert Try.main.positional_varargs is None
16
17    def test_position(self):
18        class Try(object):
19            @cli.positional(abs, str)
20            def main(selfy, x, y):
21                pass
22
23        assert Try.main.positional == [abs, str]
24        assert Try.main.positional_varargs is None
25
26    def test_mix(self):
27        class Try(object):
28            @cli.positional(abs, str, d=bool)
29            def main(selfy, x, y, z, d):
30                pass
31
32        assert Try.main.positional == [abs, str, None, bool]
33        assert Try.main.positional_varargs is None
34
35    def test_var(self):
36        class Try(object):
37            @cli.positional(abs, str, int)
38            def main(selfy, x, y, *g):
39                pass
40
41        assert Try.main.positional == [abs, str]
42        assert Try.main.positional_varargs is int
43
44    def test_defaults(self):
45        class Try(object):
46            @cli.positional(abs, str)
47            def main(selfy, x, y="hello"):
48                pass
49
50        assert Try.main.positional == [abs, str]
51
52
53class TestProg:
54    def test_prog(self, capsys):
55        class MainValidator(cli.Application):
56            @cli.positional(int, int, int)
57            def main(self, myint, myint2, *mylist):
58                print(repr(myint), myint2, mylist)
59
60        _, rc = MainValidator.run(["prog", "1", "2", "3", "4", "5"], exit=False)
61        assert rc == 0
62        assert "1 2 (3, 4, 5)" == capsys.readouterr()[0].strip()
63
64    def test_failure(self, capsys):
65        class MainValidator(cli.Application):
66            @cli.positional(int, int, int)
67            def main(self, myint, myint2, *mylist):
68                print(myint, myint2, mylist)
69
70        _, rc = MainValidator.run(["prog", "1.2", "2", "3", "4", "5"], exit=False)
71
72        assert rc == 2
73        value = capsys.readouterr()[0].strip()
74        assert "int" in value
75        assert "not" in value
76        assert "1.2" in value
77
78    def test_defaults(self, capsys):
79        class MainValidator(cli.Application):
80            @cli.positional(int, int)
81            def main(self, myint, myint2=2):
82                print(repr(myint), repr(myint2))
83
84        _, rc = MainValidator.run(["prog", "1"], exit=False)
85        assert rc == 0
86        assert "1 2" == capsys.readouterr()[0].strip()
87
88        _, rc = MainValidator.run(["prog", "1", "3"], exit=False)
89        assert rc == 0
90        assert "1 3" == capsys.readouterr()[0].strip()
91