I’ve spent most of the afternoon working on the python code to drive a browser for jssh. After yesterday’s spike, I wanted to create a simple class to eliminate the duplication in jssh_driver.py.
But I also wanted to write tests for the driver class. And I did not want to have to run a live jssh-enabled browser in order to run the tests. So my first task became using a test double for telnetlib that would respond enough like jssh that I could write tests.
So I wrote the test double using TDD. And learned more python details. The whole file is at the end of this post.
I’m sure I’m missing out on some basic python coding practices that everybody knows (except me), so if anyone is reading this and wants to criticize the code, please go ahead.
#!/usr/bin/env python
import unittest
class Telnet:
def init(self, host=None, port=0):
"""Constructor.
Description of ctor
"""
self.host = host
self.port = port
self.sent_commands = 1
self.output_buffer = "\n> "
def write(self, command_string):
self.sent_commands += 1
self.output_buffer += command_string
if command_string[0:5] == "print":
self.output_buffer += "[26]<head></head><body></body>\n"
if command_string[0:5] == "quit(":
return
if command_string[0:5] == "exit(":
return
self.output_buffer += "> "
def read_until(self, until_string):
if self.sent_commands < 1:
raise Telnet, "read without pending output"
output_string = self.output_buffer
self.output_buffer = ""
self.sent_commands -= 1
return output_string
def read_all(self):
output_string = self.output_buffer
self.output_buffer = ""
self.sent_commands = 0
self.host = None
self.port = 0
return output_string
def close(self):
self.read_all()
def open(self,host,port=0):
self.close()
self.__init__(host,port)
def get_sock(self):
return self.host
class jssh_mock_Tests(unittest.TestCase):
def setUp(self):
self.tn = Telnet('localhost',9997)
self.tn.read_until("\n> ")
def testTelnet(self):
self.failUnless((self.tn.host == 'localhost')
and (self.tn.port == 9997))
def testWrite(self):
self.tn.write("foo\n")
self.failUnless(self.tn.sent_commands == 1)
self.failUnless(self.tn.output_buffer == "foo\n> ")
self.failUnless(self.tn.read_until("\n> ") == "foo\n> ")
def testPrint(self):
self.tn.write("print(document.documentElement.innerHTML)\n")
response = self.tn.read_until("\n>")
self.failUnless(response == "print(document.documentElement.innerHTML)\n[26]<head></head><body></body>\n> ")
def testRead_all(self):
self.tn.write("quit()\n")
response = self.tn.read_all()
self.failUnless(self.tn.sent_commands == 0)
self.failUnless(self.tn.output_buffer == "")
self.failUnless(response == "quit()\n")
def testClose(self):
self.tn.close()
self.failUnless(self.tn.host == None)
self.failUnless(self.tn.port == 0)
def testOpen(self):
self.tn.open('foobar')
self.failUnless(self.tn.host == "foobar")
self.failUnless(self.tn.port == 0)
def testGetSock(self):
self.failUnless(self.tn.get_sock())
self.tn.close()
self.failUnless(not self.tn.get_sock())
def main():
unittest.main()
if name == 'main':
main()