Skip to content

Commit

Permalink
generalize test_modify_law
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Feb 24, 2024
1 parent d02dfcc commit 9de2a81
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions ext/AccessorsTestExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function Accessors.test_getset_laws(lens, obj, val1, val2; cmp=(==))
@test cmp(obj12, obj2)
end

function Accessors.test_modify_law(f, lens, obj)
function Accessors.test_modify_law(f, lens, obj; cmp=(==))
obj_modify = modify(f, obj, lens)
old_val = lens(obj)
val = f(old_val)
obj_setfget = set(obj, lens, val)
@test obj_modify == obj_setfget
old_vals = getall(obj, lens)
vals = map(f, old_vals)
setall(obj, lens, vals)
@test cmp(obj_modify, obj_setfget)
end

function Accessors.test_insertdelete_laws(lens, obj, val; cmp=(==))
Expand Down
9 changes: 5 additions & 4 deletions test/test_getsetall.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestGetSetAll
using Test
using Accessors
using Accessors: test_getsetall_laws
using Accessors: test_getsetall_laws, test_modify_law
using StaticNumbers
using StaticArrays

Expand Down Expand Up @@ -155,14 +155,15 @@ end
@test_broken ([1, 2], [3.0, 4.0, 5.0], ("6",)) == @inferred setall(obj, @optic(_ |> Elements() |> Elements()), (1, 2, 3., 4., 5., "6"))
end

@testset "getall/setall consistency" begin
@testset "getall-setall laws" begin
for (optic, obj, vals1, vals2) in [
(Elements(), (1, "2"), (2, 3), (4, 5)),
(Properties(), (a=1, b="2"), (2, 3), (4, 5)),
(Elements(), (1, false), (2, 3), (4, 5)),
(Properties(), (a=1, b=false), (2, 3), (4, 5)),
(If(x -> x isa Number) Properties(), (a=1, b="2"), (2,), (4,)),
(@optic(_.b |> Elements() |> Properties() |> _ * 3), (a=1, b=((c=3, d=4), (c=5, d=6))), 1:4, (-9, -12, -15, -18)),
]
test_getsetall_laws(optic, obj, vals1, vals2)
test_modify_law(x -> x + 1, optic, obj)
end
end

Expand Down

0 comments on commit 9de2a81

Please sign in to comment.