Skip to content

Commit

Permalink
stripe identity off ComposedOptic when test equality
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxd3 committed Apr 8, 2024
1 parent 9d35b63 commit 895aecd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,23 @@ end

Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getoptic(vn)), h)
function Base.:(==)(x::VarName, y::VarName)
return getsym(x) == getsym(y) && getoptic(x) == getoptic(y)
if getsym(x) !== getsym(y)
return false
end
o1, o2 = map((getoptic(x), getoptic(y))) do o
if o isa ComposedOptic
if o.inner == identity
return o.outer
elseif o.outer == identity
return o.inner
else
return o
end
else
return o
end
end
return o1 == o2
end

function Base.show(io::IO, vn::VarName{sym,T}) where {sym,T}
Expand Down
8 changes: 8 additions & 0 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ end
@test test_equal(@varname(x.a[1:end, end][:], true), @varname(x.a[1:3,2][1:3]))
end

@testset "equality" begin
vn1 = VarName{:a}(IndexLens(1) identity)
vn2 = VarName{:a}(IndexLens(1))
vn3 = VarName{:a}(identity IndexLens(1))
@test vn1 == vn2
@test vn1 == vn3
end

@testset "get & set" begin
x = (a = [1.0 2.0; 3.0 4.0; 5.0 6.0], b = 1.0);
@test get(x, @varname(a[1, 2])) == 2.0
Expand Down

0 comments on commit 895aecd

Please sign in to comment.