From 15fa4bcc1124cda4a8a5c52d35b6765dddfb00ed Mon Sep 17 00:00:00 2001 From: Peter Solodov Date: Tue, 2 Jul 2024 16:31:55 -0400 Subject: [PATCH] Make Quit case-sensitive Quit command is matched first before user input is lower-cased. --- examples/quantum_rpg/bb84_test.py | 4 +-- examples/quantum_rpg/main_loop.py | 9 ++++++- examples/quantum_rpg/main_loop_test.py | 36 ++++++++++++++------------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/examples/quantum_rpg/bb84_test.py b/examples/quantum_rpg/bb84_test.py index c4a506eb..e9eee03d 100644 --- a/examples/quantum_rpg/bb84_test.py +++ b/examples/quantum_rpg/bb84_test.py @@ -81,7 +81,7 @@ def test_alice_bob(): "look display", "look keyboard", "type aaaa", - "quit", + "Quit", ], file=io.StringIO(), ) @@ -159,7 +159,7 @@ def test_alice_bob(): state.file = file = io.StringIO() key = bb84.solve_bb84(state.state_dict["alice"], state.state_dict["bob"]) state.get_user_input = input_helpers.get_user_input_function( - [f"type {key}", "north", "south", "quit"] + [f"type {key}", "north", "south", "Quit"] ) loop = main_loop.MainLoop(example_world, state) loop.loop() diff --git a/examples/quantum_rpg/main_loop.py b/examples/quantum_rpg/main_loop.py index 186433a3..66be33ea 100644 --- a/examples/quantum_rpg/main_loop.py +++ b/examples/quantum_rpg/main_loop.py @@ -49,7 +49,7 @@ class Command(enum.Enum): SAVE = "save" HELP = "help" QUANTOPEDIA = "quantopedia" - QUIT = "quit" + QUIT = "Quit" @classmethod def parse(cls, s: str) -> Optional["Command"]: @@ -59,9 +59,16 @@ def parse(cls, s: str) -> Optional["Command"]: """ if not s: return None + # Quit is a special case, it's case-sensitive. It's matched first before + # input is lower-cased and matched against every other command. + if cls.QUIT.value.startswith(s): + return cls.QUIT lower_s = s.lower() candidates = [] for cmd in cls: + # Quit has already been handled above. + if cmd == cls.QUIT: + continue if cmd.value.startswith(lower_s): candidates.append(cmd) if len(candidates) == 1: diff --git a/examples/quantum_rpg/main_loop_test.py b/examples/quantum_rpg/main_loop_test.py index 93c9e620..869f76ab 100644 --- a/examples/quantum_rpg/main_loop_test.py +++ b/examples/quantum_rpg/main_loop_test.py @@ -105,16 +105,18 @@ def example_world(): def test_parse_commands() -> None: assert main_loop.Command.parse("x") is None - assert main_loop.Command.parse("qui") is main_loop.Command.QUIT + assert main_loop.Command.parse("Q") is main_loop.Command.QUIT assert main_loop.Command.parse("Qui") is main_loop.Command.QUIT assert main_loop.Command.parse("Quit") is main_loop.Command.QUIT - assert main_loop.Command.parse("quit") is main_loop.Command.QUIT + assert main_loop.Command.parse("Quitt") is None + assert main_loop.Command.parse("quit") is None + assert main_loop.Command.parse("load") is main_loop.Command.LOAD def test_simple_main_loop() -> None: c = classes.Analyst("Mensing") state = game_state.GameState( - party=[c], user_input=["look", "quit"], file=io.StringIO() + party=[c], user_input=["look", "Quit"], file=io.StringIO() ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) loop.loop() @@ -140,7 +142,7 @@ def test_simple_main_loop() -> None: def test_empty_command() -> None: state = game_state.GameState( - party=[], user_input=["", "", "quit"], file=io.StringIO() + party=[], user_input=["", "", "Quit"], file=io.StringIO() ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) loop.loop() @@ -168,10 +170,10 @@ def test_status() -> None: c2.add_quantum_effect(alpha.Superposition(), 1) c2.add_quantum_effect(alpha.Flip(effect_fraction=0.5), 2) state = game_state.GameState( - party=[c, c2], user_input=["status", "quit"], file=io.StringIO() + party=[c, c2], user_input=["status", "Quit"], file=io.StringIO() ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) - loop.loop(user_input=["quit"]) + loop.loop(user_input=["Quit"]) assert ( cast(io.StringIO, state.file).getvalue().replace("\t", " ").strip() == r""" @@ -197,7 +199,7 @@ def test_status() -> None: def test_do_simple_move() -> None: c = classes.Analyst("Mensing") state = game_state.GameState( - party=[c], user_input=["e", "read sign", "w", "quit"], file=io.StringIO() + party=[c], user_input=["e", "read sign", "w", "Quit"], file=io.StringIO() ) loop = main_loop.MainLoop(world.World(example_world()), state) loop.loop() @@ -235,7 +237,7 @@ def test_do_simple_move() -> None: def test_save() -> None: c = classes.Analyst("Mensing") state = game_state.GameState( - party=[c], user_input=["e", "save", "quit"], file=io.StringIO() + party=[c], user_input=["e", "save", "Quit"], file=io.StringIO() ) loop = main_loop.MainLoop(world.World(example_world()), state) loop.loop() @@ -267,7 +269,7 @@ def test_load() -> None: c = classes.Analyst("Broglie") state = game_state.GameState( party=[c], - user_input=["load", "2;1;Mensing#Analyst#1", "quit"], + user_input=["load", "2;1;Mensing#Analyst#1", "Quit"], file=io.StringIO(), ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) @@ -299,7 +301,7 @@ def test_bad_load() -> None: c = classes.Analyst("Broglie") state = game_state.GameState( party=[c], - user_input=["load", "", "quit"], + user_input=["load", "", "Quit"], file=io.StringIO(), ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) @@ -330,7 +332,7 @@ def test_battle() -> None: c = classes.Analyst("Mensing") state = game_state.GameState( party=[c], - user_input=["e", "south", "m", "1", "1", "", "quit"], + user_input=["e", "south", "m", "1", "1", "", "Quit"], file=io.StringIO(), ) loop = main_loop.MainLoop(state=state, world=world.World(example_world())) @@ -391,7 +393,7 @@ def test_lost_battle() -> None: c = classes.Engineer("Mensing") state = game_state.GameState( party=[c], - user_input=["e", "south", "x", "1", "1", "", "quit"], + user_input=["e", "south", "x", "1", "1", "", "Quit"], file=io.StringIO(), ) assert state.party[0].name == "Mensing" @@ -460,7 +462,7 @@ def test_escaped_battle(): c.add_quantum_effect(alpha.Flip(), 1) state = game_state.GameState( party=[c], - user_input=["e", "south", "x", "1", "1", "", "quit"], + user_input=["e", "south", "x", "1", "1", "", "Quit"], file=io.StringIO(), ) assert state.party[0].name == "Mensing" @@ -525,7 +527,7 @@ def test_item_function(): c = classes.Analyst("michalakis") state = game_state.GameState( party=[c], - user_input=["press button", "press button", "press button", "quit"], + user_input=["press button", "press button", "press button", "Quit"], file=io.StringIO(), ) loop = main_loop.MainLoop(world.World(example_world()), state) @@ -566,7 +568,7 @@ def test_main_help(): def test_main_begin(): state = game_state.GameState( - party=[], user_input=["1", "nova", "n", "quit"], file=io.StringIO() + party=[], user_input=["1", "nova", "n", "Quit"], file=io.StringIO() ) loop = main_loop.main(state) @@ -606,7 +608,7 @@ def test_main_begin(): def test_main_load(): state = game_state.GameState( party=[], - user_input=["2", "classical3;1;Doug#Analyst#1", "quit"], + user_input=["2", "classical3;1;Doug#Analyst#1", "Quit"], file=io.StringIO(), ) loop = main_loop.main(state) @@ -631,7 +633,7 @@ def test_main_load(): def test_main_bad_save_file(): state = game_state.GameState( party=[], - user_input=["2", "", "2", "classical3;1;Doug#Analyst#1", "quit"], + user_input=["2", "", "2", "classical3;1;Doug#Analyst#1", "Quit"], file=io.StringIO(), ) loop = main_loop.main(state)