Skip to content

Commit 29d70e2

Browse files
authored
parse query on Select object creation (#61)
* parse query on Select object creation * fix Select test: the macro sel throws a `LoadError` before the `ArgumentError`. * remove unnecessary object creation in test
1 parent a98a59d commit 29d70e2

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/select.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,16 @@ allselector(el) = true
436436

437437
# Acts as a function when used within typical julia filtering functions
438438
# by converting a string selection into a query call
439-
struct Select <: Function
440-
sel::String
439+
struct Select{Q} <: Function
440+
query_string::String
441+
query::Q
441442
end
442-
443-
(s::Select)(at) = apply_query(parse_query(s.sel), at)
443+
function Select(query_string::AbstractString)
444+
query = parse_query(query_string)
445+
return Select(query_string, query)
446+
end
447+
(s::Select)(at) = apply_query(s.query, at)
448+
Base.show(io::IO, ::MIME"text/plain", s::Select) = print(io, """Select("$(s.query_string)")""")
444449

445450
"String selection syntax."
446451
macro sel_str(str)

test/runtests.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,14 @@ end
937937
@test length(collectmodels(struc, sel"model 1")) == 1
938938
@test length(collectmodels(struc, sel"model 2")) == 0
939939

940-
@test_throws ArgumentError collectatoms(struc, sel"abc") # Invalid selection syntax
941-
@test_throws ArgumentError collectatoms(struc, sel"index = A") # Invalid value type
942-
@test_throws ArgumentError collectatoms(struc, sel"resnum C")
940+
@test_throws ArgumentError collectatoms(struc, BioStructures.Select("abc")) # Invalid selection syntax
941+
@test_throws ArgumentError collectatoms(struc, BioStructures.Select("index = A")) # Invalid value type
942+
@test_throws ArgumentError collectatoms(struc, BioStructures.Select("resnum C"))
943+
944+
# test show method for @sel_str
945+
buff = IOBuffer()
946+
show(buff, MIME"text/plain"(), sel"name CA and resnum 1")
947+
@test String(take!(buff)) == """Select("name CA and resnum 1")"""
943948
end
944949

945950
@testset "PDB reading" begin

0 commit comments

Comments
 (0)