1# Copyright 2014-2016 Nathan West
2#
3# This file is part of autocommand.
4#
5# autocommand is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# autocommand is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with autocommand.  If not, see <http://www.gnu.org/licenses/>.
17
18import pytest
19from autocommand.automain import automain, AutomainRequiresModuleError
20
21
22@pytest.mark.parametrize('module_name', ['__main__', True])
23def test_name_equals_main_or_true(module_name):
24    with pytest.raises(SystemExit):
25        @automain(module_name)
26        def main():
27            return 0
28
29
30def test_name_not_main_or_true():
31    def main():
32        return 0
33
34    wrapped_main = automain('some_module')(main)
35
36    assert wrapped_main is main
37
38
39def test_invalid_usage():
40    with pytest.raises(AutomainRequiresModuleError):
41        @automain
42        def main():
43            return 0
44
45
46def test_args():
47    main_called = False
48    with pytest.raises(SystemExit):
49        @automain(True, args=[1, 2])
50        def main(a, b):
51            nonlocal main_called
52            main_called = True
53            assert a == 1
54            assert b == 2
55    assert main_called
56
57
58def test_args_and_kwargs():
59    main_called = False
60    with pytest.raises(SystemExit):
61        @automain(True, args=[1], kwargs={'b': 2})
62        def main(a, b):
63            nonlocal main_called
64            main_called = True
65            assert a == 1
66            assert b == 2
67
68    assert main_called
69