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 = ""
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
def test_tree_with_no_nodes(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "()"
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
def test_node_without_tree(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = ";"
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
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)"
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
def test_all_lowercase_property(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;a[b])"
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
def test_upper_and_lowercase_property(self):
|
2019-10-14 06:30:21 -05:00
|
|
|
input_string = "(;Aa[b])"
|
2018-04-03 09:55:43 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
parse(input_string)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def test_escaped_property(self):
|
2020-02-25 15:48:19 +00:00
|
|
|
input_string = "(;A[\\]b\nc\nd\t\te \n\\]])"
|
2019-10-14 06:30:21 -05:00
|
|
|
expected = SgfTree(properties={"A": ["]b\nc\nd e \n]"]})
|
2018-12-10 15:30:17 +02:00
|
|
|
self.assertEqual(parse(input_string), expected)
|
|
|
|
|
|
2018-04-03 09:55:43 -04:00
|
|
|
# Utility functions
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
|
|
|
|
|
2019-10-14 06:30:21 -05:00
|
|
|
if __name__ == "__main__":
|
2018-04-03 09:55:43 -04:00
|
|
|
unittest.main()
|