Skip to content

Commit

Permalink
Add CLI command characters show to show a saved character
Browse files Browse the repository at this point in the history
Of note, we had to duplicate some helper functions from
`characters_test.exs` to `custom_parsers_test.exs`. It'd be desirable if
these could be shared utility methods that multiple tests can use.
However to do so we either need to include the test helper functions in
the package libary code _or_ create some test macros. The former is
undersireable as I don't believe testing helpers should generally be
distributed as part of the library. The latter is more technical than we
need at this point in time. Sometime in the future we are likely to move
to the latter option.
  • Loading branch information
QMalcolm committed Dec 30, 2024
1 parent ed7f5a9 commit 8cd1ef4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/cli/args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ defmodule ExTTRPGDev.CLI.Args do
]
]
end

def character do
[
character: [
value_name: "CHARACTER",
help: "A saved character, e.g. misu_park",
required: true,
parser: &CustomParsers.character_parser(&1)
]
]
end
end
11 changes: 11 additions & 0 deletions lib/cli/characters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ defmodule ExTTRPGDev.CLI.Characters do
list: [
name: "list",
about: "List saved characters"
],
show: [
name: "show",
about: "Show information for a character",
args: Args.character()
]
]
]
Expand Down Expand Up @@ -68,4 +73,10 @@ defmodule ExTTRPGDev.CLI.Characters do
Enum.each(characters, fn character -> IO.puts("- #{character}") end)
end
end

def handle_characters_subcommands([:show | _subcommands], %Optimus.ParseResult{
args: %{character: character}
}) do
IO.inspect(character)
end
end
12 changes: 12 additions & 0 deletions lib/cli/custom_parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule ExTTRPGDev.CLI.CustomParsers do
Custom parsers to be used with Optimus args :parse
"""
alias ExTTRPGDev.RuleSystems
alias ExTTRPGDev.Characters

@doc """
Parses a string of dice specifications seperated by commas
Expand Down Expand Up @@ -37,4 +38,15 @@ defmodule ExTTRPGDev.CLI.CustomParsers do
"\"#{system}\" is not configured, run `ex_ttrpg_dev systems list` to list configured systems"}
end
end

def character_parser(character_slug) when is_bitstring(character_slug) do
if Characters.character_exists?(character_slug) do
character_slug
|> Characters.load_character!()
|> Kernel.then(fn loaded_character -> {:ok, loaded_character} end)
else
{:error,
"Character matching \"#{character_slug}\" was not found, run `ex_ttrpg_dev characters list` to list existing characters"}
end
end
end
34 changes: 34 additions & 0 deletions test/custom_parsers_test.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
defmodule ExTTRPGDevTest.CLI.CustomParsers do
use ExUnit.Case

alias ExTTRPGDev.Characters
alias ExTTRPGDev.Characters.Character
alias ExTTRPGDev.CLI.CustomParsers
alias ExTTRPGDev.RuleSystems
alias ExTTRPGDev.RuleSystems.RuleSystem

doctest ExTTRPGDev.CLI.CustomParsers,
except: [
system_parser: 1
]

def build_test_character do
RuleSystems.list_systems()
|> List.first()
|> RuleSystems.load_system!()
|> Character.gen_character!()
end

def save_test_character do
character = build_test_character()
Characters.save_character!(character)
character
end

def delete_test_character(%Character{} = character) do
File.rm(Characters.character_file_path!(character))
end

def system_parser_test do
# If no error was raised, then all is good
{:ok, %RuleSystem{}} = CustomParsers.system_parser("dnd_5e_srd")

# Should raise an error
assert_raise RuntimeError, CustomParsers.system_parser("unknown_system")
end

def character_parser_test do
character = save_test_character()

{:ok, %Character{}} = CustomParsers.character_parser(character.metadata.slug)

# cleanup
delete_test_character(character)

# should raise an error
assert_raise RuntimeError, CustomParsers.character_parser(character.metadata.slug)

assert False
end
end

0 comments on commit 8cd1ef4

Please sign in to comment.