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 Patterns
Load the package via
using Patterns
This exports @pattern
macro.
You can create functions with pattern in the signature using @pattern
:
@pattern function f(:one)
return 1
end
You can also use multiple symbols and other parameters in the signature:
@pattern function f(::Type{String}, :one)
return "1"
end
Or you can call a function with pattern. NOTE: all the Symbol
s 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]
end
To execute the following test:
@test any(!myiseven, fixture(:numbers, :odd))
@test all( myiseven, fixture(:numbers, :even))