Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quality code tests: Aqua.jl + method ambiguities test #921

Merged
merged 10 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/devdocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In principle, the following should be done:
1. Implement the `struct` that represents your new space, while making it a subtype of `AbstractSpace`.
1. Extend `random_position(model)`.
1. Extend `add_agent_to_space!(agent, model), remove_agent_from_space!(agent, model)`. This already provides access to `add_agent!, kill_agent!` and `move_agent!`.
1. Extend `nearby_ids(position, model, r)`.
1. Extend `nearby_ids(pos, model, r)`.
1. Create a new "minimal" agent type to be used with [`@agent`](@ref) (see the source code of [`GraphAgent`](@ref) for an example).

And that's it! Every function of the main API will now work. In some situations you might want to explicitly extend other functions such as `move_agent!` for performance reasons.
Expand Down
4 changes: 2 additions & 2 deletions src/core/space_interaction_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ remove_agent_from_space!(agent, model) = notimplemented(model)
# %% IMPLEMENT: Neighbors and stuff
#######################################################################################
"""
nearby_ids(position, model::ABM, r = 1; kwargs...) → ids
nearby_ids(pos, model::ABM, r = 1; kwargs...) → ids
Tortar marked this conversation as resolved.
Show resolved Hide resolved

Return an iterable over the IDs of the agents within distance `r` (inclusive) from the given
`position`. The `position` must match type with the spatial structure of the `model`.
Expand All @@ -66,7 +66,7 @@ described in each space's documentation string.

`nearby_ids` always includes IDs with 0 distance to `position`.
"""
nearby_ids(position, model, r = 1) = notimplemented(model)
nearby_ids(pos, model, r = 1) = notimplemented(model)

"""
nearby_positions(position, model::ABM{<:DiscreteSpace}, r=1; kwargs...)
Expand Down
4 changes: 2 additions & 2 deletions src/spaces/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ function empty_positions(model::ABM{<:DiscreteSpace})
end

"""
isempty(position, model::ABM{<:DiscreteSpace})
isempty(pos, model::ABM{<:DiscreteSpace})
Return `true` if there are no agents in `position`.
"""
Base.isempty(pos, model::ABM) = isempty(ids_in_position(pos, model))
Base.isempty(pos::ValidPos, model::ABM{<:DiscreteSpace}) = isempty(ids_in_position(pos, model))
Datseris marked this conversation as resolved.
Show resolved Hide resolved

"""
has_empty_positions(model::ABM{<:DiscreteSpace})
Expand Down
2 changes: 1 addition & 1 deletion src/spaces/grid_single.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end
# move_agent! does not need be implemented.
# The generic version at core/space_interaction_API.jl covers it.
# `random_empty` comes from spaces/discrete.jl as long as we extend:
Base.isempty(pos, model::ABM{<:GridSpaceSingle}) = abmspace(model).stored_ids[pos...] == 0
Base.isempty(pos::ValidPos, model::ABM{<:GridSpaceSingle}) = abmspace(model).stored_ids[pos...] == 0
# And we also need to extend the iterator of empty positions
function empty_positions(model::ABM{<:GridSpaceSingle})
Iterators.filter(i -> abmspace(model).stored_ids[i...] == 0, positions(model))
Expand Down
2 changes: 1 addition & 1 deletion src/spaces/nothing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ function add_agent!(A::Type{<:AbstractAgent}, model::ABM{Nothing}, args::Vararg{
add_agent_pos!(newagent, model)
end

nearby_ids(position, model::ABM{Nothing}, r = 1) = allids(model)
nearby_ids(agent::AbstractAgent, model::ABM{Nothing}, r = 1) = allids(model)
remove_agent_from_space!(agent, model::ABM{Nothing}) = nothing
add_agent_to_space!(agent, model::ABM{Nothing}) = nothing
7 changes: 6 additions & 1 deletion src/spaces/openstreetmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,13 @@ ids_on_road(pos_1::Int, pos_2::Int, model::ABM{<:OpenStreetMapSpace}) =
reverse_ids_on_road(pos_1, pos_2, model),
))

Agents.nearby_positions(pos::Tuple{Int,Int,Float64}, model, args::Vararg{Any, N}; kwargs...) where {N} =
function Agents.nearby_positions(
pos::Tuple{Int,Int,Float64},
model::ABM{<:OpenStreetMapSpace},
args::Vararg{Any, N}; kwargs...
) where {N}
nearby_positions(pos[1], model, args...; kwargs...)
end

function Agents.nearby_positions(
position::Int,
Expand Down
4 changes: 4 additions & 0 deletions test/package_sanity_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

@testset "Code quality" begin
@test Test.detect_ambiguities(Agents) == Tuple{Method, Method}[]
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function flocking_model_agent_step!(bird, model)
end

@testset "Agents.jl Tests" begin
include("package_sanity_tests.jl")
include("model_creation_tests.jl")
include("api_tests.jl")
include("randomness_tests.jl")
Expand Down