0

在 doctest 和 shell 中使用相同的代码会产生不同的输出。
我有一个名为 a() 的函数,它运行了一些测试。
这些相同的测试用于 doctest (test() )。
使用 a() 我得到 OBJECT-BLANKLINE-OBJECT 而 test() 给我一个错误并且只显示第一个 OBJECT。
这是 doctest 模块中的缺陷吗?

这是顶部带有 a() 和 test() 的整个文件:

'''
>>> u1 = User("luis@fc.up.pt", "simples")
>>> u2 = User("ana@fc.up.pt", "complicada")
>>> u2.askFriend(u1)
>>> u1.recvRequest(u2)
>>> u1.confirmFriend(u2)
>>> p1 = Post(u1, "O ultimo post", "http://www.wikipedia.org")
>>> p2 = Post(u2, "A ultima resposta", "http://www.google.com")
>>> c = Comments()
>>> c.add(p1)
>>> c.add(p2)
>>> f1 = c.search(user="ana@fc.up.pt")
>>> print(f1)
A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
>>> f2 = c.search(likes=1)
>>> print(f2)
<BLANKLINE>
>>> f3 = c.search(text='post')
>>> print(f3)
O ultimo post
http://www.wikipedia.org
0 Gosto, 0 Nao gosto
<BLANKLINE>
A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
'''

def test():
    import doctest
    doctest.testmod()

def a():
    u1 = User("luis@fc.up.pt", "simples")
    u2 = User("ana@fc.up.pt", "complicada")
    u2.askFriend(u1)
    u1.recvRequest(u2)
    u1.confirmFriend(u2)
    p1 = Post(u1, "O ultimo post", "http://www.wikipedia.org")
    p2 = Post(u2, "A ultima resposta", "http://www.google.com")
    c = Comments()
    c.add(p1)
    c.add(p2)
    f3 = c.search(text="post")
    print(f3)

import string

class User:

    def __init__(self,email,passwd):
        self.email = email
        self.passwd = passwd
        self.name = None
        self.year = None
        self.active = True
        self.recv = [] 
        self.conf = []
        self.setPassword(self.passwd)

    def __str__(self):
        if self.name == None and self.active == True:
            return str(self.email) + ':' + 'ativa'

        elif self.name != None and self.active == True:
            return str(self.name) + ':' + str(self.email) + ':' + 'ativa'

        elif self.name != None and self.active == False:
            return str(self.name) + ':' + str(self.email) + ':' + 'inativa'

        else:
            return str(self.email) + ':' + 'inativa'

    def getEmail(self):
        return self.email

    def setPassword(self,passwd):
        abc = string.ascii_lowercase
        cifra = abc[3:] + abc[:3]
        dec = list(passwd)
        passwdCif = ""
        for i in dec:
            cif = cifra[abc.find(i)]
            passwdCif += cif
        self.passwd = passwdCif

    def getPassword(self):
        return self.passwd

    def setName(self,name):
        self.name = name

    def getName(self):
        return self.name

    def setBirth(self,year,month,day):
        self.year = year
        self.month = month
        self.day = day

    def getBirth(self):
        if self.year == None:
            return None
        else:
            return '(' + str(self.year) + ', ' + str(self.month) + ', ' + str(self.day) + ')'

    def isActive(self):
        if self.active == True:
            return True
        else:
            return False

    def setActive(self):
        self.active = True

    def setInactive(self):
        self.active = False

    def askFriend(self,u):
        self.conf.append(u)

    def recvRequest(self,u):
        self.recv.append(u)

    def confirmFriend(self,u):
        if len(self.recv) == 0:
            return None
        else:
            for i in self.recv:
                if i == u:
                    self.recv.remove(i)
                    self.conf.append(i)
                else:
                    return None


    def isFriend(self,u):
        if u in self.conf:
            return True
        elif self == u:
            return True
        else:
            return False

    def showPending(self):
        if len(self.recv) == 0:
            return None
        else:
            for i in self.recv:
                print i

    def showFriends(self):
        if len(self.conf) == 0:
            return None
        else:
            for i in self.conf:
                print i

class Post():
    def __init__(self,u,text='',link=None):
        self.u = u
        self.text = text
        self.link = link
        self.seg = None
        self.ant = None
        self.likeList = []
        self.dislikeList = []

    def __str__(self):
        if self.link == None:
            return str(self.text) + '\n' + str(len(self.likeList)) + ' Gosto, ' + str(len(self.dislikeList)) + ' Nao gosto'
        else:
            return str(self.text) + '\n' + str(self.link) + '\n' + str(len(self.likeList)) + ' Gosto, ' + str(len(self.dislikeList)) + ' Nao gosto'

    def updateText(self,text):
        self.text = text

    def updateLink(self,link):
        self.link = link

    def like(self,u):
        if (u in self.u.conf) or (u == self.u):
            if u in self.dislikeList:
                self.dislikeList.remove(u)
                self.likeList.append(u)
            elif u in self.likeList:
                return None
            else:
                self.likeList.append(u)
        else:
            return None

    def dislike(self,u):
        if (u in self.u.conf) or (u == self.u):
            if u in self.likeList:
                self.likeList.remove(u)
                self.dislikeList.append(u)
            elif u in self.dislikeList:
                return None
            else:
                self.dislikeList.append(u)
        else:
            return None

