File: test_util.py

package info (click to toggle)
gaupol 1.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,112 kB
  • sloc: python: 18,822; sh: 98; makefile: 79
file content (143 lines) | stat: -rw-r--r-- 5,251 bytes parent folder | download | duplicates (6)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-

# Copyright (C) 2006 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import aeidon


class TestModule(aeidon.TestCase):

    def test_atomic_open__existing_file(self):
        path = self.new_subrip_file()
        with aeidon.util.atomic_open(path, "w") as f:
            f.write("test\n")
        text = open(path, "r").read()
        assert text == "test\n"

    def test_atomic_open__no_existing_file(self):
        path = aeidon.temp.create()
        with aeidon.util.atomic_open(path, "w") as f:
            f.write("test\n")
        text = open(path, "r").read()
        assert text == "test\n"

    def test_compare_versions(self):
        assert aeidon.util.compare_versions("0.1.1", "0.1"  ) ==  1
        assert aeidon.util.compare_versions("0.2"  , "0.1"  ) ==  1
        assert aeidon.util.compare_versions("0.3"  , "0.3"  ) ==  0
        assert aeidon.util.compare_versions("0.4"  , "0.4.1") == -1
        assert aeidon.util.compare_versions("0.4"  , "0.5"  ) == -1

    def test_detect_format(self):
        for format in aeidon.formats:
            path = self.new_temp_file(format)
            assert aeidon.util.detect_format(path, "ascii") == format

    def test_detect_newlines__mac(self):
        path = aeidon.temp.create()
        open(path, "w", newline="").write("a\rb\rc\r")
        newlines = aeidon.util.detect_newlines(path)
        assert newlines == aeidon.newlines.MAC

    def test_detect_newlines__unix(self):
        path = aeidon.temp.create()
        open(path, "w", newline="").write("a\nb\nc\n")
        newlines = aeidon.util.detect_newlines(path)
        assert newlines == aeidon.newlines.UNIX

    def test_detect_newlines__windows(self):
        path = aeidon.temp.create()
        open(path, "w", newline="").write("a\r\nb\r\nc\r\n")
        newlines = aeidon.util.detect_newlines(path)
        assert newlines == aeidon.newlines.WINDOWS

    def test_flatten(self):
        lst = [1, 2, [3, 4, [5, 6, [7]], 8], 9]
        lst = aeidon.util.flatten(lst)
        assert lst == [1, 2, 3, 4, 5, 6, 7, 8, 9]

    def test_get_default_encoding(self):
        assert aeidon.util.get_default_encoding()

    def test_get_default_newline(self):
        assert aeidon.util.get_default_newline()

    def test_get_encoding_alias(self):
        assert aeidon.util.get_encoding_alias("utf8") == "utf_8"
        assert aeidon.util.get_encoding_alias("johab") == "johab"

    def test_get_ranges(self):
        lst = [0, 0, 4, 5, 3, 7, 8, 2, 7]
        lst = aeidon.util.get_ranges(lst)
        assert lst == [[0], [2, 3, 4, 5], [7, 8]]

    def test_get_unique__first(self):
        lst = [4, 1, 5, 5, 1, 1, 3, 6, 4, 4]
        lst = aeidon.util.get_unique(lst)
        assert lst == [4, 1, 5, 3, 6]

    def test_get_unique__last(self):
        lst = [4, 1, 5, 5, 1, 1, 3, 6, 4, 4]
        lst = aeidon.util.get_unique(lst, keep_last=True)
        assert lst == [5, 1, 3, 6, 4]

    def test_read__basic(self):
        path = self.new_subrip_file()
        text = open(path, "r", encoding="ascii").read().strip()
        assert aeidon.util.read(path) == text

    def test_read__fallback(self):
        path = self.new_subrip_file()
        open(path, "w", encoding="utf_8").write("\xc3\xb6\n")
        assert aeidon.util.read(path, "ascii") == "\xc3\xb6"

    def test_readlines__basic(self):
        path = self.new_subrip_file()
        lines = [x.rstrip() for x in open(path, "r").readlines()]
        assert aeidon.util.readlines(path) == lines

    def test_readlines__fallback(self):
        path = self.new_subrip_file()
        open(path, "w", encoding="utf_8").write("\xc3\xb6\n")
        assert aeidon.util.readlines(path, "ascii") == ["\xc3\xb6"]

    def test_write__basic(self):
        text = "test\ntest\n"
        path = self.new_subrip_file()
        aeidon.util.write(path, text)
        f = open(path, "r", encoding="ascii")
        assert f.read() == text

    def test_write__fallback(self):
        text = "\xc3\xb6\n"
        path = self.new_subrip_file()
        aeidon.util.write(path, text, "ascii")
        f = open(path, "r", encoding="utf_8")
        assert f.read() == text

    def test_writelines__basic(self):
        lines = ("test", "test")
        path = self.new_subrip_file()
        aeidon.util.writelines(path, lines)
        f = open(path, "r", encoding="ascii")
        assert f.read() == "test\ntest\n"

    def test_writelines__fallback(self):
        text = "\xc3\xb6"
        path = self.new_subrip_file()
        aeidon.util.writelines(path, (text,), "ascii")
        f = open(path, "r", encoding="utf_8")
        assert f.read() == text + "\n"