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")
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")
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")
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")
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")
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(), "<br>")
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
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
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
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")