From 94f3db91a041e37bf833e91c5ebc24976e9ed948 Mon Sep 17 00:00:00 2001 From: Raph Estrada Date: Sun, 22 Oct 2017 16:08:58 +0100 Subject: [PATCH] ensure symbolic error keys remain symbols Previously, when doing this add_error(:foo, :bar, "baz") one could not fetch the error by that symbol, only by string. outcome.errors["foo"] This PR makes a tiny change to preserve the symbol keys. --- lib/mutations/command.rb | 3 ++- spec/command_spec.rb | 6 ++++-- spec/errors_spec.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/mutations/command.rb b/lib/mutations/command.rb index c03cbbf..81c6230 100644 --- a/lib/mutations/command.rb +++ b/lib/mutations/command.rb @@ -106,6 +106,7 @@ def execute end # add_error("name", :too_short) + # add_error(:name, :too_short) # add_error("colors.foreground", :not_a_color) # => to create errors = {colors: {foreground: :not_a_color}} # or, supply a custom message: # add_error("name", :too_short, "The name 'blahblahblah' is too short!") @@ -119,7 +120,7 @@ def add_error(key, kind, message = nil) inner = path.inject(errs) do |cur_errors,part| cur_errors[part.to_sym] ||= ErrorHash.new end - inner[last] = ErrorAtom.new(key, kind, :message => message) + inner[last.to_sym] = ErrorAtom.new(key, kind, :message => message) end end diff --git a/spec/command_spec.rb b/spec/command_spec.rb index 3f26a29..f7ef3a9 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -153,8 +153,8 @@ class ErrorfulCommand < Mutations::Command optional { string :email } def execute - add_error("bob", :is_a_bob) - + add_error(:bob, :is_a_bob) + add_error("jane", :is_a_jane) 1 end end @@ -164,7 +164,9 @@ def execute assert !outcome.success? assert_nil outcome.result + assert_equal :is_a_bob, outcome.errors[:bob].symbolic assert_equal :is_a_bob, outcome.errors.symbolic[:bob] + assert_equal :is_a_jane, outcome.errors.symbolic["jane"] end end diff --git a/spec/errors_spec.rb b/spec/errors_spec.rb index 42a57d1..a7c4452 100644 --- a/spec/errors_spec.rb +++ b/spec/errors_spec.rb @@ -60,6 +60,19 @@ def execute assert_equal "Newsletter Subscription isn't a boolean", atom.message end + describe "errors as symbols remain symbols" do + class Foo < Mutations::Command + def execute + add_error(:foo, :bar, "baz") + end + end + let(:outcome){ Foo.run } + + it{ assert_nil(outcome.errors["foo"]) } + it{ assert(outcome.errors[:foo]) } + it{ assert_equal(outcome.errors[:foo].message, "baz") } + end + describe "Bunch o errors" do before do @outcome = GivesErrors.run(:str1 => "", :str2 => "opt9", :int1 => "zero", :hash1 => {:bool1 => "bob"}, :arr1 => ["bob", 1, "sally"])