Skip to content

Commit

Permalink
adding tests, no escaping for android yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasos Bakogiannis committed Apr 6, 2016
1 parent 8ff683c commit c060b62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
16 changes: 2 additions & 14 deletions openformats/formats/android.py
Expand Up @@ -19,6 +19,8 @@ class AndroidHandler(Handler):
'translatable': 'false'
}

EXTRACTS_RAW = False

def parse(self, content):
stringset = []
if type(content) == str:
Expand Down Expand Up @@ -345,17 +347,3 @@ def _compile_plurals(self, plurals_tag, plurals_offset):
# didn't find it, must remove by skipping it
self.transcriber.skip_until(plurals_offset +
len(plurals_tag.content))

@staticmethod
def escape(string):
escaped_string = string.replace('"', r'\"'). \
replace("'", r"\'")

return escaped_string

@staticmethod
def unescape(string):
unescaped_string = string.replace(r'\"', '"'). \
replace(r"\'", "'")

return unescaped_string
10 changes: 2 additions & 8 deletions openformats/formats/json.py
Expand Up @@ -251,18 +251,12 @@ def _get_next_string(self):

@staticmethod
def escape(string):
escaped_string = string.replace('\\', r'\\'). \
replace('.', r'\.'). \
replace('"', '\\"'). \
replace('/', '\/"')
escaped_string = string.replace('\\', r'\\').replace('"', '\\"')

return escaped_string

@staticmethod
def unescape(string):
unescaped_string = string.replace(r'\\', '\\'). \
replace(r'\.', '.'). \
replace(r'\"', '"'). \
replace(r'\/', '/"')
unescaped_string = string.replace(r'\\', '\\').replace(r'\"', '"')

return unescaped_string
21 changes: 21 additions & 0 deletions openformats/tests/formats/json/test_json.py
Expand Up @@ -268,6 +268,15 @@ def test_skip_middle_of_list(self):
def test_invalid_json(self):
self._test_parse_error('jaosjf', "No JSON object could be decoded")

def test_invalid_json_type(self):
template, stringset = self.handler.parse('[false]')
self.assertEqual(stringset, [])
self.assertEqual(template, '[false]')

template, stringset = self.handler.parse('{"false": false}')
self.assertEqual(stringset, [])
self.assertEqual(template, '{"false": false}')

def test_not_json_container(self):
self._test_parse_error('"hello"', "Input is not a JSON container")
self._test_parse_error('3', "Input is not a JSON container")
Expand All @@ -287,3 +296,15 @@ def test_display_json_errors(self):
self._test_parse_error('["]',
"Unterminated string starting at: line 1 "
"column 2 (char 1)")

def test_escape_json(self):
self.assertEqual(
self.handler.escape("a \\ string. with \"quotes\""),
"a \\\\ string. with \\\"quotes\\\""
)

def test_unescape_json(self):
self.assertEqual(
self.handler.unescape("a \\\\ string. with \\\"quotes\\\""),
"a \\ string. with \"quotes\""
)

0 comments on commit c060b62

Please sign in to comment.