PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/asserts.py

https://github.com/OddBloke/lettuce
Python | 114 lines | 88 code | 10 blank | 16 comment | 8 complexity | b735ee8e1374f77d012d621780aa7f8c MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. # <Lettuce - Behaviour Driven Development for python>
  3. # Copyright (C) <2010-2012> Gabriel Falc達o <gabriel@nacaolivre.org>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import re
  18. import sys
  19. from StringIO import StringIO
  20. from nose.tools import assert_equals, assert_not_equals
  21. from lettuce import registry
  22. from difflib import Differ
  23. def prepare_stdout():
  24. registry.clear()
  25. if isinstance(sys.stdout, StringIO):
  26. del sys.stdout
  27. std = StringIO()
  28. sys.stdout = std
  29. def prepare_stderr():
  30. registry.clear()
  31. if isinstance(sys.stderr, StringIO):
  32. del sys.stderr
  33. std = StringIO()
  34. sys.stderr = std
  35. def assert_lines(original, expected):
  36. original = original.decode('utf-8') if isinstance(original, basestring) else original
  37. assert_lines_unicode(original, expected)
  38. def assert_lines_unicode(original, expected):
  39. if isinstance(expected, unicode):
  40. expected = expected.encode('utf-8')
  41. if isinstance(original, unicode):
  42. original = original.encode('utf-8')
  43. expected_lines = expected.splitlines(1)
  44. original_lines = original.splitlines(1)
  45. if original != expected:
  46. comparison = Differ().compare(expected_lines, original_lines)
  47. if isinstance(comparison, unicode):
  48. expected = expected.encode('utf-8')
  49. diff = u''.encode('utf-8').join(comparison)
  50. msg = (u'Output differed as follows:\n{0}\n'
  51. 'Output was:\n{1}\nExpected was:\n{2}'.encode('utf-8'))
  52. raise AssertionError(repr(msg.format(diff, original, expected)).replace(r'\n', '\n'))
  53. assert_equals(
  54. len(expected), len(original),
  55. u'Output appears equal, but of different lengths.')
  56. def assert_lines_with_traceback(one, other):
  57. lines_one = one.splitlines()
  58. lines_other = other.splitlines()
  59. regex = re.compile('File "([^"]+)", line \d+, in.*')
  60. error = '%r should be in traceback line %r.\nFull output was:\n' + one
  61. for line1, line2 in zip(lines_one, lines_other):
  62. if regex.search(line1) and regex.search(line2):
  63. found = regex.search(line2)
  64. filename = found.group(1)
  65. params = filename, line1
  66. assert filename in line1, error % params
  67. else:
  68. assert_unicode_equals(line1, line2)
  69. assert_unicode_equals(len(lines_one), len(lines_other))
  70. def assert_unicode_equals(original, expected):
  71. if isinstance(original, basestring):
  72. original = original.decode('utf-8')
  73. assert_equals(original, expected)
  74. def assert_stderr(expected):
  75. string = sys.stderr.getvalue()
  76. assert_unicode_equals(string, expected)
  77. def assert_stdout(expected):
  78. string = sys.stdout.getvalue()
  79. assert_unicode_equals(string, expected)
  80. def assert_stdout_lines(other):
  81. assert_lines(sys.stdout.getvalue(), other)
  82. def assert_stderr_lines(other):
  83. assert_lines(sys.stderr.getvalue(), other)
  84. def assert_stdout_lines_with_traceback(other):
  85. assert_lines_with_traceback(sys.stdout.getvalue(), other)
  86. def assert_stderr_lines_with_traceback(other):
  87. assert_lines_with_traceback(sys.stderr.getvalue(), other)