From d27e04c0e21705fb3d5d14c53fc59d9b04cb36c4 Mon Sep 17 00:00:00 2001 From: Beforerr Date: Sat, 16 Nov 2024 16:20:23 -0800 Subject: [PATCH] feat: Add CustomParticle test: add Custom Particles test into runtest --- src/ChargedParticles.jl | 3 ++- src/custom_particles.jl | 36 ++++++++++++++++++++++++++++++++++++ test/custom_particles.jl | 15 +++++++++++++++ test/runtests.jl | 4 ++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/custom_particles.jl create mode 100644 test/custom_particles.jl diff --git a/src/ChargedParticles.jl b/src/ChargedParticles.jl index 0a8f491..75d29b6 100644 --- a/src/ChargedParticles.jl +++ b/src/ChargedParticles.jl @@ -13,9 +13,10 @@ include("./properties.jl") include("./aliases.jl") include("./utils.jl") include("./display.jl") +include("./custom_particles.jl") include("./_special_particles.jl") -export AbstractParticle, Particle +export AbstractParticle, Particle, CustomParticle export mass, charge, charge_number, atomic_number, mass_number, element, mass_energy export is_ion, is_chemical_element, is_default_isotope export electron, proton diff --git a/src/custom_particles.jl b/src/custom_particles.jl new file mode 100644 index 0000000..f378ae9 --- /dev/null +++ b/src/custom_particles.jl @@ -0,0 +1,36 @@ +""" + CustomParticle <: AbstractParticle + +A particle with user-defined mass and charge. + +# Fields +- `mass`: The mass of the particle (can be any numeric type or Unitful quantity) +- `charge_number`: Integer representing the charge state +- `symbol`: Optional symbol identifier (defaults to nothing) + +# Examples +```julia +CustomParticle(1.67e-27u"kg", 1) # A particle with proton-like mass and +1 charge +CustomParticle(2.0u"GeV", -1, :custom) # A custom particle with specified symbol +``` +""" +@kwdef struct CustomParticle <: AbstractParticle + mass::Unitful.Mass + charge_number::Int + symbol::Symbol + function CustomParticle(mass, charge_number, symbol=:custom) + new(mass, charge_number, symbol) + end +end + +function CustomParticle(mass_energy::Unitful.Energy, charge_number, symbol=:custom) + mass = uconvert(u"kg", mass_energy / Unitful.c^2) + CustomParticle(mass, charge_number, symbol) +end + +# Override mass method for CustomParticle +mass(p::CustomParticle) = p.mass +# CustomParticle doesn't have a mass number +mass_number(::CustomParticle) = nothing +# CustomParticle doesn't have an element +element(::CustomParticle) = nothing \ No newline at end of file diff --git a/test/custom_particles.jl b/test/custom_particles.jl new file mode 100644 index 0000000..db47445 --- /dev/null +++ b/test/custom_particles.jl @@ -0,0 +1,15 @@ +using Test, ChargedParticles, Unitful +using Unitful: q + +@testset "CustomParticle" begin + # Test basic construction + p1 = CustomParticle(1.67e-27u"kg", 1) + @test mass(p1) == 1.67e-27u"kg" + @test p1.charge_number == 1 + @test p1.symbol == :custom + + # Test with custom symbol + p2 = CustomParticle(2.0u"GeV", -1, :my_particle) + @test mass_energy(p2) == 2.0u"GeV" + @test charge(p2) == -Unitful.q +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index c326b5b..24265df 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -30,6 +30,10 @@ using Mendeleev: elements include("types.jl") end + @testset "Custom Particles" begin + include("custom_particles.jl") + end + @testset "Properties" begin include("properties.jl") end