Skip to content

Commit

Permalink
add corner case tests
Browse files Browse the repository at this point in the history
this hits 100%
  • Loading branch information
exaexa committed Oct 28, 2023
1 parent 7f378ac commit dd6e63b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ function parse_formula(x::Maybe{String})
return res
end

function parse_charge(x)
function parse_charge(x)::Maybe{Int}
if isa(x, Int)
return x
x
elseif isa(x, Float64)
return Int(x)::Int
Int(x)
elseif isa(x, String)
return parse(Int, x)
else
Int(parse(Float64, x))
elseif isnothing(x)
nothing
else
throw(DomainError(x, "cannot parse charge"))
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@
# caps suffix is quite common
@test all(suffix in A.filename_extensions(JSONFBCModel) for suffix in ["json", "JSON"])
end

@testset "Corner cases" begin
import JSONFBCModels: parse_charge

@test parse_charge(1) == 1
@test parse_charge(2.0) == 2
@test parse_charge("3") == 3
@test parse_charge("4.0") == 4
@test parse_charge(nothing) == nothing
@test_throws ArgumentError parse_charge("totally positive charge")
@test_throws DomainError parse_charge(["very charged"])
end

0 comments on commit dd6e63b

Please sign in to comment.