class Comments():
    def __init__(self, u=None, text='', link=None):
        self.u = u
        self.text = text
        self.link = link
        self.topo = None
        self.fim = None

    def __str__(self):
        actual = self.topo
        s = ''
        if actual == None:
            return ''
        while actual != None:
            if actual.seg == None:
                s += str(actual)
                actual = actual.seg
            elif actual.seg != None:
                s += str(actual) + '\n' + '\n'
                actual = actual.seg
        return s

    def add(self,comment):
        if self.topo == None:
            comment.ant = None
            comment.seg = None
            self.topo = comment
            self.fim = comment
        else:
            comment.ant = None
            comment.seg = self.topo
            self.topo.ant = comment
            self.topo = comment

    def remove(self,comment):
        actual = self.topo
        if (self.topo == self.fim) and (self.topo == comment):
            self.topo = None
            self.fim = None
            actual = None
        while actual!=None:
            if actual == comment:
                if actual.ant == None:
                    self.topo = actual.seg
                    actual.seg.ant = None
                elif actual.seg == None:
                    self.fim = actual.ant
                    actual.ant.seg = None
                else:
                    actual.seg.ant = actual.ant
                    actual.ant.seg = actual.seg
                break
            else:
                actual = actual.seg

    def countLike(self):
        count = 0
        actual = self.topo
        while actual != None:
            if len(actual.likeList) >= 1:
                count += 1
                actual = actual.seg
            else:
                actual = actual.seg
        return count

    def showRecentComments(self,n):
        count = 1
        actual = self.topo
        sC = ''
        if actual == None:
            return None
        while actual != None:
            if count < n:
                if actual.seg == None:
                    sC += str(actual)
                    count += 1
                    actual = actual.seg
                else:
                    sC += str(actual) + '\n' + '\n'
                    count += 1
                    actual = actual.seg
            elif count == n:
                sC += str(actual)
                count += 1
                actual = actual.seg
            elif count > n:
                break 
        print sC

    def search(self, user=None, likes=None, dislikes=None, text=None):
        result = []
        actual = self.topo
        cR = Comments()
        if actual == None:
            return None

        while actual != None:
            if user != None:
                if actual.u.email != user:
                    actual = actual.seg
                elif actual.u.email == user:
                    result.append(actual)
                    actual = actual.seg
            elif user == None:
                break
        actual = self.topo

        while actual != None:
            if likes != None:
                if likes > len(actual.likeList):
                    actual = actual.seg
                elif likes <= len(actual.likeList):
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif likes == None:
                break
        actual = self.topo

        while actual != None:
            if dislikes != None:
                if dislikes > len(actual.dislikeList):
                    actual = actual.seg
                elif dislikes <= len(actual.dislikeList):
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif dislikes == None:
                break
        actual = self.topo

        while actual != None:
            if text != None:
                if text not in actual.text:
                    actual = actual.seg
                elif text in actual.text:
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif text == None:
                break
        if len(result) != 0:
            for i in result:
                cR.add(i)
        return cR

这是我使用 a() 和 test() 得到的输出:

>>> a()
O ultimo post
http://www.wikipedia.org
0 Gosto, 0 Nao gosto

A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
>>> test()
**********************************************************************
File "__main__", line 22, in __main__
Failed example:
    print(f3)
Expected:
    O ultimo post
    http://www.wikipedia.org
    0 Gosto, 0 Nao gosto
    <BLANKLINE>
    A ultima resposta
    http://www.google.com
    0 Gosto, 0 Nao gosto
Got:
    A ultima resposta
    http://www.google.com
    0 Gosto, 0 Nao gosto
**********************************************************************
1 items had failures:
   1 of  16 in __main__
***Test Failed*** 1 failures.
>>> 

我还想为我之前问得很糟糕的问题道歉。
希望这个可以帮助我和其他人。

4

1 回答 1

2

你的 doctest 和你的函数之间有一个明显的区别:虽然你的函数 a 这样做:

# previous code
c.add(p2)
f3 = c.search(text="post")
print(f3)

你的 doctest 这样做:

# previous code
c.add(p2)
f1 = c.search(user="ana@fc.up.pt")
print(f1)
f2 = c.search(likes=1)
print(f2)
f3 = c.search(text='post')
print(f3)

我不知道那里到底发生了什么,但可能你应该编写你的函数来做同样的事情。如果结果仍然不同,则说明 doctest 有问题,否则您的代码有问题。

编辑:

现在我开始明白为什么你是麻烦了,而且是deeeeeeeep麻烦。它从搜索功能顶部的这一行开始:

actual = self.topo

我认为您不知道 Python 分配属性的方式。稍后在您的代码中更改实际,然后再次将 self.topo 分配给实际:

actual.seg = "somevalue" # sorry, i can't remember what you did"
actual = self.topo

第二行完全没有意义,因为实际已经是 self.topo!这不是 self.topo 的值,而是 self.topo。

而不是写

actual.seg = "somevalue"

你可以写

self.topo.seg = "somevalue"

两条线的作用完全相同。所以当你认为你正在改变一些独立的属性actual时,你真的一直在改变 self.topo。这意味着actual在函数的每次开始时它都不相等,但总是具有它在最后一次运行时的值c.search

我刚刚看到您也在其他一些函数中执行此操作,这意味着您每次调用此类函数时都会更改 Comments 实例 c 的状态。当然,这会导致每个测试的输出不同。

为了使这更容易理解,下面是一个带有列表的示例:

>>> a_list = ['one','two','three']
>>> b_list = a_list
>>> b_list.pop()
'three'
>>> a_list
['one', 'two']

如您所见:尽管我确实从 b_list 中弹出了元素,但它也从 a_list 中消失了。这是因为该语句的b_list = a_list字面意思是 b_list 现在与 a_list 相同。

希望这可以帮助。

于 2012-04-04T16:04:24.130 回答