def test_newline(self): zbr = zebra.trim( """ hello, world! this test is good \\ if there is no break here i want a newline after this<tag/> the end """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> hello, world!<nl/> <nl/> this test is good if there is no break here<nl/> i want a newline after this<tag/><nl/> the end<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "zbr2xml doesn't handle newlines correctly:\n%s" % actual
def test_notBlocks(self): zbr = zebra.trim( """ * if x==1: * include page_one; * ef x==2: * include page_two; """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <if condition="x==1"> <include file="page_one"/> </if> <ef condition="x==2"> <include file="page_two"/> </ef> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't handle ; blocks right:\n%s" % actual
def test_indent(self): ## the blank line before the * checks for a ## whitespace error! zbr = zebra.trim( """ This is normal text. * if 1==2: This should never show up. * el: This line isn't part of the else. """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> This is normal text.<nl/> <if condition="1==2"> This should never show up.<nl/> </if> <el> </el> This line isn't part of the else.<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "doesn't indent correctly:\n%s" % actual
def test_exec(self): zbr = zebra.trim( """ * exec: name = 'fred' xml = '<xml>woohoo!</xml>' dict = {} dict['a'] = 'b' hope(there_was_no_nl_tag_there) """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <exec> name = 'fred' xml = '<xml>woohoo!</xml>' dict = {} dict['a'] = 'b' hope(there_was_no_nl_tag_there) </exec> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with exec:\n%s" % actual
def handle_for(self, model, attrs): loopvar = self.gensym("loopvar") self.loopvars.append(loopvar) data = {"loopvar":loopvar} data.update(attrs) res = zebra.trim( ''' _%(loopvar)s_max_ = len(scope["%(series)s"]) for %(loopvar)s in range(_%(loopvar)s_max_): # handle scope inside the loop in case we have # recursive names (eg, children->children->children) scope_stack.append(copy.copy(scope)) # can't do .update if it's a UserDict: mdl = scope["%(series)s"][%(loopvar)s] for item in mdl.keys(): scope[item]=mdl[item] ''' % data) res = res + zebra.indent(self.walk(model), 1) res = res + zebra.trim( ''' # ## close for-%(series)s loop ########## globals().update(scope_stack.pop()) ''' % attrs) self.lastLoopvar = self.loopvars.pop() return res
def test_exec(self): zbr = zebra.trim(""" * exec: name = 'fred' xml = '<xml>woohoo!</xml>' dict = {} dict['a'] = 'b' hope(there_was_no_nl_tag_there) """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <exec> name = 'fred' xml = '<xml>woohoo!</xml>' dict = {} dict['a'] = 'b' hope(there_was_no_nl_tag_there) </exec> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with exec:\n%s" % actual
def test_indent(self): ## the blank line before the * checks for a ## whitespace error! zbr = zebra.trim(""" This is normal text. * if 1==2: This should never show up. * el: This line isn't part of the else. """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> This is normal text.<nl/> <if condition="1==2"> This should never show up.<nl/> </if> <el> </el> This line isn't part of the else.<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "doesn't indent correctly:\n%s" % actual
def test_forHeadBodyFoot(self): zbr = zebra.trim(""" * for people: * head: PEOPLE ------- * body: {?name?} is a nice person. * foot: ------- THE END """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="people"> <head> PEOPLE<nl/> -------<nl/> </head> <body> <var>name</var> is a nice person.<nl/> </body> <foot> -------<nl/> THE END<nl/> </foot> </for> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't do for/head/body/foot right:\n%s" % actual
def test_forNone(self): zbr = zebra.trim( """ * for people: {?name?} is a nice person. * none: No people here! """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="people"> <var>name</var> is a nice person.<nl/> </for> <none> No people here!<nl/> </none> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't do for..none right:\n%s" % actual
def test_xmlchars(self): zbr = zebra.trim(""" <P>Hello World!!! >< """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <P>Hello&nbsp;World!!! ><<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope with xmlchars correctly:\n%s" % actual
def test_expr(self): zbr = zebra.trim(""" I will be {:age + 4:} next year. """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> I will be <xpr>age + 4</xpr> next year.<nl/> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with {:exprs:}:\n%s" % actual
def check_for(self): model = { "a": "alaska", "stuff": [ { "a": "apple", "b": "banana", "c": "cherry" }, { "a": "aardvark", "b": "bull weevil", "c": "catepillar" }, { "a": "alice", "b": "betty", "c": "carol" }, ], } zbx = zebra.trim(""" <?xml version="1.0"?> <zebra> <rem>test scope</rem> <xpr>a</xpr><nl/> <for series="stuff"> <xpr>a</xpr>, <xpr>b</xpr>, <xpr>c</xpr> <nl/> </for> <xpr>a</xpr><nl/> </zebra> """) goal = zebra.trim(""" alaska apple, banana, cherry aardvark, bull weevil, catepillar alice, betty, carol alaska """) actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, \ "for() loops don't work:\n---\n%s---" % actual
def handle_zebra(self, model, attrs): res = zebra.trim( """ class Report: def show(self, model={}): print self.fetch(model) def fetch(self, model={}): import copy # used for pushing scope onto stack scope = globals() # This scope thing is so that we can generate # code that says: # # zres = zres + x # *OR* # zres = zres + scope.get(x, '') # # It also actually does variable scoping, # when combined with scope_stack, below. # # I wanted to use scope=locals(), but # then the 'zres + x' wouldn't work. # @TODO: is this scope scheme threadsafe? scope_stack = [] # scope.update(model), but model might be a UserDict: for item in model.keys(): scope[item] = model[item] # zres is the result (the output we're building) zres = "" """) res = res + zebra.indent(self.walk(model), 2) res = res + zebra.trim( ''' # end of Report.fetch() return zres def fetch(model={}): return Report().fetch(model) def show(model={}): return Report().show(model) ''') return res
def check_none(self): model = {"emptylist": [], "fulllist": [{"a": "b"}]} zbx = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="emptylist"> THIS SHOULD NOT SHOW UP </for> <none> Nothin's empty. </none> <for series="fulllist"> Somethin's full. </for> <none> THIS SHOULD NOT SHOW UP </none> </zebra> """) goal = "Nothin's empty. Somethin's full." actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, \ "none doesn't work:\n%s" % actual
def check_nested_for(self): model = { "all": [{ "subs": [{ "value": "a" }] }, { "subs": [{ "value": "b" }] }] } zbx = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="all"> <head>[</head> <for series="subs"> <head>{</head> <var>value</var> <foot>}</foot> </for> <foot>]</foot> </for> </zebra> """) goal = "[{a}{b}]" actual = zebra.Bootstrap().toObject(zbx).fetch(model) self.assertEquals(actual, goal)
def check_none(self): model = {"emptylist": [], "fulllist": [{"a": "b"}]} zbx = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="emptylist"> THIS SHOULD NOT SHOW UP </for> <none> Nothin's empty. </none> <for series="fulllist"> Somethin's full. </for> <none> THIS SHOULD NOT SHOW UP </none> </zebra> """ ) goal = "Nothin's empty. Somethin's full." actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, "none doesn't work:\n%s" % actual
def test_xmlchars(self): zbr = zebra.trim( """ <P>Hello World!!! >< """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <P>Hello&nbsp;World!!! ><<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope with xmlchars correctly:\n%s" % actual
def test_expr(self): zbr = zebra.trim( """ I will be {:age + 4:} next year. """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> I will be <xpr>age + 4</xpr> next year.<nl/> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with {:exprs:}:\n%s" % actual
def check_headFootSimple(self): # check the simple case, not the grouped version. model = {"list": [{"toy": "ball"}, {"toy": "yoyo"}]} zbx = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="list"> <head>Some toys: [</head> <var>toy</var> <glue>, </glue> <foot>]</foot> </for> </zebra> """ ) goal = "Some toys: [ball, yoyo]" ## print '--------' ## print zebra.Bootstrap().compile(zbx) ## print '--------' actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, "head/tails don't work:\n%s" % actual
def test_include(self): #@TODO: drop trailing : from include syntax zbr = zebra.trim(""" * include includefile: """) #@TODO: it should realy be <include file="includefile"/> goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <include file="includefile"> </include> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't handle includes right:\n%s" % actual
def test_comment(self): zbr = zebra.trim(""" *# this is a comment this isn't * # this is """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <rem>this is a comment</rem> this isn't<nl/> <rem>this is</rem> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't handle comments right:\n%s" % actual
def handle_zebra(self, model, attrs): res = zebra.trim(""" class Report: def show(self, model={}): print self.fetch(model) def fetch(self, model={}): import copy # used for pushing scope onto stack scope = globals() # This scope thing is so that we can generate # code that says: # # zres = zres + x # *OR* # zres = zres + scope.get(x, '') # # It also actually does variable scoping, # when combined with scope_stack, below. # # I wanted to use scope=locals(), but # then the 'zres + x' wouldn't work. # @TODO: is this scope scheme threadsafe? scope_stack = [] # scope.update(model), but model might be a UserDict: for item in model.keys(): scope[item] = model[item] # zres is the result (the output we're building) zres = "" """) res = res + zebra.indent(self.walk(model), 2) res = res + zebra.trim(''' # end of Report.fetch() return zres def fetch(model={}): return Report().fetch(model) def show(model={}): return Report().show(model) ''') return res
def test_var(self): zbr = zebra.trim(""" { My name is {?name?}. } """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> {<nl/> My name is <var>name</var>.<nl/> }<nl/> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with {?vars?}:\n%s" % actual
def test_comment(self): zbr = zebra.trim( """ *# this is a comment this isn't * # this is """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <rem>this is a comment</rem> this isn't<nl/> <rem>this is</rem> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't handle comments right:\n%s" % actual
def test_include(self): #@TODO: drop trailing : from include syntax zbr = zebra.trim( """ * include includefile: """) #@TODO: it should realy be <include file="includefile"/> goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <include file="includefile"> </include> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't handle includes right:\n%s" % actual
def check_whitespace(self): zbx = zebra.trim(""" <zebra> <xpr>5</xpr> <xpr>2</xpr><nl/> </zebra> """) goal = "5 2\n" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, \ "whitespace is screwed up:\n%s" % actual
def check_whitespace(self): zbx = zebra.trim( """ <zebra> <xpr>5</xpr> <xpr>2</xpr><nl/> </zebra> """ ) goal = "5 2\n" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, "whitespace is screwed up:\n%s" % actual
def test_var(self): zbr = zebra.trim( """ { My name is {?name?}. } """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> {<nl/> My name is <var>name</var>.<nl/> }<nl/> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "doesn't cope well with {?vars?}:\n%s" % actual
def setUp(self): self.data = zebra.trim( """ <?xml version="1.0"?> <top> <person name="Michal"> <skill>Python</skill> <skill>ASP</skill> </person> </top> """)
def check_include(self): zbx = zebra.trim(""" <zebra> <include file="test/includefile"> </include> </zebra> """) goal = "This is the include file!\n" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, \ "includes don't work:\n%s" % actual
def check_include(self): zbx = zebra.trim( """ <zebra> <include file="test/includefile"> </include> </zebra> """ ) goal = "This is the include file!\n" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, "includes don't work:\n%s" % actual
def check_for(self): model = { "a": "alaska", "stuff": [ {"a": "apple", "b": "banana", "c": "cherry"}, {"a": "aardvark", "b": "bull weevil", "c": "catepillar"}, {"a": "alice", "b": "betty", "c": "carol"}, ], } zbx = zebra.trim( """ <?xml version="1.0"?> <zebra> <rem>test scope</rem> <xpr>a</xpr><nl/> <for series="stuff"> <xpr>a</xpr>, <xpr>b</xpr>, <xpr>c</xpr> <nl/> </for> <xpr>a</xpr><nl/> </zebra> """ ) goal = zebra.trim( """ alaska apple, banana, cherry aardvark, bull weevil, catepillar alice, betty, carol alaska """ ) actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, "for() loops don't work:\n---\n%s---" % actual
def test_notBlocks(self): zbr = zebra.trim(""" * if x==1: * include page_one; * ef x==2: * include page_two; """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <if condition="x==1"> <include file="page_one"/> </if> <ef condition="x==2"> <include file="page_two"/> </ef> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't handle ; blocks right:\n%s" % actual
def test_forNone(self): zbr = zebra.trim(""" * for people: {?name?} is a nice person. * none: No people here! """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="people"> <var>name</var> is a nice person.<nl/> </for> <none> No people here!<nl/> </none> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't do for..none right:\n%s" % actual
def test_invalid(self): "check invalid tags" zbr = zebra.trim(""" * thisIsAnInvalidTag """) try: zebra.Z2X().translate(zbr) except: gotError = 1 else: gotError = 0 assert gotError, \ "Didn't get error on invalid tag."
def test_forHeadBodyFoot(self): zbr = zebra.trim( """ * for people: * head: PEOPLE ------- * body: {?name?} is a nice person. * foot: ------- THE END """) goal = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="people"> <head> PEOPLE<nl/> -------<nl/> </head> <body> <var>name</var> is a nice person.<nl/> </body> <foot> -------<nl/> THE END<nl/> </foot> </for> </zebra> """) actual = zebra.Z2X().translate(zbr) assert actual==goal, \ "Doesn't do for/head/body/foot right:\n%s" % actual
def test_newline(self): zbr = zebra.trim(""" hello, world! this test is good \\ if there is no break here i want a newline after this<tag/> the end """) goal = zebra.trim(""" <?xml version="1.0"?> <zebra> hello, world!<nl/> <nl/> this test is good if there is no break here<nl/> i want a newline after this<tag/><nl/> the end<nl/> </zebra> """) actual = zebra.zbr2xml.Z2X().translate(zbr) assert actual==goal, \ "zbr2xml doesn't handle newlines correctly:\n%s" % actual
def handle_for(self, model, attrs): loopvar = self.gensym("loopvar") self.loopvars.append(loopvar) data = {"loopvar": loopvar} data.update(attrs) res = zebra.trim(''' _%(loopvar)s_max_ = len(scope["%(series)s"]) for %(loopvar)s in range(_%(loopvar)s_max_): # handle scope inside the loop in case we have # recursive names (eg, children->children->children) scope_stack.append(copy.copy(scope)) # can't do .update if it's a UserDict: mdl = scope["%(series)s"][%(loopvar)s] for item in mdl.keys(): scope[item]=mdl[item] ''' % data) res = res + zebra.indent(self.walk(model), 1) res = res + zebra.trim(''' # ## close for-%(series)s loop ########## globals().update(scope_stack.pop()) ''' % attrs) self.lastLoopvar = self.loopvars.pop() return res
def test_invalid(self): "check invalid tags" zbr = zebra.trim( """ * thisIsAnInvalidTag """) try: zebra.Z2X().translate(zbr) except: gotError = 1 else: gotError = 0 assert gotError, \ "Didn't get error on invalid tag."
def setUp(self): file = open("test/junk.zb", "w") print >> file, "* for each:" print >> file, " {:a:}" file.close() file = open("test/xmljunk.zbx", "w") print >> file, zebra.trim(''' <?xml version="1.0"?> <zebra> <for series="each"> <xpr>a</xpr><nl/> </for> </zebra> ''') file.close()
def setUp(self): file = open("test/junk.zb","w") print >> file, "* for each:" print >> file, " {:a:}" file.close() file = open("test/xmljunk.zbx", "w") print >> file, zebra.trim( ''' <?xml version="1.0"?> <zebra> <for series="each"> <xpr>a</xpr><nl/> </for> </zebra> ''') file.close()
def check_exec(self): # note: the <>'s mean something! zbx = zebra.trim(""" <zebra> <exec> ex = '<executive' ex = ex + ' decision>' </exec> <xpr>ex</xpr> </zebra> """) goal = "<executive decision>" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, \ "expressions don't work:\n%s" % actual
def check_exec(self): # note: the <>'s mean something! zbx = zebra.trim( """ <zebra> <exec> ex = '<executive' ex = ex + ' decision>' </exec> <xpr>ex</xpr> </zebra> """ ) goal = "<executive decision>" actual = zebra.Bootstrap().toObject(zbx).fetch() assert actual == goal, "expressions don't work:\n%s" % actual
def check_basics(self): zbx = zebra.trim( """ <zebra> <rem> ignore me! </rem> hello, world! </zebra> """ ) rpt = zebra.Bootstrap().toObject(zbx) for item in ("fetch", "show"): assert hasattr(rpt, item), "Report objects don't have .%s()!" % item actual = rpt.fetch() assert actual == "hello, world!", "basic 'hello, world' doesn't work:\n%s" % actual assert actual == rpt.fetch(), "calling fetch() a second time yields different results. :/"
def check_conditionals(self): model = {"names": [{"name": "a"}, {"name": "b"}, {"name": "c"}]} zbx = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="names"> <if condition="name=='a'">Argentina</if> <ef condition="name=='b'">Bolivia</ef> <el>Chile</el> <glue>, </glue> </for> </zebra> """) goal = "Argentina, Bolivia, Chile" actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual== goal, \ "if/el/ef don't work:\n---%s---" % actual
def check_basics(self): zbx = zebra.trim(""" <zebra> <rem> ignore me! </rem> hello, world! </zebra> """) rpt = zebra.Bootstrap().toObject(zbx) for item in ("fetch", "show"): assert hasattr(rpt, item), \ "Report objects don't have .%s()!" % item actual = rpt.fetch() assert actual == "hello, world!", \ "basic 'hello, world' doesn't work:\n%s" % actual assert actual == rpt.fetch(), \ "calling fetch() a second time yields different results. :/"
def check_conditionals(self): model = {"names": [{"name": "a"}, {"name": "b"}, {"name": "c"}]} zbx = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="names"> <if condition="name=='a'">Argentina</if> <ef condition="name=='b'">Bolivia</ef> <el>Chile</el> <glue>, </glue> </for> </zebra> """ ) goal = "Argentina, Bolivia, Chile" actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, "if/el/ef don't work:\n---%s---" % actual
def check_nested_for(self): model = {"all": [{"subs": [{"value": "a"}]}, {"subs": [{"value": "b"}]}]} zbx = zebra.trim( """ <?xml version="1.0"?> <zebra> <for series="all"> <head>[</head> <for series="subs"> <head>{</head> <var>value</var> <foot>}</foot> </for> <foot>]</foot> </for> </zebra> """ ) goal = "[{a}{b}]" actual = zebra.Bootstrap().toObject(zbx).fetch(model) self.assertEquals(actual, goal)
def check_headFootSimple(self): # check the simple case, not the grouped version. model = { "list": [ { "toy": "ball" }, { "toy": "yoyo" }, ] } zbx = zebra.trim(""" <?xml version="1.0"?> <zebra> <for series="list"> <head>Some toys: [</head> <var>toy</var> <glue>, </glue> <foot>]</foot> </for> </zebra> """) goal = "Some toys: [ball, yoyo]" ## print '--------' ## print zebra.Bootstrap().compile(zbx) ## print '--------' actual = zebra.Bootstrap().toObject(zbx).fetch(model) assert actual == goal, \ "head/tails don't work:\n%s" % actual