|
1 | 1 | module TestOptics |
2 | 2 |
|
3 | 3 | using Accessors |
| 4 | +using Accessors: mapproperties |
4 | 5 | using Test |
| 6 | +import ConstructionBase |
| 7 | + |
| 8 | +@testset "mapproperties" begin |
| 9 | + res = @inferred mapproperties(x->2x, (a=1, b=2)) |
| 10 | + @test res === (a=2, b=4) |
| 11 | + @test NamedTuple() === @inferred mapproperties(cos, NamedTuple()) |
| 12 | + struct S{A,B} |
| 13 | + a::A |
| 14 | + b::B |
| 15 | + end |
| 16 | + res = @inferred mapproperties(x->2x, S(1, 2.0)) |
| 17 | + @test res === S(2, 4.0) |
| 18 | + |
| 19 | + # overloading |
| 20 | + struct AB |
| 21 | + a::Int |
| 22 | + b::Int |
| 23 | + _checksum::UInt |
| 24 | + AB(a,b) = new(a,b,hash(a,hash(b))) |
| 25 | + end |
| 26 | + |
| 27 | + ConstructionBase.getproperties(o::AB) = (a=o.a, b=o.b) |
| 28 | + ConstructionBase.setproperties(o::AB, patch::NamedTuple) = AB(patch.a, patch.b) |
| 29 | + ab = AB(1,2) |
| 30 | + ab2 = @inferred mapproperties(x -> 2x, ab) |
| 31 | + @test ab2 === AB(2,4) |
| 32 | +end |
5 | 33 |
|
6 | 34 | @testset "Properties" begin |
7 | 35 | pt = (x=1, y=2, z=3) |
8 | 36 | @test (x=0, y=1, z=2) === @set pt |> Properties() -= 1 |
| 37 | + @inferred modify(x->x-1, pt, Properties()) |
| 38 | + |
| 39 | + # custom struct |
| 40 | + struct Point{X,Y,Z} |
| 41 | + x::X; y::Y; z::Z |
| 42 | + end |
| 43 | + pt = Point(1f0, 2e0, 3) |
| 44 | + pt2 = @inferred modify(x->2x, pt, Properties()) |
| 45 | + @test pt2 === Point(2f0, 4e0, 6) |
9 | 46 | end |
10 | 47 |
|
11 | 48 | @testset "Elements" begin |
|
0 commit comments