Patterns.jl is a Julia package that provides support for pattern matching.
Patterns.jl can be installed after adding Cursor Insight's own registry to the Julia environment:
julia> ]
pkg> registry add https://github.com/cursorinsight/julia-registry
Cloning registry from "https://github.com/cursorinsight/julia-registry"
Added registry `CursorInsightJuliaRegistry` to
`~/.julia/registries/CursorInsightJuliaRegistry`
pkg> add PatternsLoad the package via
using PatternsThis exports @pattern macro.
You can create functions with pattern in the signature using @pattern:
@pattern function f(:one)
return 1
endYou can also use multiple symbols and other parameters in the signature:
@pattern function f(::Type{String}, :one)
return "1"
endOr you can call a function with pattern. NOTE: all the Symbols are replaced
in a function call.
@assert f(String, :one) == string(f(:one))Create fixtures that provide data for your tests.
@pattern function fixture(:numbers, :odd)
return [1, 7, 11]
end
@pattern function fixture(:numbers, :even)
return [2, 8, 12]
endTo execute the following test:
@test any(!myiseven, fixture(:numbers, :odd))
@test all( myiseven, fixture(:numbers, :even))