test_line_ending.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # test_line_ending.py -- Tests for the line ending functions
  3. # encoding: utf-8
  4. # Copyright (C) 2018-2019 Boris Feld <boris.feld@comet.ml>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Tests for the line ending conversion."""
  23. from dulwich.line_ending import (
  24. convert_crlf_to_lf,
  25. convert_lf_to_crlf,
  26. get_checkin_filter_autocrlf,
  27. get_checkout_filter_autocrlf,
  28. )
  29. from dulwich.tests import TestCase
  30. class LineEndingConversion(TestCase):
  31. """Test the line ending conversion functions in various cases"""
  32. def test_convert_crlf_to_lf_no_op(self):
  33. self.assertEqual(convert_crlf_to_lf(b"foobar"), b"foobar")
  34. def test_convert_crlf_to_lf(self):
  35. self.assertEqual(
  36. convert_crlf_to_lf(b"line1\r\nline2"), b"line1\nline2"
  37. )
  38. def test_convert_crlf_to_lf_mixed(self):
  39. self.assertEqual(
  40. convert_crlf_to_lf(b"line1\r\n\nline2"), b"line1\n\nline2"
  41. )
  42. def test_convert_lf_to_crlf_no_op(self):
  43. self.assertEqual(convert_lf_to_crlf(b"foobar"), b"foobar")
  44. def test_convert_lf_to_crlf(self):
  45. self.assertEqual(
  46. convert_lf_to_crlf(b"line1\nline2"), b"line1\r\nline2"
  47. )
  48. def test_convert_lf_to_crlf_mixed(self):
  49. self.assertEqual(
  50. convert_lf_to_crlf(b"line1\r\n\nline2"), b"line1\r\n\r\nline2"
  51. )
  52. class GetLineEndingAutocrlfFilters(TestCase):
  53. def test_get_checkin_filter_autocrlf_default(self):
  54. checkin_filter = get_checkin_filter_autocrlf(b"false")
  55. self.assertEqual(checkin_filter, None)
  56. def test_get_checkin_filter_autocrlf_true(self):
  57. checkin_filter = get_checkin_filter_autocrlf(b"true")
  58. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  59. def test_get_checkin_filter_autocrlf_input(self):
  60. checkin_filter = get_checkin_filter_autocrlf(b"input")
  61. self.assertEqual(checkin_filter, convert_crlf_to_lf)
  62. def test_get_checkout_filter_autocrlf_default(self):
  63. checkout_filter = get_checkout_filter_autocrlf(b"false")
  64. self.assertEqual(checkout_filter, None)
  65. def test_get_checkout_filter_autocrlf_true(self):
  66. checkout_filter = get_checkout_filter_autocrlf(b"true")
  67. self.assertEqual(checkout_filter, convert_lf_to_crlf)
  68. def test_get_checkout_filter_autocrlf_input(self):
  69. checkout_filter = get_checkout_filter_autocrlf(b"input")
  70. self.assertEqual(checkout_filter, None)