I have created my ex49(really ex48) parser_tests.py
file and I had a few errors and overall rough time with the first couple of test functions, but after those first 3 test functions, I never encountered an error and all my nosetest functions passed first try. I figured this too good to be true. Would anyone mind looking at my code and provide feedback on where I went wrong, that is if I did, which I highly suspect I did.
My code:
from nose.tools import * # noqa
from ex48 import lexicon, parser
def test_sentence():
""" test 'sentence' function """
s1 = parser.Sentence(('noun', "player"),
('verb', "attacks"),
('noun', "rat"))
assert s1.verb == "attacks"
assert s1.subject == "player"
assert s1.object == "rat"
def test_peek():
""" test 'peek' function """
word_list = lexicon.scan("princess kill bear")
assert "noun" == parser.peek(word_list)
def test_match():
""" test 'match' function """
word_list = lexicon.scan("bear eat princess")
assert_equal(('noun', "bear"), parser.match(word_list, 'noun'))
assert_equal(None, parser.match(word_list, 'noun'))
def test_skip():
""" test 'skip' function """
word_list = lexicon.scan("princess kill the bear")
assert_equal(('noun', "princess"), parser.match(word_list, 'noun'))
assert_equal(('verb', "kill"), parser.match(word_list, 'verb'))
assert_equal(('stop', "the"), parser.match(word_list, 'stop'))
assert_equal(('noun', "bear"), parser.match(word_list, 'noun'))
assert_equal(None, parser.skip(word_list, 'stop'))
def test_parse_verb():
""" test 'parse_verb' function """
word_list = lexicon.scan("princess kill the bear")
assert_equal(('noun', "princess"), parser.match(word_list, 'noun'))
assert_equal(parser.parse_verb(word_list), ('verb', "kill"))
def test_parse_object():
""" test 'parse_object' function """
word_list = lexicon.scan("princess kill the bear")
assert_equal(parser.parse_object(word_list), ('noun', "princess"))
def test_parse_subject():
""" test 'parse_subject' function """
word_list = lexicon.scan("go north")
assert_equal(parser.parse_subject(word_list), ('noun', "player"))
# test again
word_list = lexicon.scan("princess kill the bear")
assert_equal(parser.parse_subject(word_list), ('noun', "princess"))
def test_parse_sentence():
""" test 'parse_sentence' function """
word_list = lexicon.scan("princess kill the bear")
s = parser.parse_sentence(word_list)
assert s.subject == "princess"
assert s.verb == "kill"
assert s.object == "bear"
# test again
word_list = lexicon.scan("go north")
s = parser.parse_sentence(word_list)
assert s.subject == "player"
assert s.verb == "go"
assert s.object == "north"
# test again
word_list = lexicon.scan("north")
assert_raises(parser.ParserError, parser.parse_sentence, word_list)
Thank you