Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
serial_io.py: Update Unittests
Browse files Browse the repository at this point in the history
The existing unit tests were not working correctly after #60. They were now
reviewed and should work okay.

See GitHub Issue #70 for details.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
  • Loading branch information
erikbgithub committed Jul 23, 2013
1 parent b2a5753 commit efa13c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 100 deletions.
1 change: 1 addition & 0 deletions monk_tf/serial_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, *args, **kwargs):
"""
:param prompt: set a prompt for the communication
"""
print "wrong init :("
self._logger = logging.getLogger(__name__)
if "linesep" in args:
self.linesep = args["linesep"]
Expand Down
138 changes: 38 additions & 100 deletions test/test_serial_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,127 +10,65 @@
# 3 of the License, or (at you option) any later version.
#

import logging

from nose import tools as nt

from monk_tf import serial_io as sio


def test_simple():
"""serial_io: check wether creating a SerialIO object works
""" serial_io: create a SerialIO Object without problems
"""
#nothing to prepare
#
#execute
# set up
# exec
sut = sio.SerialIO()
#assert
nt.ok_(sut, "should contain a monk_tf.serial_io.SerialIO object, but instead contains this: '{}'".format(sut))
# assert
nt.ok_(isinstance(sut,sio.SerialIO),
"should have type SerialIO, but has {}".format(type(sut)))


def test_cmd_set_attribs():
"""serial_io: check wether cmd automatically updates the attributes
def test_cmd_hello_world():
""" serial_io: cmd("hello") returns "world"
"""
# prepare
send_cmd = "qwer"
expected_calls = ['write', 'read_until']
expected_cmd = send_cmd + "\n"
expected_confidence = True
expected_output = 'abcd'
sut = MockSerialIOCmd((True, expected_output))
# execute
sut.cmd(send_cmd)
# evaluate
# set up
any_txt = "hello"
expected_out = "world"
expected_calls = ["write('{}\n')".format(any_txt), "readall()"]
sut = MockSerial()
sut.out = "hello\n{}\n<prompt>$ ".format(expected_out)
# exec
out = sut.cmd(any_txt)
# assert
nt.eq_(expected_out, out)
nt.eq_(expected_calls, sut.calls)
nt.eq_(expected_cmd, sut.last_cmd)
nt.eq_(expected_confidence, sut.last_confidence)
nt.eq_(expected_output, sut.last_output)
# clean up
# not needed


def test_read_until_strips_end():
"""serial_io: check wether reading really strips end_strip
def test_cmd_with_windows_newlines():
""" serial_io: make sure that windows newlines get replaced
"""
# prepare
expected_calls = ["read"]
expected_out = "trewq\n"
expected_confidence = True
in_strip = "abcd"
in_sleep = 0.0
in_mock_output = expected_out + in_strip
sut = MockSerialIORead(in_mock_output)
# execute
out_confidence, output = sut.read_until(in_strip, sleep_time=in_sleep)
# evalute
# set up
any_txt = "hello"
expected_out = "world"
expected_calls = ["write('{}\n')".format(any_txt), "readall()"]
sut = MockSerial()
sut._linesep = "\n"
sut.out = "hello\n\r{}\n\r<prompt>$ ".format(expected_out)
# exec
out = sut.cmd(any_txt)
# assert
nt.eq_(expected_out, out)
nt.eq_(expected_calls, sut.calls)
nt.eq_(expected_confidence, out_confidence)
nt.eq_(expected_out, output)
# clean up
# not needed


def test_read_until_strips_start():
"""serial_io: check wether reading really strips start_strip
"""
# prepare
expected_calls = ["read", "read"]
expected_out = "trewq\n"
expected_confidence = True
in_start_strip = "abcd\n"
in_stop_strip = "dcba"
in_sleep = 0.0
in_mock_output = in_start_strip + expected_out + in_stop_strip
sut = MockSerialIORead(in_mock_output)
# execute
out_confidence, output = sut.read_until(
in_stop_strip,
start_strip=in_start_strip,
sleep_time=in_sleep
)
# evalute
nt.eq_(expected_calls, sut.calls)
nt.eq_(expected_confidence, out_confidence)
nt.eq_(expected_out, output)
# clean up
# not needed

class MockSerial(sio.SerialIO):

class MockSerialIOCmd(sio. SerialIO):
""" mocks specifically for testing the cmd() method
"""

def __init__(self, readout=None):
def __init__(self):
self.calls = []
self.readout = readout
super(MockSerialIOCmd, self).__init__()


def write(self,cmd=None):
self.calls.append("write")


def read_until(self, cmd=None, prompt=None, sleep_time=None, timeout=None,
start_strip=None):
self.calls.append("read_until")
return self.readout


class MockSerialIORead(sio.SerialIO):

def __init__(self, readout=None):
self.calls = []
self.readout = readout
super(MockSerialIORead, self).__init__()


def write(self,cmd=None):
self.calls.append("write")


def read(self, number=None):
self.calls.append("read")
return self.readout
def readall(self):
self.calls.append("readall()")
return self.out

def inWaiting(self):
return 0
def write(self,msg):
self.calls.append("write('{}')".format(msg))

0 comments on commit efa13c1

Please sign in to comment.