def testParameterParser(self): tests = [ # trivial ('', '', 0, {}), # fixed ('%s%i%f%b', '"test",42,23.0,True', 4, {0: 'test', 1: 42, 2: 23.0, 3: True}), # fixed and named ('%s%(x)i%(y)i', '"test"', 1, {0: 'test', 'x': None, 'y': None}), ('%s%(x)i%(y)i', '"test",1', 1, {0: 'test', 'x': 1, 'y': None}), ('%s%(x)i%(y)i', '"test",1,2', 1, {0: 'test', 'x': 1, 'y': 2}), ('%s%(x)i%(y)i', '"test",x=1', 1, {0: 'test', 'x': 1, 'y': None}), ('%s%(x)i%(y)i', '"test",x=1,y=2', 1, {0: 'test', 'x': 1, 'y': 2}), ('%s%(x)i%(y)i', '"test",y=2', 1, {0: 'test', 'x': None, 'y': 2}), # test mixed acceptance ("%ifs", '100', 1, {0: 100}), ("%ifs", '100.0', 1, {0: 100.0}), ("%ifs", '"100"', 1, {0: "100"}), # boolean ("%(t)b%(f)b", '', 0, {'t': None, 'f': None}), ("%(t)b%(f)b", 't=1', 0, {'t': True, 'f': None}), ("%(t)b%(f)b", 'f=False', 0, {'t': None, 'f': False}), ("%(t)b%(f)b", 't=True, f=0', 0, {'t': True, 'f': False}), # integer ("%(width)i%(height)i", '', 0, {'width': None, 'height': None}), ("%(width)i%(height)i", 'width=100', 0, {'width': 100, 'height': None}), ("%(width)i%(height)i", 'height=200', 0, {'width': None, 'height': 200}), ("%(width)i%(height)i", 'width=100, height=200', 0, {'width': 100, 'height': 200}), # float ("%(width)f%(height)f", '', 0, {'width': None, 'height': None}), ("%(width)f%(height)f", 'width=100.0', 0, {'width': 100.0, 'height': None}), ("%(width)f%(height)f", 'height=2.0E2', 0, {'width': None, 'height': 200.0}), ("%(width)f%(height)f", 'width=1000.0E-1, height=200.0', 0, {'width': 100.0, 'height': 200.0}), # string ("%(width)s%(height)s", '', 0, {'width': None, 'height': None}), ("%(width)s%(height)s", 'width="really wide"', 0, {'width': 'really wide', 'height': None}), ("%(width)s%(height)s", 'height="not too high"', 0, {'width': None, 'height': 'not too high'}), ("%(width)s%(height)s", 'width="really wide", height="not too high"', 0, {'width': 'really wide', 'height': 'not too high'}), # conversion from given type to expected type ("%(width)s%(height)s", 'width=100', 0, {'width': '100', 'height': None}), ("%(width)s%(height)s", 'width=100, height=200', 0, {'width': '100', 'height': '200'}), # complex test ("%i%sf%s%ifs%(a)s|%(b)s", ' 4,"DI\'NG", b=retry, a="DING"', 2, {0: 4, 1: "DI'NG", 'a': 'DING', 'b': 'retry'}), ] for format, args, expected_fixed_count, expected_dict in tests: argParser = wikiutil.ParameterParser(format) fixed_count, arg_dict = argParser.parse_parameters(args) assert (fixed_count, arg_dict) == (expected_fixed_count, expected_dict)
def testWrongTypeNamedArgument(self): args = 'flag=0.0' argParser = wikiutil.ParameterParser("%(flag)b") py.test.raises(ValueError, argParser.parse_parameters, args)
def testWrongTypeFixedPosArgument(self): args = '0.0' argParser = wikiutil.ParameterParser("%b") py.test.raises(ValueError, argParser.parse_parameters, args)
def testMalformedArguments(self): args = '=' argParser = wikiutil.ParameterParser("%(width)s%(height)s") py.test.raises(ValueError, argParser.parse_parameters, args)
def testTooMuchWantedArguments(self): args = 'width=100, height=200, alt=Example' argParser = wikiutil.ParameterParser("%(width)s%(height)s") py.test.raises(ValueError, argParser.parse_parameters, args)
def execute(macro, args): import re from MoinMoin import wikiutil def escape(x): if x is None: return x else: return wikiutil.escape(x) parser = wikiutil.ParameterParser( '%(section)s%(toshow)s%(tohide)s%(show)b%(bg)s%(inline)s%(image)s%(speed)s%(seesaw)b%(addclass)s%(type)s' ) (count, dict) = parser.parse_parameters(args) (section, toshow, tohide, show, bg, inline, image, speed, seesaw, addclass, type) = (dict[x] for x in ('section', 'toshow', 'tohide', 'show', 'bg', 'inline', 'image', 'speed', 'seesaw', 'addclass', 'type')) if section is None: section = 'section' if speed is None: speed = 0 try: speed = int(speed) except: speed = "'%s'" % speed if seesaw is None or seesaw: seesaw = 'true' else: seesaw = 'false' if show is None: show = False if tohide is None and toshow is None: if inline is None: toshow = 'Show' tohide = 'Hide' else: toshow = u'»»' tohide = u'««' elif tohide is None and toshow is not None: tohide = toshow showpart = 'showpart' showstyle = '''style="display:inline"''' hidepart = 'hidepart' hidestyle = '''style="display:none"''' if show: showstyle, hidestyle = hidestyle, showstyle if addclass is None: addclass = "" if type is None or type == 'link': openaction = 'a href=""' closeaction = 'a' else: openaction = closeaction = 'button' regex = re.compile(r'(.*)<<(.*)>>(.*)') preshw = postshw = prehd = posthd = "" matches = regex.match(toshow) if matches is not None: preshw, toshow, postshw = matches.groups() matches = regex.match(tohide) if matches is not None: prehd, tohide, posthd = matches.groups() showimage = hideimage = "" img = False if image is not None: imagepair = imageset.get(image) if imagepair is not None: (showimage, hideimage) = (img_prefix.rstrip("/") + "/" + x for x in imagepair) img = True if img: toshow = '''<img src="%s" style="vertical-align:middle"/>''' % showimage tohide = '''<img src="%s" style="vertical-align:middle"/>''' % hideimage sections = section.strip().replace(' ', '/') sections = sections.split('/') defining = [ x.encode("iso-8859-1") for x in sections if not re.compile(r'^[%+-].*').match(x) ] toggling = [ x.lstrip('%').encode("iso-8859-1") for x in sections if x.startswith('%') ] showing = [ x.lstrip('+').encode("iso-8859-1") for x in sections if x.startswith('+') ] hiding = [ x.lstrip('-').encode("iso-8859-1") for x in sections if x.startswith('-') ] sectionarg = '''{toggle:%s, show:%s, hide:%s}''' % (defining + toggling, showing, hiding) section = ' '.join(defining) section, preshw, postshw, prehd, posthd, bg, inline = (escape(x) for x in (section, preshw, postshw, prehd, posthd, bg, inline)) if not img: toshow = escape(toshow) tohide = escape(tohide) inlinesection = divstyle = "" if inline is None: if bg is not None: divstyle = '''<input class="seesaw" type="hidden" value="%(section)s-bg:%(bg)s">''' % locals( ) else: inlinestyle = hidestyle if section != "comment" and bg is not None: inlinestyle = inlinestyle[: -1] + ''';background-color:%(bg)s"''' % locals( ) inlinesection = '''<span class="seesaw %(section)s %(addclass)s %(hidepart)s" %(inlinestyle)s>%(inline)s</span>''' % locals( ) prepost = '''<span class="seesaw %s %s %s" %s>%s</span>''' % ( section, addclass, '%s', '%s', '%s') preshow = prehide = postshow = posthide = '' if preshw != '': preshow = prepost % (showpart, showstyle, preshw) if prehd != '': prehide = prepost % (hidepart, hidestyle, prehd) if postshw != '': postshow = prepost % (showpart, showstyle, postshw) if posthd != '': posthide = prepost % (hidepart, hidestyle, posthd) html = ''' %(preshow)s %(prehide)s <%(openaction)s onClick="seeSaw(%(sectionarg)s,%(speed)s,%(seesaw)s);return false;"> <span class="seesaw %(section)s %(addclass)s %(showpart)s" %(showstyle)s>%(toshow)s</span> <span class="seesaw %(section)s %(addclass)s %(hidepart)s" %(hidestyle)s>%(tohide)s</span> </%(closeaction)s> %(postshow)s %(posthide)s %(inlinesection)s %(divstyle)s ''' % locals() return macro.formatter.rawHTML(html)