Skip to content

Commit f16af25

Browse files
authored
Fix inspect exception with ANSI enabled (#148)
1 parent 1026e38 commit f16af25

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

lib/nimble_options/validation_error.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ defmodule NimbleOptions.ValidationError do
5151
|> Map.drop([:__struct__, :__exception__])
5252
|> Map.update!(:value, &if(redacted?, do: "**redacted**", else: &1))
5353
|> Enum.sort_by(fn {key, _val} -> key end)
54-
|> Enum.map(fn {key, val} -> [string("#{key}:"), break(), to_doc(val, opts)] end)
55-
|> Enum.intersperse([string(","), break()])
56-
|> List.flatten()
54+
|> Enum.map(fn {key, val} -> concat([string("#{key}: "), to_doc(val, opts)]) end)
55+
|> Enum.intersperse(string(", "))
5756

58-
concat(["##{inspect(@for)}<"] ++ fields ++ [">"])
57+
concat(["##{inspect(@for)}<", concat(fields), ">"])
5958
end
6059
end
6160
end

test/nimble_options/validation_error_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ defmodule NimbleOptions.ValidationErrorTest do
4545
~s(#NimbleOptions.ValidationError<key: :foo, keys_path: [], message: "invalid value for :foo option: expected integer, got: true", redact: false, value: true>)
4646
end
4747

48+
test "with syntax colors" do
49+
schema = [foo: [type: :integer]]
50+
opts = [foo: true]
51+
52+
{:error, error} = NimbleOptions.validate(opts, schema)
53+
54+
assert inspect(error, syntax_colors: [atom: :red]) ==
55+
~s(#NimbleOptions.ValidationError<key: \e[31m:foo\e[0m, keys_path: [], message: "invalid value for :foo option: expected integer, got: true", redact: false, value: true>)
56+
end
57+
4858
test "with a redacted option" do
4959
schema = [foo: [type: :integer, redact: true]]
5060

0 commit comments

Comments
 (0)