Skip to content

Commit

Permalink
Merge pull request #250 from Cal-CS-61A-Staff/client-rawstring
Browse files Browse the repository at this point in the history
Handle raw strings in client test dumping
  • Loading branch information
soumyabasu committed Oct 10, 2014
2 parents 9a6a4b6 + c9ca933 commit abc32b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ gaenv/
# pip-related files
*.egg-info
dist
*.ok_refresh
*.ok_messages
26 changes: 21 additions & 5 deletions client/tests/utils/formatting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,43 @@ def testString_singleLine(self):

def testString_multipleLines(self):
self.assertFormat("""
\"\"\"
r\"\"\"
hello
world
\"\"\"
""", "hello\nworld")

def testString_multipleLines(self):
def testString_multipleLinesSurroundingNewlines(self):
self.assertFormat("""
\"\"\"
r\"\"\"
hello
world
\"\"\"
""", "\nhello\nworld\n")

def testString_rawStringSingleLine(self):
self.assertFormat(r"""
'hello \\ there'
""", r"hello \ there")

def testString_rawStringMultiLine(self):
self.assertFormat("""
r\"\"\"
hello \\
there
\"\"\"
""", r"""
hello \
there
""")

def testList_onlyPrimitives(self):
self.assertFormat("""
[
42,
3.14,
'hello world',
\"\"\"
r\"\"\"
hello
world
\"\"\"
Expand Down Expand Up @@ -66,7 +82,7 @@ def testDict_onlyPrimitives(self):
self.assertFormat("""
{
'answer': 'hello world',
'multi': \"\"\"
'multi': r\"\"\"
answer
here
\"\"\",
Expand Down
5 changes: 4 additions & 1 deletion client/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ def prettyjson(json, indentation=' '):
json -- Python object that is serializable into json.
indentation -- str; represents one level of indentation
NOTES:
All multiline strings are treated as raw strings.
RETURNS:
str; the formatted json-like string.
"""
if isinstance(json, int) or isinstance(json, float):
return str(json)
elif isinstance(json, str):
if '\n' in json:
return '"""\n' + dedent(json) + '\n"""'
return 'r"""\n' + dedent(json) + '\n"""'
return repr(json)
elif isinstance(json, list):
lst = [indent(prettyjson(el, indentation), indentation) for el in json]
Expand Down

0 comments on commit abc32b3

Please sign in to comment.