diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-09-24 10:16:46 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 10:16:46 -0400 |
commit | e5e8a7954b9ace78cd49ef43c5f8d6203c227f0f (patch) | |
tree | d6589102bb8044ef9a03b4f6ac36ed63951b9329 /tests/test_utils.py | |
parent | dbe485957b421f6fd973b3a493de7b264b363d54 (diff) | |
parent | 0bf2824bcb9c00ee56dde660b432990fc0df22f4 (diff) | |
download | cmd2-git-e5e8a7954b9ace78cd49ef43c5f8d6203c227f0f.tar.gz |
Merge pull request #534 from python-cmd2/copy_stream
Fixed several hack classes built to simulate file descriptors
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r-- | tests/test_utils.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index 8c8daa39..53031567 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -5,6 +5,10 @@ Unit testing for cmd2/utils.py module. Copyright 2018 Todd Leonhardt <todd.leonhardt@gmail.com> Released under MIT license, see LICENSE file """ +import sys + +import pytest + from colorama import Fore import cmd2.utils as cu @@ -110,4 +114,55 @@ def test_quot_string_if_needed_no(): assert cu.quote_string_if_needed(your_str) == your_str +@pytest.fixture +def stdout_sim(): + stdsim = cu.StdSim(sys.stdout) + return stdsim + +def test_stdsim_write_str(stdout_sim): + my_str = 'Hello World' + stdout_sim.write(my_str) + assert stdout_sim.getvalue() == my_str + +def test_stdsim_write_bytes(stdout_sim): + b_str = b'Hello World' + with pytest.raises(TypeError): + stdout_sim.write(b_str) + +def test_stdsim_buffer_write_bytes(stdout_sim): + b_str = b'Hello World' + stdout_sim.buffer.write(b_str) + assert stdout_sim.getvalue() == b_str.decode() + +def test_stdsim_buffer_write_str(stdout_sim): + my_str = 'Hello World' + with pytest.raises(TypeError): + stdout_sim.buffer.write(my_str) + +def test_stdsim_read(stdout_sim): + my_str = 'Hello World' + stdout_sim.write(my_str) + # getvalue() returns the value and leaves it unaffected internally + assert stdout_sim.getvalue() == my_str + # read() returns the value and then clears the internal buffer + assert stdout_sim.read() == my_str + assert stdout_sim.getvalue() == '' + +def test_stdsim_clear(stdout_sim): + my_str = 'Hello World' + stdout_sim.write(my_str) + assert stdout_sim.getvalue() == my_str + stdout_sim.clear() + assert stdout_sim.getvalue() == '' + +def test_stdsim_getattr_exist(stdout_sim): + # Here the StdSim getattr is allowing us to access methods within StdSim + my_str = 'Hello World' + stdout_sim.write(my_str) + val_func = getattr(stdout_sim, 'getvalue') + assert val_func() == my_str + +def test_stdsim_getattr_noexist(stdout_sim): + # Here the StdSim getattr is allowing us to access methods defined by the inner stream + assert not stdout_sim.isatty() |