'Functional test for the automatic reloader using either
\'%autoreload 1\' or \'%autoreload 2\''

def _check_smoketest(self, use_aimport=True):

    (mod_name, mod_fn) = self.new_module("\nx    =    9\n\nz    =    123        #    this    item    will    be    deleted\n\ndef    foo(y):\n                return    y    +    3\n\nclass    Baz(object):\n                def    __init__(self,    x):\n                                self.x    =    x\n                def    bar(self,    y):\n                                return    self.x    +    y\n                @property\n                def    quux(self):\n                                return    42\n                def    zzz(self):\n                                '''This    method    will    be    deleted    below'''\n                                return    99\n\nclass    Bar:                #    old-style    class:    weakref    doesn't    work    for    it    on    Python    <    2.7\n                def    foo(self):\n                                return    1\n")
    if use_aimport:
        self.shell.magic_autoreload('1')
        self.shell.magic_aimport(mod_name)
        stream = StringIO()
        self.shell.magic_aimport('', stream=stream)
        nt.assert_in(('Modules    to    reload:\n%s' % mod_name), stream.getvalue())
        with nt.assert_raises(ImportError):
            self.shell.magic_aimport('tmpmod_as318989e89ds')
    else:
        self.shell.magic_autoreload('2')
        self.shell.run_code(('import    %s' % mod_name))
        stream = StringIO()
        self.shell.magic_aimport('', stream=stream)
        nt.assert_true(('Modules    to    reload:\nall-except-skipped' in stream.getvalue()))
    nt.assert_in(mod_name, self.shell.ns)
    mod = sys.modules[mod_name]
    old_foo = mod.foo
    old_obj = mod.Baz(9)
    old_obj2 = mod.Bar()
    def check_module_contents():
        nt.assert_equal(mod.x, 9)
        nt.assert_equal(mod.z, 123)
        nt.assert_equal(old_foo(0), 3)
        nt.assert_equal(mod.foo(0), 3)
        obj = mod.Baz(9)
        nt.assert_equal(old_obj.bar(1), 10)
        nt.assert_equal(obj.bar(1), 10)
        nt.assert_equal(obj.quux, 42)
        nt.assert_equal(obj.zzz(), 99)
        obj2 = mod.Bar()
        nt.assert_equal(old_obj2.foo(), 1)
        nt.assert_equal(obj2.foo(), 1)
    check_module_contents()
    self.write_file(mod_fn, '\na    syntax    error\n')
    with tt.AssertPrints(('[autoreload    of    %s    failed:' % mod_name), channel='stderr'):
        self.shell.run_code('pass')
    with tt.AssertNotPrints(('[autoreload    of    %s    failed:' % mod_name), channel='stderr'):
        self.shell.run_code('pass')
    check_module_contents()
    self.write_file(mod_fn, '\nx    =    10\n\ndef    foo(y):\n                return    y    +    4\n\nclass    Baz(object):\n                def    __init__(self,    x):\n                                self.x    =    x\n                def    bar(self,    y):\n                                return    self.x    +    y    +    1\n                @property\n                def    quux(self):\n                                return    43\n\nclass    Bar:                #    old-style    class\n                def    foo(self):\n                                return    2\n')
    def check_module_contents():
        nt.assert_equal(mod.x, 10)
        nt.assert_false(hasattr(mod, 'z'))
        nt.assert_equal(old_foo(0), 4)
        nt.assert_equal(mod.foo(0), 4)
        obj = mod.Baz(9)
        nt.assert_equal(old_obj.bar(1), 11)
        nt.assert_equal(obj.bar(1), 11)
        nt.assert_equal(old_obj.quux, 43)
        nt.assert_equal(obj.quux, 43)
        nt.assert_false(hasattr(old_obj, 'zzz'))
        nt.assert_false(hasattr(obj, 'zzz'))
        obj2 = mod.Bar()
        nt.assert_equal(old_obj2.foo(), 2)
        nt.assert_equal(obj2.foo(), 2)
    self.shell.run_code('pass')
    check_module_contents()
    os.unlink(mod_fn)
    self.shell.run_code('pass')
    check_module_contents()
    if use_aimport:
        self.shell.magic_aimport(('-' + mod_name))
        stream = StringIO()
        self.shell.magic_aimport('', stream=stream)
        nt.assert_true((('Modules    to    skip:\n%s' % mod_name) in stream.getvalue()))
        self.shell.magic_aimport('-tmpmod_as318989e89ds')
    else:
        self.shell.magic_autoreload('0')
    self.write_file(mod_fn, '\nx    =    -99\n')
    self.shell.run_code('pass')
    self.shell.run_code('pass')
    check_module_contents()
    if use_aimport:
        self.shell.magic_aimport(mod_name)
    else:
        self.shell.magic_autoreload('')
    self.shell.run_code('pass')
    nt.assert_equal(mod.x, (-99))
