diff --git a/docs/make.jl b/docs/make.jl index 09e7e08..adc8a22 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -35,6 +35,6 @@ makedocs(; ) deploydocs(; - repo="github.com/cthonios/TabularFunctions.jl", + repo="github.com/Cthonios/TabularFunctions.jl", devbranch="main" ) diff --git a/src/TabularFunctions.jl b/src/TabularFunctions.jl index ff4f0a2..43f0bec 100644 --- a/src/TabularFunctions.jl +++ b/src/TabularFunctions.jl @@ -203,6 +203,14 @@ function _is_number_expr(ex) return ex isa Number end +struct BadMacroInput <: Exception + msg::String +end + +function _bad_macro_input(msg::String) + throw(BadMacroInput(msg)) +end + """ $(TYPEDSIGNATURES) Example: @@ -233,7 +241,10 @@ macro piecewise_analytic(expr) elseif line isa LineNumberNode continue else - error("@piecewise_analytic: each line must be 'x, func'") + _bad_macro_input( + "@piecewise_analytic: each line must be 'x, func'" * + "\nbad line provided: $line" + ) end x_expr = esc(args[1]) @@ -279,7 +290,10 @@ macro piecewise_linear(expr) push!(xs, esc(p.args[1])) push!(ys, esc(p.args[2])) else - error("@piecewise_linear: each line must be 'x, y'") + _bad_macro_input( + "@piecewise_linear: each line must be 'x, y'" * + "\nbad line provided: $line" + ) end end diff --git a/test/runtests.jl b/test/runtests.jl index 7a54007..0cd76ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,3 +1,4 @@ +import TabularFunctions: BadMacroInput import TabularFunctions: XsNotMonotonicallyIncreasing using Adapt using Aqua @@ -24,6 +25,52 @@ end @testset "Error checking" begin @test_throws AssertionError PiecewiseLinearFunction([0., 1.], [0., 1., 2.]) @test_throws XsNotMonotonicallyIncreasing PiecewiseLinearFunction([0., 0.1, -0.1, 1.], [0., 0., 0., 0.]) + + code = """ + @piecewise_analytic begin + 0.0 # malformed + 1.0, cos + end + """ + @test_throws LoadError eval(Meta.parse(code)) + + # code = """ + # @piecewise_linear begin + # 0.0 + # 1.0, 1.0 + # end + # """ + # @test_throws LoadError eval(Meta.parse(code)) +end + +@testset "Macros" begin + func = @piecewise_analytic begin + 0.0, sin + 1.0, cos + end + @test func(0.5) == sin(0.5) + @test func(1.5) == cos(1.5) + + func = @piecewise_analytic begin + 0.0, x -> sin(x) + 1.0, x -> cos(x) + end + @test func(0.5) == sin(0.5) + @test func(1.5) == cos(1.5) + + func = @piecewise_analytic begin + 0.0, 0.0 + 1.0, sin + end + @test func(0.5) == 0.0 + @test func(1.5) == sin(1.5) + + func = @piecewise_linear begin + 0.0, 0.0 + 0.25, 0.25 + 1.0, 1.0 + end + @test func(0.5) == 0.5 end @testset "PiecewiseAnalyticFunction - Identity function" begin @@ -78,36 +125,6 @@ end @test f(1.05) ≈ 1. end -@testset "Macros" begin - func = @piecewise_analytic begin - 0.0, sin - 1.0, cos - end - @test func(0.5) == sin(0.5) - @test func(1.5) == cos(1.5) - - func = @piecewise_analytic begin - 0.0, x -> sin(x) - 1.0, x -> cos(x) - end - @test func(0.5) == sin(0.5) - @test func(1.5) == cos(1.5) - - func = @piecewise_analytic begin - 0.0, 0.0 - 1.0, sin - end - @test func(0.5) == 0.0 - @test func(1.5) == sin(1.5) - - func = @piecewise_linear begin - 0.0, 0.0 - 0.25, 0.25 - 1.0, 1.0 - end - @test func(0.5) == 0.5 -end - # Aqua testing @testset "Aqua.jl" begin Aqua.test_all(TabularFunctions)