Created
October 31, 2014 16:54
-
-
Save benawad/3742e5d5f9f47074c187 to your computer and use it in GitHub Desktop.
Test file for chapter 49 of Learning Python The Hard Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nose.tools import * | |
from ex48 import parser | |
def test_sentence(): | |
s1 = parser.Sentence(('noun', 'bear'), ('verb', 'kill'), ('direction', 'north')) | |
assert_equal(s1.subject, 'bear') | |
assert_equal(s1.verb, 'kill') | |
assert_equal(s1.object, 'north') | |
def test_peek(): | |
s1 = [('noun', 'bear'), ('verb', 'kill'), ('direction', 'north')] | |
p1 = parser.peek(s1) | |
p2 = parser.peek(s1) | |
assert_equal =(p1, 'noun') | |
assert_equal = (p2, 'noun') | |
def test_match(): | |
s1 = [('noun', 'bear'), ('verb', 'kill'), ('direction', 'north')] | |
m1 = parser.match(s1, 'verb') | |
assert_equal(m1, None) | |
m2 = parser.match(s1, 'verb') | |
assert_equal(m2[0], 'verb') | |
m3 = parser.match(s1, 'direction') | |
assert_equal(m3[0], 'direction') | |
assert_raises(AttributeError, parser.match, 'yellow', 'blue') | |
def test_skip(): | |
s1 = [('noun', 'bear'), ('verb', 'kill'), ('direction', 'north')] | |
parser.skip(s1, 'noun') | |
parser.skip(s1, 'verb') | |
assert_equal(s1, [('direction', 'north')]) | |
def test_parse_verb(): | |
s1 = [('verb', 'kill'), ('noun', 'bear')] | |
verb = parser.parse_verb(s1) | |
assert_equal(verb, ('verb', 'kill')) | |
s2 = [('noun', 'door')] | |
assert_raises(parser.ParserError, parser.parse_verb, s2) | |
def test_parse_object(): | |
s1 = [('noun', 'bear'), ('verb', 'kill'), ('noun', 'bear')] | |
assert_equal(parser.parse_object(s1), ('noun', 'bear')) | |
s2 = [('direction', 'up'), ('noun', 'bear')] | |
assert_equal(parser.parse_object(s2), ('direction', 'up')) | |
s3 = [('verb', 'kill'), ('noun', 'bear')] | |
assert_raises(parser.ParserError, parser.parse_object, s3) | |
def test_parse_subject(): | |
s1 = [('noun', 'bear'), ('verb', 'kill'), ('noun', 'bear')] | |
assert_equal(parser.parse_subject(s1), ('noun', 'bear')) | |
s2 = [('verb', 'kill'), ('noun', 'bear')] | |
assert_equal(parser.parse_subject(s2), ('noun', 'player')) | |
s3 = [('direction', 'up'), ('noun', 'bear')] | |
assert_raises(parser.ParserError, parser.parse_subject, s3) | |
def test_sentence(): | |
x = parser.parse_sentence([('noun', 'bear'), ('verb', 'eat'), ('stop', 'the'), ('noun', 'honey')]) | |
assert_equal(x.subject, 'bear') | |
assert_equal(x.verb, 'eat') | |
assert_equal(x.object, 'honey') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment