diff --git a/test/show_methods.jl b/test/show_methods.jl index 07bfcced..cd30bfe9 100644 --- a/test/show_methods.jl +++ b/test/show_methods.jl @@ -1,36 +1,30 @@ @testitem "show methods" begin - function test_show( - x, s::String; + struct TestShowString + parsed_show::String + end + Base.show(io::IO, x::TestShowString) = print(io, x.parsed_show) + + function Base.isequal( + x::TestShowString, + y::TestShowString; f64 = (x1,x2) -> isapprox(x1,x2,rtol=1e-3), i64 = (x1,x2) -> x1 == x2, - vector_simplify = true, - repl = Dict(), ) match(f,x1,x2) = begin if !f(x1,x2) - @warn """ + throw(AssertionError(""" - show method test failed with $x1 ($(typeof(x1))) == $x2 ($(typeof(x2)))") + show method equality failed with $x1 ($(typeof(x1))) == $x2 ($(typeof(x2)))") - """ + """)) return false end return true end - buff = IOBuffer() - show(buff, MIME"text/plain"(), x) - ss = String(take!(buff)) + s = x.parsed_show + ss = y.parsed_show # Custom substitutions - s = replace(s, repl...) - ss = replace(ss, repl...) - # add spaces between digits and other characters (except dots), to interpret them as numbers - s = replace(s, r"(?<=\d)(?=[^\d.])|(?<=[^\d.])(?=\d)" => s" ") - ss = replace(ss, r"(?<=\d)(?=[^\d.])|(?<=[^\d.])(?=\d)" => s" ") - if vector_simplify # keep only first and last array elements - s = replace(s, r"\[ (\S+).* (\S+)\ ]" => s"[ \1 \2 ]") - ss = replace(ss, r"\[ (\S+).* (\S+)\ ]" => s"[ \1 \2 ]") - end sfields = split(s) ssfields = split(ss) all_match = true @@ -54,6 +48,36 @@ end return all_match end + Base.isequal(x::TestShowString, y::String; kargs...) = isequal(x, parse_show(y); kargs...) + Base.isequal(x::String, y::TestShowString; kargs...) = isequal(parse_show(x), y; kargs...) + + import Base: == + ==(x::TestShowString, y::TestShowString; kargs...) = isequal(x, y; kargs...) + ==(x::TestShowString, y::String; kargs...) = isequal(x, y; kargs...) + ==(x::String, y::TestShowString; kargs...) = isequal(x, y; kargs...) + + function parse_show(x; + vector_simplify = true, + repl = Dict(), + ) + buff = IOBuffer() + show(buff, MIME"text/plain"(), x) + parse_show(String(take!(buff)); vector_simplify, repl) + end + + function parse_show(x::String; + vector_simplify = true, + repl = Dict(), + ) + # Custom replacements + s = replace(x, repl...) + # add spaces between digits and other characters (except dots), to interpret them as numbers + s = replace(s, r"(?<=\d)(?=[^\d.])|(?<=[^\d.])(?=\d)" => s" ") + if vector_simplify # keep only first and last array elements + s = replace(s, r"\[ (\S+).* (\S+)\ ]" => s"[ \1 \2 ]") + end + return TestShowString(s) + end using ComplexMixtures using PDBTools: readPDB, select @@ -66,45 +90,39 @@ trajectory_file = "$data_dir/toy/cross.pdb" trajectory_format = "PDBTraj" - @test test_show( - Options(), - """ - -------------------------------------------------------------------------------- - Options - ComplexMixtures - -------------------------------------------------------------------------------- - Trajectory frames: - First frame to be considered: firstframe = 1 - Last frame to be considered (-1 is last): lastframe = -1 - Stride: stride = 1 - - Bulk region, cutoff, and histogram: - Bin step of histogram: binstep = 0.02 - Bulk range: >= 10.0 - (dbulk = 10.0, cutoff = 10.0, usecutoff = false) - - Computation details: - Reference atom for random rotations: irefatom = -1 - Number of random samples per frame: n_random_samples = 10 - Linked cell partition: lcell = 1 - Force garbage collection: GC = true - Memory threshold for GC: GC_threshold = 0.3 - Seed for random number generator: 321 - Use stable random number generator: StableRNG = false - Number of threads to use (0 is all): nthreads = 0 - Silent output: false - -------------------------------------------------------------------------------- + @test parse_show(Options()) == """ + -------------------------------------------------------------------------------- + Options - ComplexMixtures + -------------------------------------------------------------------------------- + Trajectory frames: + First frame to be considered: firstframe = 1 + Last frame to be considered (- 1 is last): lastframe = - 1 + Stride: stride = 1 + + Bulk region, cutoff, and histogram: + Bin step of histogram: binstep = 0.02 + Bulk range: >= 10.0 + (dbulk = 10.0 , cutoff = 10.0 , usecutoff = false) + + Computation details: + Reference atom for random rotations: irefatom = - 1 + Number of random samples per frame: n_random_samples = 10 + Linked cell partition: lcell = 1 + Force garbage collection: GC = true + Memory threshold for GC: GC_threshold = 0.3 + Seed for random number generator: 321 + Use stable random number generator: StableRNG = false + Number of threads to use ( 0 is all): nthreads = 0 + Silent output: false + -------------------------------------------------------------------------------- """ - ) - @test test_show( - protein, - """ + @test parse_show(protein) == """ AtomSelection 1 atoms belonging to 1 molecule(s). Atoms per molecule: 1 Number of groups: 1 """ - ) nthreads = 1 lastframe = 2 @@ -120,11 +138,10 @@ ) R = coordination_number(trajectory_file, protein, water, options; trajectory_format, low_memory) - @test test_show( - R, + @test parse_show(R; repl = Dict(r"Version.*" => "Version")) == """ -------------------------------------------------------------------------------- - MDDF Overview - ComplexMixtures - Version 2.11.3 + MDDF Overview - ComplexMixtures - Version -------------------------------------------------------------------------------- Solvent properties: @@ -155,47 +172,36 @@ Long range RDF mean (expected 1.0): 0.0 ± 0.0 -------------------------------------------------------------------------------- - """, - repl = Dict(r"Version.*" => "Version") - ) + """ - @test test_show( - R.volume, + @test parse_show(R.volume) == """ Total volume: 27000.0 Bulk volume: 0.0 Domain volume: 0.0 Shell volumes: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] """ - ) - @test test_show( - R.density, + @test parse_show(R.density) == """ Density of solute: 3.7037037037037037e-5 Density of solvent: 0.00011111111111111112 Density of solvent in bulk: 0.0 """ - ) - @test test_show( - SoluteGroup(select(atoms, "protein and residue 2")), + @test parse_show(SoluteGroup(select(atoms, "protein and residue 2"))) == """ SoluteGroup defined by: atom_indices: [ 10 ] - 1 atoms """ - ) - @test test_show( - SolventGroup(select(atoms, "protein and residue 2")), + @test parse_show(SolventGroup(select(atoms, "protein and residue 2"))) == """ SolventGroup defined by: atom_indices: [ 10 ] - 1 atoms """ - ) - @test test_show( - Trajectory(trajectory_file, protein, water), + @test parse_show(Trajectory(trajectory_file, protein, water)) == """ Trajectory in PDB format with: 2 frames. @@ -203,6 +209,5 @@ Solvent contains 9 atoms. Unit cell in current frame: [ 30.00 0 0; 0 30.00 0; 0 0 30.00 ] """ - ) end \ No newline at end of file