Skip to content

Commit

Permalink
test: tweak coverage ignores
Browse files Browse the repository at this point in the history
Also:
- Alphabetized functions
- Added tests for map[:key] syntax
  • Loading branch information
hpopp committed Aug 31, 2024
1 parent 1bfcf24 commit 19fdf5a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/accessible.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
11 changes: 11 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/accessible_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit 19fdf5a

Please sign in to comment.