我遇到了一种情况,在测试用户定义的函数时,我观察到了两种不同的结果:retrieve_query_tags。当将map对象分配给contained_tags变量时,将为每个测试方法引发AssertionErrors。
然而,当map对象被胁迫到一个list中,并且该列表被分配给contained_tags变量时,所有的测试都会通过。胁迫/不强迫映射对象进入列表是什么原因造成了这种行为上的差异?
class TestQueryStringTags(SimpleTestCase):
'''Verify that any tags [...] within a search query string are cleaned'''
def setUp(self):
self.submitted_tags = [
"-- [ Pyth % on]",
"[_%!---%%%django__+___ -@& rest_framework (^) ] [api]",
"[-- django models ] ",
"$))[[django-views]]",
" [ ] [] >>>> [[]] [$&))( @%) ]"
]
self.cleaned_tags = [
["python"], ["django-rest-framework", "api"],
['djangomodels'], ["django-views"], None
]
def test_searched_query_tags(self):
for i, tag_query in enumerate(self.submitted_tags):
with self.subTest(i=i, tag_query=tag_query):
tags = retrieve_query_tags(tag_query)
self.assertEqual(tags, self.cleaned_tags[i])def retrieve_query_tags(string):
contained_tags = list(map(
lambda match: re.sub(r"[*!#$&'\"()%*+,/:;=?@\[\]<>\s]", "", match[0])
, re.finditer(r"(?<=\[)[^\[\]]+(?=\])", string)
))
if all(not tag for tag in contained_tags):
return None
tag_content = list(map(
lambda match: "-".join(
re.findall(r"([a-zA-Z]+)", match.lower())
), contained_tags
))[:2]
return tag_contenthttps://stackoverflow.com/questions/71961335
复制相似问题