console.log('imports.pyj ...\n')
from _import_one import toplevel_var, toplevel_func as tf, TopLevel, true_var, false_var
from import_two import toplevel_var2, toplevel_func2, TopLevel2 as TL2, test__init__imports, sub as imp_two_sub
import import_two.subpack.nested_mod as nested_mod
import import_two.subpack
from import_two import subpack

# test import into __init__
test__init__imports()

# test `from package import module`
assert.equal('sub', imp_two_sub.sub_var)

# test nested package
assert.equal(nested_mod.NESTED_MOD_VAR, 'nested_var')
assert.equal(nested_mod.fun(), 'nested_fun')
assert.equal(nested_mod.Foo().name, 'nested_foo')
assert.equal(new import_two.subpack.Boo().name, 'nested_foo')
assert.equal(new subpack.Boo().name, 'nested_foo')

def AClass(x):
    return this

# Test import of top-level variables and callables
assert.equal(toplevel_var, 'foo')
assert.equal(tf('x'), 'xtoplevel')
assert.equal(toplevel_var2, 'foo2')
assert.equal(toplevel_func2('x'), 'xtoplevel2')

# Test import of top-level vars in a conditional
assert.equal('true', true_var)

# Test plain imports
import _import_one
assert.equal(_import_one.toplevel_var, toplevel_var)
assert.equal(_import_one.toplevel_func('x'), tf('x'))

# Test recognition of imported classes
tl = TopLevel('1')
assert.equal(tl.a, '1')
tl2 = TL2('x')
assert.equal(tl2.a, 'x')

# Test extends class via plain import
class ExtTopLevel(_import_one.TopLevel):
    def __init__(self, v):
        a = v+'2'
        _import_one.TopLevel.__init__(self, a)
        self.b = 'b'
extl = ExtTopLevel('1')
assert.equal(extl.a, '12')
assert.equal(extl.b, 'b')

# Test access to submodules via plain imports
import import_two.sub, import_two.sub as ts
assert.equal('sub', import_two.sub.sub_var)
assert.equal('sub', ts.sub_var)
assert.equal('sub', import_two.sub.sub_func())

# Test that class accessed via plain import is
# recognized
s = import_two.sub.Sub(1)
assert.equal(s.a, 1)
s2 = ts.Sub(1)
assert.equal(s2.a, 1)


# Test that a class imported into an inner scope is not recognized as a class
# outside that scope
def inner():
    from _import_one import AClass
    a = AClass(1)
    assert.equal(a.a, 1)

inner()
b = AClass(1)
assert.equal(b, this)

# Test global symbol declared in other module
assert.equal(GLOBAL_SYMBOL, 'i am global')

# Test custom error while reading module file
try:
    rapydscript.parse(
        'import abc\n',
        {
            readfile: def(fp): raise Error('custom_error');, 
            basedir: '/' 
        }
    )
except as e: # e is the instance of ImportError that should contain readfile_error
    assert.ok(isinstance(e, rapydscript.ImportError))
    assert.ok(e.readfile_error)
    assert(e.readfile_error.message is 'custom_error')
