summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/parsing/test_convert_bool.py
blob: d0ade8bd085cb5125de0fea22c3bab9b2e1343b7 (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
# -*- coding: utf-8 -*-
# Copyright: (c) 2017 Ansible Project
# License: GNU General Public License v3 or later (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt )

from __future__ import annotations

import pytest

from ansible.module_utils.parsing.convert_bool import boolean


class TestBoolean:
    def test_bools(self):
        assert boolean(True) is True
        assert boolean(False) is False

    def test_none(self):
        with pytest.raises(TypeError):
            assert boolean(None, strict=True) is False
        assert boolean(None, strict=False) is False

    def test_numbers(self):
        assert boolean(1) is True
        assert boolean(0) is False
        assert boolean(0.0) is False

# Current boolean() doesn't consider these to be true values
#    def test_other_numbers(self):
#        assert boolean(2) is True
#        assert boolean(-1) is True
#        assert boolean(0.1) is True

    def test_strings(self):
        assert boolean("true") is True
        assert boolean("TRUE") is True
        assert boolean("t") is True
        assert boolean("yes") is True
        assert boolean("y") is True
        assert boolean("on") is True

    def test_junk_values_nonstrict(self):
        assert boolean("flibbity", strict=False) is False
        assert boolean(42, strict=False) is False
        assert boolean(42.0, strict=False) is False
        assert boolean(object(), strict=False) is False

    def test_junk_values_strict(self):
        with pytest.raises(TypeError):
            assert boolean("flibbity", strict=True) is False

        with pytest.raises(TypeError):
            assert boolean(42, strict=True) is False

        with pytest.raises(TypeError):
            assert boolean(42.0, strict=True) is False

        with pytest.raises(TypeError):
            assert boolean(object(), strict=True) is False