From 19fdf5a69b45d4d3ee8a19a81db989bf6013c04a Mon Sep 17 00:00:00 2001 From: Henry Popp Date: Sat, 31 Aug 2024 13:38:39 -0500 Subject: [PATCH] test: tweak coverage ignores Also: - Alphabetized functions - Added tests for map[:key] syntax --- lib/accessible.ex | 32 ++++++++++++++++---------------- mix.exs | 11 +++++++++++ test/accessible_test.exs | 10 ++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/lib/accessible.ex b/lib/accessible.ex index 9ff2844..4984c6b 100644 --- a/lib/accessible.ex +++ b/lib/accessible.ex @@ -5,6 +5,10 @@ defmodule Accessible do quote location: :keep do @behaviour Access + def delete(struct, key) do + put(struct, key, struct(__MODULE__)[key]) + end + @impl Access defdelegate fetch(struct, key), to: Map @@ -15,18 +19,6 @@ defmodule Accessible do end end - def put(struct, key, val) do - if Map.has_key?(struct, key) do - Map.put(struct, key, val) - else - struct - end - end - - def delete(struct, key) do - put(struct, key, struct(__MODULE__)[key]) - end - @impl Access def get_and_update(struct, key, fun) when is_function(fun, 1) do current = get(struct, key) @@ -50,12 +42,20 @@ defmodule Accessible do {val, updated} end - defoverridable fetch: 2, + def put(struct, key, val) do + if Map.has_key?(struct, key) do + Map.put(struct, key, val) + else + struct + end + end + + defoverridable delete: 2, + fetch: 2, get: 3, - put: 3, - delete: 2, get_and_update: 3, - pop: 3 + pop: 3, + put: 3 end end end diff --git a/mix.exs b/mix.exs index 6f2b47f..6629203 100644 --- a/mix.exs +++ b/mix.exs @@ -17,6 +17,7 @@ defmodule Accessible.Mixfile do package: package(), source_url: "https://github.com/codedge-llc/accessible", start_permanent: Mix.env() == :prod, + test_coverage: test_coverage(), version: @version ] end @@ -67,4 +68,14 @@ defmodule Accessible.Mixfile do maintainers: ["Henry Popp"] ] end + + defp test_coverage do + [ + ignore_modules: [ + Accessible.TestModule, + Accessible.TestModuleEnforceKeys + ], + summary: [threshold: 70] + ] + end end diff --git a/test/accessible_test.exs b/test/accessible_test.exs index ad487e9..6a9b942 100644 --- a/test/accessible_test.exs +++ b/test/accessible_test.exs @@ -5,6 +5,10 @@ defmodule AccessibleTest do alias Accessible.{TestModule, TestModuleEnforceKeys} describe "normal struct" do + test "enables map accessor syntax" do + assert %TestModule{}[:key_1] == 1234 + end + test "fetch/2 returns value for given key" do assert Access.fetch(%TestModule{}, :key_1) == {:ok, 1234} end @@ -23,6 +27,12 @@ defmodule AccessibleTest do end describe "struct with enforce keys" do + test "enables map accessor syntax" do + data = %TestModuleEnforceKeys{key_1: :example, key_2: 1234} + assert data[:key_1] == :example + assert data[:key_2] == 1234 + end + test "fetch/2 returns value for given key" do data = %TestModuleEnforceKeys{key_1: :example, key_2: 1234} assert Access.fetch(data, :key_1) == {:ok, :example}