From 636974e21bc04c74b5830d71b09277cd11fe0e3a Mon Sep 17 00:00:00 2001 From: Leandro Martinez Date: Sun, 22 Dec 2024 17:49:09 -0300 Subject: [PATCH] use ShowMethodTesting for testing show functions --- Project.toml | 4 +- test/show_methods.jl | 100 ++++--------------------------------------- 2 files changed, 12 insertions(+), 92 deletions(-) diff --git a/Project.toml b/Project.toml index 4cf97753..838f525f 100644 --- a/Project.toml +++ b/Project.toml @@ -48,6 +48,7 @@ Printf = "1.9" ProgressMeter = "1.10.1" Random = "1.9" RandomNumbers = "1.5" +ShowMethodTesting = "1.0.0" StableRNGs = "1" StaticArrays = "1.6" Statistics = "1.9" @@ -66,8 +67,9 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" PDBTools = "e29189f1-7114-4dbd-93d0-c5673a921a58" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +ShowMethodTesting = "aed2ae9c-e0a3-4a50-8068-bdeec279239a" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" [targets] -test = ["Test", "PDBTools", "StaticArrays", "CellListMap", "BenchmarkTools", "TestItemRunner", "Aqua", "DelimitedFiles", "Documenter", "Random", "Plots"] +test = ["Test", "PDBTools", "StaticArrays", "CellListMap", "BenchmarkTools", "TestItemRunner", "Aqua", "DelimitedFiles", "Documenter", "Random", "Plots", "ShowMethodTesting"] diff --git a/test/show_methods.jl b/test/show_methods.jl index 64d3c913..9cd022bc 100644 --- a/test/show_methods.jl +++ b/test/show_methods.jl @@ -1,88 +1,6 @@ @testitem "show methods" begin - 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, - path = (x1,x2) -> last(splitpath(x1)) == last(splitpath(x2)), - assertion_error = true, - ) - match(f,x1,x2) = begin - if !f(x1,x2) - if assertion_error - throw(AssertionError(""" - - show method equality failed with $x1 ($(typeof(x1))) == $x2 ($(typeof(x2)))") - - """)) - end - return false - end - return true - end - s = x.parsed_show - ss = y.parsed_show - # Custom substitutions - sfields = split(s) - ssfields = split(ss) - all_match = true - for (f1, f2) in zip(sfields, ssfields) - !all_match && break - if ispath(f2) || ispath(f1) # only compares the last entry for paths - all_match = match(path, last(splitpath(f1)), last(splitpath(f2))) - continue - end - value = tryparse(Int, f1) # test if f1 can be interpreted as an integer - if !isnothing(value) - all_match = match(i64, value, tryparse(Int, f2)) - continue - end - value = tryparse(Float64, f1) # test if f1 can be interpreted as a float - if !isnothing(value) - all_match = match(f64, value, tryparse(Float64,f2)) - continue - end - all_match = match(isequal, f1, f2) - 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 ShowMethodTesting using ComplexMixtures using PDBTools: readPDB, select using ComplexMixtures.Testing: data_dir @@ -94,7 +12,7 @@ trajectory_file = "$data_dir/toy/cross.pdb" trajectory_format = "PDBTraj" - @test parse_show(Options()) == """ + @test parse_show(Options()) ≈ """ -------------------------------------------------------------------------------- Options - ComplexMixtures -------------------------------------------------------------------------------- @@ -121,7 +39,7 @@ -------------------------------------------------------------------------------- """ - @test parse_show(protein) == """ + @test parse_show(protein) ≈ """ AtomSelection 1 atoms belonging to 1 molecule(s). Atoms per molecule: 1 @@ -142,7 +60,7 @@ ) R = coordination_number(trajectory_file, protein, water, options; trajectory_format, low_memory) - @test parse_show(R; repl = Dict(r"Version.*" => "Version")) == + @test parse_show(R; repl = Dict(r"Version.*" => "Version")) ≈ """ -------------------------------------------------------------------------------- MDDF Overview - ComplexMixtures - Version @@ -178,7 +96,7 @@ -------------------------------------------------------------------------------- """ - @test parse_show(R.volume) == + @test parse_show(R.volume) ≈ """ Total volume: 27000.0 Bulk volume: 0.0 @@ -186,26 +104,26 @@ 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 parse_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 parse_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 parse_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 parse_show(Trajectory(trajectory_file, protein, water)) == + @test parse_show(Trajectory(trajectory_file, protein, water)) ≈ """ Trajectory in PDB format with: 2 frames.