"""
SuiteGenerator.py
Author: Mike Lynch
Date: 12-19-06
COMS-W4115: Prof. Edwards

Automatically divides test case programs into a functionality suite and a 
syntax suite for use by RegressionTester.py
"""

import os

syntactic = []
functionality = []
functionality_noOut = []
forceSyntactic = []

def extToDSPL(filename):
    """
    Change a program's extension from .* to .dspl and return the new
    filename
    """
    return "%sdspl" % (filename[:-3])

#go through each file in test directory and decide which test suite it belongs to
for filename in os.listdir("testcases%scorrect" % os.path.sep):
    if filename.endswith(".err"):
        syntactic.append("testcases%s%s" % (os.path.sep, filename))
    elif filename.endswith(".out"):
        firstLine = open("testcases%scorrect%s%s" % (os.path.sep, os.path.sep, filename)).readline()
        if firstLine.startswith("[["):
            functionality_noOut.append("testcases%s%s" % (os.path.sep, filename))
        else:
            functionality.append("testcases%s%s" % (os.path.sep, filename))
    else:
        print "Unrecognized extension on file: %s" % filename

count = 0

syntactic.sort();

print "Writing syntax file suite..."
synFile = open(r"testcases%ssuites%s_syntax.suite" % (os.path.sep, os.path.sep),
     "w")
for filename in syntactic:
    dsplFilename = extToDSPL(filename)
    synFile.write("%s\n" % dsplFilename)
    print dsplFilename
    count += 1
synFile.close()
print "%d files" % count
print ""
count = 0

print "Writing functionality file suite..."
funcFile = open(r"testcases%ssuites%s_functionality.suite" % (os.path.sep, os.path.sep),
     "w")
for filename in functionality:
    dsplFilename = extToDSPL(filename)
    funcFile.write("%s\n" % dsplFilename)
    print dsplFilename
    count += 1
funcFile.close()
print "%d files\n" % count


count = 0
if len(functionality_noOut) > 0:
    print "Writing functionality (no output) file suite..."
    funcNoOutFile = open(r"testcases%ssuites%s_functionality_noOut.suite" % (os.path.sep, os.path.sep),
         "w")
    for filename in functionality_noOut:
        dsplFilename = extToDSPL(filename)
        funcNoOutFile.write("%s\n" % dsplFilename)
        print dsplFilename
        count += 1
    funcNoOutFile.close()
    print "%d files" % count
