From f9b5ae545f23f41c2087117584f3f78130ed573b Mon Sep 17 00:00:00 2001 From: Josh Price Date: Tue, 10 Oct 2017 15:36:41 +1100 Subject: [PATCH 1/4] Fix compile warnings --- mix.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mix.exs b/mix.exs index 29d1c6e..50ba596 100644 --- a/mix.exs +++ b/mix.exs @@ -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 From d4b2890ae6fe79acd46a9ea18ffa756d1f76b33b Mon Sep 17 00:00:00 2001 From: Josh Price Date: Tue, 10 Oct 2017 15:36:55 +1100 Subject: [PATCH 2/4] Failing test for missing rules --- test/voxpop_test.exs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/voxpop_test.exs b/test/voxpop_test.exs index 9815702..9e0bce5 100644 --- a/test/voxpop_test.exs +++ b/test/voxpop_test.exs @@ -21,4 +21,9 @@ 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 Voxpop.generate(concat_grammar) == "Hello " + end end From 27beac3323b1fea2383695729675bf0ac3fb0b65 Mon Sep 17 00:00:00 2001 From: Josh Price Date: Tue, 10 Oct 2017 16:25:13 +1100 Subject: [PATCH 3/4] Show error inline perhaps? --- lib/voxpop/non_terminal.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/voxpop/non_terminal.ex b/lib/voxpop/non_terminal.ex index 8e7d2fb..7048c03 100644 --- a/lib/voxpop/non_terminal.ex +++ b/lib/voxpop/non_terminal.ex @@ -5,6 +5,8 @@ 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) + registry.rules + |> Map.get(non_terminal.rule, %Voxpop.Terminal{atom: ""}) + |> Voxpop.Production.evaluate(registry) end end From 3abe05724e109812eaa6138bb51b429f0b10f819 Mon Sep 17 00:00:00 2001 From: Josh Price Date: Tue, 10 Oct 2017 16:36:30 +1100 Subject: [PATCH 4/4] Raise error on missing rule --- lib/voxpop/non_terminal.ex | 10 +++++++--- test/voxpop_test.exs | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/voxpop/non_terminal.ex b/lib/voxpop/non_terminal.ex index 7048c03..1f26960 100644 --- a/lib/voxpop/non_terminal.ex +++ b/lib/voxpop/non_terminal.ex @@ -5,8 +5,12 @@ end defimpl Voxpop.Production, for: Voxpop.NonTerminal do def evaluate(non_terminal, registry) do - registry.rules - |> Map.get(non_terminal.rule, %Voxpop.Terminal{atom: ""}) - |> 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 diff --git a/test/voxpop_test.exs b/test/voxpop_test.exs index 9e0bce5..c887478 100644 --- a/test/voxpop_test.exs +++ b/test/voxpop_test.exs @@ -24,6 +24,8 @@ defmodule VoxpopTest do test "gracefully handles missing rules" do concat_grammar = %Voxpop.Grammar.Definition{start: "{greeting} world", rules: %{greeting: "Hello {invalid}"}} - assert Voxpop.generate(concat_grammar) == "Hello " + assert_raise RuntimeError, "Invalid rule: `invalid`", fn -> + Voxpop.generate(concat_grammar) + end end end