Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BackendSelection"
uuid = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.1.5"
version = "0.1.6"

[compat]
julia = "1.10"
5 changes: 5 additions & 0 deletions src/backend_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ function generate_backend_type_expr(type::Symbol)
backend_string(::$type{Back}) where {Back} = string(Back)
parameters(backend::$type) = getfield(backend, :kwargs)

# NamedTuple interface functions.
Base.getproperty(backend::$type, name::Symbol) = parameters(backend)[name]
Base.propertynames(backend::$type) = propertynames(parameters(backend))
Base.getindex(backend::$type, name::Symbol) = parameters(backend)[name]
function Base.get(backend::$type, name::Symbol, default)
get(parameters(backend), name, default)
end

function Base.show(io::IO, backend::$type)
return print(
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a"

[compat]
Aqua = "0.8"
Expand Down
22 changes: 16 additions & 6 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
using BackendSelection: BackendSelection, @Algorithm_str, @Backend_str, Algorithm, Backend
using Test: @test, @testset
using TestExtras: @constinferred

@testset "BackendSelection" begin
for type in (Algorithm, Backend)
@testset "$type" begin
# Note: constructing from a string is not type stable.
@test type("backend") isa type{:backend}
@test type(:backend) isa type{:backend}
backend = type("backend"; x=2, y=3)
@test @constinferred(type(:backend)) isa type{:backend}
backend = @constinferred type(:backend; x=2, y=3)
@test backend === type("backend"; x=2, y=3)
@test backend isa type{:backend}
@test backend.x == 2
@test backend.y == 3
@test propertynames(backend) == (:x, :y)
@test @constinferred(getproperty(backend, :x)) == 2
@test @constinferred(getproperty(backend, :y)) == 3
@test @constinferred(backend[:x]) == 2
@test @constinferred(backend[:y]) == 3
@test @constinferred(get(backend, :x, "default")) == 2
@test @constinferred(get(backend, :y, "default")) == 3
@test @constinferred(get(backend, :z, "default")) == "default"
@test @constinferred(propertynames(backend)) == (:x, :y)
(; x, y) = backend
@test x == 2
@test y == 3
@test BackendSelection.parameters(backend) === (; x=2, y=3)
@test @constinferred(BackendSelection.parameters(backend)) === (; x=2, y=3)
end
end
# Macro syntax.
@test Algorithm"backend"(; x=2, y=3) === Algorithm("backend"; x=2, y=3)
@test Backend"backend"(; x=2, y=3) === Backend("backend"; x=2, y=3)
@test @constinferred(Algorithm"backend"(; x=2, y=3)) === Algorithm("backend"; x=2, y=3)
@test @constinferred(Backend"backend"(; x=2, y=3)) === Backend("backend"; x=2, y=3)
@test isnothing(show(Algorithm("")))
end
Loading