diff --git a/src/axioms.jl b/src/axioms.jl index e134cc1..fa1feaa 100644 --- a/src/axioms.jl +++ b/src/axioms.jl @@ -98,12 +98,25 @@ function (si::SpectralIndex)(args::Number...) return arg_type(result) end -function Base.show(io::IO, index::SpectralIndex) - println(io, index.short_name, ": ", index.long_name) - println(io, "Application domain: ", index.application_domain) - println(io, "Bands/Parameters: ", index.bands) - println(io, "Formula: ", index.formula) - return println(io, "Reference: ", index.reference) +# Machine-readable output +function Base.show(io::IO, si::SpectralIndex) + print(io, "SpectralIndex(") + println(io, "short_name: $(si.short_name),") + println(io, "long_name: $(si.long_name),") + println(io, "application_domain: $(si.application_domain),") + println(io, "bands: $(si.bands),") + println(io, "formula: $(si.formula),") + println(io, "reference: $(si.reference)") + return println(io, ")") +end + +# Human-readable output +function Base.show(io::IO, ::MIME"text/plain", si::SpectralIndex) + println(io, "$(si.short_name): $(si.long_name)") + println(io, "* Application Domain: $(si.application_domain)") + println(io, "* Bands/Parameters: $(si.bands)") + println(io, "* Formula: $(si.formula)") + return println(io, "* Reference: $(si.reference)") end """ diff --git a/src/utils.jl b/src/utils.jl index b41c499..50504ea 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -54,11 +54,15 @@ indices = _get_indices(true) ''' ``` """ -function _get_indices(online::Bool=false) +function _get_indices( + online::Bool=false; + filename="spectral-indices-dict.json", + fileloc=joinpath(dirname(@__FILE__), "..", "data", filename), +) if online indices_loc = Downloads.download( "https://raw.githubusercontent.com/awesome-spectral-indices/awesome-spectral-indices/main/output/spectral-indices-dict.json", - string(pwd(), "spectral-indices-dict.json"), + fileloc, ) indices = JSON.parsefile(indices_loc) else diff --git a/test/axioms.jl b/test/axioms.jl index a57fd82..7bcb355 100644 --- a/test/axioms.jl +++ b/test/axioms.jl @@ -29,3 +29,27 @@ C_vals = fill(0.6, 5) I_vals = fill(0.2, 5) computed_array_index = si.(C_vals, I_vals) @test all(computed_array_index .≈ 0.5) + +# Test Human-readable Output +@test begin + io_buffer = IOBuffer() + show(io_buffer, MIME("text/plain"), si) + human_readable_output = String(take!(io_buffer)) + expected_human_readable_output = """ + CI: Custom Index + * Application Domain: Vegetation + * Bands/Parameters: ["C", "I"] + * Formula: (C-I)/(C+I) + * Reference: Doe et al., 1984 + """ + human_readable_output == expected_human_readable_output +end + +# Test Machine-readable Output +@test begin + io_buffer = IOBuffer() + show(io_buffer, si) + machine_readable_output = String(take!(io_buffer)) + expected_machine_readable_output = "SpectralIndex(short_name: CI,\nlong_name: Custom Index,\napplication_domain: Vegetation,\nbands: [\"C\", \"I\"],\nformula: (C-I)/(C+I),\nreference: Doe et al., 1984\n)\n" + machine_readable_output == expected_machine_readable_output +end diff --git a/test/utils.jl b/test/utils.jl index 8546be1..47a02cd 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -8,7 +8,25 @@ using SpectralIndices # Test offline @test SpectralIndices._get_indices() isa Dict # Test online -@test SpectralIndices._get_indices(true) isa Dict +@testset "Download Indices Test" begin + test_dir = mktempdir() + try + # Define the file location within the test directory + test_fileloc = joinpath(test_dir, "spectral-indices-dict.json") + + # Run the _get_indices function with online = true + indices = SpectralIndices._get_indices(true; fileloc=test_fileloc) + + # Test if the file was downloaded and parsed successfully + @test isfile(test_fileloc) + @test !isempty(indices) + @test indices isa Dict + + finally + # Clean up: Delete the test file and directory + rm(test_dir; recursive=true) + end +end params = Dict("N" => 0.6, "R" => 0.3) # Test correctness