def test_add_comment_text(self): sgf_game = gosgf.Sgf_game(9) root = sgf_game.get_root() root.add_comment_text(b"hello\nworld") self.assertEqual(root.get(b'C'), b"hello\nworld") root.add_comment_text(b"hello\naga]in") self.assertEqual(root.get(b'C'), b"hello\nworld\n\nhello\naga]in")
def test_extend_main_sequence(self): g1 = gosgf.Sgf_game(9) for i in range(6): g1.extend_main_sequence().set(b"N", ("e%d" % i).encode('ascii')) self.assertEqual( g1.serialise(), b"(;FF[4]CA[UTF-8]GM[1]SZ[9];N[e0];N[e1];N[e2];N[e3];N[e4];N[e5])\n") g2 = gosgf.Sgf_game.from_string(b"(;SZ[9](;N[n1];N[n3])(;N[n2]))") for i in range(6): g2.extend_main_sequence().set(b"N", ("e%d" % i).encode('ascii')) self.assertEqual( g2.serialise(), b"(;SZ[9](;N[n1];N[n3];N[e0];N[e1];N[e2];N[e3];N[e4];N[e5])(;N[n2]))\n")
def test_encoding(self): g1 = gosgf.Sgf_game(19) self.assertEqual(g1.get_charset(), "UTF-8") root = g1.get_root() self.assertEqual(root.get_encoding(), "UTF-8") root.set(b"C", u"£".encode('utf-8')) self.assertEqual(root.get(b"C"), u"£".encode('utf-8')) self.assertEqual(root.get_raw(b"C"), u"£".encode('utf-8')) self.assertEqual(g1.serialise(), dedent(u"""\ (;FF[4]C[£]CA[UTF-8]GM[1]SZ[19]) """).encode('utf-8')) g2 = gosgf.Sgf_game(19, encoding="iso-8859-1") self.assertEqual(g2.get_charset(), "ISO-8859-1") root = g2.get_root() self.assertEqual(root.get_encoding(), "ISO-8859-1") root.set(b"C", u"£".encode('utf-8')) self.assertEqual(root.get(b"C"), u"£".encode('utf-8')) self.assertEqual(root.get_raw(b"C"), b"\xa3") self.assertEqual(g2.serialise(), b"(;FF[4]C[\xa3]CA[ISO-8859-1]GM[1]SZ[19])\n") self.assertRaisesRegexp(ValueError, "unknown encoding", gosgf.Sgf_game, 19, "unknownencoding")
def test_new_sgf_game(self): g1 = gosgf.Sgf_game(9) self.assertEqual(g1.get_size(), 9) root = g1.get_root() self.assertEqual(root.get_raw(b'FF'), b'4') self.assertEqual(root.get_raw(b'GM'), b'1') self.assertEqual(root.get_raw(b'SZ'), b'9') self.assertEqual(root.get_raw_property_map(), { b'FF': [b'4'], b'GM': [b'1'], b'SZ': [b'9'], b'CA': [b'UTF-8'], }); self.assertEqual(list(root), []) self.assertEqual(root.parent, None) self.assertIs(root.owner, g1)
def test_reparent(self): g1 = gosgf.Sgf_game.from_string(b"(;SZ[9](;N[n1];N[n3])(;N[n2]))") root = g1.get_root() # Test with unexpanded root self.assertRaisesRegexp(ValueError, "would create a loop", root.reparent, root) n1 = root[0] n2 = root[1] n3 = root[0][0] self.assertEqual(n1.get(b"N"), b"n1") self.assertEqual(n2.get(b"N"), b"n2") self.assertEqual(n3.get(b"N"), b"n3") n3.reparent(n2) self.assertEqual(g1.serialise(), b"(;SZ[9](;N[n1])(;N[n2];N[n3]))\n") n3.reparent(n2) self.assertEqual(g1.serialise(), b"(;SZ[9](;N[n1])(;N[n2];N[n3]))\n") self.assertRaisesRegexp(ValueError, "would create a loop", root.reparent, n3) self.assertRaisesRegexp(ValueError, "would create a loop", n3.reparent, n3) g2 = gosgf.Sgf_game(9) self.assertRaisesRegexp( ValueError, "new parent doesn't belong to the same game", n3.reparent, g2.get_root())
def test_tree_mutation(self): sgf_game = gosgf.Sgf_game(9) root = sgf_game.get_root() n1 = root.new_child() n1.set(b"N", b"n1") n2 = root.new_child() n2.set(b"N", b"n2") n3 = n1.new_child() n3.set(b"N", b"n3") n4 = root.new_child(1) n4.set(b"N", b"n4") self.assertEqual( sgf_game.serialise(), b"(;FF[4]CA[UTF-8]GM[1]SZ[9](;N[n1];N[n3])(;N[n4])(;N[n2]))\n") self.assertEqual( [node.get_raw_property_map() for node in sgf_game.main_sequence_iter()], [node.get_raw_property_map() for node in (root, root[0], n3)]) self.assertIs(sgf_game.get_last_node(), n3) n1.delete() self.assertEqual( sgf_game.serialise(), b"(;FF[4]CA[UTF-8]GM[1]SZ[9](;N[n4])(;N[n2]))\n") self.assertRaises(ValueError, root.delete)