Skip to content

Commit 552b4c4

Browse files
authored
Allow Inspect.Algebra.to_doc/2 Options To Be Overriden (#5)
* [WORK-289] Inspect.Algebra.to_doc/1 Options Can Be Overriden * [WORK-289] Pull Request Suggestions Added - Default `structs` value now is `false` - Pattern matching used by the test compares with expected struct. - Removed branch statement.
1 parent ff36b64 commit 552b4c4

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/snapshy.ex

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ defmodule Snapshy do
179179

180180
defp serialize(value) do
181181
value
182-
|> Inspect.Algebra.to_doc(%Inspect.Opts{
183-
limit: :infinity,
184-
printable_limit: :infinity,
185-
pretty: true
186-
})
182+
|> Inspect.Algebra.to_doc(inspect_options())
187183
|> Inspect.Algebra.group()
188184
|> Inspect.Algebra.format(80)
189185
|> Enum.join()
@@ -195,6 +191,22 @@ defmodule Snapshy do
195191
term
196192
end
197193

194+
defp inspect_options do
195+
opts = default_inspect_options()
196+
|> Keyword.merge(Application.get_env(:snapshy, :serialize_inspect_options, []))
197+
198+
struct(Inspect.Opts, opts)
199+
end
200+
201+
defp default_inspect_options do
202+
[
203+
limit: :infinity,
204+
printable_limit: :infinity,
205+
pretty: true,
206+
structs: false
207+
]
208+
end
209+
198210
#############################################################################
199211
# Filename calculation #
200212
#############################################################################

test/unit/snapshy_test.exs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,59 @@ defmodule SnapshyTest do
2424
match_snapshot(%Struct{key: "different_value"})
2525
end
2626

27+
test "saved file can be evaluated for opaque structs when serialize structs option is false (default)", %{test: test_name} do
28+
snap_filename = snap_file_for_test(test_name)
29+
30+
assert File.exists?(snap_filename) == false
31+
32+
original_serialized_inspect_options = Application.get_env(:snapshy, :serialize_inspect_options, [])
33+
34+
opaque_struct = %Struct{key: Version.parse!("1.2.3")}
35+
36+
match_snapshot(opaque_struct)
37+
38+
assert {:ok, serialized_value} = File.read(snap_filename)
39+
40+
assert {^opaque_struct, []} = Code.eval_string(serialized_value, [], __ENV__)
41+
42+
assert File.rm(snap_filename)
43+
44+
Application.put_env(:snapshy, :serialize_inspect_options, original_serialized_inspect_options)
45+
end
46+
47+
test "saved file cannot be evaluated for opaque structs when serialize structs option is true", %{test: test_name} do
48+
snap_filename = snap_file_for_test(test_name)
49+
50+
assert File.exists?(snap_filename) == false
51+
52+
original_serialized_inspect_options = Application.get_env(:snapshy, :serialize_inspect_options, [])
53+
54+
Application.put_env(:snapshy, :serialize_inspect_options, [structs: true])
55+
56+
opaque_struct = %Struct{key: Version.parse!("1.2.3")}
57+
58+
match_snapshot(opaque_struct)
59+
60+
assert {:ok, serialized_value} = File.read(snap_filename)
61+
62+
assert_raise TokenMissingError, fn ->
63+
assert {^opaque_struct, []} = Code.eval_string(serialized_value, [], __ENV__)
64+
end
65+
66+
assert File.rm(snap_filename)
67+
68+
Application.put_env(:snapshy, :serialize_inspect_options, original_serialized_inspect_options)
69+
end
70+
71+
defp snap_file_for_test(test_name) do
72+
[
73+
"test/__snapshots__/unit/snapshy_test/",
74+
Atom.to_string(test_name) |> String.replace(" ", "_"),
75+
".snap"
76+
]
77+
|> Enum.join()
78+
end
79+
2780
describe "test_snapshot" do
2881
setup do
2982
[key: :value]

0 commit comments

Comments
 (0)