[Checkins] SVN: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/ 100% coverage of conversion script
Godefroid Chapelle
gotcha at bubblenet.be
Wed Jan 5 06:56:52 EST 2011
Log message for revision 119384:
100% coverage of conversion script
Changed:
U gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/converthtmltests.py
A gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/nocommands.html
A gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/notitle.html
U gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/test_converthtmltests.py
-=-
Modified: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/converthtmltests.py
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/converthtmltests.py 2011-01-05 11:55:20 UTC (rev 119383)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/converthtmltests.py 2011-01-05 11:56:51 UTC (rev 119384)
@@ -21,7 +21,7 @@
layer = $layer
-$tests
+$methods
def test_suite():
return unittest.makeSuite(TestAll)
@@ -29,7 +29,7 @@
variable_regexp = re.compile('\$\{(?P<varname>\w*)\}')
-method_body_template = Template('''\
+method_template = Template('''\
def test_$testname(self):
selenium = self.selenium
$commands
@@ -47,8 +47,9 @@
matched = variable_regexp.match(arg)
if matched is None:
arguments.append('"%s"' % arg)
- else:
- arguments.append("self.getVar('%s')" % matched.group('varname'))
+ #XXX selenese should implement storeText
+ #else:
+ # arguments.append("self.getVar('%s')" % matched.group('varname'))
return ' selenium.%s(%s)' % (command, ', '.join(arguments))
@@ -102,12 +103,12 @@
print "Parsing [%s]" % filename
filename = os.path.abspath(filename)
testname, commands = parse_file(filename)
- if testname is None:
+ if testname is None or len(commands) == 0:
continue
- method_body = method_body_template.substitute(dict(
+ method = method_template.substitute(dict(
testname=testname,
commands='\n'.join(commands)))
- yield method_body
+ yield method
def parse_file(filename):
@@ -125,24 +126,35 @@
return testname, commands
-def main():
+def make_module(methods, layer, layer_module):
+ return module_template.substitute(dict(
+ testname='all',
+ methods='\n'.join(methods),
+ layer=layer,
+ layer_module=layer_module,
+ ))
+
+
+def main(args=None):
parser = make_parser()
- options, directory = parse_options(parser)
- tests = parse_directory(directory, options.verbose)
+ options, directory = parse_options(parser, args)
+ verbose = options.verbose
+ target = os.path.abspath(options.target)
+ layer = options.layer
+ layer_module = ".".join(layer.split('.')[:-1])
- target = os.path.abspath(options.target)
+ methods = [method for method in parse_directory(directory, verbose)]
+
+ if len(methods) == 0:
+ print "No file was generated !"
+ return
if options.verbose:
print "Generating [%s]" % target
f = open(target, 'wb')
- layer = options.layer
- layer_module = ".".join(layer.split('.')[:-1])
- f.write(module_template.substitute(dict(
- testname='all',
- tests='\n'.join(tests),
- layer=layer,
- layer_module=layer_module,
- )))
+ module = make_module(methods, layer, layer_module)
+ f.write(module)
f.close()
+
if __name__ == '__main__':
main()
Added: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/nocommands.html
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/nocommands.html (rev 0)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/nocommands.html 2011-01-05 11:56:51 UTC (rev 119384)
@@ -0,0 +1,9 @@
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>nocommands</title>
+</head>
+<body>
+<p>No commands</p>
+</body>
+</html>
Added: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/notitle.html
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/notitle.html (rev 0)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/notitle.html 2011-01-05 11:56:51 UTC (rev 119384)
@@ -0,0 +1,12 @@
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">notitle</td></tr>
+</thead><tbody>
+</tbody></table>
+</body>
+</html>
Modified: gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/test_converthtmltests.py
===================================================================
--- gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/test_converthtmltests.py 2011-01-05 11:55:20 UTC (rev 119383)
+++ gocept.selenium/branches/gotcha-generator/src/gocept/selenium/scripts/tests/test_converthtmltests.py 2011-01-05 11:56:51 UTC (rev 119384)
@@ -1,8 +1,47 @@
import unittest
import mock
import tempfile
+import os
+import sys
+import StringIO
+PLONE3LOGIN_METHOD = '''\
+ def test_plone3login(self):
+ selenium = self.selenium
+ selenium.open("/plone/login_form")
+ selenium.type("__ac_name", "admin")
+ selenium.type("__ac_password", "admin")
+ selenium.clickAndWait("submit")
+ selenium.assertText("//a[@id=\'user-name\']/span", "admin")
+'''
+LAYER = "module.layer"
+
+PLONE3LOGIN_MODULE = '''\
+import unittest
+
+import gocept.selenium.plonetesting
+import module
+
+
+class TestAll(gocept.selenium.plonetesting.TestCase):
+
+ layer = module.layer
+
+ def test_plone3login(self):
+ selenium = self.selenium
+ selenium.open("/plone/login_form")
+ selenium.type("__ac_name", "admin")
+ selenium.type("__ac_password", "admin")
+ selenium.clickAndWait("submit")
+ selenium.assertText("//a[@id=\'user-name\']/span", "admin")
+
+
+def test_suite():
+ return unittest.makeSuite(TestAll)
+'''
+
+
class TestConversion(unittest.TestCase):
def test_parse_options_no_layer(self):
@@ -21,7 +60,7 @@
from gocept.selenium.scripts.converthtmltests import DIRECTORY_REQUIRED
parser = make_parser()
parser.error = mock.Mock()
- parse_options(parser, ['-l', 'module.layer'])
+ parse_options(parser, ['-l', LAYER])
self.failUnless(parser.error.called)
parser.error.assert_called_with(DIRECTORY_REQUIRED)
@@ -41,7 +80,7 @@
from gocept.selenium.scripts.converthtmltests import ONE_DIRECTORY
parser = make_parser()
parser.error = mock.Mock()
- parse_options(parser, ['-l', 'module.layer', 'first', 'second'])
+ parse_options(parser, ['-l', LAYER, 'first', 'second'])
self.failUnless(parser.error.called)
parser.error.assert_called_with(ONE_DIRECTORY)
@@ -52,7 +91,7 @@
DIRECTORY_NOT_EXIST)
parser = make_parser()
parser.error = mock.Mock()
- parse_options(parser, ['-l', 'module.layer', 'first'])
+ parse_options(parser, ['-l', LAYER, 'first'])
self.failUnless(parser.error.called)
parser.error.assert_called_with(DIRECTORY_NOT_EXIST)
@@ -63,11 +102,10 @@
parser = make_parser()
parser.error = mock.Mock()
source = tempfile.gettempdir()
- layer = 'module.layer'
options, directory = parse_options(parser,
- ['-l', layer, source])
+ ['-l', LAYER, source])
self.assertEquals(source, directory)
- self.assertEquals(options.layer, layer)
+ self.assertEquals(options.layer, LAYER)
self.assertEquals(options.target, DEFAULT_TARGET)
self.failUnless(options.verbose)
@@ -77,9 +115,8 @@
parser = make_parser()
parser.error = mock.Mock()
source = tempfile.mkdtemp()
- layer = 'module.layer'
options, directory = parse_options(parser,
- ['-q', '-l', layer, source])
+ ['-q', '-l', LAYER, source])
self.failIf(options.verbose)
def test_parse_options_target(self):
@@ -89,7 +126,133 @@
parser.error = mock.Mock()
source = tempfile.mkdtemp()
target = tempfile.mktemp()
- layer = 'module.layer'
options, directory = parse_options(parser,
- ['-f', target, '-l', layer, source])
+ ['-f', target, '-l', LAYER, source])
self.assertEquals(options.target, target)
+
+ def test_parse_file(self):
+ from gocept.selenium.scripts.converthtmltests import parse_file
+
+ import gocept.selenium.scripts.tests
+ tests_dir = os.path.dirname(gocept.selenium.scripts.tests.__file__)
+ filename = os.path.join(tests_dir, 'plone3login.html')
+
+ testname, commands = parse_file(filename)
+ self.assertEquals(testname, 'plone3login')
+ self.assertEquals(len(commands), 5)
+ self.assertEquals(' selenium.open("/plone/login_form")',
+ commands[0])
+
+ def test_parse_file_no_title(self):
+ from gocept.selenium.scripts.converthtmltests import parse_file
+
+ import gocept.selenium.scripts.tests
+ tests_dir = os.path.dirname(gocept.selenium.scripts.tests.__file__)
+ filename = os.path.join(tests_dir, 'notitle.html')
+
+ testname, commands = parse_file(filename)
+ self.assertEquals(None, testname)
+
+ def test_parse_file_no_commands(self):
+ from gocept.selenium.scripts.converthtmltests import parse_file
+
+ import gocept.selenium.scripts.tests
+ tests_dir = os.path.dirname(gocept.selenium.scripts.tests.__file__)
+ filename = os.path.join(tests_dir, 'nocommands.html')
+
+ testname, commands = parse_file(filename)
+ self.assertEquals('nocommands', testname)
+ self.assertEquals(len(commands), 0)
+
+ def test_formatcommand_no_command(self):
+ from gocept.selenium.scripts.converthtmltests import formatcommand
+ line = formatcommand('')
+ self.assertEquals(line, '')
+
+ def test_formatcommand_command_without_args(self):
+ from gocept.selenium.scripts.converthtmltests import formatcommand
+ line = formatcommand('command')
+ self.assertEquals(line, ' selenium.command()')
+
+ def test_formatcommand_command_with_single_arg(self):
+ from gocept.selenium.scripts.converthtmltests import formatcommand
+ line = formatcommand('command', 'arg')
+ self.assertEquals(line, ' selenium.command("arg")')
+
+ def test_formatcommand_command_with_two_args(self):
+ from gocept.selenium.scripts.converthtmltests import formatcommand
+ line = formatcommand('command', 'arg1', 'arg2')
+ self.assertEquals(line, ' selenium.command("arg1", "arg2")')
+
+ def test_formatcommand_command_with_three_args_one_empty(self):
+ from gocept.selenium.scripts.converthtmltests import formatcommand
+ line = formatcommand('command', 'arg1', '', 'arg2')
+ self.assertEquals(line, ' selenium.command("arg1", "arg2")')
+
+ def test_parse_directory(self):
+ from gocept.selenium.scripts.converthtmltests import parse_directory
+
+ import gocept.selenium.scripts.tests
+ tests_dir = os.path.dirname(gocept.selenium.scripts.tests.__file__)
+
+ tests = [test for test in parse_directory(tests_dir, False)]
+ self.assertEquals(len(tests), 1)
+ self.assertEquals(tests[0], PLONE3LOGIN_METHOD)
+
+ def test_parse_directory_no_html(self):
+ from gocept.selenium.scripts.converthtmltests import parse_directory
+
+ import gocept.selenium.scripts
+ tests_dir = os.path.dirname(gocept.selenium.scripts.__file__)
+
+ tests = [test for test in parse_directory(tests_dir, False)]
+ self.assertEquals(len(tests), 0)
+
+ def test_make_module(self):
+ from gocept.selenium.scripts.converthtmltests import make_module
+
+ module = make_module([PLONE3LOGIN_METHOD], LAYER, 'module')
+ self.assertEquals(module, PLONE3LOGIN_MODULE)
+
+ def test_main(self):
+ from gocept.selenium.scripts.converthtmltests import main
+ import gocept.selenium.scripts.tests
+ tests_dir = os.path.dirname(gocept.selenium.scripts.tests.__file__)
+ target = tempfile.mktemp()
+
+ output = StringIO.StringIO()
+
+ sys.stdout = output
+ try:
+ main(['-f', target, '-l', LAYER, tests_dir])
+ module = open(target).read()
+ self.assertEquals(module, PLONE3LOGIN_MODULE)
+ text = output.getvalue()
+ lines = text.splitlines()
+ self.assertEquals(len(lines), 4)
+ self.failUnless(lines[0].startswith('Parsing ['))
+ self.failUnless('plone3login.html]' in text)
+ self.failUnless(lines[1].startswith('Parsing ['))
+ self.failUnless('nocommands.html]' in text)
+ self.failUnless(lines[2].startswith('Parsing ['))
+ self.failUnless('notitle.html]' in text)
+ self.failUnless(lines[3].startswith('Generating ['))
+ self.failUnless(target in lines[3])
+ finally:
+ sys.stdout = sys.__stdout__
+
+ def test_main_no_html(self):
+ from gocept.selenium.scripts.converthtmltests import main
+ import gocept.selenium.scripts
+ tests_dir = os.path.dirname(gocept.selenium.scripts.__file__)
+ target = tempfile.mktemp()
+
+ output = StringIO.StringIO()
+
+ sys.stdout = output
+ try:
+ main(['-f', target, '-l', LAYER, tests_dir])
+ self.failIf(os.path.exists(target))
+ self.assertEquals(output.getvalue(), 'No file was generated !\n')
+ finally:
+ sys.stdout = sys.__stdout__
More information about the checkins
mailing list