|
| 1 | +import pytest |
| 2 | +from functools import partial |
| 3 | + |
| 4 | +from pandas.io.formats.console import detect_console_encoding, locale |
| 5 | + |
| 6 | + |
| 7 | +def mock_raises_exception(error=Exception): |
| 8 | + raise error |
| 9 | + |
| 10 | + |
| 11 | +def test_detect_console_encoding_stdout(monkeypatch): |
| 12 | + monkeypatch.setattr('sys.stdin.encoding', '') |
| 13 | + monkeypatch.setattr('sys.stdout.encoding', 'foo') |
| 14 | + assert detect_console_encoding() == 'foo' |
| 15 | + |
| 16 | + |
| 17 | +def test_detect_console_encoding_stdin(monkeypatch): |
| 18 | + monkeypatch.setattr('sys.stdout.encoding', '') |
| 19 | + monkeypatch.setattr('sys.stdin.encoding', 'foo') |
| 20 | + assert detect_console_encoding() == 'foo' |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize('error', [AttributeError, IOError]) |
| 24 | +def test_detect_console_encoding_stdout_error_uses_locale(monkeypatch, error): |
| 25 | + monkeypatch.setattr('locale.getpreferredencoding', lambda: 'foo') |
| 26 | + monkeypatch.setattr('sys.stdout', partial(mock_raises_exception, error)) |
| 27 | + assert detect_console_encoding() == 'foo' |
| 28 | + |
| 29 | + |
| 30 | +def test_detect_console_encoding_sys_default_encoding(monkeypatch): |
| 31 | + def mock_raises_exception(): |
| 32 | + raise Exception |
| 33 | + monkeypatch.setattr('locale.getpreferredencoding', mock_raises_exception) |
| 34 | + monkeypatch.setattr('sys.stdout', mock_raises_exception) |
| 35 | + monkeypatch.setattr('sys.getdefaultencoding', lambda: 'sysDefaultEncoding') |
| 36 | + assert detect_console_encoding() == 'sysDefaultEncoding' |
0 commit comments