import nose.tools as nose
import os
import subprocess


ASSEMBLY_OUTPUT = "__test_output.obj"
SOURCE_DIR = "examples"
BINARY_DIR = "test_binaries"

def tearDownModule():
    if os.path.exists(ASSEMBLY_OUTPUT):
        os.remove(ASSEMBLY_OUTPUT)

def example(name):
    return os.path.join(SOURCE_DIR, name + ".asm")

def check_path(assembler, path):
    code = subprocess.call([assembler, path, ASSEMBLY_OUTPUT])
    nose.assert_equal(code, 0, assembler)
    
    assert path.endswith(".asm")
    binary = os.path.join(BINARY_DIR, os.path.basename(path)[:-4] + ".bin")
    if os.path.exists(binary):
        with open(ASSEMBLY_OUTPUT, "rb") as testing, open(binary, "rb") as tested:
            nose.assert_equal(testing.read(), tested.read(), assembler)


# asm.py
def test_example_asm():
    check_path("./asm.py", "example.asm")

def test_hello_asm():
    check_path("./asm.py", example("hello"))

def test_hello2_asm():
    check_path("./asm.py", example("hello2"))

def test_fibonacci_asm():
    check_path("./asm.py", example("ique_fibonacci"))


# asm_pyparsing.py
def test_example_pyparsing():
    check_path("./asm_pyparsing.py", "example.asm")

def test_hello_pyparsing():
    check_path("./asm_pyparsing.py", example("hello"))

def test_hello2_pyparsing():
    check_path("./asm_pyparsing.py", example("hello2"))

def test_fibonacci_pyparsing():
    check_path("./asm_pyparsing.py", example("ique_fibonacci"))

@@ -437,25 +437,54 @@
 ode, 0, 
-a
+%22A
 ssembl
-er
+y of %7B0%7D failed!%22.format(path)
 )%0A    %0A 
@@ -766,25 +766,54 @@
 read(), 
-assembler
+%22Produced and tested binaries differ!%22
 )%0A%0A%0A# as

Improve messaged in tests