In this thread I'll discuss issues related to unit tests for the python importer.
The unit tests will change
I've said that the new importer must pass all existing unit tests, but that's not exactly true, for the following reasons:
1. The new importer won't necessarily generate the same nodes.
2. I shall soon change some of the existing unit tests.
The real requirements:
1. The new importer must pass all perfect-import tests.
2. The new importer must generate reasonable nodes, preferably by using the existing python post pass.
The structure of typical unit tests
Let's use test_basic_nesting_1 as an example. It is:
def test_basic_nesting_1(self):
s = """
import sys
def f1():
pass
class Class1:
def method11():
pass
def method12():
pass
a = 2
def f2():
pass
# An outer comment
@myClassDecorator
class Class2:
@myDecorator
def method21():
pass
def method22():
pass
# About main.
def main():
pass
if __name__ == '__main__':
main()
"""
p = self.run_test(s, verbose=False)
self.check_headlines(p, (
(1, 'Organizer: Declarations'),
(1, 'f1'),
(1, 'class Class1'),
(2, 'method11'),
(2, 'method12'),
(1, 'Organizer: a = 2'),
(1, 'f2'),
(1, 'class Class2'),
(2, 'method21'),
(2, 'method22'),
(1, 'main'),
))
This is an important test. It verifies that common python code will import correctly, and will have the expected structure. The line:
p = self.run_test(s, verbose=False)
contains the perfect import test. That is, BaseTestImporter.run_test will fail if writing p does not generate (modulo blank lines) the string s.
The call to
BaseTestImporter.check_headlines has the form:
self.check_headlines(p, (
<< table of tuples (node level, node headline) >>
))
Clearly, the table may have to change. During early development, I would be OK with disabling such calls altogether.
However, it's important to handle decorators correctly! They should not reside in their own nodes.
Converting old-style structure tests
A cff reveals that 17 unit tests should be converted to call check_headlines. At present, they use clumsy, by-hand code that is roughly equivalent. I'll do the conversion today or tomorrow and push the result to devel.
Summary
BaseTestImporter.run_test verifies perfect importer.
BaseTestImporter.check_headlines runs only if the perfect-import checks pass. That table that check_headlines uses may have to change. That's fine with me, provided that decorators are placed correctly.
I shall soon convert all tests so that they use check_headlines rather than the more verbose equivalent.
All questions and comments are welcome.
Edward