2018-04-03 09:55:43 -04:00
|
|
|
import unittest
|
|
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from sgf_parsing import (
|
|
|
|
|
parse,
|
|
|
|
|
SgfTree,
|
|
|
|
|
)
|
2018-04-03 09:55:43 -04:00
|
|
|
|
2020-10-15 12:46:24 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
2018-08-27 12:46:30 -04:00
|
|
|
|
2019-10-14 06:30:21 -05:00
|
|
|
|
2018-04-03 09:55:43 -04:00
|
|
|
class SgfParsingTest(unittest.TestCase):
|
|
|
|
|
def test_empty_input(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = ""
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "tree missing")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_tree_with_no_nodes(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "()"
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "tree with no nodes")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_node_without_tree(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = ";"
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "tree missing")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_node_without_properties(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;)"
|
2018-04-03 09:55:43 -04:00
|
|
|
expected = SgfTree()
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_single_node_tree(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;A[B])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["B"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_multiple_properties(self):
|
|
|
|
|
input_string = "(;A[b]C[d])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["b"], "C": ["d"]})
|
2018-04-03 09:55:43 -04:00
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_properties_without_delimiter(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;A)"
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "properties without delimiter")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_all_lowercase_property(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;a[b])"
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "property must be in uppercase")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_upper_and_lowercase_property(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;Aa[b])"
|
2021-11-07 14:51:13 -08:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2018-04-03 09:55:43 -04:00
|
|
|
parse(input_string)
|
2021-11-07 14:51:13 -08:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(err.exception.args[0], "property must be in uppercase")
|
2018-04-03 09:55:43 -04:00
|
|
|
|
|
|
|
|
def test_two_nodes(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;A[B];B[C])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["B"]}, children=[SgfTree({"B": ["C"]})])
|
2018-04-03 09:55:43 -04:00
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_two_child_trees(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;A[B](;B[C])(;C[D]))"
|
2018-04-03 09:55:43 -04:00
|
|
|
expected = SgfTree(
|
2019-10-14 06:30:21 -05:00
|
|
|
properties={"A": ["B"]},
|
|
|
|
|
children=[SgfTree({"B": ["C"]}), SgfTree({"C": ["D"]})],
|
2018-04-03 09:55:43 -04:00
|
|
|
)
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_multiple_property_values(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;A[b][c][d])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["b", "c", "d"]})
|
2018-04-03 09:55:43 -04:00
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
2022-12-18 10:53:15 -08:00
|
|
|
def test_within_property_values_whitespace_characters_such_as_tab_are_converted_to_spaces(
|
|
|
|
|
self,
|
|
|
|
|
):
|
|
|
|
|
input_string = "(;A[hello\t\tworld])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["hello world"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_within_property_values_newlines_remain_as_newlines(self):
|
|
|
|
|
input_string = "(;A[hello\n\nworld])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["hello\n\nworld"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket(
|
|
|
|
|
self,
|
|
|
|
|
):
|
|
|
|
|
input_string = "(;A[\\]])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["]"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_escaped_backslash_in_property_value_becomes_just_a_backslash(self):
|
|
|
|
|
input_string = "(;A[\\\\])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["\\"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_opening_bracket_within_property_value_doesn_t_need_to_be_escaped(self):
|
|
|
|
|
input_string = "(;A[x[y\\]z][foo]B[bar];C[baz])"
|
|
|
|
|
expected = SgfTree(
|
|
|
|
|
properties={"A": ["x[y]z", "foo"], "B": ["bar"]},
|
|
|
|
|
children=[SgfTree({"C": ["baz"]})],
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
2022-12-02 22:43:01 -08:00
|
|
|
def test_semicolon_in_property_value_doesn_t_need_to_be_escaped(self):
|
|
|
|
|
input_string = "(;A[a;b][foo]B[bar];C[baz])"
|
|
|
|
|
expected = SgfTree(
|
|
|
|
|
properties={"A": ["a;b", "foo"], "B": ["bar"]},
|
|
|
|
|
children=[SgfTree({"C": ["baz"]})],
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_parentheses_in_property_value_don_t_need_to_be_escaped(self):
|
|
|
|
|
input_string = "(;A[x(y)z][foo]B[bar];C[baz])"
|
|
|
|
|
expected = SgfTree(
|
|
|
|
|
properties={"A": ["x(y)z", "foo"], "B": ["bar"]},
|
|
|
|
|
children=[SgfTree({"C": ["baz"]})],
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_escaped_tab_in_property_value_is_converted_to_space(self):
|
2022-12-18 10:53:15 -08:00
|
|
|
input_string = "(;A[hello\\\tworld])"
|
2022-12-02 22:43:01 -08:00
|
|
|
expected = SgfTree(properties={"A": ["hello world"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_escaped_newline_in_property_value_is_converted_to_nothing_at_all(self):
|
2022-12-18 10:53:15 -08:00
|
|
|
input_string = "(;A[hello\\\nworld])"
|
2022-12-02 22:43:01 -08:00
|
|
|
expected = SgfTree(properties={"A": ["helloworld"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
|
|
|
|
def test_escaped_t_and_n_in_property_value_are_just_letters_not_whitespace(self):
|
2022-12-18 10:53:15 -08:00
|
|
|
input_string = "(;A[\\t = t and \\n = n])"
|
2022-12-02 22:43:01 -08:00
|
|
|
expected = SgfTree(properties={"A": ["t = t and n = n"]})
|
2021-11-09 04:04:42 -08:00
|
|
|
self.assertEqual(parse(input_string), expected)
|
2022-12-18 10:53:15 -08:00
|
|
|
|
|
|
|
|
def test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value(
|
|
|
|
|
self,
|
|
|
|
|
):
|
|
|
|
|
input_string = "(;A[\\]b\nc\\\nd\t\te\\\\ \\\n\\]])"
|
|
|
|
|
expected = SgfTree(properties={"A": ["]b\ncd e\\ ]"]})
|
|
|
|
|
self.assertEqual(parse(input_string), expected)
|