Ejemplo n.º 1
0
 def test_eq(self) -> None:
     """test RegexParser.__eq__"""
     self.assertEqual(
         self.parser,
         parsers.RegexParser(re.compile("{"), re.compile("}"))
     )
     self.assertNotEqual(
         self.parser,
         parsers.RegexParser(re.compile("a"), re.compile("}"))
     )
     self.assertNotEqual(
         self.parser,
         parsers.RegexParser(re.compile("{"), re.compile("b"))
     )
Ejemplo n.º 2
0
 def test_from_config(self) -> None:
     """test RegexParser.from_config"""
     self.assertEqual(
         self.parser,
         parsers.RegexParser.from_config({"start": "{", "end": "}"})
     )
     self.assertEqual(
         parsers.RegexParser.from_config({}),
         parsers.RegexParser(re.compile(r"<\?pyhp\s"), re.compile(r"\s\?>"))
     )
     with self.assertRaises(Exception):
         parsers.RegexParser.from_config({"start": 42})
Ejemplo n.º 3
0
"""Tests for pyhp.caching.zipfiles"""

import unittest
import os
import re
import tempfile
import zipfile
import inspect
import importlib.util
from importlib.machinery import ModuleSpec
from datetime import datetime
from pyhp.compiler import parsers, util, generic
from pyhp.backends import zipfiles

compiler = util.Compiler(
    parsers.RegexParser(re.compile(r"<\?pyhp\s"), re.compile(r"\s\?>")),
    util.Dedenter(generic.GenericCodeBuilder(-1)))

compiler2 = util.Compiler(
    parsers.RegexParser(re.compile(r"<\?pyhp\s"), re.compile(r"\s\?>")),
    generic.GenericCodeBuilder(0)  # different than compiler!
)


