+
+class Test_Regex(unittest.TestCase):
+
+ def setUp(self):
+ self.wiki = 'regextest'
+ self.wikiq_out_name = self.wiki + '.tsv'
+ self.infile = "{0}.xml.bz2".format(self.wiki)
+
+ self.input_dir = "dumps"
+ self.input_file = os.path.join(".", self.input_dir,self.infile)
+
+ if not os.path.exists("test_output"):
+ os.mkdir("test_output")
+
+ self.test_output_dir = os.path.join(".", "test_output")
+ self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
+ # we have two base calls, one for checking inputs and the other for checking outputs
+ self.base_call = "../wikiq {0}"
+ self.base_call_outs = "../wikiq {0} -o {1}"
+
+ self.baseline_output_dir = "baseline_output"
+
+ # sample inputs for checking that bad inputs get terminated / test_regex_inputs
+ self.bad_input1 = "-RP '\\b\\d+\\b'" #label is missing
+ self.bad_input2 = "-RP 'NPO V' -RP THE -RPl testlabel" #number of reg and number of labels do not match
+ self.bad_input3 = "-CP '(Tamil|Li)' -RPl testlabel" #cp but rp label
+ self.bad_input4 = "-CPl testlabel" #regex is missing
+ self.bad_input5 = "-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'"
+
+ self.bad_inputs_list = [self.bad_input1,self.bad_input2,self.bad_input3,self.bad_input4,self.bad_input5]
+
+ # sample inputs for checking the outcomes of good inputs / test_basic_regex
+ self.good_input1 = "-RP '\\b\\d{3}\\b' -RPl threedigits"
+ self.good_input2 = "-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word"
+ self.good_input3 = "-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning"
+ self.good_input4 = "-CP 'WP:EVADE' -CPl wp_evade"
+
+ self.good_inputs_list = [self.good_input1,self.good_input2,self.good_input3, self.good_input4]
+
+ # and with capture group(s) / test_capturegroup_regex
+ self.cap_input1 = "-RP 'Li Chevalier' -RPl li_cheval -CP '(?P<letter>\\b[a-zA-Z]{3}\\b)|(?P<number>\\b\\d+\\b)|(?P<cat>\\bcat\\b)' -CPl three"
+ self.cap_input2 = "-CP '(?P<a>\\bTestCaseA\\b)|(?P<b>\\bTestCaseB\\b)|(?P<c>\\bTestCaseC\\b)|(?P<d>\\bTestCaseD\\b)' -CPl testcase -RP '(?P<npov>npov|NPOV)|(?P<neutral>neutral point of view)' -RPl npov"
+
+ self.cap_inputs_list = [self.cap_input1,self.cap_input2]
+
+ def test_regex_inputs(self):
+ for input in self.bad_inputs_list:
+ call = self.base_call.format(self.input_file)
+ call = call + " --stdout " + input
+ print(call)
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
+ stdout,stderr = proc.communicate()
+ #print(proc.returncode)
+
+ # we want to check that the bad inputs were caught and sys.exit is stopping the code
+ print(stderr.decode("utf-8"))
+ self.assertNotEqual(proc.returncode,0)
+
+ def test_basic_regex(self):
+ i = 1
+ for input in self.good_inputs_list:
+
+ test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
+ #print(test_filename)
+ test_file = os.path.join(self.test_output_dir, test_filename)
+ if os.path.exists(test_file):
+ os.remove(test_file)
+
+ call = self.base_call_outs.format(self.input_file, self.test_output_dir)
+ call = call + " " + input
+ print(call)
+
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
+ proc.wait()
+
+ copyfile(self.call_output, test_file)
+ f = open(self.call_output, 'w')
+ f.close()
+
+ # don't have a baseline file to compare a test to??
+ # baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+ i += 1
+
+ #TODO
+ #a proper assert statement still needs to go here, but I checked out the generated files for now and it functions
+
+
+ def test_capturegroup_regex(self):
+ i = 1
+ for input in self.cap_inputs_list:
+ test_filename = "capturegroup_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
+ print(test_filename)
+ test_file = os.path.join(self.test_output_dir, test_filename)
+ if os.path.exists(test_file):
+ os.remove(test_file)
+
+ call = self.base_call_outs.format(self.input_file, self.test_output_dir)
+ call = call + " " + input
+ print(call)
+
+ proc = subprocess.Popen(call,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
+ proc.wait()
+
+ copyfile(self.call_output, test_file)
+ f = open(self.call_output, 'w')
+ f.close()
+
+ # don't have a baseline file to compare a test to??
+ # baseline_file = os.path.join(".", self.baseline_output_dir, test_filename)
+ i += 1
+
+ #TODO
+ #a proper assert statement still needs to go here, but I checked out the generated files for now and it functions
+