1
# Copyright (c) 2006 Divmod.
2
# See LICENSE for details.
5
Test the dependency tracking and javascript generation code in
9
from textwrap import dedent
10
from twisted.internet.utils import getProcessOutput
11
from twisted.trial.unittest import TestCase
12
from nevow.testutil import setJavascriptInterpreterOrSkip
13
from nevow.jsutil import getDependencies, generateTestScript
14
from nevow import athena
16
class _ConsoleJSTestMixin:
18
Things that might be useful for testing JavaScript interaction functions
19
from L{nevow.testutil}.
22
def _getPackages(self):
24
@return: the mapping of all javascript packages plus some fake modules
26
packages = athena.allJavascriptPackages()
28
{'ConsoleJSTestFoo': self._outputToTempFile(
29
'print("hello from ConsoleJSTestFoo");'),
30
'ConsoleJSTestFoo.Bar': self._outputToTempFile(
33
// import ConsoleJSTestFoo
34
print("hello from ConsoleJSTestFoo.Bar");
36
'ConsoleJSTestFoo.Baz': self._outputToTempFile(
39
// import ConsoleJSTestFoo
40
// import ConsoleJSTestFoo.Bar
41
print("hello from ConsoleJSTestFoo.Baz");
45
def _outputToTempFile(self, s):
47
Write the contents of string C{s} to a tempfile and return the
48
filename that was used
50
@param s: file contents
57
fObj = file(fname, 'w')
62
class DependenciesTestCase(TestCase, _ConsoleJSTestMixin):
64
Tests for L{getDependencies}
66
def test_getDependenciesNoModules(self):
68
Test that L{getDependencies} returns the empty list when the js module
69
it's passed doesn't explicitly import anything and the C{bootstrap} and
70
C{ignore} parameters are empty
72
deps = getDependencies(
73
self._outputToTempFile(''), ignore=(), bootstrap=())
74
self.assertEqual(len(deps), 0)
77
def test_getDependenciesBootstrap(self):
79
Test that L{getDependencies} returns a list containing only the
80
bootstrap modules when the js module it's passed doesn't explicitly
81
import anything and the "ignore" parameter is empty.
83
bootstrap = ['ConsoleJSTestFoo.Bar', 'ConsoleJSTestFoo.Baz']
85
deps = getDependencies(
86
self._outputToTempFile(''),
89
packages=self._getPackages())
90
self.assertEqual([d.name for d in deps], bootstrap)
93
def test_getDependenciesIgnore(self):
95
Test that L{getDependencies} observes the C{ignore} parameter
97
deps = getDependencies(
98
self._outputToTempFile(
101
// import ConsoleJSTestFoo.Bar
102
// import ConsoleJSTestFoo.Baz
104
ignore=('ConsoleJSTestFoo.Bar',),
106
packages=self._getPackages())
108
self.assertEqual([d.name for d in deps], ['ConsoleJSTestFoo', 'ConsoleJSTestFoo.Baz'])
110
def test_getDependenciesAll(self):
112
Test that L{getDependencies} works if we import a single module which
113
in turn depends on multiple modules
115
fname = self._outputToTempFile(
116
'// import ConsoleJSTestFoo.Baz')
118
deps = getDependencies(
122
packages=self._getPackages())
124
self.assertEqual([d.name for d in deps], ['ConsoleJSTestFoo', 'ConsoleJSTestFoo.Bar', 'ConsoleJSTestFoo.Baz'])
128
class JSGenerationTestCase(TestCase, _ConsoleJSTestMixin):
130
Tests for L{generateTestScript}
132
javascriptInterpreter = None
134
def test_generateTestScript(self):
136
Test for L{generateTestScript}
138
fname = self._outputToTempFile(
141
// import ConsoleJSTestFoo.Bar
142
// import ConsoleJSTestFoo.Baz
143
print("hello from the test module");
146
deps = getDependencies(
150
packages=self._getPackages())
152
script = generateTestScript(
156
scriptfname = self._outputToTempFile(script)
159
self.assertEqual(s.split('\n'),
160
['hello from ConsoleJSTestFoo',
161
'hello from ConsoleJSTestFoo.Bar',
162
'hello from ConsoleJSTestFoo.Baz',
163
'hello from the test module',
166
result = getProcessOutput(self.javascriptInterpreter, ('-f', scriptfname))
167
result.addCallback(gotResult)
170
setJavascriptInterpreterOrSkip(JSGenerationTestCase)