import unittest
from gradescope_utils.autograder_utils.decorators import number, weight
import subprocess

def nonEmptyLine(s):
        return len(s.strip()) != 0
    
def stripstr(s):
    return s.strip()

def removeEmptyLines(text):
    lst = filter(nonEmptyLine, list(map(stripstr, text.split("\n"))))
    return "\n".join(list(lst))

class TestDiff(unittest.TestCase):
    def setUp(self):
        pass 

    # Associated test number within Gradescope
    @number("0")
    # Associated point value within Gradescope
    @weight(5)
    def test_Compile(self):
        # Title used by Gradescope 
        """Clean Compile"""

        # Create a subprocess to run the students make file to ensure it compiles
        test = subprocess.Popen(["make"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = test.stderr.read().strip().decode('utf-8')
        test.kill()

        # Standard unit test case with an associated error message
        self.assertTrue(output == "", msg=output)
        test.terminate()
        
    # Associated test number within Gradescope
    @number("1")
    # Associated point value within Gradescope
    @weight(25)
    def test_Output(self):
        # Title used by Gradescope 
        """Check that program prints correct output"""

        # Create a subprocess to run the students code to obtain an output
        test = subprocess.Popen(["./PA2.out"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        
        # Try to decode stdout
        try:
            output = test.stdout.read().strip().decode('utf-8')
            test.kill()
            
            # Open reference output and decode
            reference = open('reference/expected.txt', 'rb').read().strip().decode('utf-8')
            
            # Remove empty lines from both output and reference
            output = removeEmptyLines(output)
            reference = removeEmptyLines(reference)
            
            # Check that output and reference are equal
            self.assertEqual(output, reference, msg='Program output does not match expected output')
        # Catch exception for decode error
        except (UnicodeDecodeError):
            test.kill()
            msg = 'Your program printed a character that the autograder cannot decode. Ensure your program prints valid characters.'
            # Use assertTrue to print regular message as test failure in Gradescope
            self.assertTrue(False, msg)
        
        test.terminate()