Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup #114

Merged
merged 5 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[weakdeps]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"

Expand Down
5 changes: 5 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Documenter = "0.27"
Literate = "2"
StaticArrays = "1"
6 changes: 3 additions & 3 deletions ext/AccessorsStructArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ using Accessors
using StructArrays

# set: all eltypes
Accessors.set(x::StructArray, o::Accessors.PropertyLens, v) = set(x, o ∘ StructArrays.components, v)
Accessors.set(x::StructArray, o::PropertyLens, v) = set(x, o ∘ StructArrays.components, v)

# insert, delete: only (named)tuples
Accessors.insert(x::StructArray{<:Union{Tuple, NamedTuple}}, o::Accessors.PropertyLens, v) = insert(x, o ∘ StructArrays.components, v)
Accessors.delete(x::StructArray{<:Union{Tuple, NamedTuple}}, o::Accessors.PropertyLens) = delete(x, o ∘ StructArrays.components)
Accessors.insert(x::StructArray{<:Union{Tuple, NamedTuple}}, o::PropertyLens, v) = insert(x, o ∘ StructArrays.components, v)
Accessors.delete(x::StructArray{<:Union{Tuple, NamedTuple}}, o::PropertyLens) = delete(x, o ∘ StructArrays.components)

# (named)tuple eltypes: only component arrays themselves are needed in the constructor
# can change component number/names
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
[compat]
Aqua = "0.6"
AxisKeys = "0.2"
BenchmarkTools = "1.3"
ConstructionBase = "1.5"
ConstructionBaseExtras = "0.1"
BenchmarkTools = "1.3"
IntervalSets = "0.7"
InverseFunctions = "0.1.5"
MacroTools = "0.4.4, 0.5"
Expand Down
5 changes: 1 addition & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module TestAccessors
import PerformanceTestTools
import Accessors
import Aqua
Aqua.test_all(Accessors,
stale_deps=false,
project_toml_formatting=false,
)
VERSION ≥ v"1.9-" && Aqua.test_all(Accessors)
PerformanceTestTools.@include("perf.jl")

include("test_examples.jl")
Expand Down
20 changes: 10 additions & 10 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,21 @@ end

@testset "IndexLens" begin
l = @optic _[]
@test l isa Accessors.IndexLens
@test l isa IndexLens
x = randn()
obj = Ref(x)
@test l(obj) == x

l = @optic _[][]
@test l.outer isa Accessors.IndexLens
@test l.inner isa Accessors.IndexLens
@test l.outer isa IndexLens
@test l.inner isa IndexLens
inner = Ref(x)
obj = Base.RefValue{typeof(inner)}(inner)
@test l(obj) == x

obj = (1,2,3)
l = @optic _[1]
@test l isa Accessors.IndexLens
@test l isa IndexLens
@test l(obj) == 1
@test set(obj, l, 6) == (6,2,3)

Expand All @@ -257,7 +257,7 @@ end

nt = (a=1, b=2, c=3)
l = @optic _[(:a, :c)]
@test l isa Accessors.IndexLens
@test l isa IndexLens
VERSION >= v"1.7" && @test l(nt) === (a=1, c=3)
@test set(nt, l, ('1', '2')) === (a='1', b=2, c='2')
@test set(nt, l, (c='2', a='1')) === (a='1', b=2, c='2')
Expand Down Expand Up @@ -588,14 +588,14 @@ end
@test strip(string(@doc(my_x))) == "Documentation for my_x"
@test (@set my_x(s) = 456) === MyStruct(456)
@test (@set +s = 456) === MyStruct((a=5-456,))
Accessors.test_getset_laws(my_x, s, 456, "1")
Accessors.test_getset_laws(+, s, 456, 1.0)
test_getset_laws(my_x, s, 456, "1")
test_getset_laws(+, s, 456, 1.0)

s = MyStruct((1, 2.0))
Accessors.test_getset_laws(Int, s, 1, 2)
Accessors.test_getset_laws(Float64, s, 1., 2.)
test_getset_laws(Int, s, 1, 2)
test_getset_laws(Float64, s, 1., 2.)

Accessors.test_getset_laws(MyStruct(2), MyStruct(1), 1, 2)
test_getset_laws(MyStruct(2), MyStruct(1), 1, 2)
end

