File: test_utils.py

package info (click to toggle)
pystemd 0.13.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,296 kB
  • sloc: python: 1,431; sh: 17; makefile: 8
file content (77 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (2)
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
74
75
76
77
#!/usr/bin/env python3
#
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the LICENSE file in
# the root directory of this source tree.
#

import pathlib
from unittest import TestCase
from unittest.mock import sentinel

from pystemd.utils import x2char_star, x2cmdlist


class TestContextToCharStar(TestCase):
    def test_pass_normal_vars(self):
        for elem in (0, b"hi", True, [], {}, (3, 4), {3, 4}):
            self.assertEqual(elem, x2char_star(elem))

    def test_convert_to_char(self):
        for elem in ("", "hi all"):
            self.assertEqual(elem.encode(), x2char_star(elem))

        self.assertEqual(b"/this/is/path", x2char_star(pathlib.Path("/this/is/path")))

    def test_convert_all(self):
        self.assertEqual(b"true", x2char_star(True, convert_all=True))
        self.assertEqual(b"false", x2char_star(False, convert_all=True))
        self.assertEqual(b"1", x2char_star(1, convert_all=True))
        self.assertEqual(b"3.14159", x2char_star(3.14159, convert_all=True))
        self.assertEqual(b"100", x2char_star(100, convert_all=True))


class TestX2x2Cmdlist(TestCase):
    def tests_none(self):
        self.assertEqual(x2cmdlist(None), [])
        self.assertEqual(x2cmdlist([]), [])

    def tests_strings(self):
        self.assertEqual(
            x2cmdlist("foo", sentinel.i_pass), [(b"foo", (b"foo",), sentinel.i_pass)]
        )
        self.assertEqual(
            x2cmdlist("foo bar", sentinel.i_pass),
            [(b"foo", (b"foo", b"bar"), sentinel.i_pass)],
        )

    def tests_bytes(self):
        self.assertEqual(
            x2cmdlist(b"foo", sentinel.i_pass), [(b"foo", (b"foo",), sentinel.i_pass)]
        )
        self.assertEqual(
            x2cmdlist(b"foo bar", sentinel.i_pass),
            [(b"foo", (b"foo", b"bar"), sentinel.i_pass)],
        )

    def test_array(self):
        self.assertEqual(
            x2cmdlist(
                [
                    [b"foo", "bar"],
                    [b"bing", "bang"],
                ],
                sentinel.i_pass,
            ),
            [
                (b"foo", (b"foo", b"bar"), sentinel.i_pass),
                (b"bing", (b"bing", b"bang"), sentinel.i_pass),
            ],
        )

        self.assertEqual(
            x2cmdlist([b"foo", "bar"], sentinel.i_pass),
            [(b"foo", (b"foo", b"bar"), sentinel.i_pass)],
        )