笨办法学python(49)创建句子

为习题49写测试方案
assert_raises()用来测试异常。第一个变量为异常的类名(python所有的异常都继承自Exception类),第二个变量为要测试的函数名,第三个变量为函数输入。
</pre><pre class="html" name="code">from nose.tools import *
from ex49 import parser

def test_Sentence():
	parse = parser.Sentence(('noun', 'bear'), ('verb', 'go'), ('direction', 'north'))
	
	assert_equal(parse.subject, 'bear')
	assert_equal(parse.verb, 'go')
	assert_equal(parse.object, 'north')
	
	
def test_peek():
	assert_equal(parser.peek([('direction', 'north')]), 'direction')
	assert_equal(parser.peek([('verb', 'go'), ('direction', 'north')]), 'verb')
	assert_equal(parser.peek([('stop', 'of'), ('number', 1234), ('noun', 'princess')]), 'stop')
	assert_equal(parser.peek([]), None)
	

def test_match():
	assert_equal(parser.match([('direction', 'north')], 'direction'), ('direction', 'north'))
	assert_equal(parser.match([('direction', 'north')], 'verb'), None)
	assert_equal(parser.match([('verb', 'go'), ('direction', 'north')], 'verb'), ('verb', 'go'))
	assert_equal(parser.match([('stop', 'of'), ('number', 1234), ('noun', 'princess')], 'stop'), ('stop', 'of'))
	assert_equal(parser.match([('stop', 'of'), ('number', 1234), ('noun', 'princess')], 'verb'), None)
	

def test_parse_verb():
	assert_equal(parser.parse_verb([('stop', 'of'), ('verb', 'go'), ('direction', 'north')]), ('verb', 'go'))
	assert_equal(parser.parse_verb([('stop', 'the'), ('verb', 'go'), ('direction', 'north')]), ('verb', 'go'))
	assert_raises(parser.ParserError, parser.parse_verb, [('stop', 'in'), ('number', 1234), ('noun', 'princess')])
	
	
def test_parse_object():
	assert_equal(parser.parse_object([('noun', 'bear'), ('verb', 'go'), ('direction', 'north')]), ('noun', 'bear'))
	assert_equal(parser.parse_object([('direction', 'south'), ('verb', 'go'), ('noun', 'bear')]), ('direction', 'south'))
	assert_raises(parser.ParserError, parser.parse_object, [('verb', 'kill'), ('stop', 'of'), ('verb', 'go')])
	
	
def test_parse_subject():
	sub1 = parser.parse_subject([('verb', 'go'), ('direction', 'north')], ('noun', 'princess'))
	assert_equal(sub1.subject, 'princess')
	assert_equal(sub1.verb, 'go')
	assert_equal(sub1.object, 'north')
	
	sub2 = parser.parse_subject([('verb', 'kill'), ('stop', 'of'), ('noun', 'bear')], ('noun', 'player'))
	assert_equal(sub2.subject, 'player')
	assert_equal(sub2.verb, 'kill')
	assert_equal(sub2.object, 'bear')
	

def test_parse_sentence():
	sen1 = parser.parse_sentence([('verb', 'kill'), ('stop', 'of'), ('noun', 'bear')])
	assert_equal(sen1.subject, 'player')
	assert_equal(sen1.verb, 'kill')
	assert_equal(sen1.object, 'bear')
	
	sen2 = parser.parse_sentence([('noun', 'princess'), ('stop', 'of'), ('verb', 'go'), ('direction', 'west')])
	assert_equal(sen2.subject, 'princess')
	assert_equal(sen2.verb, 'go')
	assert_equal(sen2.object, 'west')
	
	assert_raises(parser.ParserError, parser.parse_sentence, [('stop', 'the'), ('stop', 'of'), ('direction', 'west'), ('verb', 'go')])
测试函数可能出现的情况。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值