[KSS-checkins] r44898 - kukit/kss.commands/trunk/kss/commands
jvloothuis at codespeak.net
jvloothuis at codespeak.net
Tue Jul 10 15:38:02 CEST 2007
Author: jvloothuis
Date: Tue Jul 10 15:38:00 2007
New Revision: 44898
Modified:
kukit/kss.commands/trunk/kss/commands/README.txt
kukit/kss.commands/trunk/kss/commands/commands.py
kukit/kss.commands/trunk/kss/commands/selectors.py
kukit/kss.commands/trunk/kss/commands/selectors.txt
Log:
Added easy (doct)testing support so you can print the commands and get nice string representation
Modified: kukit/kss.commands/trunk/kss/commands/README.txt
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/README.txt (original)
+++ kukit/kss.commands/trunk/kss/commands/README.txt Tue Jul 10 15:38:00 2007
@@ -57,6 +57,32 @@
>>> commands.render()
'...some value...some arg...'
+It is also possible to get a string representation of the
+commandset. This can be used in doctests like this.
+
+ >>> commands = KSSCommands()
+
+By default it does nothing.
+
+ >>> print commands
+ <BLANKLINE>
+
+It becomes more interesting if we add a command.
+
+ >>> commands.add('replaceHTML', CSS('#someid'))
+ >>> print commands
+ replaceHTML(css('#someid'))
+
+If you add parameters they are also shown in the string
+representation. Parameters are sorted before display to make them
+stable for testing.
+
+ >>> commands = KSSCommands()
+ >>> commands.add('replaceHTML', CSS('#someid'), b_arg='some data', a_arg='cheese')
+ >>> print commands
+ replaceHTML(css('#someid'), a_arg='cheese', b_arg='some data')
+
+
Command sets
============
Modified: kukit/kss.commands/trunk/kss/commands/commands.py
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/commands.py (original)
+++ kukit/kss.commands/trunk/kss/commands/commands.py Tue Jul 10 15:38:00 2007
@@ -55,6 +55,21 @@
def get(self, name):
return command_set_registry.get(name)(self)
+ def __str__(self):
+ def format_options(options):
+ if not options:
+ return ''
+ return ', ' + ', '.join(
+ ["%s='%s'" % item for item in options.items()])
+
+ lines = []
+ for action, selector, options in self.commands:
+ lines.append("%(action)s(%(selector)s%(options)s)" % {
+ 'action': action,
+ 'selector': selector,
+ 'options': format_options(options)})
+ return '\n'.join(lines)
+
class KSSCommandSet(object):
def __init__(self, commands):
self.commands = commands
Modified: kukit/kss.commands/trunk/kss/commands/selectors.py
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/selectors.py (original)
+++ kukit/kss.commands/trunk/kss/commands/selectors.py Tue Jul 10 15:38:00 2007
@@ -3,6 +3,9 @@
self.type = type
self.value = value
+ def __str__(self):
+ return "%s('%s')" % (self.type, self.value)
+
class CSS(Selector):
def __init__(self, value):
super(CSS, self).__init__('css', value)
Modified: kukit/kss.commands/trunk/kss/commands/selectors.txt
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/selectors.txt (original)
+++ kukit/kss.commands/trunk/kss/commands/selectors.txt Tue Jul 10 15:38:00 2007
@@ -21,8 +21,8 @@
Standard selectors
------------------
-In the core package you can find a few standard selectors. The most basic
-selectors are the CSS and HTML id selectors.
+In the core package you can find a few standard selectors. The most
+basic selectors are the CSS and HTML id selectors.
>>> from kss.commands.selectors import CSS, HTMLId
@@ -40,9 +40,9 @@
>>> selector.value
'someid'
-There are also two somewhat different selectors. The first is special in that
-it selects the same node that was used to call the server action. Also note
-that it does not accept any arguments.
+There are also two somewhat different selectors. The first is special
+in that it selects the same node that was used to call the server
+action. Also note that it does not accept any arguments.
>>> from kss.commands.selectors import SameNode
>>> selector = SameNode()
@@ -51,8 +51,9 @@
>>> selector.value
''
-Another core selector is parentnode. This one can be used to use a CSS query
-which only operates on nodes that are the parent (or grandparents).
+Another core selector is parentnode. This one can be used to use a CSS
+query which only operates on nodes that are the parent (or
+grandparents).
>>> from kss.commands.selectors import ParentNode
>>> selector = ParentNode('a')
@@ -60,3 +61,14 @@
'parentnode'
>>> selector.value
'a'
+
+String representation
+---------------------
+
+For testing purposes we have a string representation of the selectors.
+
+ >>> print Selector('type', 'value')
+ type('value')
+
+ >>> print CSS('div.content')
+ css('div.content')
More information about the Kukit-checkins
mailing list