Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandeborde committed Apr 24, 2024
1 parent dbda11d commit eff0498
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Empty file added tests/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from gigax.parse import CharacterAction, get_guided_regex
from tests.utils import create_scene


def test_parse():
_, locations, NPCs, protagonist, items, _ = create_scene()

test_command = 'attack John the Brave "Hello, how are you"'

# Test the compiled regex
compiled_combined_regex = get_guided_regex(
protagonist.skills, NPCs, locations, items
)
assert compiled_combined_regex
assert compiled_combined_regex.match(test_command)

# Test the from_str method
character_action = CharacterAction.from_str(
test_command, protagonist, NPCs, locations, items, compiled_combined_regex
)
assert character_action
assert character_action.command == "Attack"
assert character_action.protagonist == protagonist
assert character_action.parameters == [NPCs[0]]
30 changes: 30 additions & 0 deletions tests/test_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from gigax.prompt import NPCPrompt
from tests.utils import create_scene


def test_prompt():
prompt = NPCPrompt(*create_scene())
assert prompt

test_prompt = """- WORLD KNOWLEDGE: A vast open world full of mystery and adventure.
- KNOWN LOCATIONS: Old Town
- NPCS: John the Brave
- CURRENT LOCATION: Old Town: A quiet and peaceful town.
- CURRENT LOCATION ITEMS: Sword
- LAST EVENTS:
Aldren: Say Sword What a fine sword!
- PROTAGONIST NAME: Aldren
- PROTAGONIST PSYCHOLOGICAL PROFILE: Brave and curious
- PROTAGONIST MEMORIES:
Saved the village
Lost a friend
- PROTAGONIST PENDING QUESTS:
Find the ancient artifact
Defeat the evil warlock
- PROTAGONIST ALLOWED ACTIONS:
Attack <character> : Deliver a powerful blow
Aldren:"""

assert prompt == test_prompt, f"{prompt} != {test_prompt}"
39 changes: 39 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from gigax.scene import Character, Item, Location, ParameterType
from gigax.parse import CharacterAction, ProtagonistCharacter, Skill


def create_scene():
# Example usage of the function
context = "A vast open world full of mystery and adventure."
locations = [Location(name="Old Town", description="A quiet and peaceful town.")]
NPCs = [
Character(
name="John the Brave",
description="A fearless warrior",
current_location=locations[0],
)
]
protagonist = ProtagonistCharacter(
name="Aldren",
description="Brave and curious",
current_location=locations[0],
memories=["Saved the village", "Lost a friend"],
quests=["Find the ancient artifact", "Defeat the evil warlock"],
skills=[
Skill(
name="Attack",
description="Deliver a powerful blow",
parameter_types=[ParameterType.character],
)
],
psychological_profile="Determined and compassionate",
)
items = [Item(name="Sword", description="A sharp blade")]
events = [
CharacterAction(
command="Say",
protagonist=protagonist,
parameters=[items[0], "What a fine sword!"],
)
]
return context, locations, NPCs, protagonist, items, events

0 comments on commit eff0498

Please sign in to comment.