import sys
import subprocess
import pandas as pd

project = sys.argv[1]
project = project[0].upper() + project[1:] # first letter has to be capitalized
if project == 'Chart':
	versionRange = 27
if project == 'Lang':
	versionRange = 66
if project == 'Math':
        versionRange = 107

def getTests(project, version, attribute): # attribute can be strong or weak
	csvFile = "/home/user/Projects/499/Oracles/" + project
	if attribute == "strong":
		csvFile = csvFile + "/Strong"
	elif attribute == "weak":
		csvFile = csvFile + "/Weak"

	csvFile = csvFile + "/" + project + "_Pass.csv"
	df = pd.read_csv(csvFile)
	df = df[df.VERSION == version]
	df = df['TESTNAME'].tolist()
	df = set(df) # remove duplicates
	return df

def getTestCases(testClass, version):
	with open("/home/user/Projects/499/" + project + "/v" + str(version) + "/daikon-all-tests.txt") as file: # read all the tests
		allTestCases = file.read().splitlines()
	found = []
	for testCase in allTestCases:
		if testClass in testCase: #if we found a test case that belongs to the test class
			found.append(testCase)
	return found

command = './get_failing.sh ' + project + ' ' + str(versionRange)
subprocess.call(command.split())


for i in range(1,versionRange):

	# Correct the formatting of the all-tests.txt file generated by defects4j

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/all-tests.txt") as file:
		content = file.readlines()
	content = [x.strip() for x in content] 
	content = [x.split('(')[1][:-1] + '::' + x.split('(')[0] for x in content]
	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/daikon-all-tests.txt",'w') as outFile:
		for line in content:
			outFile.write(line + '\n')

	# Get which tests are strong and weak

	strong = getTests(project, i, "strong")
	weak = getTests(project, i, "weak")

	weak = weak - strong # remove occurences of strong cc from weak
	strong = set([".".join(x.split('.')[:-1]) + '::' + x.split('.')[-1] for x in strong]) # correct formatting for daikon
	weak = set([".".join(x.split('.')[:-1]) + '::' + x.split('.')[-1] for x in weak])

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/strong.txt",'w') as outFile: # write strong cc
		for line in strong:
			outFile.write(line + '\n')

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/weak.txt",'w') as outFile: # write weak cc
		for line in weak:
			outFile.write(line + '\n')

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/daikon-all-tests.txt") as file: # read all the tests
		allTests = file.read().splitlines()

	allTests = set(allTests) # remove duplicates

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/fail.txt") as file: # read failing tests (generated by a bash script)
		failingTests = file.read().splitlines()

	failingTests = set(failingTests)
	passingTests = set(allTests)

	passingTests = passingTests - failingTests
	passingTests = passingTests - strong
	passingTests = passingTests - weak

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/pass.txt",'w') as outFile: # write passing tests
		for line in passingTests:
			outFile.write(line + '\n')

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/all-tests.txt") as file: # read all the tests
		allTests = file.read().splitlines()
	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/strong.txt") as file: # read all the tests
		strongTests = file.read().splitlines()
	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/weak.txt") as file: # read all the tests
		weakTests = file.read().splitlines()
	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/fail.txt") as file: # read all the tests
		failTests = file.read().splitlines()

	allTests = set(allTests)
	strongTests = set(strongTests)
	weakTests = set(weakTests)
	failTests = set(failTests)
	allTestClasses = []
	for line in allTests:
		line = line.split('(')[1][:-1]
		allTestClasses.append(line)
	allTestClasses = set(allTestClasses)

	allStrongClasses = []
	for line in strongTests:
		line = line.split('::')[0]
		allStrongClasses.append(line)
	allStrongClasses = set(allStrongClasses)

	allWeakClasses = []
	for line in weakTests:
		line = line.split('::')[0]
		allWeakClasses.append(line)
	allWeakClasses = set(allWeakClasses)

	allFailClasses = []
	for line in failTests:
		line = line.split('::')[0]
		allFailClasses.append(line)
	allFailClasses = set(allFailClasses)

	allPassClasses = allTestClasses - allStrongClasses
	allPassClasses = allPassClasses - allWeakClasses
	allPassClasses = allPassClasses - allFailClasses

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/pass.txt",'w') as outFile: # write strong cc
		for line in allPassClasses:
			outFile.write(line + '\n')

	allOtherClasses = allTestClasses - allPassClasses
	allOtherCases = []
	for testClass in allOtherClasses:
		allOtherCases = allOtherCases + getTestCases(testClass, i) # get all the test cases that are not part of the allPassClasses list

	allOtherCases = set(allOtherCases)
	allOtherCases = allOtherCases - strongTests
	allOtherCases = allOtherCases - weakTests
	allOtherCases = allOtherCases - failTests

	with open("/home/user/Projects/499/" + project + "/v" + str(i) + "/pass.txt",'a') as outFile: # write passing tests
		for line in allOtherCases:
			outFile.write(line + '\n')

