-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add StringInterface and update the others * add StringInterface implementations * update BaseInterfaces README * update BaseInterfaces readme and use it as the module docs * limit LazyString to > v1.8
- Loading branch information
Showing
10 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
module BaseInterfaces | ||
|
||
@doc let | ||
path = joinpath(dirname(@__DIR__), "README.md") | ||
include_dependency(path) | ||
read(path, String) | ||
end BaseInterfaces | ||
|
||
using Interfaces | ||
|
||
import Interfaces: test, test_objects, implements, description, components, requiredtype, @implements | ||
|
||
export Interfaces | ||
|
||
export ArrayInterface, DictInterface, IterationInterface, SetInterface | ||
export ArrayInterface, DictInterface, IterationInterface, SetInterface, StringInterface | ||
|
||
include("interfaces/iteration.jl") | ||
include("interfaces/dict.jl") | ||
include("interfaces/set.jl") | ||
include("interfaces/array.jl") | ||
include("interfaces/string.jl") | ||
|
||
include("implementations/iteration.jl") | ||
include("implementations/dict.jl") | ||
include("implementations/set.jl") | ||
include("implementations/array.jl") | ||
include("implementations/string.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# We may want to add matching @implements declarations for | ||
# - SubstitutionString | ||
|
||
@implements StringInterface{:length} String ["abc"] | ||
@implements StringInterface{:length} SubString [view("abc", 2:3)] | ||
@implements StringInterface{:length} SubstitutionString [SubstitutionString("abc \\1")] | ||
if VERSION > v"1.8.0" | ||
@implements StringInterface{:length} LazyString [LazyString("abc", "def")] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# This is not actually loaded yet... | ||
# Should iteration be here? | ||
|
||
mandatory = (; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,62 @@ | ||
# This is a completely insufficient interface for AbstractDict | ||
# See https://github.com/JuliaLang/julia/issues/25941 for discussion | ||
|
||
@interface DictInterface AbstractDict ( # <: CollectionInterface | ||
mandatory = (; | ||
iterate = "AbstractDict follows the IterationInterface" => a -> Interfaces.test(IterationInterface, a.d; show=false) && first(iterate(a.d)) isa Pair, | ||
eltype = "eltype is a Pair" => a -> eltype(a.d) <: Pair, | ||
keytype = a -> keytype(a.d) == eltype(a.d).parameters[1], | ||
valtype = a -> valtype(a.d) == eltype(a.d).parameters[2], | ||
keys = a -> all(k -> k isa keytype(a.d), keys(a.d)), | ||
values = a -> all(v -> v isa valtype(a.d), values(a.d)), | ||
# This is kind of unsatisfactory as interface inheritance, but its simple | ||
iterate = "AbstractDict follows the IterationInterface" => | ||
a -> Interfaces.test(IterationInterface, a.d; show=false) && first(iterate(a.d)) isa Pair, | ||
length = "length is defined" => a -> length(a.d) isa Integer, | ||
eltype = ( | ||
"eltype is a Pair" => a -> eltype(a.d) <: Pair, | ||
"the first value isa eltype" => a -> eltype(a.d) <: Pair, | ||
), | ||
keytype = "keytype is the same as the first type in eltype parameters" => | ||
a -> keytype(a.d) == eltype(a.d).parameters[1], | ||
valtype = "valtype is the same as the second type in eltype paremeters" => | ||
a -> valtype(a.d) == eltype(a.d).parameters[2], | ||
keys = "keys are all of type keytype" => a -> all(k -> k isa keytype(a.d), keys(a.d)), | ||
values = "values are all of type valtype" => a -> all(v -> v isa valtype(a.d), values(a.d)), | ||
getindex = ( | ||
a -> a.d[first(keys(a.d))] === first(values(a.d)), | ||
a -> all(k -> a.d[k] isa valtype(a.d), keys(a.d)), | ||
"getindex of the first key is the first object in `values`" => | ||
a -> a.d[first(keys(a.d))] === first(values(a.d)), | ||
"getindex of all keys match values" => | ||
a -> all(p -> a.d[p[1]] == p[2], a.d), | ||
), | ||
), | ||
optional = (; | ||
setindex! = ( | ||
"test object `d` does not yet have test key `k`" => | ||
a -> !haskey(a.d, a.k), | ||
"can set key `k` to value `v`" => | ||
a -> (a.d[a.k] = a.v; a.d[a.k] == a.v), | ||
), | ||
get! = ( | ||
"test object `d` does not yet have test key `k`" => a -> !haskey(a.d, a.k), | ||
"can set key `k` to value `v`" => a -> (a.d[a.k] = a.v; a.d[a.k] == a.v), | ||
"can set and get key `k` to value `v` with using get!" => | ||
a -> begin | ||
v = get!(a.d, a.k, a.v) | ||
v == a.d[a.k] == a.v | ||
end, | ||
), | ||
delete! = "can delete existing keys from the object" => | ||
a -> begin | ||
k = first(keys(a.d)) | ||
delete!(a.d, k) | ||
!haskey(a.d, k) | ||
end, | ||
empty! = "can empty the dictionary" => | ||
a -> begin | ||
empty!(a.d) | ||
length(a.d) == 0 | ||
end, | ||
) | ||
) """ | ||
`AbstractDict` interface requires Arguments, with `d = the_dict` mandatory, and | ||
when `setindex` is needed, `k = any_valid_key_not_in_d, v = any_valid_val` | ||
`AbstractDict` interface | ||
Requires test data wrapped with `Interfaces.Arguments`, with | ||
- `d = the_dict` mandatory | ||
When `get!` or `setindex!` is needed | ||
- `k`: any valid key not initially in d | ||
- `v`: any valid value | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# The AbstractString interface is not clearly documented and large. | ||
# | ||
# This discourse post likely has the best information, although outdated | ||
# https://discourse.julialang.org/t/what-is-the-interface-of-abstractstring/8937/4 | ||
# | ||
# See required methods here: https://github.com/JuliaLang/julia/blob/b88f64f16c454c238c9fa0ae858ca02b7084f329/base/strings/basic.jl#L41 | ||
|
||
@interface StringInterface AbstractString (; | ||
mandatory = (; | ||
ncodeunits = "ncodeunit returns an Int" => s -> ncodeunits(s) isa Int, | ||
iterate = "AbstractString follows the IterationInterface" => | ||
s -> Interfaces.test(IterationInterface, s; show=false) && first(iterate(s)) isa AbstractChar, | ||
codeunit = "the first codeunit is a UInt8/16/32" => s -> codeunit(s, 1) isa Union{UInt8,UInt16,UInt32}, | ||
isvalid = "isvalid returns a Bool" => s -> isvalid(s, 1) isa Bool, | ||
eltype = "eltype returns a type <: AbatractChar" => s -> eltype(s) <: AbstractChar, | ||
), | ||
optional = (; | ||
length = "length return an Int" => s -> length(s) isa Int, | ||
# ? | ||
) | ||
) """ | ||
`AbstractString` interface | ||
Test objects must not be empty, so that `isempty(obj) == false`. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters