/run_tests.py

https://gitlab.com/thomasave/Parsodus · Python · 107 lines · 67 code · 19 blank · 21 comment · 8 complexity · bdd3ad641f7b1110d340e7e854a7f167 MD5 · raw file

  1. #!/usr/bin/python3
  2. # Parsodus - A language agnostic parser generator
  3. # Copyright © 2016-2017 Thomas Avé, Robin Jadoul, Kobe Wullaert
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  19. # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  21. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. import unittest, subprocess, filecmp, argparse, os.path, os, shutil
  23. REFERENCE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_data")
  24. JSON_TESTS = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1]
  25. BF_TESTS = [0, 0, 0, 0, 0, 0]
  26. def make_json_test(idx, should_fail=0):
  27. def test(self):
  28. inpath = os.path.join(REFERENCE_DIR, "json%03d.json" % idx)
  29. infile = open(inpath)
  30. outpath1 = os.path.join(data_dir, "json%03d.out1.json" % idx)
  31. outfile1 = open(outpath1, "w")
  32. outpath2 = os.path.join(data_dir, "json%03d.out2.json" % idx)
  33. outfile2 = open(outpath2, "w")
  34. p = subprocess.Popen([os.path.join(args.builddir, "examples", "json", "json")], stdin=infile, stdout=outfile1, stderr=subprocess.DEVNULL)
  35. p.communicate()
  36. infile.close()
  37. outfile1.close()
  38. if should_fail == 1:
  39. outfile2.close()
  40. self.assertNotEqual(0, p.returncode)
  41. return
  42. self.assertEqual(0, p.returncode)
  43. outfile1 = open(outpath1)
  44. p = subprocess.Popen([os.path.join(args.builddir, "examples", "json", "json")], stdin=outfile1, stdout=outfile2, stderr=subprocess.DEVNULL)
  45. p.communicate()
  46. outfile1.close()
  47. outfile2.close()
  48. if should_fail == 2:
  49. self.assertNotEqual(0, p.returncode)
  50. return
  51. self.assertEqual(0, p.returncode)
  52. self.assertTrue(filecmp.cmp(outpath1, outpath2), "Testcase %d for json example failed" % idx)
  53. return test
  54. def make_bf_test(idx, should_fail=False):
  55. def test(self):
  56. argpath = os.path.join(REFERENCE_DIR, "bf%03d.bf" % idx)
  57. inpath = os.path.join(REFERENCE_DIR, "bf%03d.in" % idx)
  58. infile = open(inpath)
  59. exppath = os.path.join(REFERENCE_DIR, "bf%03d.exp" % idx)
  60. outpath = os.path.join(data_dir, "bf%03d.out" % idx)
  61. outfile = open(outpath, "w")
  62. p = subprocess.Popen([os.path.join(args.builddir, "examples", "brainfuck", "bf"), argpath], stdin=infile, stdout=outfile, stderr=subprocess.DEVNULL)
  63. p.communicate()
  64. infile.close()
  65. outfile.close()
  66. if should_fail:
  67. self.assertNotEqual(0, p.returncode)
  68. return
  69. self.assertEqual(0, p.returncode)
  70. self.assertTrue(filecmp.cmp(exppath, outpath), "Testcase %d for bf example failed" % idx)
  71. return test
  72. class Tests(unittest.TestCase):
  73. pass
  74. if __name__ == "__main__":
  75. parser = argparse.ArgumentParser()
  76. parser.add_argument("--builddir", metavar="builddir", default=os.path.join(os.path.abspath(os.path.dirname(__file__)), "build"), required=False)
  77. args = parser.parse_args()
  78. data_dir = os.path.join(args.builddir, "test_data_out")
  79. try:
  80. shutil.rmtree(data_dir)
  81. except Exception as err:
  82. pass
  83. os.mkdir(data_dir)
  84. for i, should_fail in enumerate(JSON_TESTS):
  85. setattr(Tests, "test_json_%03d" % (i + 1), make_json_test(i + 1, should_fail))
  86. for i, should_fail in enumerate(BF_TESTS):
  87. setattr(Tests, "test_bf_%03d" % (i + 1), make_bf_test(i + 1, should_fail))
  88. unittest.main()