I am learning Python through LPTHW. There is an exercise where we need to create test scenarios. The following is the code for whom Test Case have to be made:
#############################
# Sentence Parser #
#############################
class ParserError(Exception):
pass
class Sentence(Object):
def __init__(self, subject, verb, object):
# remember, we take ('noun','princess') and convert them
self.subject = subject[1]
self.verb = verb[1]
self.object = object[1]
def peek(word_list):
if word_list:
word = word_list[0]
return word[0]
else:
return None
def match(word_list, expecting):
if word_list:
word = word_list.pop(0)
if word[0] == expecting:
return word
else:
return None
def skip(word_list, word_type):
while peek(word_list) == word_type:
match(word_list, word_type)
def parse_verb(word_list):
skip(word_list, 'stop')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
def parse_object(word_list):
skip(word_list, 'stop')
next == peek(word_list)
if next == 'noun':
return match(word_list, 'noun')
if next == 'direction':
return match(word_list, 'direction')
else:
raise ParserError("Expected a nour or direction next")
def pars_subject(word_list, subj):
verb = parse_verb(word_list)
obj = parse_object(word_list)
return Sentence(subj, verb, obj)
def parse_sentence(word_list):
skip(word_list, 'stop')
if start == 'noun':
subj = match(word_list, 'noun')
return parse_subject(word_list, subj)
elif start == 'verb':
# assume the subject is a player then
return parse_subject(word_list,('noun', 'player'))
else:
raise ParserError("Must start with subject, object, or verb not:%s" % start)
The Following the test script:
---------------------------------------------------------------------------------------------------------------
from nose.tools import *
from ex48.lexicon import lexicon
from ex49.ex49 import *
def test_parse_subject():
testsentence = "princess go east"
result = lexicon.scan(testsentence)
Sent = parse_sentence(result)
ResultSent = Sentence(('subject', 'princess'),
('verb', 'go'),
('object', 'east'))
print ResultSent.subject
print ResultSent.verb
print ResultSent.object
print Sent.subject
print Sent.verb
print Sent.object
assert_equal(Sent, ResultSent)
When i run nosetests command to execute this, it gives an error message:
PS C:\Users\rk\projects\parser> nosetests
E
======================================================================
ERROR: Failure: NameError (name 'Object' is not defined)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 390, in loadTestsFro
mName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 39, in importFromP
ath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 86, in importFromD
ir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\Users\rk\projects\parser\tests\ex49_test.py", line 3, in <module>
from ex49.ex49 import *
File "C:\Users\rk\projects\parser\ex49\ex49.py", line 8, in <module>
class Sentence(Object):
NameError: name 'Object' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.031s
FAILED (errors=1)