def test_escape(self):
     cases = (
         # simple => simple
         ([u's', u'i', u'm', u'p', u'l',
           u'e'], [u's', u'i', u'm', u'p', u'l', u'e']),
         # hεllo => hεllo
         ([u'h', u'ε', u'l', u'l', u'o'], [u'h', u'ε', u'l', u'l', u'o']),
         # h\u03b5llo => h\\u03b5llo
         ([u'h', u'\\', u'u', u'0', u'3', u'b', u'5', u'l', u'l', u'o'], [
             u'h', u'\\', u'\\', u'u', u'0', u'3', u'b', u'5', u'l', u'l',
             u'o'
         ]),
         # a"b =>a\"b
         ([u'a', u'"', u'b'], [u'a', u'\\', u'"', u'b']),
         # a/b =>a/b
         ([u'a', u'/', u'b'], [u'a', u'/', u'b']),
         # a?b =>a\/b, ? = BACKSPACE
         ([u'a', u'\b', u'b'], [u'a', u'\\', u'b', u'b']),
         # a?b =>a\fb, ? = FORMFEED
         ([u'a', u'\f', u'b'], [u'a', u'\\', u'f', u'b']),
         # a?b =>a\nb, ? = NEWLINE
         ([u'a', u'\n', u'b'], [u'a', u'\\', u'n', u'b']),
         # a?b =>a\rb, ? = CARRIAGE_RETURN
         ([u'a', u'\r', u'b'], [u'a', u'\\', u'r', u'b']),
         # a?b => a\tb, ? = TAB
         ([u'a', u'\t', u'b'], [u'a', u'\\', u't', u'b']),
     )
     for rich, raw in cases:
         self.assertEqual(
             StructuredJsonHandler.escape(bytes_to_string(rich)),
             bytes_to_string(raw))
 def test_escape(self):
     cases = (
         # simple => simple
         ([u's', u'i', u'm', u'p', u'l', u'e'],
          [u's', u'i', u'm', u'p', u'l', u'e']),
         # hεllo => hεllo
         ([u'h', u'ε', u'l', u'l', u'o'],
          [u'h', u'ε', u'l', u'l', u'o']),
         # h\u03b5llo => h\\u03b5llo
         ([u'h', u'\\', u'u', u'0', u'3', u'b', u'5', u'l', u'l', u'o'],
          [u'h', u'\\', u'\\', u'u', u'0', u'3', u'b', u'5', u'l', u'l',
           u'o']),
         # a"b =>a\"b
         ([u'a', u'"', u'b'], [u'a', u'\\', u'"', u'b']),
         # a/b =>a/b
         ([u'a', u'/', u'b'], [u'a', u'/', u'b']),
         # a?b =>a\/b, ? = BACKSPACE
         ([u'a', u'\b', u'b'], [u'a', u'\\', u'b', u'b']),
         # a?b =>a\fb, ? = FORMFEED
         ([u'a', u'\f', u'b'], [u'a', u'\\', u'f', u'b']),
         # a?b =>a\nb, ? = NEWLINE
         ([u'a', u'\n', u'b'], [u'a', u'\\', u'n', u'b']),
         # a?b =>a\rb, ? = CARRIAGE_RETURN
         ([u'a', u'\r', u'b'], [u'a', u'\\', u'r', u'b']),
         # a?b => a\tb, ? = TAB
         ([u'a', u'\t', u'b'], [u'a', u'\\', u't', u'b']),
     )
     for rich, raw in cases:
         self.assertEqual(JsonHandler.escape(bytes_to_string(rich)),
                          bytes_to_string(raw))
