Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengfei Chen committed Feb 5, 2024
1 parent 42b29a4 commit a23e960
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
17 changes: 13 additions & 4 deletions unitary/examples/quantum_chinese_chess/board_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Color,
Type,
SquareState,
TerminalType,
)
from unitary.examples.quantum_chinese_chess.board import Board
from unitary.examples.quantum_chinese_chess.piece import Piece
Expand All @@ -36,7 +37,9 @@ def test_init_with_default_fen():

# test English print
assert (
re.sub("\\033\[\d{1,2}m", "", board.to_str(None, False)).replace("\b", "")
re.sub("\\033\[\d{1,2}m", "", board.to_str(TerminalType.MAC_OR_LINUX)).replace(
"\b", ""
)
== """
a b c d e f g h i
0 R H E A K A E H R 0
Expand All @@ -56,7 +59,9 @@ def test_init_with_default_fen():
# test Chinese print
board.set_language(Language.ZH)
assert (
re.sub("\\033\[\d{1,2}m", "", board.to_str(None, False)).replace("\b", "")
re.sub("\\033\[\d{1,2}m", "", board.to_str(TerminalType.MAC_OR_LINUX)).replace(
"\b", ""
)
== """
  a  b  c  d  e  f  g  h  i
0 车  马  象  士  将  士  象  马  车  0
Expand All @@ -82,7 +87,9 @@ def test_init_with_specified_fen():

# test English print
assert (
re.sub("\\033\[\d{1,2}m", "", board.to_str(None, False)).replace("\b", "")
re.sub("\\033\[\d{1,2}m", "", board.to_str(TerminalType.MAC_OR_LINUX)).replace(
"\b", ""
)
== """
a b c d e f g h i
0 · · · · k a R · · 0
Expand All @@ -102,7 +109,9 @@ def test_init_with_specified_fen():
# test Chinese print
board.set_language(Language.ZH)
assert (
re.sub("\\033\[\d{1,2}m", "", board.to_str(None, False)).replace("\b", "")
re.sub("\\033\[\d{1,2}m", "", board.to_str(TerminalType.MAC_OR_LINUX)).replace(
"\b", ""
)
== """
  a  b  c  d  e  f  g  h  i
0 ・  ・  ・  ・  帥  仕  车  ・  ・  0
Expand Down
24 changes: 13 additions & 11 deletions unitary/examples/quantum_chinese_chess/chess_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@
SquareState,
MoveType,
MoveVariant,
TerminalType,
)


def test_game_init(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
output = io.StringIO()
sys.stdout = output
game = QuantumChineseChess()
assert game.lang == Language.ZH
assert game.terminal == TerminalType.MAC_OR_LINUX
assert game.players_name == ["Bob", "Ben"]
assert game.current_player == 0
assert "Welcome" in output.getvalue()
sys.stdout = sys.__stdout__


def test_parse_input_string_success(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
assert game.parse_input_string("a1b1") == (["a1"], ["b1"])
Expand All @@ -56,7 +58,7 @@ def test_parse_input_string_success(monkeypatch):


def test_parse_input_string_fail(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
with pytest.raises(ValueError, match="Invalid sources/targets string "):
Expand All @@ -76,7 +78,7 @@ def test_parse_input_string_fail(monkeypatch):


def test_apply_move_fail(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
with pytest.raises(ValueError, match="Could not move empty piece."):
Expand All @@ -94,7 +96,7 @@ def test_apply_move_fail(monkeypatch):
def test_game_invalid_move(monkeypatch):
output = io.StringIO()
sys.stdout = output
inputs = iter(["y", "Bob", "Ben", "a1n1", "exit"])
inputs = iter(["y", "n", "Bob", "Ben", "a1n1", "exit"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
game.play()
Expand All @@ -108,7 +110,7 @@ def test_game_invalid_move(monkeypatch):
def test_check_classical_rule(monkeypatch):
output = io.StringIO()
sys.stdout = output
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = game.board.board
Expand Down Expand Up @@ -207,7 +209,7 @@ def test_check_classical_rule(monkeypatch):
def test_classify_move_fail(monkeypatch):
output = io.StringIO()
sys.stdout = output
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = game.board.board
Expand Down Expand Up @@ -259,7 +261,7 @@ def test_classify_move_fail(monkeypatch):
def test_classify_move_success(monkeypatch):
output = io.StringIO()
sys.stdout = output
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = game.board.board
Expand Down Expand Up @@ -367,7 +369,7 @@ def test_classify_move_success(monkeypatch):
def test_update_board_by_sampling(monkeypatch):
output = io.StringIO()
sys.stdout = output
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = game.board.board
Expand All @@ -390,7 +392,7 @@ def test_update_board_by_sampling(monkeypatch):


def test_undo_single_effect_per_move(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = set_board(["a1", "b1", "c1"])
Expand Down Expand Up @@ -431,7 +433,7 @@ def test_undo_single_effect_per_move(monkeypatch):


def test_undo_multiple_effects_per_move(monkeypatch):
inputs = iter(["y", "Bob", "Ben"])
inputs = iter(["y", "n", "Bob", "Ben"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
game = QuantumChineseChess()
board = set_board(["a1", "b1", "c1"])
Expand Down

0 comments on commit a23e960

Please sign in to comment.