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

Address Aqua type piracy errors #31

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 5 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
name = "HerbCore"
uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
authors = ["Jaap de Jong <[email protected]>", "Nicolae Filat <[email protected]>", "Tilman Hinnerichs <[email protected]>", "Sebastijan Dumancic <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

[compat]
AbstractTrees = "0.4.5"
julia = "^1.8"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
4 changes: 3 additions & 1 deletion src/HerbCore.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module HerbCore

using AbstractTrees

include("grammar.jl")
include("rulenode.jl")
include("constraint.jl")
include("grammar.jl")

export
AbstractRuleNode,
Expand Down
8 changes: 8 additions & 0 deletions src/grammar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ If the grammar is non-probabilistic, the list can be `nothing`.
For concrete types, see `ContextSensitiveGrammar` within the `HerbGrammar` module.
"""
abstract type AbstractGrammar end

function Base.show(io::IO, grammar::AbstractGrammar)
for i in eachindex(grammar.rules)
println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i])
end
end

Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ]
4 changes: 4 additions & 0 deletions src/rulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Expression trees consist of [`RuleNode`](@ref)s and [`AbstractHole`](@ref)s.
"""
abstract type AbstractRuleNode end

# Interface to AbstractTrees.jl
AbstractTrees.children(node::AbstractRuleNode) = get_children(node)
AbstractTrees.nodevalue(node::AbstractRuleNode) = get_rule(node)

"""
RuleNode <: AbstractRuleNode

Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
35 changes: 35 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
using AbstractTrees: children, nodevalue, treeheight
using Aqua
using HerbCore
using Test

@testset "HerbCore.jl" verbose=true begin
@testset "Aqua Tests" Aqua.test_all(HerbCore)

@testset "AbstractTrees Interface" begin
@test nodevalue(RuleNode(1)) == 1
@test isempty(children(RuleNode(1)))
@test length(children(RuleNode(1, [RuleNode(2), RuleNode(2)]))) == 2
@test treeheight(RuleNode(1)) == 0
@test treeheight(RuleNode(1, [RuleNode(2), RuleNode(2)])) == 1
end

@testset "RuleNode tests" begin
@testset "Equality tests" begin
@test RuleNode(1) == RuleNode(1)
Expand Down Expand Up @@ -335,4 +347,27 @@ using Test
"12{14,2{4{hole[Bool[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}},2{4{6}}}"
end
end

@testset "Grammar" begin
struct ExGrammar <: AbstractGrammar
rules::Vector{Any}
types::Vector{Symbol}
bytype::Dict{Symbol, Vector{Int}}
# ...
# only partially implementing the AbstractGrammar interface
# to test the Base.show
end

g = ExGrammar([1], [:A], Dict([:A => [1]]))

@testset "show" begin
io = IOBuffer()
Base.show(io, g)
@test String(take!(io)) == "1: A = 1\n"
end

@testset "get_index" begin
@test g[:A] == [1]
end
end
end