Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge commit 'origin/next' into slide-compat

  • Loading branch information...
commit ec4dcae501e8af81e927f79e9621ea7df8ff3ff8 2 parents d2a3e20 + 052298e
@rtyler authored
View
17 SetupConfig.py
@@ -1,4 +1,6 @@
#-------Main Package Settings-----------#
+import sys
+
name = 'Cheetah'
from cheetah.Version import Version as version
maintainer = "R. Tyler Ballance"
@@ -59,13 +61,14 @@
install_requires = [
"Markdown >= 2.0.1",
]
- # use 'entry_points' instead of 'scripts'
- del scripts
- entry_points = {
- 'console_scripts': [
- 'cheetah = Cheetah.CheetahWrapper:_cheetah',
- 'cheetah-compile = Cheetah.CheetahWrapper:_cheetah_compile',
- ]
+ if sys.platform == 'win32':
+ # use 'entry_points' instead of 'scripts'
+ del scripts
+ entry_points = {
+ 'console_scripts': [
+ 'cheetah = Cheetah.CheetahWrapper:_cheetah',
+ 'cheetah-compile = Cheetah.CheetahWrapper:_cheetah_compile',
+ ]
}
except ImportError:
print 'Not using setuptools, so we cannot install the Markdown dependency'
View
220 cheetah/Tests/Performance.py
@@ -0,0 +1,220 @@
+#!/usr/bin/env python
+
+import Cheetah.NameMapper
+import Cheetah.Template
+from Cheetah.Utils import statprof
+
+import os
+import sys
+import unittest
+
+from test import pystone
+import time
+
+# This can be turned on with the `--debug` flag when running the test
+# and will cause the tests to all just dump out how long they took
+# insteasd of asserting on duration
+DEBUG = False
+
+# TOLERANCE in Pystones
+kPS = 1000
+TOLERANCE = 0.5*kPS
+
+class DurationError(AssertionError):
+ pass
+
+_pystone_calibration_mark = None
+def _pystone_calibration():
+ global _pystone_calibration_mark
+ if not _pystone_calibration_mark:
+ _pystone_calibration_mark = pystone.pystones(loops=pystone.LOOPS)
+ return _pystone_calibration_mark
+
+def perftest(max_num_pystones, current_pystone=None):
+ '''
+ Performance test decorator based off the 'timedtest'
+ decorator found in this Active State recipe:
+ http://code.activestate.com/recipes/440700/
+ '''
+ if not isinstance(max_num_pystones, float):
+ max_num_pystones = float(max_num_pystones)
+
+ if not current_pystone:
+ current_pystone = _pystone_calibration()
+
+ def _test(function):
+ def wrapper(*args, **kw):
+ start_time = time.time()
+ try:
+ return function(*args, **kw)
+ finally:
+ total_time = time.time() - start_time
+ if total_time == 0:
+ pystone_total_time = 0
+ else:
+ pystone_rate = current_pystone[0] / current_pystone[1]
+ pystone_total_time = total_time / pystone_rate
+ global DEBUG
+ if DEBUG:
+ print 'The test "%s" took: %s pystones' % (function.func_name,
+ pystone_total_time)
+ else:
+ if pystone_total_time > (max_num_pystones + TOLERANCE):
+ raise DurationError((('Test too long (%.2f Ps, '
+ 'need at most %.2f Ps)')
+ % (pystone_total_time,
+ max_num_pystones)))
+ return wrapper
+ return _test
+
+
+class DynamicTemplatePerformanceTest(unittest.TestCase):
+ loops = 10
+ @perftest(1200)
+ def test_BasicDynamic(self):
+ template = '''
+ #def foo(arg1, arg2)
+ #pass
+ #end def
+ '''
+ for i in xrange(self.loops):
+ klass = Cheetah.Template.Template.compile(template)
+ assert klass
+
+class PerformanceTest(unittest.TestCase):
+ iterations = 1000000
+ display = False
+ def setUp(self):
+ super(PerformanceTest, self).setUp()
+ statprof.start()
+
+ def runTest(self):
+ for i in xrange(self.iterations):
+ if hasattr(self, 'performanceSample'):
+ self.display = True
+ self.performanceSample()
+
+ def tearDown(self):
+ super(PerformanceTest, self).tearDown()
+ statprof.stop()
+ if self.display:
+ print '>>> %s (%d iterations) ' % (self.__class__.__name__,
+ self.iterations)
+ statprof.display()
+
+class DynamicMethodCompilationTest(PerformanceTest):
+ def performanceSample(self):
+ template = '''
+ #import sys
+ #import os
+ #def testMethod()
+ #set foo = [1, 2, 3, 4]
+ #return $foo[0]
+ #end def
+ '''
+ template = Cheetah.Template.Template.compile(template,
+ keepRefToGeneratedCode=False)
+ template = template()
+ value = template.testMethod()
+
+class DynamicSimpleCompilationTest(PerformanceTest):
+ def performanceSample(self):
+ template = '''
+ #import sys
+ #import os
+ #set foo = [1,2,3,4]
+
+ Well hello there! This is basic.
+
+ Here's an array too: $foo
+ '''
+ template = Cheetah.Template.Template.compile(template,
+ keepRefToGeneratedCode=False)
+ template = template()
+ template = unicode(template)
+
+
+class FilterTest(PerformanceTest):
+ template = None
+ def setUp(self):
+ super(FilterTest, self).setUp()
+ template = '''
+ #import sys
+ #import os
+ #set foo = [1, 2, 3, 4]
+
+ $foo, $foo, $foo
+ '''
+ template = Cheetah.Template.Template.compile(template,
+ keepRefToGeneratedCode=False)
+ self.template = template()
+
+ def performanceSample(self):
+ value = unicode(self.template)
+
+
+class LongCompileTest(PerformanceTest):
+ ''' Test the compilation on a sufficiently large template '''
+ def compile(self, template):
+ return Cheetah.Template.Template.compile(template, keepRefToGeneratedCode=False)
+
+ def performanceSample(self):
+ template = '''
+ #import sys
+ #import Cheetah.Template
+
+ #extends Cheetah.Template.Template
+
+ #def header()
+ <center><h2>This is my header</h2></center>
+ #end def
+
+ #def footer()
+ #return "Huzzah"
+ #end def
+
+ #def scripts()
+ #pass
+ #end def
+
+ #def respond()
+ <html>
+ <head>
+ <title>${title}</title>
+
+ $scripts()
+ </head>
+ <body>
+ $header()
+
+ #for $i in $xrange(10)
+ This is just some stupid page!
+ <br/>
+ #end for
+
+ <br/>
+ $footer()
+ </body>
+ </html>
+ #end def
+
+ '''
+ return self.compile(template)
+
+class LongCompile_CompilerSettingsTest(LongCompileTest):
+ def compile(self, template):
+ return Cheetah.Template.Template.compile(template, keepRefToGeneratedCode=False,
+ compilerSettings={'useStackFrames' : True, 'useAutocalling' : True})
+
+class LongCompileAndRun(LongCompileTest):
+ def performanceSample(self):
+ template = super(LongCompileAndRun, self).performanceSample()
+ template = template(searchList=[{'title' : 'foo'}])
+ template = template.respond()
+
+
+if __name__ == '__main__':
+ if '--debug' in sys.argv:
+ DEBUG = True
+ sys.argv = [arg for arg in sys.argv if not arg == '--debug']
+ unittest.main()
View
6 cheetah/Tests/Test.py
@@ -13,8 +13,6 @@
import sys
import unittest_local_copy as unittest
-
-
import SyntaxAndOutput
import NameMapper
import Template
@@ -22,6 +20,7 @@
import Cheps
import Regressions
import Unicode
+import VerifyType
suites = [
unittest.findTestCases(SyntaxAndOutput),
@@ -30,6 +29,7 @@
unittest.findTestCases(Regressions),
unittest.findTestCases(Unicode),
unittest.findTestCases(Cheps),
+ unittest.findTestCases(VerifyType),
]
if not sys.platform.startswith('java'):
@@ -41,5 +41,5 @@
import xmlrunner
runner = xmlrunner.XMLTestRunner(filename='Cheetah-Tests.xml')
- results = runner.run(unittest.TestSuite(suites))
+ results = runner.run(unittest.TestSuite(suites))
View
157 cheetah/Tests/VerifyType.py
@@ -0,0 +1,157 @@
+import unittest
+
+from Cheetah.Utils import VerifyType
+from Cheetah import _verifytype
+
+class VerifyType_Test(unittest.TestCase):
+ def test_Verified(self):
+ arg = 'foo'
+ legalTypes = [str, unicode]
+ try:
+ rc = VerifyType.VerifyType(arg, 'arg', legalTypes, 'string')
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ try:
+ rc = _verifytype.verifyType(arg, 'arg', legalTypes, 'string')
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ def test_Unverified(self):
+ arg = 'foo'
+ legalTypes = [list, dict]
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg,
+ 'arg', legalTypes, 'list or dict')
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg,
+ 'arg', legalTypes, 'list or dict')
+
+ def test_IncorrectNumberOfArgs(self):
+ arg = 'foo'
+ legalTypes = [str, unicode]
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType)
+ self.failUnlessRaises(TypeError, _verifytype.verifyType)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg)
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg,
+ 'arg')
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg,
+ 'arg')
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg,
+ 'arg', legalTypes)
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg,
+ 'arg', legalTypes)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg,
+ 'arg', legalTypes, 'string', 'errmsgExtra', 'one more')
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg,
+ 'arg', legalTypes, 'string', 'errmsgExtra', 'one more')
+
+ def test_LegalTypesNotIterable(self):
+ arg = 'foo'
+ legalTypes = 1
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyType, arg,
+ 'arg', legalTypes, 'string')
+ self.failUnlessRaises(TypeError, _verifytype.verifyType, arg,
+ 'arg', legalTypes, 'string')
+
+class FakeClass(dict):
+ pass
+
+class VerifyTypeClass_Test(unittest.TestCase):
+ def test_VerifiedClass(self):
+ arg = FakeClass
+ legalTypes = [type]
+ try:
+ rc = VerifyType.VerifyTypeClass(arg, 'arg', legalTypes, '', dict)
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ try:
+ rc = _verifytype.verifyTypeClass(arg, 'arg', legalTypes, 'foo', dict)
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ def test_UnverifiedClass(self):
+ arg = FakeClass
+ legalTypes = [type]
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ legalTypes, 'subclass of list', list)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ legalTypes, 'subclass of list', list)
+
+ def test_Verified(self):
+ arg = 'foo'
+ legalTypes = [str, unicode]
+ try:
+ rc = VerifyType.VerifyTypeClass(arg, 'arg', legalTypes, 'string', int)
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ try:
+ rc = _verifytype.verifyTypeClass(arg, 'arg', legalTypes, 'string', int)
+ assert rc
+ except TypeError:
+ self.fail('Should not have raised a TypeError here')
+
+ def test_Unverified(self):
+ arg = 'foo'
+ legalTypes = [list, dict]
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg', legalTypes, 'list or dict', int)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg', legalTypes, 'list or dict', int)
+
+ def test_IncorrectNumberOfArgs(self):
+ arg = 'foo'
+ legalTypes = [str, unicode]
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg')
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg')
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg', legalTypes)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg', legalTypes)
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg', legalTypes, 'string')
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg', legalTypes, 'string')
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg', legalTypes, 'string', int, 'errmsgExtra', 'one more')
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg', legalTypes, 'string', int, 'errmsgExtra', 'one more')
+
+ def test_LegalTypesNotIterable(self):
+ arg = 'foo'
+ legalTypes = 1
+
+ self.failUnlessRaises(TypeError, VerifyType.VerifyTypeClass, arg,
+ 'arg', legalTypes, 'string', int)
+ self.failUnlessRaises(TypeError, _verifytype.verifyTypeClass, arg,
+ 'arg', legalTypes, 'string', int)
+
+
+
+
+if __name__ == '__main__':
+ unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.