From 00422b069e9781daf9fc97e34d738d5312b9b8c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20J=C3=A4rvinen?= Date: Sat, 20 Sep 2025 16:50:58 +0300 Subject: [PATCH 1/2] fix #139 --- src/utils/chemspecies.jl | 19 +++++++++++++++++-- test/species.jl | 10 ++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/utils/chemspecies.jl b/src/utils/chemspecies.jl index 27200fd..9956294 100644 --- a/src/utils/chemspecies.jl +++ b/src/utils/chemspecies.jl @@ -139,9 +139,24 @@ end ChemicalSpecies(sym::ChemicalSpecies) = sym -==(a::ChemicalSpecies, sym::Symbol) = - ((a == ChemicalSpecies(sym)) && (Symbol(a) == sym)) +function ==(a::ChemicalSpecies, sym::Symbol) + # We need to catch ArgumentError here as e.g. + # ChemicalSpecies(:H) == :not_atom + # does throw an error but should return false + local tmp + try + tmp = ChemicalSpecies(sym) + catch e + if e isa ArgumentError + return false + else + rethrow() + end + end + return (a == tmp) && (Symbol(a) == sym) +end +==(sym::Symbol, a::ChemicalSpecies) = ==(a, sym) function ==(cs1::ChemicalSpecies, cs2::ChemicalSpecies) if cs1.atomic_number != cs2.atomic_number diff --git a/test/species.jl b/test/species.jl index b2fa8fb..d97293b 100644 --- a/test/species.jl +++ b/test/species.jl @@ -36,6 +36,15 @@ end @test ChemicalSpecies(:U238) == :U238 @test ChemicalSpecies(:Cl35) == :Cl35 @test ChemicalSpecies(:He3) == :He3 +@test ChemicalSpecies(:H) != :not_atom +@test :H == ChemicalSpecies(:H) +@test :D == ChemicalSpecies(:D) +@test :T == ChemicalSpecies(:T) +@test :X == ChemicalSpecies(:X) +@test :Fe56 == ChemicalSpecies(:Fe56) +@test :Al27 == ChemicalSpecies(:Al27) +@test :He4 == ChemicalSpecies(:He4) +@test :not_atom != ChemicalSpecies(:O) @test_throws ArgumentError ChemicalSpecies(:C; atom_name=:MyLongC) @test_throws ArgumentError ChemicalSpecies(:U2389) @@ -66,6 +75,7 @@ end tmp = ChemicalSpecies(:C12; atom_name=:MyC) @test atom_name(tmp) != atomic_symbol(tmp) +@test atomic_symbol(tmp) != atom_name(tmp) @test mass(ChemicalSpecies(:C)) != mass(ChemicalSpecies(:C12)) @test mass(ChemicalSpecies(:C12)) != mass(ChemicalSpecies(:C13)) From d74783f5d74ced80fb62c4bcc67c23a207f57cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20J=C3=A4rvinen?= Date: Thu, 25 Sep 2025 13:05:19 +0300 Subject: [PATCH 2/2] remove local and use return instead --- src/utils/chemspecies.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/chemspecies.jl b/src/utils/chemspecies.jl index 9956294..0320d92 100644 --- a/src/utils/chemspecies.jl +++ b/src/utils/chemspecies.jl @@ -143,9 +143,8 @@ function ==(a::ChemicalSpecies, sym::Symbol) # We need to catch ArgumentError here as e.g. # ChemicalSpecies(:H) == :not_atom # does throw an error but should return false - local tmp - try - tmp = ChemicalSpecies(sym) + tmp = try + ChemicalSpecies(sym) catch e if e isa ArgumentError return false