Files
katrain-qt/tests/test_parser.py
T
+3 bf70f2cf23 1.9 dev (#411)
* refactor priorities

* performance report doodling

* shutup pycharm

* more doodling

* doodle tricol

* tweak report, fix bug in insert

* reorder colors

* another metric

* sorting engines, trying to catch exc

* restructure engine err

* restructure engine err

* test stuff

* test report

* test report

* test and readme

* test and readme

* layout

* engine recov

* pixelfix and exes

* readme

* keyboard shortcut override

* oops

* mistakes only deeper

* black and readme

* readme

* fix #421, scale repor

* Add shortcut variables (#420)

* Add shortcut variables

* Address comments and missed shortcuts

* fix shortcuts

* rename, design report, fix report

* make translators work more

* relative mid/opening/endgame bounds

* win size dbg

* more errros

* entry

* jeff mode

* fix

* readme

* edit THEMES.md

* edit katrain/core/ai.py, edit katrain/gui/kivyutils.py

* jp (#423)

* edit THEMES.md

* edit katrain/__main__.py, edit katrain/gui/kivyutils.py and 1 other changes

* Update katrain.po (#425)

1.9 first French .po

* edit katrain/i18n/locales/fr/LC_MESSAGES/katrain.mo

* edit katrain/core/ai.py, edit katrain/gui/popups.py

* bluedabadee

* fix sgf and black

* edit katrain/i18n/locales/fr/LC_MESSAGES/katrain.po

* ko

* Update zh-TW translation (#426)

* cn

* cn

* Some corrections to Chinese translations (#427)

* some corrections in cn and tw

* better trans

* edit CONTRIBUTIONS.md, edit katrain/i18n/locales/cn/LC_MESSAGES/katrain.mo and 1 other changes

* de

* tw

* player what only

* add green background to better value (#428)

* edit katrain/gui/popups.py, edit katrain/gui/theme.py

* Russian for 1.9 (#432)

* edit katrain/i18n/locales/ru/LC_MESSAGES/katrain.po, edit katrain/gui.kv and 1 other changes

* sound fix

* sound fix

* fmt

* fix sound

* edit THEMES.md, edit CONTRIBUTIONS.md and 1 other changes

* sound fix

* Do not use ffpyplayer on Windows (#434)

* edit katrain/gui/sound.py

* no pr

* jeff theme

* stopsound

* sound and theme fix

* font fix and more errs

* French po corrections (#441)

Former translations were misleading. It should be accurate now.

* add mouse scroll to browser through node variations. (#442)

* add mouse scroll to browser through node variations.

* add mouse scroll to browser through node variations.

* add mouse scroll to browser through node variations.

* edit katrain/popups.kv, edit katrain/__main__.py and 2 other changes

* windows binary, black

* linux binary

* edit INSTALL.md

* edit .github/workflows/osxbuild.yaml

* edit .github/workflows/osxbuild.yaml

* edit .github/workflows/osxbuild.yaml

Co-authored-by: Sander Land <sander.land@cognite.com>
Co-authored-by: roux g. buciu <11182210+electricRGB@users.noreply.github.com>
Co-authored-by: kaorahi <38910552+kaorahi@users.noreply.github.com>
Co-authored-by: Dontbtme <66470440+Dontbtme@users.noreply.github.com>
Co-authored-by: Liou, Guan-Cheng <Tony-Liou@users.noreply.github.com>
Co-authored-by: Ching-yu Lin <60384727+chingyulin@users.noreply.github.com>
Co-authored-by: xiaoyifang <105986+xiaoyifang@users.noreply.github.com>
Co-authored-by: Alexander Kiselev <satoshi.yoda.register.264@gmail.com>
Co-authored-by: Sander Land <Sander@bioeng219-lap.isd.kcl.ac.uk>
Co-authored-by: Carton He <cartonhe@gmail.com>
Co-authored-by: Sander Land <sander.land@aroni.fritz.box>
Co-authored-by: Sander Land <sander.land@futurefacts.nl>
2021-06-28 16:59:17 +02:00

190 lines
5.7 KiB
Python

import os
from unittest.mock import MagicMock
from katrain.core.base_katrain import KaTrainBase
from katrain.core.game import Game, KaTrainSGF
from katrain.core.sgf_parser import SGF, SGFNode
def test_simple():
input_sgf = "(;GM[1]FF[4]SZ[19]DT[2020-04-12]AB[dd][dj];B[dp];W[pp];B[pj])"
root = SGF.parse_sgf(input_sgf)
assert "4" == root.get_property("FF")
assert root.get_property("XYZ") is None
assert "dp" == root.children[0].get_property("B")
assert input_sgf == root.sgf()
def test_branch():
input_sgf = "(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.43.3]KM[6.5]SZ[19]DT[2020-04-12]AB[dd][dj](;B[dp];W[pp](;B[pj])(;PL[B]AW[jp]C[sdfdsfdsf]))(;B[pd]))"
root = SGF.parse_sgf(input_sgf)
assert input_sgf == root.sgf()
def test_dragon_weirdness(): # dragon go server has weird line breaks
input_sgf = "\n(\n\n;\nGM[1]\nFF[4]\nCA[UTF-8]AP[Sabaki:0.43.3]KM[6.5]SZ[19]DT[2020-04-12]AB[dd]\n[dj]\n(\n;\nB[dp]\n;\nW[pp]\n(\n;\nB[pj]\n)\n(\n;\nPL[B]\nAW[jp]\nC[sdfdsfdsf]\n)\n)\n(\n;\nB[pd]\n)\n)\n"
root = SGF.parse_sgf(input_sgf)
assert input_sgf.replace("\n", "") == root.sgf()
def test_weird_escape():
input_sgf = """(;GM[1]FF[4]CA[UTF-8]AP[Sabaki:0.43.3]KM[6.5]SZ[19]DT[2020-04-12]C[how does it escape
[
or \\]
])"""
root = SGF.parse_sgf(input_sgf)
assert input_sgf == root.sgf()
def test_backslash_escape():
nasty_string = "[]]\\"
nasty_strings = ["[\\]\\]\\\\", "[", "]", "\\", "\\[", "\\]", "\\\\[", "\\\\]", "]]]\\]]\\]]["]
assert "[\\]\\]\\\\" == SGFNode._escape_value(nasty_string)
for x in nasty_strings:
assert x == SGFNode._unescape_value(SGFNode._escape_value(x))
c2 = ["]", "\\"]
node = SGFNode(properties={"C1": nasty_string})
node.set_property("C2", c2)
assert "(;C1[[\\]\\]\\\\]C2[\\]][\\\\])" == node.sgf()
assert {"C1": [nasty_string], "C2": c2} == SGF.parse_sgf(node.sgf()).properties
def test_alphago():
file = os.path.join(os.path.dirname(__file__), "data/LS vs AG - G4 - English.sgf")
SGF.parse_file(file)
def test_pandanet():
file = os.path.join(os.path.dirname(__file__), "data/panda1.sgf")
root = SGF.parse_file(file)
root_props = {
"GM",
"EV",
"US",
"CP",
"GN",
"RE",
"PW",
"WR",
"NW",
"PB",
"BR",
"NB",
"PC",
"DT",
"SZ",
"TM",
"KM",
"LT",
"RR",
"HA",
"AB",
"C",
}
assert root_props == root.properties.keys()
move = root
while move.children:
move = move.children[0]
assert 94 == len(move.get_list_property("TW"))
assert "Trilan" == move.get_property("OS")
while move.parent:
move = move.parent
assert move is root
def test_old_long_properties():
file = os.path.join(os.path.dirname(__file__), "data/xmgt97.sgf")
SGF.parse_file(file)
def test_old_server_style():
input_sgf = "... 01:23:45 +0900 (JST) ... (;SZ[19];B[aa];W[ba];)"
SGF.parse_sgf(input_sgf)
def test_old_server_style_again():
input_sgf = """(;
SZ[19]TM[600]KM[0.500000]LT[]
;B[fp]BL[500];
)"""
tree = SGF.parse_sgf(input_sgf)
assert 2 == len(tree.nodes_in_tree)
def test_ogs():
file = os.path.join(os.path.dirname(__file__), "data/ogs.sgf")
SGF.parse_file(file)
def test_gibo():
file = os.path.join(os.path.dirname(__file__), "data/test.gib")
root = SGF.parse_file(file)
assert {
"PW": ["wildsim1"],
"WR": ["2D"],
"PB": ["kim"],
"BR": ["2D"],
"RE": ["W+T"],
"KM": [6.5],
"DT": ["2020-06-14"],
} == root.properties
assert "pd" == root.children[0].get_property("B")
def test_ngf():
file = os.path.join(os.path.dirname(__file__), "data/handicap2.ngf")
root = SGF.parse_file(file)
root.properties["AB"].sort()
assert {
"AB": ["dp", "pd"],
"DT": ["2017-03-16"],
"HA": [2],
"PB": ["p81587"],
"PW": ["ace550"],
"RE": ["W+"],
"SZ": [19],
} == root.properties
assert "pq" == root.children[0].get_property("W")
def test_foxwq():
for sgf in ["data/fox sgf error.sgf", "data/fox sgf works.sgf"]:
file = os.path.join(os.path.dirname(__file__), sgf)
move_tree = KaTrainSGF.parse_file(file)
katrain = KaTrainBase(force_package_config=True, debug_level=0)
game = Game(katrain, MagicMock(), move_tree)
assert [] == move_tree.placements
assert [] == game.root.placements
while game.current_node.children:
assert 1 == len(game.current_node.children)
game.redo(1)
def test_next_player():
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb])"
assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[B])"
assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[W])"
assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa])"
assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]PL[B])"
assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa];B[dd])" # branch exists
assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa];W[dd])" # branch exists
assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player