/DLR_Main/Languages/IronPython/Tests/modules/misc/_warnings_test.py

https://bitbucket.org/mdavid/dlr · Python · 112 lines · 93 code · 0 blank · 19 comment · 1 complexity · 4cdd4d555b4cf093bc3e9fb1e00deeef 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. '''
  16. CPython's _warnings module. http://docs.python.org/library/warnings.html
  17. '''
  18. #--IMPORTS---------------------------------------------------------------------
  19. from iptest.assert_util import *
  20. skiptest("silverlight")
  21. import _warnings
  22. from iptest.assert_util import stderr_trapper as output_trapper
  23. #--GLOBALS---------------------------------------------------------------------
  24. EXPECTED = [] # expected output (ignoring filename, lineno, and line)
  25. WARN_TYPES = [Warning, UserWarning, PendingDeprecationWarning, SyntaxWarning,
  26. RuntimeWarning, FutureWarning, ImportWarning, UnicodeWarning,
  27. BytesWarning]
  28. #--HELPERS---------------------------------------------------------------------
  29. def cleanup():
  30. '''
  31. Clean up after possible incomplete test runs.
  32. '''
  33. global EXPECTED
  34. EXPECTED = []
  35. def expect(warn_type, message):
  36. '''
  37. Helper for test output
  38. '''
  39. for filter in _warnings.filters:
  40. if filter[0] == "ignore" and issubclass(warn_type, filter[2]):
  41. return
  42. EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n")
  43. #--TEST CASES------------------------------------------------------------------
  44. @skip("multiple_execute")
  45. def test_sanity():
  46. global EXPECTED
  47. try:
  48. with output_trapper() as output:
  49. # generate test output
  50. _warnings.warn("Warning Message!")
  51. expect(UserWarning, "Warning Message!")
  52. for warn_type in WARN_TYPES:
  53. _warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
  54. expect(warn_type, "Type-overriding message!")
  55. _warnings.warn("Another Warning Message!", warn_type)
  56. expect(warn_type, "Another Warning Message!")
  57. _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
  58. expect(warn_type, "Explicit Warning!")
  59. _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
  60. expect(warn_type, "Explicit Warning!")
  61. _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
  62. expect(warn_type, "Explicit Warning!")
  63. _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
  64. expect(warn_type, "Explicit Warning!")
  65. temp_messages = output.messages
  66. #No point in going further if the number of lines is not what we expect
  67. nlines = len([x for x in temp_messages if not x.startswith(" ")])
  68. AreEqual(nlines, len(EXPECTED))
  69. # match lines
  70. for line in temp_messages:
  71. if line.startswith(" "):
  72. continue
  73. temp = EXPECTED.pop(0).rstrip()
  74. Assert(line.endswith(temp), str(line) + " does not end with " + temp)
  75. finally:
  76. # remove generated files
  77. cleanup()
  78. def test_default_action():
  79. print "TODO"
  80. def test_filters():
  81. print "TODO"
  82. def test_once_registry():
  83. print "TODO"
  84. def test_warn():
  85. print "TODO"
  86. def test_warn_explicit():
  87. print "TODO"
  88. ##############################################################################
  89. #--MAIN------------------------------------------------------------------------
  90. run_test(__name__)