2018-01-01 16:38:33 +01:00
|
|
|
# Copyright 2018 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
# This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
# license. See the LICENSE.
|
|
|
|
|
2017-08-06 02:29:15 +02:00
|
|
|
import hy
|
2017-04-10 02:27:51 +02:00
|
|
|
from hy.importer import (import_file_to_module, import_buffer_to_ast,
|
|
|
|
MetaLoader, get_bytecode_path)
|
2014-02-11 16:57:31 +01:00
|
|
|
from hy.errors import HyTypeError
|
2016-12-27 16:09:58 +01:00
|
|
|
import os
|
2013-04-02 04:13:45 +02:00
|
|
|
import ast
|
2017-04-10 02:27:51 +02:00
|
|
|
import tempfile
|
2017-08-06 02:29:15 +02:00
|
|
|
from fractions import Fraction
|
2018-03-31 08:51:48 +02:00
|
|
|
import pytest
|
2013-03-04 01:40:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_basics():
|
2013-03-06 04:15:45 +01:00
|
|
|
"Make sure the basics of the importer work"
|
2013-04-06 21:22:35 +02:00
|
|
|
import_file_to_module("basic",
|
|
|
|
"tests/resources/importer/basic.hy")
|
2013-04-02 04:13:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_stringer():
|
2017-03-26 01:15:10 +01:00
|
|
|
_ast = import_buffer_to_ast("(defn square [x] (* x x))", '')
|
2013-04-02 04:13:45 +02:00
|
|
|
assert type(_ast.body[0]) == ast.FunctionDef
|
2013-07-06 20:35:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_imports():
|
2016-12-27 16:09:58 +01:00
|
|
|
path = os.getcwd() + "/tests/resources/importer/a.hy"
|
|
|
|
testLoader = MetaLoader(path)
|
|
|
|
|
2013-07-06 20:35:26 +02:00
|
|
|
def _import_test():
|
|
|
|
try:
|
2016-12-27 16:09:58 +01:00
|
|
|
return testLoader.load_module("tests.resources.importer.a")
|
2013-07-06 20:35:26 +02:00
|
|
|
except:
|
|
|
|
return "Error"
|
|
|
|
|
|
|
|
assert _import_test() == "Error"
|
2013-07-06 20:39:02 +02:00
|
|
|
assert _import_test() is not None
|
2014-02-11 16:57:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_error_reporting():
|
|
|
|
"Make sure that (import) reports errors correctly."
|
|
|
|
|
|
|
|
def _import_error_test():
|
|
|
|
try:
|
|
|
|
import_buffer_to_ast("(import \"sys\")", '')
|
|
|
|
except HyTypeError:
|
|
|
|
return "Error reported"
|
|
|
|
|
|
|
|
assert _import_error_test() == "Error reported"
|
|
|
|
assert _import_error_test() is not None
|
2017-04-10 02:27:51 +02:00
|
|
|
|
|
|
|
|
2018-03-31 08:51:48 +02:00
|
|
|
@pytest.mark.skipif(os.environ.get('PYTHONDONTWRITEBYTECODE'),
|
|
|
|
reason="Bytecode generation is suppressed")
|
2017-04-10 02:27:51 +02:00
|
|
|
def test_import_autocompiles():
|
|
|
|
"Test that (import) byte-compiles the module."
|
|
|
|
|
|
|
|
f = tempfile.NamedTemporaryFile(suffix='.hy', delete=False)
|
|
|
|
f.write(b'(defn pyctest [s] (+ "X" s "Y"))')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(get_bytecode_path(f.name))
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
import_file_to_module("mymodule", f.name)
|
|
|
|
assert os.path.exists(get_bytecode_path(f.name))
|
|
|
|
|
|
|
|
os.remove(f.name)
|
|
|
|
os.remove(get_bytecode_path(f.name))
|
2017-08-06 02:29:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_eval():
|
|
|
|
def eval_str(s):
|
|
|
|
return hy.eval(hy.read_str(s))
|
|
|
|
|
|
|
|
assert eval_str('[1 2 3]') == [1, 2, 3]
|
|
|
|
assert eval_str('{"dog" "bark" "cat" "meow"}') == {
|
|
|
|
'dog': 'bark', 'cat': 'meow'}
|
|
|
|
assert eval_str('(, 1 2 3)') == (1, 2, 3)
|
|
|
|
assert eval_str('#{3 1 2}') == {1, 2, 3}
|
|
|
|
assert eval_str('1/2') == Fraction(1, 2)
|
|
|
|
assert eval_str('(.strip " fooooo ")') == 'fooooo'
|
|
|
|
assert eval_str(
|
|
|
|
'(if True "this is if true" "this is if false")') == "this is if true"
|
2018-06-07 21:18:49 +02:00
|
|
|
assert eval_str('(lfor num (range 100) :if (= (% num 2) 1) (pow num 2))') == [
|
2017-08-06 02:29:15 +02:00
|
|
|
pow(num, 2) for num in range(100) if num % 2 == 1]
|