<commit_before>import unittest
from io import StringIO

from ..lekvar.lekvar import *
from .emitter import *

class LLVMEmitterTest(unittest.TestCase):
    def helloWorld(self): #TODO: Standardise tests
        return Module( {
            "helloWorld": Function([], [
                Call("print", [
                    Literal( LLVMType("String"), "Hello World!" )
                ] )
            ], LLVMType("Void")),
            "print" : ExternalFunction("puts", [LLVMType("String")], LLVMType("Int"))
        }, Function([], [
            Call("helloWorld", [])
        ], []))

    def test_helloWorld(self):
        test = self.helloWorld()
        test.verify()
        output = StringIO()
        emitter = Emitter(output)
        test.emitDefinition(emitter)
        emitter.finalize()
        print(output.getvalue())
<commit_msg>Made llvm testing automated using clang<commit_after>import unittest
from io import StringIO
from tempfile import NamedTemporaryFile
import os, subprocess

from ..lekvar.lekvar import *
from .emitter import *

class LLVMEmitterTest(unittest.TestCase):
    def helloWorld(self): #TODO: Standardise tests
        test = Module( {
            "helloWorld": Function([], [
                Call("print", [
                    Literal( LLVMType("String"), "Hello World!" )
                ] )
            ], LLVMType("Void")),
            "print" : ExternalFunction("puts", [LLVMType("String")], LLVMType("Int"))
        }, Function([], [
            Call("helloWorld", [])
        ], []))
        test.verify()
        return test

    def test_helloWorld(self):
        test = self.helloWorld()

        # Compile and run the generate llvm ir
        os.makedirs("builds", exist_ok=True)
        with open("builds/helloWorld.ll", "w") as f:
            emitter = Emitter(f)
            test.emitDefinition(emitter)
            emitter.finalize()

        # Compile with clang, asserting no errors are found
        self.assertEqual(subprocess.call(["clang -o builds/helloWorld builds/helloWorld.ll"], shell=True), 0)
        # test output
        output = subprocess.check_output(["builds/helloWorld"]).decode("utf-8")
        print(output)
        self.assertEqual(output, "Hello World!\n")

{'fraction_non_alphanumeric': 0.13228699551569506, 'fraction_numerical': 0.0008968609865470852, 'mean_word_length': 2.3002958579881656, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7519977', 'n_tokens_mistral': 649, 'n_tokens_neox': 621, 'n_words': 141}