Python执行nosetests测试时出现的问题

Electro01 发布于 2017/08/29 20:43
阅读 182
收藏 0

 

首先,这是一个需要测试的代码:

class ParserError(Exception):
    pass

class Sentence(object):

    def __init__(self, subject, verb, object):
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
    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 noun or direction next.')

def parse_subject(word_list, subj):
    verb = parse_verb(word_list)
    obj = parse_object(word_list)

    return Sentence(subj, verb, obj) # 执行 class Sentence

def parse_sentence(word_list):
    skip(word_list, 'stop')

    start = peek(word_list)

    if start == 'noun':
        subj = match(word_list, 'noun')
        return parse_subject(word_list, subj)
    elif start == 'verb':
        # assume the subject is the player then
        return parse_subject(word_list, ('noun', 'player'))
    else:
        raise ParserError("Must start with subject, object, or verb not: %s" % start)

我有点不明白他书中给出的expecting具体是指的什么

def match(word_list, expecting):
    # 用pop( )的方法删除这个元素,并且它还返回这个元素
    if word_list:
        word = word_list.pop(0)

        if word[0] == expecting:
            return word
        else:
            return None
    else:
        return None

我给进去的list[('verb', 'taste'),('direction','good'),('noun','pizza')]第一个就是verb并且等于word_type :'verb'
按理说应该是匹配的,然而skipA的值是None,也就是说('verb', 'taste') != expecting 吗?

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/admin/mystuff/test/ex49_tests.py", line 38, in test_skip
    assert_equal(skipA, 'verb')
AssertionError: None != 'verb'

 

 

加载中
返回顶部
顶部