# Path: nolang/main.py
# def run_code(fname):
#     interpreter = Interpreter()
#     space.setup(interpreter)
#     try:
#         source = open(fname).read()
#     except (OSError, IOError):
#         print "Error reading file %s" % fname
#         return 1
#     # XXX error handling
#     try:
#         ast = parser.parse(lexer.lex(fname, source), ParsingState(fname,
#                            source))
#     except ParseError as pe:
#         format_parser_error(pe)
#         return 1
#     importer = Importer(space, dirname(os.path.abspath(fname)), parser, lexer)
#     dotted_name = parse_name(fname)
#     w_mod = compile_module(space, fname, dotted_name, source, ast, importer)
#     w_mod.setup(space)
#     try:
#         space.call_method(w_mod, 'main', [], None)
#     except AppError as e:
#         os.write(2, format_traceback(space, e))
#         return 1
#     return 0


from support import BaseTest, reformat_code
from nolang.main import run_code


class TestImport(BaseTest):
    def test_basic_import_self(self, tmpdir):
        main_file = tmpdir.join('main.q')
        main_file.write(reformat_code('''
            import self.foo.bar
            import self.foo

            def main() {
                print(bar(3) + foo.bar(13))
            }
            '''))

        foo_file = tmpdir.join('foo.q')
        foo_file.write(reformat_code('''
            def bar(i) {
                return i + 3
            }
        '''))

# Path: nolang/main.py
# def run_code(fname):
#     interpreter = Interpreter()
#     space.setup(interpreter)
#     try:
#         source = open(fname).read()
#     except (OSError, IOError):
#         print "Error reading file %s" % fname
#         return 1
#     # XXX error handling
#     try:
#         ast = parser.parse(lexer.lex(fname, source), ParsingState(fname,
#                            source))
#     except ParseError as pe:
#         format_parser_error(pe)
#         return 1
#     importer = Importer(space, dirname(os.path.abspath(fname)), parser, lexer)
#     dotted_name = parse_name(fname)
#     w_mod = compile_module(space, fname, dotted_name, source, ast, importer)
#     w_mod.setup(space)
#     try:
#         space.call_method(w_mod, 'main', [], None)
#     except AppError as e:
#         os.write(2, format_traceback(space, e))
#         return 1
#     return 0


# Path: tests/test_import.py
from support import BaseTest, reformat_code
from nolang.main import run_code



class TestImport(BaseTest):
    def test_basic_import_self(self, tmpdir):
        main_file = tmpdir.join('main.q')
        main_file.write(reformat_code('''
            import self.foo.bar
            import self.foo

            def main() {
                print(bar(3) + foo.bar(13))
            }
            '''))

        foo_file = tmpdir.join('foo.q')
        foo_file.write(reformat_code('''
            def bar(i) {
                return i + 3
            }
        '''))

        run_code(str(main_file))