end
11 changes: 6 additions & 5 deletions test/test_extensions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestExtensions
using Test
using Accessors
using Accessors: test_getset_laws
using AxisKeys
using IntervalSets
using StaticArrays, StaticNumbers
Expand Down Expand Up @@ -54,16 +55,16 @@ VERSION >= v"1.9-" && @testset "IntervalSets" begin
@test Interval{:open, :closed}(-2, 5) === @set leftendpoint(int) = -2
@test Interval{:open, :closed}(1, 2) === @set rightendpoint(int) = 2
@test Interval{:closed, :closed}(1, 5) === @set first(closedendpoints(int)) = true
Accessors.test_getset_laws(endpoints, int, (10, 11), (-3, 2))
Accessors.test_getset_laws(closedendpoints, int, (true, true), (true, false))
Accessors.test_getset_laws(leftendpoint, int, 2, 3)
Accessors.test_getset_laws(rightendpoint, int, 2, 3)
test_getset_laws(endpoints, int, (10, 11), (-3, 2))
test_getset_laws(closedendpoints, int, (true, true), (true, false))
test_getset_laws(leftendpoint, int, 2, 3)
test_getset_laws(rightendpoint, int, 2, 3)

@test 1 === @set 2 |> mod(_, 0..3) = 1
@test 31 === @set 32 |> mod(_, 0..3) = 1
@test 2 === @set 2 |> mod(_, 20..23) = 20
@test 33 === @set 32 |> mod(_, 20..23) = 21
Accessors.test_getset_laws(@optic(mod(_, 5..8)), 20, 6, 5)
test_getset_laws(@optic(mod(_, 5..8)), 20, 6, 5)
end

@testset "StaticArrays" begin
Expand Down
22 changes: 11 additions & 11 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ end
@test set("абв", @optic(first(_, 2)), "xж") == "xжв"
@test_throws DimensionMismatch set("абв", @optic(first(_, 2)), "x")

Accessors.test_getset_laws(first, obj, 123, "456")
Accessors.test_getset_laws(first, "abc", 'x', ' ')
test_getset_laws(first, obj, 123, "456")
test_getset_laws(first, "abc", 'x', ' ')
end

@testset "last" begin
Expand All @@ -65,17 +65,17 @@ end
@test set("абв", @optic(last(_, 2)), "xж") == "аxж"
@test_throws DimensionMismatch set("абв", @optic(last(_, 2)), "x")

Accessors.test_getset_laws(last, obj, 123, "456")
Accessors.test_getset_laws(last, "abc", 'x', ' ')
test_getset_laws(last, obj, 123, "456")
test_getset_laws(last, "abc", 'x', ' ')
end

@testset "front, tail" begin
obj = (1, 2.0, '3')
@test (@set Base.front(obj) = ("5", 6)) === ("5", 6, '3')
@test set(obj, Base.tail, ("5", 6)) === (1, "5", 6)

Accessors.test_getset_laws(Base.front, obj, (), ("456", 7))
Accessors.test_getset_laws(Base.tail, obj, (123,), ("456", 7))
test_getset_laws(Base.front, obj, (), ("456", 7))
test_getset_laws(Base.tail, obj, (123,), ("456", 7))
end

@testset "change types" begin
Expand Down Expand Up @@ -232,8 +232,8 @@ end
@test @inferred(modify(x -> -2x, "3", @optic parse(Int, _))) == "-6"
@test_throws ErrorException modify(log10, "100", @optic parse(Int, _))
@test modify(log10, "100", @optic parse(Float64, _)) == "2.0"
Accessors.test_getset_laws(@optic(parse(Int, _)), "3", -10, 123)
Accessors.test_getset_laws(@optic(parse(Float64, _)), "3.0", -10., 123.)
test_getset_laws(@optic(parse(Int, _)), "3", -10, 123)
test_getset_laws(@optic(parse(Float64, _)), "3.0", -10., 123.)
end

# setting inverse
Expand Down Expand Up @@ -266,15 +266,15 @@ end

l = @optic DateTime(_, dateformat"yyyy_mm_dd")
@test @inferred(set("2020_03_04", month ∘ l, 10)) == "2020_10_04"
Accessors.test_getset_laws(month ∘ l, "2020_03_04", 10, 11)
test_getset_laws(month ∘ l, "2020_03_04", 10, 11)

l = @optic Date(_, dateformat"yyyy/mm/dd")
@test set("2020/03/04", day ∘ l, 10) == "2020/03/10"
Accessors.test_getset_laws(day ∘ l, "2020/03/04", 10, 11)
test_getset_laws(day ∘ l, "2020/03/04", 10, 11)
@test_throws ArgumentError set("2020_03_04", month ∘ l, 10)

l = @optic Time(_, dateformat"HH:MM")
Accessors.test_getset_laws(hour ∘ l, "12:34", 10, 11)
test_getset_laws(hour ∘ l, "12:34", 10, 11)
end

@testset "strings" begin
Expand Down
3 changes: 2 additions & 1 deletion test/test_getsetall.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestGetSetAll
using Test
using Accessors
using Accessors: test_getsetall_laws
using StaticNumbers
using StaticArrays

Expand Down Expand Up @@ -145,7 +146,7 @@ end
(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)),
]
Accessors.test_getsetall_laws(optic, obj, vals1, vals2)
test_getsetall_laws(optic, obj, vals1, vals2)
end
end

Expand Down
Loading