2012-09-22 02:40:50 +00:00
|
|
|
# Protocol Buffers - Google's data interchange format
|
|
|
|
|
# Copyright 2008 Google Inc. All rights reserved.
|
|
|
|
|
#
|
2023-09-08 18:03:16 -07:00
|
|
|
# Use of this source code is governed by a BSD-style
|
|
|
|
|
# license that can be found in the LICENSE file or at
|
|
|
|
|
# https://developers.google.com/open-source/licenses/bsd
|
2012-09-22 02:40:50 +00:00
|
|
|
|
2014-08-12 21:10:30 +00:00
|
|
|
"""Tests for google.protobuf.text_encoding."""
|
2012-09-22 02:40:50 +00:00
|
|
|
|
2021-09-09 08:21:42 -07:00
|
|
|
import unittest
|
2016-03-30 11:39:59 -07:00
|
|
|
|
2014-08-12 21:10:30 +00:00
|
|
|
from google.protobuf import text_encoding
|
2012-09-22 02:40:50 +00:00
|
|
|
|
2014-08-12 21:10:30 +00:00
|
|
|
TEST_VALUES = [
|
2026-04-29 13:39:31 -07:00
|
|
|
("foo\\rbar\\nbaz\\t", "foo\\rbar\\nbaz\\t", b"foo\rbar\nbaz\t"),
|
|
|
|
|
(
|
|
|
|
|
'\\\'full of \\"sound\\" and \\"fury\\"\\\'',
|
|
|
|
|
'\\\'full of \\"sound\\" and \\"fury\\"\\\'',
|
|
|
|
|
b'\'full of "sound" and "fury"\'',
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"signi\\\\fying\\\\ nothing\\\\",
|
|
|
|
|
"signi\\\\fying\\\\ nothing\\\\",
|
|
|
|
|
b"signi\\fying\\ nothing\\",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"\\010\\t\\n\\013\\014\\r",
|
|
|
|
|
"\\010\\t\\n\\013\\014\\r",
|
|
|
|
|
b"\010\011\012\013\014\015",
|
|
|
|
|
),
|
|
|
|
|
]
|
2012-09-22 02:40:50 +00:00
|
|
|
|
|
|
|
|
|
2015-01-13 14:47:32 -05:00
|
|
|
class TextEncodingTestCase(unittest.TestCase):
|
2026-04-29 13:39:31 -07:00
|
|
|
|
2014-08-12 21:10:30 +00:00
|
|
|
def testCEscape(self):
|
|
|
|
|
for escaped, escaped_utf8, unescaped in TEST_VALUES:
|
2024-01-23 20:29:09 -08:00
|
|
|
self.assertEqual(escaped, text_encoding.CEscape(unescaped, as_utf8=False))
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
escaped_utf8, text_encoding.CEscape(unescaped, as_utf8=True)
|
|
|
|
|
)
|
2012-09-22 02:40:50 +00:00
|
|
|
|
2014-08-12 21:10:30 +00:00
|
|
|
def testCUnescape(self):
|
|
|
|
|
for escaped, escaped_utf8, unescaped in TEST_VALUES:
|
2015-01-13 15:47:55 -05:00
|
|
|
self.assertEqual(unescaped, text_encoding.CUnescape(escaped))
|
|
|
|
|
self.assertEqual(unescaped, text_encoding.CUnescape(escaped_utf8))
|
2014-08-12 21:10:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-01-13 14:47:32 -05:00
|
|
|
unittest.main()
|