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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
# pylint: skip-file
# type: ignore
#
# tests.test_encoding_functions.py is part of the docformatter project
#
# Copyright (C) 2012-2023 Steven Myint
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Module for testing functions used determine file encodings.
Encoding functions are:
- detect_encoding()
- find_newline()
- open_with_encoding()
"""
# Standard Library Imports
import io
import sys
# Third Party Imports
import pytest
# docformatter Package Imports
from docformatter import Encoder
SYSTEM_ENCODING = sys.getdefaultencoding()
@pytest.mark.usefixtures("temporary_file")
class TestDetectEncoding:
"""Class for testing the detect_encoding() function."""
@pytest.mark.unit
@pytest.mark.parametrize("contents", ["# -*- coding: utf-8 -*-\n"])
def test_detect_encoding_with_explicit_utf_8(
self, temporary_file, contents
):
"""Return utf-8 when explicitly set in file."""
uut = Encoder()
uut.do_detect_encoding(temporary_file)
assert "utf_8" == uut.encoding
@pytest.mark.unit
@pytest.mark.parametrize(
"contents", ["# Wow! docformatter is super-cool.\n"]
)
def test_detect_encoding_with_non_explicit_setting(
self, temporary_file, contents
):
"""Return default system encoding when encoding not explicitly set."""
uut = Encoder()
uut.do_detect_encoding(temporary_file)
assert "ascii" == uut.encoding
@pytest.mark.unit
@pytest.mark.parametrize("contents", ["# -*- coding: blah -*-"])
def test_detect_encoding_with_bad_encoding(self, temporary_file, contents):
"""Default to latin-1 when unknown encoding detected."""
uut = Encoder()
uut.do_detect_encoding(temporary_file)
assert "ascii" == uut.encoding
class TestFindNewline:
"""Class for testing the find_newline() function."""
@pytest.mark.unit
def test_find_newline_only_cr(self):
"""Return carriage return as newline type."""
uut = Encoder()
source = ["print 1\r", "print 2\r", "print3\r"]
assert uut.CR == uut.do_find_newline(source)
@pytest.mark.unit
def test_find_newline_only_lf(self):
"""Return line feed as newline type."""
uut = Encoder()
source = ["print 1\n", "print 2\n", "print3\n"]
assert uut.LF == uut.do_find_newline(source)
@pytest.mark.unit
def test_find_newline_only_crlf(self):
"""Return carriage return, line feed as newline type."""
uut = Encoder()
source = ["print 1\r\n", "print 2\r\n", "print3\r\n"]
assert uut.CRLF == uut.do_find_newline(source)
@pytest.mark.unit
def test_find_newline_cr1_and_lf2(self):
"""Favor line feed over carriage return when both are found."""
uut = Encoder()
source = ["print 1\n", "print 2\r", "print3\n"]
assert uut.LF == uut.do_find_newline(source)
@pytest.mark.unit
def test_find_newline_cr1_and_crlf2(self):
"""Favor carriage return, line feed when mix of newline types."""
uut = Encoder()
source = ["print 1\r\n", "print 2\r", "print3\r\n"]
assert uut.CRLF == uut.do_find_newline(source)
@pytest.mark.unit
def test_find_newline_should_default_to_lf(self):
"""Default to line feed when no newline type found."""
uut = Encoder()
assert uut.LF == uut.do_find_newline([])
assert uut.LF == uut.do_find_newline(["", ""])
@pytest.mark.unit
def test_find_dominant_newline(self):
"""Should detect carriage return as the dominant line endings."""
uut = Encoder()
goes_in = '''\
def foo():\r
"""\r
Hello\r
foo. This is a docstring.\r
"""\r
'''
assert uut.CRLF == uut.do_find_newline(goes_in.splitlines(True))
@pytest.mark.usefixtures("temporary_file")
class TestOpenWithEncoding:
"""Class for testing the open_with_encoding() function."""
@pytest.mark.unit
@pytest.mark.parametrize("contents", ["# -*- coding: utf-8 -*-\n"])
def test_open_with_utf_8_encoding(self, temporary_file, contents):
"""Return TextIOWrapper object when opening file with encoding."""
uut = Encoder()
uut.do_detect_encoding(temporary_file)
assert isinstance(
uut.do_open_with_encoding(temporary_file),
io.TextIOWrapper,
)
@pytest.mark.unit
@pytest.mark.parametrize("contents", ["# -*- coding: utf-8 -*-\n"])
def test_open_with_wrong_encoding(self, temporary_file, contents):
"""Raise LookupError when passed unknown encoding."""
uut = Encoder()
uut.encoding = "cr1252"
with pytest.raises(LookupError):
uut.do_open_with_encoding(temporary_file)
|