test_consolejstest.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » test » test_consolejstest.py
# Copyright (c) 2006 Divmod.
# See LICENSE for details.

"""
Test the dependency tracking and javascript generation code in
L{nevow.jsutil}.
"""

from textwrap import dedent
from twisted.internet.utils import getProcessOutput
from twisted.trial.unittest import TestCase
from nevow.testutil import setJavascriptInterpreterOrSkip
from nevow.jsutil import getDependencies,generateTestScript
from nevow import athena

class _ConsoleJSTestMixin:
    """
    Things that might be useful for testing JavaScript interaction functions
    from L{nevow.testutil}.
    """

    def _getPackages(self):
        """
        @return: the mapping of all javascript packages plus some fake modules
        """
        packages = athena.allJavascriptPackages()
        packages.update(
            {'ConsoleJSTestFoo': self._outputToTempFile(
                        'print("hello from ConsoleJSTestFoo");'),
             'ConsoleJSTestFoo.Bar': self._outputToTempFile(
                            dedent(
                                '''
                                // import ConsoleJSTestFoo
                                print("hello from ConsoleJSTestFoo.Bar");
                                ''')),
             'ConsoleJSTestFoo.Baz': self._outputToTempFile(
                            dedent(
                                '''
                                // import ConsoleJSTestFoo
                                // import ConsoleJSTestFoo.Bar
                                print("hello from ConsoleJSTestFoo.Baz");
                                '''))})
        return packages

    def _outputToTempFile(self, s):
        """
        Write the contents of string C{s} to a tempfile and return the
        filename that was used

        @param s: file contents
        @type s: C{str}

        @return: filename
        @rtype: C{str}
        """
        fname = self.mktemp()
        fObj = file(fname, 'w')
        fObj.write(s)
        fObj.close()
        return fname

class DependenciesTestCase(TestCase, _ConsoleJSTestMixin):
    """
    Tests for L{getDependencies}
    """
    def test_getDependenciesNoModules(self):
        """
        Test that L{getDependencies} returns the empty list when the js module
        it's passed doesn't explicitly import anything and the C{bootstrap} and
        C{ignore} parameters are empty
        """
        deps = getDependencies(
                self._outputToTempFile(''), ignore=(), bootstrap=())
        self.assertEqual(len(deps), 0)


    def test_getDependenciesBootstrap(self):
        """
        Test that L{getDependencies} returns a list containing only the
        bootstrap modules when the js module it's passed doesn't explicitly
        import anything and the "ignore" parameter is empty.
        """
        bootstrap = ['ConsoleJSTestFoo.Bar', 'ConsoleJSTestFoo.Baz']

        deps = getDependencies(
                self._outputToTempFile(''),
                ignore=(),
                bootstrap=bootstrap,
                packages=self._getPackages())
        self.assertEqual([d.name for d in deps], bootstrap)


    def test_getDependenciesIgnore(self):
        """
        Test that L{getDependencies} observes the C{ignore} parameter
        """
        deps = getDependencies(
                self._outputToTempFile(
                    dedent(
                        '''
                        // import ConsoleJSTestFoo.Bar
                        // import ConsoleJSTestFoo.Baz
                        ''')),
                ignore=('ConsoleJSTestFoo.Bar',),
                bootstrap=(),
                packages=self._getPackages())

        self.assertEqual([d.name for d in deps], ['ConsoleJSTestFoo', 'ConsoleJSTestFoo.Baz'])

    def test_getDependenciesAll(self):
        """
        Test that L{getDependencies} works if we import a single module which
        in turn depends on multiple modules
        """
        fname = self._outputToTempFile(
            '// import ConsoleJSTestFoo.Baz')

        deps = getDependencies(
                fname,
                ignore=(),
                bootstrap=(),
                packages=self._getPackages())

        self.assertEqual([d.name for d in deps], ['ConsoleJSTestFoo', 'ConsoleJSTestFoo.Bar', 'ConsoleJSTestFoo.Baz'])



class JSGenerationTestCase(TestCase, _ConsoleJSTestMixin):
    """
    Tests for L{generateTestScript}
    """
    javascriptInterpreter = None

    def test_generateTestScript(self):
        """
        Test for L{generateTestScript}
        """
        fname = self._outputToTempFile(
                    dedent(
                        '''
                        // import ConsoleJSTestFoo.Bar
                        // import ConsoleJSTestFoo.Baz
                        print("hello from the test module");
                        '''))

        deps = getDependencies(
                fname,
                ignore=(),
                bootstrap=(),
                packages=self._getPackages())

        script = generateTestScript(
                    fname,
                    dependencies=deps)

        scriptfname = self._outputToTempFile(script)

        def gotResult(s):
            self.assertEqual(s.split('\n'),
                             ['hello from ConsoleJSTestFoo',
                              'hello from ConsoleJSTestFoo.Bar',
                              'hello from ConsoleJSTestFoo.Baz',
                              'hello from the test module',
                              ''])

        result = getProcessOutput(self.javascriptInterpreter, ('-f', scriptfname))
        result.addCallback(gotResult)
        return result

setJavascriptInterpreterOrSkip(JSGenerationTestCase)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.