|
| 1 | +"""Tests for json_minify |
| 2 | +""" |
| 3 | + |
| 4 | +# Python 2.6+ needed to run tests |
| 5 | +import json |
| 6 | +import textwrap |
| 7 | +import unittest |
| 8 | + |
| 9 | +from json_minify import json_minify |
| 10 | + |
| 11 | + |
| 12 | +class JsonMinifyTestCase(unittest.TestCase): |
| 13 | + """Tests for json_minify""" |
| 14 | + |
| 15 | + tests = [ |
| 16 | + [ |
| 17 | + ''' |
| 18 | + // this is a JSON file with comments |
| 19 | + { |
| 20 | + "foo": "bar", // this is cool |
| 21 | + "bar": [ |
| 22 | + "baz", "bum" |
| 23 | + ], |
| 24 | + /* the rest of this document is just fluff |
| 25 | + in case you are interested. */ |
| 26 | + "something": 10, |
| 27 | + "else": 20 |
| 28 | + } |
| 29 | +
|
| 30 | + /* NOTE: You can easily strip the whitespace and comments |
| 31 | + from such a file with the JSON.minify() project hosted |
| 32 | + here on github at http://github.com/getify/JSON.minify |
| 33 | + */''', |
| 34 | + '{"foo":"bar","bar":["baz","bum"],"something":10,"else":20}' |
| 35 | + ], |
| 36 | + [ |
| 37 | + ''' |
| 38 | + {"/*":"*/","//":"",/*"//"*/"/*/":// |
| 39 | + "//"}''', |
| 40 | + '{"/*":"*/","//":"","/*/":"//"}' |
| 41 | + ], |
| 42 | + [ |
| 43 | + r''' |
| 44 | + /* |
| 45 | + this is a |
| 46 | + multi line comment */{ |
| 47 | +
|
| 48 | + "foo" |
| 49 | + : |
| 50 | + "bar/*"// something |
| 51 | + , "b\"az":/* |
| 52 | + something else */"blah" |
| 53 | +
|
| 54 | + } |
| 55 | + ''', |
| 56 | + r'{"foo":"bar/*","b\"az":"blah"}' |
| 57 | + ], |
| 58 | + [ |
| 59 | + r''' |
| 60 | + {"foo": "ba\"r//", "bar\\": "b\\\"a/*z", |
| 61 | + "baz\\\\": /* yay */ "fo\\\\\"*/o" |
| 62 | + } |
| 63 | + ''', |
| 64 | + r'{"foo":"ba\"r//","bar\\":"b\\\"a/*z","baz\\\\":"fo\\\\\"*/o"}' # noqa |
| 65 | + ] |
| 66 | + ] |
| 67 | + |
| 68 | + def template(self, in_string, expected): |
| 69 | + in_dict = json.loads(json_minify(in_string)) |
| 70 | + expected_dict = json.loads(textwrap.dedent(expected)) |
| 71 | + self.assertEqual(in_dict, expected_dict) |
| 72 | + |
| 73 | + def test_1(self): |
| 74 | + self.template(*self.tests[0]) |
| 75 | + |
| 76 | + def test_2(self): |
| 77 | + self.template(*self.tests[1]) |
| 78 | + |
| 79 | + def test_3(self): |
| 80 | + self.template(*self.tests[2]) |
| 81 | + |
| 82 | + def test_4(self): |
| 83 | + self.template(*self.tests[3]) |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + unittest.main() |
0 commit comments