[KSS-checkins] r48489 - kukit/kss.base/branches/basehttpserver-functional-testing/kss/base

jvloothuis at codespeak.net jvloothuis at codespeak.net
Sat Nov 10 01:36:25 CET 2007


Author: jvloothuis
Date: Sat Nov 10 01:36:25 2007
New Revision: 48489

Added:
   kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/testing.py
Modified:
   kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/config.py
Log:
Added basic prototype (not working fully yet)

Modified: kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/config.py
==============================================================================
--- kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/config.py	(original)
+++ kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/config.py	Sat Nov 10 01:36:25 2007
@@ -4,6 +4,8 @@
 from kss.base.corecommands import KSSCoreCommands
 from kss.base.coreselectors import css, htmlid, samenode, parentnode
 
+from kss.base.testing import Section, Test
+
 kukit_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kukit')
 
 # Define the Javascripts by hand to ensure the order
@@ -48,3 +50,9 @@
 
     selectors = [css, htmlid, samenode, parentnode]
 
+    functional_tests = [Test(
+            name='Focus', 
+            html='/tmp/kss_demo.html', 
+            css='/tmp/demo.css', 
+            kss='blah.kss', 
+            handler=lambda request: 'ok')]

Added: kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/testing.py
==============================================================================
--- (empty file)
+++ kukit/kss.base/branches/basehttpserver-functional-testing/kss/base/testing.py	Sat Nov 10 01:36:25 2007
@@ -0,0 +1,147 @@
+import string
+from kss.base.plugin import activated_plugins
+from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
+
+front_page = '''
+<html>
+<body>
+<h1>Tests for the activated plugins</h1>
+
+$plugins
+
+</body>
+</html>
+'''
+
+plugin_html = '''
+<h2>$id</h2>
+<ol>
+$tests
+</ol>
+'''
+
+test_html = '''
+<li><a href="$url">$name</a></li>
+'''
+
+test_page_html = '''
+<html>
+  <head>
+    <link rel="stylesheet" type="text/css" href="$css_url"/>
+    <link rel="kinetic-stylesheet" type="text/css" href="$kss_url" />
+   </head>
+   <body>
+   $html
+   </body>
+</html>
+'''
+
+class PluginTestsuiteRequestHandler(BaseHTTPRequestHandler):
+
+    def do_GET(self):
+        path = self.path
+        if path == '/':
+            return self.do_index()
+        elif path.endswith('.html'):
+            return self.do_test_html()
+        else:
+            return self.do_test_css()
+
+    def send_html_headers(self):
+        self.send_response(200)
+        self.send_header('Content-type', 'text/html')
+        self.end_headers()
+
+    def name_to_url_safe(self, name):
+        return name.replace(' ', '-')
+
+    def css_url(self, plugin_id, test):
+        return self.test_base_url(plugin_id, test) + '.css'
+
+    def kss_url(self, plugin_id, test):
+        return self.test_base_url(plugin_id, test) + '.kss'
+
+    def html_url(self, plugin_id, test):
+        return self.test_base_url(plugin_id, test) + '.html'
+
+    def test_base_url(self, plugin_id, test):
+        return '/%s/%s' % (self.name_to_url_safe(plugin_id), 
+                           self.name_to_url_safe(test.name))
+
+    def load_from_path(self, extension):
+        parts = self.path[:-len(extension)].split('/')
+        print parts
+        for id, plugin in activated_plugins():
+            if parts[1] == id:
+                for test in plugin.functional_tests:
+                    if self.name_to_url_safe(test.name) == parts[2]:
+                        return (id, plugin, test)
+
+    def do_test_css(self):
+        self.send_response(200)
+        self.send_header('Content-Type', 'text/css')
+        self.end_headers()
+
+        id, plugin, test = self.load_from_path('.css')
+
+        self.wfile.write(open(test.css).read())
+        self.wfile.close()
+
+    def do_test_html(self):
+        self.send_html_headers()
+
+        html = string.Template(test_page_html)
+        id, plugin, test = self.load_from_path('.html')
+        self.wfile.write(html.substitute(
+                css_url=self.css_url(id, test),
+                kss_url=self.kss_url(id, test),
+                html=open(test.html).read()))
+        self.wfile.close()
+
+    def do_index(self):
+        self.send_html_headers()
+
+        html = string.Template(front_page)
+        plugins_html = []
+        for id, plugin in activated_plugins():
+            plugins_html.append(self.plugin_section(id, plugin))
+
+        phtml = ''.join(plugins_html)
+        self.wfile.write(html.substitute(plugins=phtml))
+        self.wfile.close()
+
+    def plugin_section(self, id, plugin):
+        html = string.Template(plugin_html)
+        tests_html = []
+        for test in plugin.functional_tests:
+            tests_html.append(self.test_section(id, plugin, test))
+        return html.substitute(id=id, tests=''.join(tests_html))
+
+    def test_section(self, plugin_id, plugin, test):
+        html = string.Template(test_html)
+        url = self.html_url(plugin_id, test)
+        return html.substitute(name=test.name, url=url)
+
+
+def create_test_server(port=8000):
+    return HTTPServer(('', port), PluginTestsuiteRequestHandler)
+
+
+class Section(object):
+    def __init__(self, name, tests):
+        self.name = name
+        self.tests = tests
+
+class Test(object):
+    def __init__(self, name, html, css, kss, handler):
+        self.name = name
+        self.html = html
+        self.css = css
+        self.kss = kss
+        self.handler = handler
+
+
+if __name__ == '__main__':
+    from kss.base import load_plugins
+    load_plugins('kss-core')
+    create_test_server().serve_forever()


More information about the Kukit-checkins mailing list