from itertools import chain from datetime import date import unittest from unittest import TestCase import os from sgfParser import strRowCol from sgfParser.collection import Collection from sgfParser.property import Property,DateProperty,DateException from sgfParser.propValues import text,compose dataDir=os.path.join(os.path.dirname(__file__), "data") class TestUtils(TestCase): def testTextPos(self): s="abc\ndef\rgh\r\nij\n\rklmn" rc=[ [1,2,3,4], [1,2,3,4], [1,2,3,4], [1,2,3], [1], # don't care about LFCR, we unicode now [1,2,3,4] ] res=chain((r+1,c) for (r,row) in enumerate(rc) for c in row) for (i,(r,c)) in zip(range(len(s)+1), res): self.assertEqual(strRowCol(s, i), (r, c)) class TestProperty(TestCase): def testName(self): with self.assertRaises(AssertionError): Property.create("[99]",0) with self.assertRaises(AssertionError): Property.create("99[99]",0) i,prop=Property.create("MN[99]",0) self.assertNotEqual((i,prop), (0,None)) self.assertEqual((i,prop.name), (6,"MN")) def testText(self): s=r"""[abc\ def ghi]""" self.assertEqual(text()(s,1)[1], "abcdef ghi") self.assertEqual(text(False)(s,1)[1], "abcdef\nghi") s="""[m\\no\\\tpqr\\]\\\\]""" self.assertEqual(text()(s,1)[1], "mno pqr]\\") self.assertEqual(text(False)(s,1)[1], "mno pqr]\\") s="""[abc:def]""" parsed=compose(text(composed=True),text(composed=True))(s,1) self.assertEqual(str(parsed[1]), "abc:def") class TestDateProperty(TestCase): def testSingle(self): self.assertEqual(DateProperty.parseSingle("2019","Y")[1], date(2019,1,1)) self.assertEqual(DateProperty.parseSingle("2019-06","YM")[1], date(2019,6,1)) self.assertEqual(DateProperty.parseSingle("2019-06-22","YMD")[1], date(2019,6,22)) d=date(2019,6,21) self.assertEqual(DateProperty.parseSingle("22","D",d)[1], date(2019,6,22)) self.assertEqual(DateProperty.parseSingle("07-22","MD",d)[1], date(2019,7,22)) self.assertEqual(DateProperty.parseSingle("2020-07-22","YMD",d)[1], date(2020,7,22)) with self.assertRaises(ValueError): DateProperty.parseSingle("2019-31","YMD") def testParse(self): self.assertEqual(DateProperty("1996-05,06").value, [date(1996,5,1),date(1996,6,1)]) self.assertEqual(DateProperty("1996-05-06,07,08").value, [date(1996,5,6),date(1996,5,7),date(1996,5,8)]) self.assertEqual(DateProperty("1996,1997").value, [date(1996,1,1),date(1997,1,1)]) self.assertEqual(DateProperty("1996-12-27,28,1997-01-03,04").value, [date(1996,12,27),date(1996,12,28),date(1997,1,3),date(1997,1,4)]) self.assertEqual(DateProperty("1997-05-05,1997-05-06").value, [date(1997,5,5),date(1997,5,6)]) self.assertEqual(DateProperty("Published on 1997-05-06").value, [date(1997,5,6)]) with self.assertRaises(DateException): DateProperty("unknown") class TestCollection(TestCase): def testSubtrees(self): c=Collection(""" (;B[aa] (;W[ab]PB[Some Black]PW[Some White];B[ac]) (;W[bb]PB[Other Black]PW[Other White]) )""") games=list(c.listGames()) self.assertEqual(len(games),2) self.assertRegex(games[0].export(), r"^\(;B\[aa]\n;(PB\[Some Black]|PW\[Some White]|W\[ab]){3}\n;B\[ac]\n\)\n$") self.assertRegex(games[1].export(), r"^\(;B\[aa]\n;(PB\[Other Black]|PW\[Other White]|W\[bb]){3}\n\)\n$") def testEmptySgf(self): Collection("(;)") def testSimpleSgf(self): with open(os.path.join(dataDir, "simple.sgf")) as f: Collection(f.read()) def testComplexSgf(self): with open(os.path.join(dataDir, "kogos.sgf")) as f: Collection(f.read()) if __name__ == '__main__': unittest.main()