Beispiel #3
0
 def test_escape(self):
     cases = (
         # a"b => a\"b
         ([u'a', u'"', u'b'], [u'a', u'\\', u'"', u'b']),
         # a'b => a\'b
         ([u'a', u"'", u'b'], [u'a', u'\\', u"'", u'b']),
         # a\b => a\b
         ([u'a', u'\\', u'b'], [u'a', u'\\', u'b']),
         # a\\b => a\\b
         ([u'a', u'\\', u'\\', u'b'], [u'a', u'\\', u'\\', u'b']),
         # a"b\c => a\"b\c
         ([u'a', u'"', u'b', u'\\', u'c'],
          [u'a', u'\\', u'"', u'b', u'\\', u'c']),
         # "a" => \"a\"
         ([u'"', u'a', u'"'], [u'\\', u'"', u'a', u'\\', u'"']),
         # "a"b" => \"\a\"\b\"
         ([u'"', u'a', u'"', u'b', u'"'],
          [u'\\', u'"', u'a', u'\\', u'"', u'b', u'\\', u'"']),
         # "a'b" => \"\a\"\b\"
         ([u'"', u'a', u"'", u'b', u'"'],
          [u'\\', u'"', u'a', u'\\', u"'", u'b', u'\\', u'"']),
         # Simple
         (u'<x y="z">hello</x>', u'<x y=\\"z\\">hello</x>'),
         (u'<a b="c">"hello"</a>', u'<a b="c">\\"hello\\"</a>'),
         # Combined
         (u'<a b="c">hello</a><x y="z">hello</x>',
          u'<a b="c">hello</a><x y=\\"z\\">hello</x>'),
         # Nested
         (u'<a b="c"><x y="z">hello</x></a>',
          u'<a b="c"><x y=\\"z\\">hello</x></a>'),
         (u'<x y="z"><a b="c">hello</a></x>',
          u'<x y=\\"z\\"><a b="c">hello</a></x>'),
         # Heads and tails
         (u'this <x y="z">is escaped</x>, this <a b="c">isnt</a>, ok?',
          u'this <x y=\\"z\\">is escaped</x>, this <a b="c">isnt</a>, ok?'),
         # Not proper XML
         (u'"0 < "1', u'\\"0 < \\"1'),
         (u'<a>"b"</c>', u'<a>\\"b\\"</c>'),
         # Single tags
         (u'<a b="c" />', u'<a b="c" />'),
         (u'<x y="z" />', u'<x y=\\"z\\" />'),
         # xliff:g tag
         (u'<xliff:g y="z">hello</xliff:g>',
          u'<xliff:g y="z">hello</xliff:g>'),
         # annotation tag
         (u'<annotation y="z">hello</annotation>',
          u'<annotation y="z">hello</annotation>'),
         # At-sign cases
         (u'@', '\@'),
         (u'@something', u'\@something'),
         (u'"@enclosed"', u'\\"@enclosed\\"'),
         (u'no need @', u'no need @'),
         # Identifiers should remain intact
         (u'@string/one', u'@string/one'),
     )
     for rich, raw in cases:
         self.assertEqual(AndroidHandler.escape(bytes_to_string(rich)),
                           bytes_to_string(raw))
Beispiel #4
0
 def test_unescape(self):
     cases = (
         # a"b => a"b
         ([u'a', u'"', u'b'], [u'a', u'"', u'b']),
         # a'b => a'b
         ([u'a', u"'", u'b'], [u'a', u"'", u'b']),
         # a\b => a\b
         ([u'a', u'\\', u'b'], [u'a', u'\\', u'b']),
         # a\"b => a"b
         ([u'a', u'\\', u'"', u'b'], [u'a', u'"', u'b']),
         # a\'b => a'b
         ([u'a', u'\\', u"'", u'b'], [u'a', u"'", u'b']),
         # a\\b => a\\b
         ([u'a', u'\\', u'\\', u'b'], [u'a', u'\\', u'\\', u'b']),
         # "a"b" => a"b
         ([u'"', u'a', u'"', u'b', u'"'], [u'a', u'"', u'b']),
         # "a'b" => a'b
         ([u'"', u'a', u"'", u'b', u'"'], [u'a', u"'", u'b']),
         # "a\b" => a\b
         ([u'"', u'a', u'\\', u'b', u'"'], [u'a', u'\\', u'b']),
         # "a\"b" => a"b
         ([u'"', u'a', u'\\', u'"', u'b', u'"'], [u'a', u'"', u'b']),
         # "a\'b" => a\'b
         ([u'"', u'a', u'\\', u"'", u'b', u'"'], [u'a', u'\\', u"'", u'b']),
         # "a\\b" => a\\b
         ([u'"', u'a', u'\\', u'\\', u'b', u'"'],
          [u'a', u'\\', u'\\', u'b']),
         # a"b\c => a"b\c
         ([u'a', u'"', u'b', u'\\', u'c'], [u'a', u'"', u'b', u'\\', u'c']),
         # a'b\c => a'b\c
         ([u'a', u"'", u'b', u'\\', u'c'], [u'a', u"'", u'b', u'\\', u'c']),
         # a\\"b => a\"b
         ([u'a', u'\\', u'\\', u'"', u'b'], [u'a', u'\\', u'"', u'b']),
         # At-sign cases
         (u'\@', '@'),
         (u'\@something', u'@something'),
         (u'\\"@enclosed\\"', u'"@enclosed"'),
         (u'no need @', u'no need @'),
         (u'@string/one', u'@string/one'),
     )
     for raw, rich in cases:
         self.assertEqual(AndroidHandler.unescape(bytes_to_string(raw)),
                           bytes_to_string(rich))