blob: 338a287b1b0bdac469cc4afb907a2c32d0a0d8a9 [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
jieluo@google.combde4a322014-08-12 21:10:30 +00002#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07005# https://developers.google.com/protocol-buffers/
jieluo@google.combde4a322014-08-12 21:10:30 +00006#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11# * Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13# * Redistributions in binary form must reproduce the above
14# copyright notice, this list of conditions and the following disclaimer
15# in the documentation and/or other materials provided with the
16# distribution.
17# * Neither the name of Google Inc. nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33"""Tests for google.protobuf.text_encoding."""
34
Dan O'Reilly2621c8a2015-08-14 22:54:53 -040035try:
36 import unittest2 as unittest
37except ImportError:
38 import unittest
jieluo@google.combde4a322014-08-12 21:10:30 +000039from google.protobuf import text_encoding
40
41TEST_VALUES = [
42 ("foo\\rbar\\nbaz\\t",
43 "foo\\rbar\\nbaz\\t",
44 b"foo\rbar\nbaz\t"),
45 ("\\'full of \\\"sound\\\" and \\\"fury\\\"\\'",
46 "\\'full of \\\"sound\\\" and \\\"fury\\\"\\'",
47 b"'full of \"sound\" and \"fury\"'"),
48 ("signi\\\\fying\\\\ nothing\\\\",
49 "signi\\\\fying\\\\ nothing\\\\",
50 b"signi\\fying\\ nothing\\"),
51 ("\\010\\t\\n\\013\\014\\r",
52 "\x08\\t\\n\x0b\x0c\\r",
53 b"\010\011\012\013\014\015")]
54
55
Tres Seaver7ee25832015-01-13 14:47:32 -050056class TextEncodingTestCase(unittest.TestCase):
jieluo@google.combde4a322014-08-12 21:10:30 +000057 def testCEscape(self):
58 for escaped, escaped_utf8, unescaped in TEST_VALUES:
Tres Seavera2abc942015-01-13 15:47:55 -050059 self.assertEqual(escaped,
jieluo@google.combde4a322014-08-12 21:10:30 +000060 text_encoding.CEscape(unescaped, as_utf8=False))
Tres Seavera2abc942015-01-13 15:47:55 -050061 self.assertEqual(escaped_utf8,
jieluo@google.combde4a322014-08-12 21:10:30 +000062 text_encoding.CEscape(unescaped, as_utf8=True))
63
64 def testCUnescape(self):
65 for escaped, escaped_utf8, unescaped in TEST_VALUES:
Tres Seavera2abc942015-01-13 15:47:55 -050066 self.assertEqual(unescaped, text_encoding.CUnescape(escaped))
67 self.assertEqual(unescaped, text_encoding.CUnescape(escaped_utf8))
jieluo@google.combde4a322014-08-12 21:10:30 +000068
69
70if __name__ == "__main__":
Tres Seaver7ee25832015-01-13 14:47:32 -050071 unittest.main()