Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 2 | # |
| 3 | # Protocol Buffers - Google's data interchange format |
| 4 | # Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 5 | # https://developers.google.com/protocol-buffers/ |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 6 | # |
| 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'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 35 | try: |
| 36 | import unittest2 as unittest |
| 37 | except ImportError: |
| 38 | import unittest |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 39 | from google.protobuf import text_encoding |
| 40 | |
| 41 | TEST_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 Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 56 | class TextEncodingTestCase(unittest.TestCase): |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 57 | def testCEscape(self): |
| 58 | for escaped, escaped_utf8, unescaped in TEST_VALUES: |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 59 | self.assertEqual(escaped, |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 60 | text_encoding.CEscape(unescaped, as_utf8=False)) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 61 | self.assertEqual(escaped_utf8, |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 62 | text_encoding.CEscape(unescaped, as_utf8=True)) |
| 63 | |
| 64 | def testCUnescape(self): |
| 65 | for escaped, escaped_utf8, unescaped in TEST_VALUES: |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 66 | self.assertEqual(unescaped, text_encoding.CUnescape(escaped)) |
| 67 | self.assertEqual(unescaped, text_encoding.CUnescape(escaped_utf8)) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 71 | unittest.main() |