summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-24 09:49:46 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-09-24 09:49:46 -0400
commitca4ac5cfe4fae041463674cd8ee40b62444693f8 (patch)
tree181e1fb0a5f35d25b14f58541d38d0bbebcf9853 /tests/test_utils.py
parent35550e048bde73b08fad28c2a8d844dcbdea7f35 (diff)
downloadcmd2-git-ca4ac5cfe4fae041463674cd8ee40b62444693f8.tar.gz
StdSim write methods now raise a TypeError exception if passed the wrong type
Also: - Added explicit unit tests for StdSim to test_utils.py
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r--tests/test_utils.py45
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 8c8daa39..12d87218 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,43 @@ 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() == ''