Exemplo n.º 1
0
 def test_get_value(self):
     scope = Scope()
     variable = VariableNode("person", scope)
     member = MemberNode("name", variable)
     # when undefined
     self.assertEqual(member.get_value(defaultvalue=None), None)
     # when did set
     variable.set_value({"name": "tikubonn"})
     self.assertEqual(member.get_value(defaultvalue=None), "tikubonn")
Exemplo n.º 2
0
 def test_write(self):
     with StringIO() as stream:
         separator = TextNode(",")
         scope = Scope()
         variable = VariableNode("a", scope)
         variable.set_value(["a", "b", "c"])
         node = JoinNode(separator, variable)
         node.write(stream)
         self.assertEqual(stream.getvalue(), "a,b,c")
Exemplo n.º 3
0
 def test_render(self):
     with StringIO() as stream:
         scope = Scope()
         sentence = SentenceNode()
         sentence.add(VariableNode("a", scope))
         sentence.add(VariableNode("b", scope))
         sentence.add(VariableNode("c", scope))
         template = Template(sentence, scope)
         template.render(stream, a="a", b="b", c="c")
         self.assertEqual(stream.getvalue(), "abc")
Exemplo n.º 4
0
 def test_write(self):
     with StringIO() as stream:
         scope = Scope()
         source = VariableNode("a", scope)
         source.set_value(["a", "b", "c"])
         variable = VariableNode("a", scope)
         sentence = SentenceNode()
         sentence.add(variable)
         fornode = ForNode(source, variable, sentence, scope)
         fornode.write(stream)
         self.assertEqual(stream.getvalue(), "abc")
Exemplo n.º 5
0
 def test_write(self):
     scope = Scope()
     variable = VariableNode("person", scope)
     member = MemberNode("name", variable)
     # when undefined
     with StringIO() as stream:
         member.write(stream)
         self.assertEqual(stream.getvalue(), "")
     # when did set
     variable.set_value({"name": "tikubonn"})
     with StringIO() as stream:
         member.write(stream)
         self.assertEqual(stream.getvalue(), "tikubonn")
Exemplo n.º 6
0
 def test_write(self):
     scope = Scope()
     variable = VariableNode("a", scope)
     node = SanitizeNode(variable)
     # when undefined
     with StringIO() as stream:
         node.write(stream)
         self.assertEqual(stream.getvalue(), "")
     # when set value
     variable.set_value("<br>")
     with StringIO() as stream:
         node.write(stream)
         self.assertEqual(stream.getvalue(), "&lt;br&gt;")
Exemplo n.º 7
0
def read_variable(preread, stream, parser):
    text = read_variable_text(stream)
    node = None
    for part in text.split("."):
        if node is None:
            node = VariableNode(part, scope=parser.get_scope())
        else:
            node = MemberNode(part, node)
    return node
Exemplo n.º 8
0
 def test_access_value(self):
     value = "this is variable nodes value."
     scope = Scope()
     node = VariableNode("a", scope)
     self.assertEqual(node.get_value(defaultvalue=None), None)  # test
     node.set_value(value)
     self.assertEqual(node.get_value(defaultvalue=None), value)  # test
Exemplo n.º 9
0
 def test_write(self):
     value = "this is variable nodes value."
     scope = Scope()
     node = VariableNode("a", scope)
     node.set_value(value)
     with StringIO() as stream:
         node.write(stream)
         self.assertEqual(stream.getvalue(), value)  # test
Exemplo n.º 10
0
 def test_write(self):
     scope = Scope()
     variable = VariableNode("a", scope)
     thennode = TextNode("true")
     elsenode = TextNode("false")
     ifnode = IfNode(variable, thennode, elsenode)
     # when undefined
     with StringIO() as stream:
         ifnode.write(stream)
         self.assertEqual(stream.getvalue(), "false")
     # when true
     variable.set_value(True)
     with StringIO() as stream:
         ifnode.write(stream)
         self.assertEqual(stream.getvalue(), "true")
     # when false
     variable.set_value(False)
     with StringIO() as stream:
         ifnode.write(stream)
         self.assertEqual(stream.getvalue(), "false")