Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Quit case-sensitive #197

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/quantum_rpg/bb84_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_alice_bob():
"look display",
"look keyboard",
"type aaaa",
"quit",
"Quit",
],
file=io.StringIO(),
)
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion examples/quantum_rpg/main_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand All @@ -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:
Expand Down
36 changes: 19 additions & 17 deletions examples/quantum_rpg/main_loop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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"""
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading