Skip to content

Commit

Permalink
Merge f3b8d4f into 5b13f85
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed May 7, 2019
2 parents 5b13f85 + f3b8d4f commit 4dba0b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
41 changes: 40 additions & 1 deletion django_js_reverse/tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.core.management import call_command
from django.template import Context, RequestContext, Template
from django.utils.encoding import smart_str
import js2py
from helper import is_django_ver_gte_2
from utils import script_prefix

Expand All @@ -27,6 +28,20 @@
from django.test.utils import override_settings # noqa: E402 isort:skip


def node_jseval(expr):
module = 'console.log({});'.format(expr)
stdout = (
subprocess
.check_output(['node', '-e', module.encode('utf8')])
.decode('utf8')
)
return re.sub(r'\n$', '', stdout)


def js2py_jseval(expr):
return js2py.eval_js(expr)


class AbstractJSReverseTestCase(object):
client = Client()

Expand All @@ -47,6 +62,19 @@ def assertEqualJSEval(self, js, url_call, expected_url):
)
self.assertEqual(re.sub(r'\n$', '', stdout), expected_url)

def assertEqualJSUrlEval(self, url_call, expected_url):
response = self.client.post('/jsreverse/')
script = '{}return {};'.format(smart_str(response.content), url_call)
module = 'new Function({})()'.format(json.dumps(script))

def url(jseval):
if not callable(expected_url):
return expected_url
return expected_url(jseval)

self.assertEqual(node_jseval(module), url(node_jseval))
self.assertEqual(js2py_jseval('(function () {{ {} }}())'.format(script)), url(js2py_jseval))

def assertEqualJSUrlEval(self, *args, **kwargs):
js = smart_str(self.client.post('/jsreverse/').content)
self.assertEqualJSEval(js, *args, **kwargs)
Expand Down Expand Up @@ -179,7 +207,18 @@ def test_int_args(self):

def test_float_args(self):
self.assertEqualJSUrlEval('Urls.test_two_url_args(0, 5.5)', '/test_two_url_args/0-5.5/')
self.assertEqualJSUrlEval('Urls.test_two_url_args(0.00001, 5.5)', '/test_two_url_args/0.00001-5.5/')
def get_url(js_eval):
"""
js2py doesn't follow the Number.prototype.toString ES5 spec
correctly.
https://github.com/PiotrDabkowski/Js2Py/issues/82
Assert on whatever a float becomes - not what a float should be.
"""
return '/test_two_url_args/{}-5.5/'.format(js_eval('(0.00001).toString()'))

self.assertEqualJSUrlEval('Urls.test_two_url_args(0.00001, 5.5)', get_url)

def test_django_path_syntax(self):
if is_django_ver_gte_2():
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pip_pre = true
commands = coverage run -p django_js_reverse/tests/unit_tests.py
deps=
coverage==4.5.1
js2py==0.59
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7
django17: Django>=1.7,<1.8
Expand Down

0 comments on commit 4dba0b2

Please sign in to comment.