def test_write(self): with StringIO() as stream: text1 = "a" text2 = "b" text3 = "c" node = SentenceNode() node.add(TextNode(text1)) node.add(TextNode(text2)) node.add(TextNode(text3)) node.write(stream) self.assertEqual(stream.getvalue(), text1 + text2 + text3)
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_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")
def read_text(preread, stream, parser): with StringIO() as readstr: readstr.write(preread) while True: character = stream.peek() if not character: break if parser.is_syntax_character(character): break readstr.write(character) stream.get() return TextNode(readstr.getvalue())
def read_at(preread, stream, parser): return TextNode("@")
def read_in(preread, stream, parser): if stream.read(2) != "in": raise SyntaxError("could not read \"in\".") return TextNode("")
def read_close_paren(preread, stream, parser): read_whitespace("", stream, parser) if stream.read(3) != "-->": raise SyntaxError("could not read \"-->\".") return TextNode("")
def test_write(self): content = "this is text nodes content." node = TextNode(content) with StringIO() as stream: node.write(stream) self.assertEqual(stream.getvalue(), content) # test