diff --git a/Dockerfile b/Dockerfile index 0dadb15..f4c67fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.4 +FROM regexpress/base:latest COPY Python3Tester.py /root/Python3Tester.py COPY Python3TesterTest.py /root/Python3TesterTest.py @@ -9,7 +9,7 @@ RUN apk add --no-cache python3 && \ pip3 install --upgrade pip setuptools && \ rm -r /root/.cache && \ cd /root && \ - echo "python3 /root/Python3Tester.py \"\$@\"" > run.sh && \ + echo "arg=();for var in \"\$@\";do arg+=(\"\$(echo -n \"\$var\" | base64 -d)\"); done; python3 /root/Python3Tester.py \"\${arg[@]}\"" > run.sh && \ chmod 755 run.sh -ENTRYPOINT ["/bin/sh", "/root/run.sh"] \ No newline at end of file +ENTRYPOINT ["/bin/bash", "/root/run.sh"] \ No newline at end of file diff --git a/Python3Tester.py b/Python3Tester.py index 75aadfc..ffe8e60 100644 --- a/Python3Tester.py +++ b/Python3Tester.py @@ -46,13 +46,18 @@ def test_regex(self, config, test_strings): result["result"]["resultList"].append(False) elif test_type == "group": result["type"] = "GROUP" + result["result"]["columns"] = [] + + for i in range(0, pattern.groups + 1): + result["result"]["columns"].append("Group #" + str(i)) + for test_string in test_strings: iterator = pattern.finditer(test_string) groupsList = {"list": []} result["result"]["resultList"].append(groupsList) for match in iterator: - groups = [] + groups = [match.group(0)] for group in match.groups(): groups.append(group) groupsList["list"].append(groups); @@ -66,21 +71,20 @@ def test_regex(self, config, test_strings): result["result"]["resultList"].append(replaced) elif test_type == "findall": result["type"] = "GROUP" + result["result"]["columns"] = ["Found"]; for test_string in test_strings: - groupsList = {"list": [[]]} + groupsList = {"list": []} result["result"]["resultList"].append(groupsList) found = pattern.findall(test_string) for word in found: - groupsList["list"][0].append(word); + groupsList["list"].append([word]); except Exception as e: result = { "exception": str(e) } - print("##START_RESULT##") - print(json.dumps(result)) - print("##END_RESULT##") + return result def main(): @@ -88,7 +92,11 @@ def main(): test_strings = json.loads(sys.argv[2]) tester = Python3Tester() - tester.test_regex(config, test_strings) + result = tester.test_regex(config, test_strings) + + print("##START_RESULT##") + print(json.dumps(result)) + print("##END_RESULT##") if __name__ == "__main__": diff --git a/Python3TesterTest.py b/Python3TesterTest.py index d177f66..a01ebdc 100644 --- a/Python3TesterTest.py +++ b/Python3TesterTest.py @@ -1,33 +1,52 @@ -import re -import json -import sys +import unittest from Python3Tester import Python3Tester -tester = Python3Tester() -test_strings =["Hello Python Test!", "Hello2 Python2 Test2"] - -config = { - "test_type": "match", - "regex": "([A-Z])" -} -tester.test_regex(config, test_strings) - -config = { - "test_type": "group", - "regex": "([A-Za-z]*)" -} -tester.test_regex(config, test_strings) - -config = { - "test_type": "replace", - "regex": "([A-Z])", - "replaceString": "\\1_" -} -tester.test_regex(config, test_strings) - -config = { - "test_type": "findall", - "regex": "([A-Z])" -} -tester.test_regex(config, test_strings) \ No newline at end of file + +class Python3TesterTest(unittest.TestCase): + tester = Python3Tester() + test_strings = ["Hello Python Test!", "Hello2 Python2 Test2"] + + def test_match(self): + config = { + "test_type": "match", + "regex": "([A-Z])" + } + result = self.tester.test_regex(config, self.test_strings) + self.assertEqual(result["result"]["resultList"][0], True) + self.assertEqual(result["result"]["resultList"][1], True) + + def test_group(self): + config = { + "test_type": "group", + "regex": "([A-Za-z]*)" + } + result = self.tester.test_regex(config, self.test_strings) + self.assertEqual(result["result"]["resultList"][0]["list"][0], ["Hello", "Hello"]) + self.assertEqual(result["result"]["resultList"][0]["list"][2], ["Python", "Python"]) + self.assertEqual(result["result"]["resultList"][0]["list"][4], ["Test", "Test"]) + self.assertEqual(result["result"]["resultList"][1]["list"][0], ["Hello", "Hello"]) + self.assertEqual(result["result"]["resultList"][1]["list"][3], ["Python", "Python"]) + self.assertEqual(result["result"]["resultList"][1]["list"][6], ["Test", "Test"]) + + def test_replace(self): + config = { + "test_type": "replace", + "regex": "([A-Z])", + "replace": "\\1_" + } + result = self.tester.test_regex(config, self.test_strings) + self.assertEqual(result["result"]["resultList"][0], "H_ello P_ython T_est!") + self.assertEqual(result["result"]["resultList"][1], "H_ello2 P_ython2 T_est2") + + def test_findall(self): + config = { + "test_type": "findall", + "regex": "([A-Z])" + } + result = self.tester.test_regex(config, self.test_strings) + self.assertEqual(result["result"]["resultList"][0]["list"], [["H"], ["P"], ["T"]]) + self.assertEqual(result["result"]["resultList"][1]["list"], [["H"], ["P"], ["T"]]) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file