Permalink
Browse files

Add TranslatorTest

  • Loading branch information...
1 parent ba8666b commit 9134a9ddd0ad1d858ae4e12d56f7b784330a8fb3 @imom0 imom0 committed Oct 7, 2013
Showing with 39 additions and 24 deletions.
  1. +18 −24 nvd3/translator.py
  2. +21 −0 tests.py
View
@@ -11,7 +11,10 @@ def __init__(self, content=None):
for attr, value in self.attrs])
def __str__(self):
- return '<%s%s>\n %s\n</%s>' % (self.name, self.attrs and ' ' + self.attrs or '', self.content, self.name)
+ return '<%s%s>\n %s\n</%s>' % (self.name,
+ ' ' + self.attrs if self.attrs else '',
+ self.content,
+ self.name)
class ScriptTag(Tag):
@@ -32,22 +35,23 @@ class Function(object):
def __init__(self, name):
self.name = name
- self.statements = []
+ self._calls = []
def __str__(self):
operations = [self.name]
- operations.extend(str(statement) for statement in self.statements)
+ operations.extend(str(call) for call in self._calls)
return '%s' % ('.'.join(operations),)
def __getattr__(self, attr):
- self.statements.append(attr)
+ self._calls.append(attr)
return self
- def __call__(self, args=None):
- if isinstance(args, (Function, AnonymousFunction, basestring)):
- self.statements[-1] = self.statements[-1] + '(%s)' % (args,)
+ def __call__(self, *args):
+ if not args:
+ self._calls[-1] = self._calls[-1] + '()'
else:
- self.statements[-1] = self.statements[-1] + '()'
+ arguments = ','.join([str(arg) for arg in args])
+ self._calls[-1] = self._calls[-1] + '(%s)' % (arguments,)
return self
@@ -59,19 +63,9 @@ def __init__(self, key, value, scoped=True):
self.scoped = scoped
def __str__(self):
- return '%s%s = %s;' % (self.scoped and 'var ' or '', self.key, self.value)
-
-
-if __name__ == '__main__':
- nv = Function('nv').addGraph(
- AnonymousFunction('', Assignment('chart',
- Function('nv').models.pieChart(
- ).x(
- AnonymousFunction('d', 'return d.label;')
- ).y(
- AnonymousFunction('d', 'return d.value;')
- ).showLabels('true')
- )
- )
- )
- print nv
+ return '%s%s = %s;' % ('var ' if self.scoped else '', self.key, self.value)
+
+
+def indent(func):
+ # TODO: Add indents to function str
+ return str(func)
View
@@ -11,6 +11,7 @@
from nvd3 import discreteBarChart
from nvd3 import pieChart
from nvd3 import multiBarChart
+from nvd3.translator import Function, AnonymousFunction, Assignment
import random
import unittest
import datetime
@@ -148,6 +149,26 @@ def test_pieChart(self):
chart.buildhtml()
+class TranslatorTest(unittest.TestCase):
+
+ def test_pieChart(self):
+ func = Function('nv').addGraph(
+ AnonymousFunction('', Assignment('chart',
+ Function('nv').models.pieChart(
+ ).x(
+ AnonymousFunction('d', 'return d.label;')
+ ).y(
+ AnonymousFunction('d', 'return d.value;')
+ ).showLabels('true')
+ )
+ )
+ )
+ self.assertEqual(str(func),
+ 'nv.addGraph(function() { var chart = '
+ 'nv.models.pieChart().x(function(d) { return d.label; '
+ '}).y(function(d) { return d.value; }).showLabels(true); })')
+
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 9134a9d

Please sign in to comment.