Skip to content

Commit

Permalink
Add to SafeJSONEncoder support for boolean type and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Mar 1, 2015
1 parent 6882fc9 commit 1ad3f4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions cms/tests/templatetags.py
Expand Up @@ -12,6 +12,7 @@
from django.test import RequestFactory, TestCase
from django.template.base import Template
from django.utils.html import escape
from django.utils.timezone import now
from djangocms_text_ckeditor.cms_plugins import TextPlugin

from cms.api import create_page, create_title, add_plugin
Expand All @@ -20,6 +21,7 @@
from cms.templatetags.cms_tags import (_get_page_by_untyped_arg,
_show_placeholder_for_page,
_get_placeholder, RenderPlugin)
from cms.templatetags.cms_js_tags import json_filter
from cms.test_utils.fixtures.templatetags import TwoPagesFixture
from cms.test_utils.testcases import SettingsOverrideTestCase, CMSTestCase
from cms.test_utils.util.context_managers import SettingsOverride
Expand Down Expand Up @@ -71,6 +73,17 @@ class FakeRequest(object):
self.assertNotEqual(script, output)
self.assertEqual(escape(script), output)

def test_json_encoder(self):
self.assertEqual(json_filter(True), 'true')
self.assertEqual(json_filter(False), 'false')
self.assertEqual(json_filter([1, 2, 3]), '[1, 2, 3]')
self.assertEqual(json_filter((1, 2, 3)), '[1, 2, 3]')
filtered_dict = json_filter({'item1': 1, 'item2': 2, 'item3': 3})
self.assertTrue('"item1": 1' in filtered_dict)
self.assertTrue('"item2": 2' in filtered_dict)
self.assertTrue('"item3": 3' in filtered_dict)
self.assertEqual('"%s"' % now().today().isoformat()[:-3], json_filter(now().today()))


class TemplatetagDatabaseTests(TwoPagesFixture, SettingsOverrideTestCase):
def _getfirst(self):
Expand Down
2 changes: 1 addition & 1 deletion cms/utils/encoder.py
Expand Up @@ -15,7 +15,7 @@ def _recursive_escape(self, o, esc=conditional_escape):
return o
try:
return type(o)(esc(o))
except ValueError:
except (ValueError, TypeError):
return self.default(o)

def encode(self, o):
Expand Down

0 comments on commit 1ad3f4f

Please sign in to comment.