sample_test_script.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/opt/local/bin/python
  2. import json
  3. import urllib
  4. import urllib2
  5. import sys
  6. from compiler import main
  7. input_file = '../test/acceptance/mirror.al'
  8. mode = "CS"
  9. test_input = 1
  10. test_output = "1"
  11. user = 'test'
  12. timeout = 10
  13. grammar_file = '../grammars/actionlanguage.g'
  14. if mode == "CS":
  15. interface = 1
  16. elif mode == "PS":
  17. interface = 0
  18. else:
  19. raise RuntimeError("Mode be either PS or CS")
  20. def set_input(value, user=user, timeout=timeout):
  21. if type(value) in (list, tuple):
  22. args = {"op": "set_input",
  23. "data": json.dumps(value),
  24. "username": user}
  25. else:
  26. args = {"op": "set_input",
  27. "element_type": "V",
  28. "value": value,
  29. "username": user}
  30. urllib2.urlopen(
  31. urllib2.Request("http://localhost:8001/", urllib.urlencode(args)),
  32. timeout=timeout).read()
  33. def get_output(user=user, timeout=timeout):
  34. return urllib2.urlopen(
  35. urllib2.Request("http://localhost:8001/",
  36. urllib.urlencode({"op": "get_output",
  37. "username": user})),
  38. timeout=timeout).read()
  39. # create user
  40. set_input('"{}"'.format(user), "user_manager")
  41. # set interface
  42. set_input(interface)
  43. # compile to code
  44. code = main(input_file, grammar_file, mode)
  45. # send code
  46. if mode == "CS":
  47. var_list = {}
  48. data = []
  49. for p in code:
  50. if isinstance(p, int):
  51. if p not in var_list:
  52. set_input(data)
  53. data = []
  54. val = get_output()
  55. val = val.split("=", 2)[1].split("&", 1)[0]
  56. var_list[p] = val
  57. continue
  58. else:
  59. val = var_list[p]
  60. t = "R"
  61. else:
  62. val = p
  63. t = "V"
  64. data.append([t, val])
  65. set_input(data)
  66. elif mode == "PS":
  67. set_input(json.dumps(code))
  68. # send input (may be a list or a single value)
  69. set_input(test_input)
  70. # get output
  71. val = get_output()
  72. val = val.split("=", 2)[2]
  73. # print("For input %s, got output %s, instead of %s" % (test_input, val,
  74. # test_output))
  75. assert val == test_output
  76. print val == test_output