class TestZIPSource(unittest.TestCase):
    """test ZIPSource"""
    def test_eq(self) -> None:
        """test ZIPSource.__eq__"""
        with zipfile.ZipFile("tests/embedding.zip", "r") as file:
            sources = [
                zipfiles.ZIPSource(file, file.getinfo("syntax.pyhp"),
Ejemplo n.º 4
0
class TestCompiler(unittest.TestCase):
    """Test the compiler facade"""

    compiler = util.Compiler(
        parsers.RegexParser(re.compile(r"\<\?pyhp\s"), re.compile(r"\s\?\>")),
        generic.GenericCodeBuilder(-1))

    def test_from_config(self) -> None:
        """test Compiler.from_config"""
        self.assertEqual(
            util.Compiler.from_config(self.compiler.parser, {"dedent": False}),
            self.compiler)
        self.assertIsInstance(
            util.Compiler.from_config(self.compiler.parser, {}).base_builder,
            util.Dedenter)
        with self.assertRaises(ValueError):
            util.Compiler.from_config(self.compiler.parser,
                                      {"optimization_level": "test"})

    def test_builder(self) -> None:
        """test if the builder is independent from the compiler"""
        self.assertFalse(self.compiler.builder() is self.compiler.base_builder)

    def test_raw(self) -> None:
        """test Compiler.compile_raw"""
        source = "text1<?pyhp code1 ?>text2<?pyhp code2 ?>"
        spec = ModuleSpec("__main__", None, origin="Test", is_package=False)
        code = self.compiler.compile_raw(source, spec)
        builder = self.compiler.builder()
        self.compiler.parser.build(source, builder)
        code2 = builder.code(spec)
        self.assertEqual(code, code2)

    def test_string(self) -> None:
        """test the compilation of strings"""
        source = "text1<?pyhp code1 ?>text2<?pyhp code2 ?>"
        code = self.compiler.compile_str(source, "Test")
        builder = self.compiler.builder()
        self.compiler.parser.build(source, builder)
        code2 = builder.code(
            ModuleSpec("__main__", None, origin="Test", is_package=False))
        self.assertEqual(code, code2)

    def test_file(self) -> None:
        """test the compilation of files"""
        path = "./tests/embedding/syntax.pyhp"
        with open(path, "r") as file:
            code = self.compiler.compile_file(file)

        builder = self.compiler.builder()
        spec = ModuleSpec("__main__", None, origin=path, is_package=False)
        spec.has_location = True
        with open(path, "r") as file:
            self.compiler.parser.build(file.read(), builder)
        self.assertEqual(code, builder.code(spec))

    def test_file_shebang(self) -> None:
        """test the handling of shebangs in files"""
        path = "./tests/embedding/shebang.pyhp"
        with open(path, "r") as file:
            code = self.compiler.compile_file(file)
        builder = self.compiler.builder()
        spec = ModuleSpec("__main__", None, origin=path, is_package=False)
        spec.has_location = True
        with open(path, "r") as file:
            file.readline()  # discard shebang
            self.compiler.parser.build(file.read(), builder, 1)
        self.assertEqual(code, builder.code(spec))

    def test_string_shebang(self) -> None:
        """test the handling of shebangs in strings"""
        source = "#!test\ntext1<?pyhp code1 ?>text2<?pyhp code2 ?>"
        code = self.compiler.compile_str(source, "Test")
        builder = self.compiler.builder()
        self.compiler.parser.build("text1<?pyhp code1 ?>text2<?pyhp code2 ?>",
                                   builder, 1)
        code2 = builder.code(
            ModuleSpec("__main__", None, origin="Test", is_package=False))
        self.assertEqual(code, code2)

    def test_filename(self) -> None:
        """test filename of SyntaxErrors"""
        spec = ModuleSpec("__main__", None, origin="42", is_package=False)
        try:
            self.compiler.compile_raw("<?pyhp a[1 ?>", spec)
        except SyntaxError as e:
            self.assertEqual(e.filename, "42")
        else:
            raise AssertionError(
                "Compiler.compile_raw did not raise a SyntaxError")
        file = StringIO("<?pyhp a[1 ?>")
        file.name = "42"
        try:
            self.compiler.compile_file(file)
        except SyntaxError as e:
            self.assertEqual(e.filename, "42")
        else:
            raise AssertionError(
                "Compiler.compile_file did not raise a SyntaxError")
Ejemplo n.º 5
0
class TestRegexParser(unittest.TestCase):
    """Test the regex parser implementation"""
    parser = parsers.RegexParser(re.compile("{"), re.compile("}"))

    def test_from_config(self) -> None:
        """test RegexParser.from_config"""
        self.assertEqual(
            self.parser,
            parsers.RegexParser.from_config({"start": "{", "end": "}"})
        )
        self.assertEqual(
            parsers.RegexParser.from_config({}),
            parsers.RegexParser(re.compile(r"<\?pyhp\s"), re.compile(r"\s\?>"))
        )
        with self.assertRaises(Exception):
            parsers.RegexParser.from_config({"start": 42})

    def test_syntax(self) -> None:
        """test basic syntax"""
        sections = list(self.parser.parse("text1{code1}\n{code2}{\ncode3\n}text3\n"))
        self.assertEqual(
            sections,
            [
                ("text1", 0, False),
                ("code1", 0, True),
                ("\n", 0, False),
                ("code2", 1, True),
                ("", 1, False),
                ("\ncode3\n", 1, True),
                ("text3\n", 3, False)
            ]
        )

    def test_missing_end(self) -> None:
        """test behavior on missing end of code section"""
        sections = list(self.parser.parse("text1{code1"))
        self.assertEqual(
            sections,
            [
                ("text1", 0, False),
                ("code1", 0, True)
            ]
        )

    def test_code_start(self) -> None:
        """test behavior on starting code section"""
        sections = list(self.parser.parse("{code1}text1"))
        self.assertEqual(
            sections,
            [
                ("", 0, False),
                ("code1", 0, True),
                ("text1", 0, False)
            ]
        )

    def test_eq(self) -> None:
        """test RegexParser.__eq__"""
        self.assertEqual(
            self.parser,
            parsers.RegexParser(re.compile("{"), re.compile("}"))
        )
        self.assertNotEqual(
            self.parser,
            parsers.RegexParser(re.compile("a"), re.compile("}"))
        )
        self.assertNotEqual(
            self.parser,
            parsers.RegexParser(re.compile("{"), re.compile("b"))
        )