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

Raise error on missing rules #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion lib/voxpop/non_terminal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ end

defimpl Voxpop.Production, for: Voxpop.NonTerminal do
def evaluate(non_terminal, registry) do
registry.rules |> Map.get(non_terminal.rule) |> Voxpop.Production.evaluate(registry)
value = Map.get(registry.rules, non_terminal.rule)

if value do
Voxpop.Production.evaluate(value, registry)
else
raise "Invalid rule: `#{non_terminal.rule}`"
end
end
end
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ defmodule Voxpop.Mixfile do
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps,
package: package,
description: description
deps: deps(),
package: package(),
description: description()
]
end

Expand Down
7 changes: 7 additions & 0 deletions test/voxpop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ defmodule VoxpopTest do
concat_grammar = %Voxpop.Grammar.Definition{start: "{greeting} world", rules: %{greeting: "Hello"}}
assert Voxpop.generate(concat_grammar) == "Hello world"
end

test "gracefully handles missing rules" do
concat_grammar = %Voxpop.Grammar.Definition{start: "{greeting} world", rules: %{greeting: "Hello {invalid}"}}
assert_raise RuntimeError, "Invalid rule: `invalid`", fn ->
Voxpop.generate(concat_grammar)
end
end
end