def run_file(filename): from hy.importer import import_file_to_module try: import_file_to_module("__main__", filename) except (HyTypeError, LexException) as e: if SIMPLE_TRACEBACKS: sys.stderr.write(str(e)) return 1 raise except Exception: raise return 0
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))
def run_file(filename): from hy.importer import import_file_to_module import_file_to_module("__main__", filename) return 0 # right?
def test_basics(): "Make sure the basics of the importer work" import_file_to_module("basic", "tests/resources/importer/basic.hy")
from hy.importer import import_file_to_module __hymain__ = import_file_to_module('__hymain__', 'tryhy.hy')
from hy.importer import import_file_to_module __hymain__ = import_file_to_module('__hymain__', 'main.hy')
#!/usr/bin/env python import os plugin_to_load = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../plugins/github.hy") from hy.importer import import_file_to_module g = import_file_to_module("github", plugin_to_load) def test_get_github_issue(): expected = " ".join( ["Issue #" + "180", "on", "hylang/hy", "by", "khinsen:", "Macro expansion works differently from Lisp conventions", "(open)", "[bug]", "<https://github.com/hylang/hy/issues/180>"]) actual = g.get_github_issue(None, None, "180", dry_run=True) assert expected == actual def test_get_github_commit(): expected = " ".join( ["Commit", "3e8941c", "on", "hylang/hy", "by", "Berker Peksag:", "Convert stdout and stderr to UTF-8 properly in the run_cmd helper.", "<https://github.com/hylang/hy" + "/commit/3e8941cdde01635890db524c4789f0640fe665c3>"]) actual = g.get_github_commit(None, None, "3e8941c", dry_run=True) assert expected == actual
from hy.importer import import_file_to_module from subprocess import CalledProcessError metadataparser = import_file_to_module("metadataparser", "metadataparser.hy") def test_nonexistent_font(): "test when non existent file is given to function" assert not metadataparser.get_font_metadata("nonexistent.otf") assert not metadataparser.get_font_supported_langs("nonexistent.otf") def test_invalid_font(): "Pass invalid otf file" def _invalid_font_metadata(): try: return metadataparser.get_font_metadata( "./tests/resources/notavalidotf.ttf") except CalledProcessError: return "Error" def _invalid_font_supported_langs(): try: return metadataparser.get_font_supported_langs( "./tests/resources/notavalidotf.ttf") except CalledProcessError: return "Error" assert _invalid_font_metadata() == "Error" assert _invalid_font_supported_langs() == "Error"
#!/usr/bin/env python import os plugin_to_load = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../plugins/codeeval.hy") from hy.importer import import_file_to_module g = import_file_to_module("codeeval", plugin_to_load) def test_eval_code(): expected = "[2, 2, 2, 4, 6, 10, 16, 26, 42, 68]" actual = g.eval_code(None, None, "(defn fib [x] (if (<= x 2) 2 (+ (fib (- x 1)) " + "(fib (- x 2))))) (list-comp (fib x) [x (range 10)])", dry_run=True) # strip the space resulting from eval_code function assert actual.strip() == expected.strip() def test_source_code(): expected = "def fib(x):\n return (2 if (x <= 2) else" +\ " (fib((x - 1)) + fib((x - 2))))\nprint([fib(x) for x in range(10)])" actual = g.source_code(None, None, "(defn fib [x] (if (<= x 2) 2 (+ (fib (- x 1))" + " (fib (- x 2))))) (print (list-comp (fib x)" + " [x (range 10)]))",
#!/usr/bin/env python import os plugin_to_load = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../plugins/codeeval.hy") from hy.importer import import_file_to_module g = import_file_to_module("codeeval", plugin_to_load) def test_eval_code(): expected = "[2, 2, 2, 4, 6, 10, 16, 26, 42, 68]" actual = g.eval_code(None, None, "(defn fib [x] (if (<= x 2) 2 (+ (fib (- x 1)) " + "(fib (- x 2))))) (list-comp (fib x) [x (range 10)])", dry_run=True) # strip the space resulting from eval_code function assert actual.strip() == expected.strip() def test_source_code(): expected = "def fib(x):\n return (2 if (x <= 2) else" +\ " (fib((x - 1)) + fib((x - 2))))\nprint([fib(x) for x in range(10)])" actual = g.source_code(None, None, "(defn fib [x] (if (<= x 2) 2 (+ (fib (- x 1))" + " (fib (- x 2))))) (print (list-comp (fib x)" + " [x (range 10)]))", dry_run=True) assert actual == expected