PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/Tests/test_exceptionconverter.py

https://github.com/thomo13/ironruby
Python | 142 lines | 87 code | 35 blank | 20 comment | 14 complexity | 992ceaf3c5fef108f46ac0194606961d MD5 | raw file
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. skiptest("win32")
  17. load_iron_python_test()
  18. import IronPythonTest
  19. import System
  20. #from IronPython.Runtime.Exceptions import ExceptionConverter as EC
  21. #from IronPython.Runtime.Exceptions import ExceptionMapping
  22. # CreatePythonException
  23. def test_CreatePythonException_name():
  24. e = EC.CreatePythonException("foo")
  25. AreEqual("foo", e.__name__)
  26. def test_CreatePythonException_defaultmodule():
  27. e = EC.CreatePythonException("bar")
  28. AreEqual("exceptions", e.__module__)
  29. def test_CreatePythonException_othermodule():
  30. e = EC.CreatePythonException("baz", "quux")
  31. AreEqual("quux", e.__module__)
  32. def test_CreatePythonException_otherbase():
  33. class Base:
  34. pass
  35. e = EC.CreatePythonException("abc", "exceptions", Base)
  36. Assert(issubclass(e, Base), "wrong base type")
  37. def test_CreatePythonException_doublecreate_identity():
  38. e1 = EC.CreatePythonException("hey")
  39. e2 = EC.CreatePythonException("hey")
  40. AreEqual(e1, e2)
  41. def test_CreatePythonException_differentbases():
  42. class NewBase:
  43. pass
  44. e1 = EC.CreatePythonException("hey", "exceptions")
  45. success = False
  46. try:
  47. e2 = EC.CreatePythonException("hey", "exceptions", NewBase)
  48. except System.InvalidOperationException:
  49. success = True
  50. Assert(success, "creation of exception with same name and different base class should have failed")
  51. # GetPythonException
  52. def test_GetPythonException_nonexistant():
  53. success = False
  54. try:
  55. e = EC.GetPythonException("neverneverland")
  56. except System.Collections.Generic.KeyNotFoundException:
  57. success = True
  58. Assert(success, "lookup of nonexistant Python exception should have failed")
  59. def test_GetPythonException_defaultmodule():
  60. e = EC.CreatePythonException("fiddle")
  61. AreEqual(e, EC.GetPythonException("fiddle"))
  62. AreEqual(e, EC.GetPythonException("fiddle", "exceptions"))
  63. def test_GetPythonException_othermodule():
  64. e = EC.CreatePythonException("qix", "mymodule")
  65. AreEqual(e, EC.GetPythonException("qix", "mymodule"))
  66. # CreateExceptionMapping
  67. def test_CreateExceptionMapping_Py2CLR_NoMapping():
  68. pyex1 = EC.CreatePythonException("PythonException1")
  69. success = False
  70. try:
  71. raise pyex1()
  72. except IronPythonTest.CLRException1:
  73. success = False
  74. except pyex1:
  75. success = True
  76. Assert(success, "CLR version of Python exception caught even without mapping")
  77. def test_CreateExceptionMapping_Py2CLR_WithMapping():
  78. pyex2 = EC.CreatePythonException("PythonException2")
  79. m = ExceptionMapping("PythonException2", IronPythonTest.CLRException2)
  80. EC.CreateExceptionMapping(EC.GetPythonException("Exception"), m)
  81. success = False
  82. try:
  83. raise pyex2()
  84. except IronPythonTest.CLRException2:
  85. success = True
  86. Assert(success, "CLR version of Python exception not caught, despite mapping")
  87. def test_CreateExceptionMapping_CLR2Py_NoMapping():
  88. pyex3 = EC.CreatePythonException("PythonException3")
  89. success = False
  90. try:
  91. raise IronPythonTest.CLRException3()
  92. except pyex3:
  93. success = False
  94. except IronPythonTest.CLRException3:
  95. success = True
  96. Assert(success, "Python version of CLR exception caught even without mapping")
  97. def test_CreateExceptionMapping_CLR2Py_WithMapping():
  98. pyex4 = EC.CreatePythonException("PythonException4")
  99. m = ExceptionMapping("PythonException4", IronPythonTest.CLRException4)
  100. EC.CreateExceptionMapping(EC.GetPythonException("Exception"), m)
  101. success = False
  102. try:
  103. raise pyex4()
  104. except IronPythonTest.CLRException4:
  105. success = True
  106. Assert(success, "Python version of CLR exception not caught, despite mapping")
  107. #run_test(__name__)