Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testHost.py 8.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import sys
  2. import unittest
  3. import pythoncom
  4. import win32com.server.policy
  5. import win32com.test.util
  6. from win32com.axscript import axscript
  7. from win32com.axscript.server import axsite
  8. from win32com.axscript.server.error import Exception
  9. from win32com.client.dynamic import Dispatch
  10. from win32com.server import connect, util
  11. from win32com.server.exception import COMException
  12. verbose = "-v" in sys.argv
  13. class MySite(axsite.AXSite):
  14. def __init__(self, *args):
  15. self.exception_seen = None
  16. axsite.AXSite.__init__(self, *args)
  17. def OnScriptError(self, error):
  18. self.exception_seen = exc = error.GetExceptionInfo()
  19. context, line, char = error.GetSourcePosition()
  20. if not verbose:
  21. return
  22. print(" >Exception:", exc[1])
  23. try:
  24. st = error.GetSourceLineText()
  25. except pythoncom.com_error:
  26. st = None
  27. if st is None:
  28. st = ""
  29. text = st + "\n" + (" " * (char - 1)) + "^" + "\n" + exc[2]
  30. for line in text.splitlines():
  31. print(" >" + line)
  32. class MyCollection(util.Collection):
  33. def _NewEnum(self):
  34. return util.Collection._NewEnum(self)
  35. class Test:
  36. _public_methods_ = ["echo", "fail"]
  37. _public_attrs_ = ["collection"]
  38. def __init__(self):
  39. self.verbose = verbose
  40. self.collection = util.wrap(MyCollection([1, "Two", 3]))
  41. self.last = ""
  42. self.fail_called = 0
  43. # self._connect_server_ = TestConnectServer(self)
  44. def echo(self, *args):
  45. self.last = "".join([str(s) for s in args])
  46. if self.verbose:
  47. for arg in args:
  48. print(arg, end=" ")
  49. print()
  50. def fail(self, *args):
  51. print("**** fail() called ***")
  52. for arg in args:
  53. print(arg, end=" ")
  54. print()
  55. self.fail_called = 1
  56. # self._connect_server_.Broadcast(last)
  57. #### Connections currently wont work, as there is no way for the engine to
  58. #### know what events we support. We need typeinfo support.
  59. IID_ITestEvents = pythoncom.MakeIID("{8EB72F90-0D44-11d1-9C4B-00AA00125A98}")
  60. class TestConnectServer(connect.ConnectableServer):
  61. _connect_interfaces_ = [IID_ITestEvents]
  62. # The single public method that the client can call on us
  63. # (ie, as a normal COM server, this exposes just this single method.
  64. def __init__(self, object):
  65. self.object = object
  66. def Broadcast(self, arg):
  67. # Simply broadcast a notification.
  68. self._BroadcastNotify(self.NotifyDoneIt, (arg,))
  69. def NotifyDoneIt(self, interface, arg):
  70. interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)
  71. VBScript = """\
  72. prop = "Property Value"
  73. sub hello(arg1)
  74. test.echo arg1
  75. end sub
  76. sub testcollection
  77. if test.collection.Item(0) <> 1 then
  78. test.fail("Index 0 was wrong")
  79. end if
  80. if test.collection.Item(1) <> "Two" then
  81. test.fail("Index 1 was wrong")
  82. end if
  83. if test.collection.Item(2) <> 3 then
  84. test.fail("Index 2 was wrong")
  85. end if
  86. num = 0
  87. for each item in test.collection
  88. num = num + 1
  89. next
  90. if num <> 3 then
  91. test.fail("Collection didn't have 3 items")
  92. end if
  93. end sub
  94. """
  95. PyScript = """\
  96. # A unicode \xa9omment.
  97. prop = "Property Value"
  98. def hello(arg1):
  99. test.echo(arg1)
  100. def testcollection():
  101. # test.collection[1] = "New one"
  102. got = []
  103. for item in test.collection:
  104. got.append(item)
  105. if got != [1, "Two", 3]:
  106. test.fail("Didn't get the collection")
  107. pass
  108. """
  109. # XXX - needs py3k work! Throwing a bytes string with an extended char
  110. # doesn't make much sense, but py2x allows it. What it gets upset with
  111. # is a real unicode arg - which is the only thing py3k allows!
  112. PyScript_Exc = """\
  113. def hello(arg1):
  114. raise RuntimeError("exc with extended \xa9har")
  115. """
  116. ErrScript = """\
  117. bad code for everyone!
  118. """
  119. state_map = {
  120. axscript.SCRIPTSTATE_UNINITIALIZED: "SCRIPTSTATE_UNINITIALIZED",
  121. axscript.SCRIPTSTATE_INITIALIZED: "SCRIPTSTATE_INITIALIZED",
  122. axscript.SCRIPTSTATE_STARTED: "SCRIPTSTATE_STARTED",
  123. axscript.SCRIPTSTATE_CONNECTED: "SCRIPTSTATE_CONNECTED",
  124. axscript.SCRIPTSTATE_DISCONNECTED: "SCRIPTSTATE_DISCONNECTED",
  125. axscript.SCRIPTSTATE_CLOSED: "SCRIPTSTATE_CLOSED",
  126. }
  127. def _CheckEngineState(engine, name, state):
  128. got = engine.engine.eScript.GetScriptState()
  129. if got != state:
  130. got_name = state_map.get(got, str(got))
  131. state_name = state_map.get(state, str(state))
  132. raise RuntimeError(
  133. "Warning - engine %s has state %s, but expected %s"
  134. % (name, got_name, state_name)
  135. )
  136. class EngineTester(win32com.test.util.TestCase):
  137. def _TestEngine(self, engineName, code, expected_exc=None):
  138. echoer = Test()
  139. model = {
  140. "test": util.wrap(echoer),
  141. }
  142. site = MySite(model)
  143. engine = site._AddEngine(engineName)
  144. try:
  145. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_INITIALIZED)
  146. engine.AddCode(code)
  147. engine.Start()
  148. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_STARTED)
  149. self.assertTrue(not echoer.fail_called, "Fail should not have been called")
  150. # Now call into the scripts IDispatch
  151. ob = Dispatch(engine.GetScriptDispatch())
  152. try:
  153. ob.hello("Goober")
  154. self.assertTrue(
  155. expected_exc is None,
  156. "Expected %r, but no exception seen" % (expected_exc,),
  157. )
  158. except pythoncom.com_error:
  159. if expected_exc is None:
  160. self.fail(
  161. "Unexpected failure from script code: %s"
  162. % (site.exception_seen,)
  163. )
  164. if expected_exc not in site.exception_seen[2]:
  165. self.fail(
  166. "Could not find %r in %r"
  167. % (expected_exc, site.exception_seen[2])
  168. )
  169. return
  170. self.assertEqual(echoer.last, "Goober")
  171. self.assertEqual(str(ob.prop), "Property Value")
  172. ob.testcollection()
  173. self.assertTrue(not echoer.fail_called, "Fail should not have been called")
  174. # Now make sure my engines can evaluate stuff.
  175. result = engine.eParse.ParseScriptText(
  176. "1+1", None, None, None, 0, 0, axscript.SCRIPTTEXT_ISEXPRESSION
  177. )
  178. self.assertEqual(result, 2)
  179. # re-initialize to make sure it transitions back to initialized again.
  180. engine.SetScriptState(axscript.SCRIPTSTATE_INITIALIZED)
  181. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_INITIALIZED)
  182. engine.Start()
  183. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_STARTED)
  184. # Transition back to initialized, then through connected too.
  185. engine.SetScriptState(axscript.SCRIPTSTATE_INITIALIZED)
  186. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_INITIALIZED)
  187. engine.SetScriptState(axscript.SCRIPTSTATE_CONNECTED)
  188. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_CONNECTED)
  189. engine.SetScriptState(axscript.SCRIPTSTATE_INITIALIZED)
  190. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_INITIALIZED)
  191. engine.SetScriptState(axscript.SCRIPTSTATE_CONNECTED)
  192. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_CONNECTED)
  193. engine.SetScriptState(axscript.SCRIPTSTATE_DISCONNECTED)
  194. _CheckEngineState(site, engineName, axscript.SCRIPTSTATE_DISCONNECTED)
  195. finally:
  196. engine.Close()
  197. engine = None
  198. site = None
  199. def testVB(self):
  200. self._TestEngine("VBScript", VBScript)
  201. def testPython(self):
  202. self._TestEngine("Python", PyScript)
  203. def testPythonUnicodeError(self):
  204. self._TestEngine("Python", PyScript)
  205. def testVBExceptions(self):
  206. self.assertRaises(pythoncom.com_error, self._TestEngine, "VBScript", ErrScript)
  207. def testPythonExceptions(self):
  208. expected = "RuntimeError: exc with extended \xa9har"
  209. self._TestEngine("Python", PyScript_Exc, expected)
  210. if __name__ == "__main__":
  211. unittest.main()