Skip to content

Commit

Permalink
Specify code style with a configuration file (#120)
Browse files Browse the repository at this point in the history
* Rename workflow

* Add workflow permissions

* Specify style via config file

* Apply formatting
  • Loading branch information
omus authored Jul 13, 2024
1 parent 0a12d05 commit 8dbab5e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 45 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name: Format suggestions

name: Format
on:
pull_request:
paths:
- "**.jl"
- ".github/workflows/blue_style_formatter.yml"

- ".github/workflows/Format.yml"
jobs:
format:
# These permissions are needed to:
# - Checkout the repo
# - Delete old caches: https://github.com/julia-actions/cache#usage
# - Post formatting suggestions: https://github.com/reviewdog/action-suggester#required-permissions
permissions:
actions: write
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -24,7 +30,7 @@ jobs:
shell: julia --color=yes {0}
run: |
using JuliaFormatter
format(".", BlueStyle(); verbose=true)
format("."; verbose=true)
- uses: reviewdog/action-suggester@v1
with:
tool_name: JuliaFormatter
Expand Down
3 changes: 0 additions & 3 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# https://github.com/JuliaLang/julia/blob/master/doc/src/devdocs/types.md#subtyping-and-method-sorting
type_morespecific(a, b) = ccall(:jl_type_morespecific, Bool, (Any, Any), a, b)


"""
anonymous_signature(m::Method) -> Type{<:Tuple}
Expand All @@ -26,7 +25,6 @@ anonymous_signature(m::Method) = anonymous_signature(m.sig)
anonymous_signature(sig::DataType) = Tuple{sig.parameters[2:end]...}
anonymous_signature(sig::UnionAll) = UnionAll(sig.var, anonymous_signature(sig.body))


"""
anon_morespecific(a::Method, b::Method) -> Bool
Expand All @@ -42,7 +40,6 @@ function anon_morespecific(a::Method, b::Method)
return type_morespecific(a_sig, b_sig)
end


"""
dispatch(funcs::AbstractVector, args...) -> Tuple{Method, Any}
Expand Down
2 changes: 1 addition & 1 deletion src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function rewrite_do(expr::Expr)
expr.head == :do || error("expression is not a do-block")
call, body = expr.args
Expr(:call, call.args[1], body, call.args[2:end]...)
return Expr(:call, call.args[1], body, call.args[2:end]...)
end

iskwarg(x::Any) = isa(x, Expr) && (x.head === :parameters || x.head === :kw)
Expand Down
2 changes: 1 addition & 1 deletion test/anonymous-param.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Issue #15
@testset "anonymous parameter" begin
f(::Type{T}, n::Int) where T<:Unsigned = rand(T, n)
f(::Type{T}, n::Int) where {T<:Unsigned} = rand(T, n)

patch = @patch f(::Type{UInt8}, n::Int) = collect(UnitRange{UInt8}(1:n))

Expand Down
54 changes: 28 additions & 26 deletions test/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@
end

@testset "union all" begin
@test type_morespecific(Tuple{Int}, Tuple{T} where T <: Integer)
@test type_morespecific(Tuple{Int}, Tuple{T} where {T<:Integer})

@test !type_morespecific(Tuple{Integer}, Tuple{T} where T <: Integer)
@test !type_morespecific(Tuple{T} where T <: Integer, Tuple{Integer})
@test !type_morespecific(Tuple{Integer}, Tuple{T} where {T<:Integer})
@test !type_morespecific(Tuple{T} where {T<:Integer}, Tuple{Integer})

@test type_morespecific(Tuple{Type{Integer}}, Tuple{Type{T}} where T <: Integer)
@test !type_morespecific(Tuple{Type{T}} where T <: Integer, Tuple{Type{Integer}})
@test type_morespecific(Tuple{Type{Integer}}, Tuple{Type{T}} where {T<:Integer})
@test !type_morespecific(Tuple{Type{T}} where {T<:Integer}, Tuple{Type{Integer}})
end
end

@testset "anonymous_signature" begin
@testset "diagonal dispatch" begin
diag(::T, ::T) where T <: Integer = nothing
diag(::T, ::T) where {T<:Integer} = nothing

diag_method = first(methods(diag, (Int, Int)))
diag_sig = Tuple{typeof(diag),T,T} where T <: Integer
diag_sig = Tuple{typeof(diag),T,T} where {T<:Integer}
@test diag_method.sig == diag_sig

@test anonymous_signature(Tuple{typeof(diag),Int,Int}) == Tuple{Int,Int}
@test anonymous_signature(diag_sig) == Tuple{T,T} where T <: Integer
@test anonymous_signature(diag_method) == Tuple{T,T} where T <:Integer
@test anonymous_signature(diag_sig) == Tuple{T,T} where {T<:Integer}
@test anonymous_signature(diag_method) == Tuple{T,T} where {T<:Integer}
end

@testset "triangular dispatch" begin
tri(::Vector{T}, ::S) where {T, S<:T} = nothing
tri(::Vector{T}, ::S) where {T,S<:T} = nothing

tri_method = first(methods(tri, (Vector, Int)))
tri_sig = Tuple{typeof(tri),Vector{T},S} where {T, S<:T}
tri_sig = Tuple{typeof(tri),Vector{T},S} where {T,S<:T}
@test tri_method.sig == tri_sig

@test anonymous_signature(Tuple{typeof(tri),Vector,Int}) == Tuple{Vector,Int}
@test anonymous_signature(tri_sig) == Tuple{Vector{T},S} where {T, S<:T}
@test anonymous_signature(tri_method) == Tuple{Vector{T},S} where {T, S<:T}
@test anonymous_signature(tri_sig) == Tuple{Vector{T},S} where {T,S<:T}
@test anonymous_signature(tri_method) == Tuple{Vector{T},S} where {T,S<:T}
end
end

Expand All @@ -82,9 +82,12 @@ end
end

@testset "no arguments" begin
#! format: off
funcs = [
() -> 1
]
#! format: on

m, f = dispatch(funcs)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1])}
Expand All @@ -98,12 +101,11 @@ end

m, f = dispatch(funcs, zero(Int))
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Int}

@test m.sig == Tuple{typeof(funcs[1]),Int}

m, f = dispatch(funcs, zero(UInt))
@test f == funcs[2]
@test m.sig == Tuple{typeof(funcs[2]), Integer}
@test m.sig == Tuple{typeof(funcs[2]),Integer}
end

@testset "more specific method" begin
Expand All @@ -115,11 +117,11 @@ end

m, f = dispatch(funcs, zero(Int))
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Int}
@test m.sig == Tuple{typeof(funcs[1]),Int}

m, f = dispatch(funcs, zero(UInt))
@test f == funcs[2]
@test m.sig == Tuple{typeof(funcs[2]), Integer}
@test m.sig == Tuple{typeof(funcs[2]),Integer}
end

@testset "ambiguous" begin
Expand All @@ -131,11 +133,11 @@ end
# In ambiguous cases the last function is preferred
m, f = dispatch(funcs, 0)
@test f == funcs[2]
@test m.sig == Tuple{typeof(funcs[2]), Integer}
@test m.sig == Tuple{typeof(funcs[2]),Integer}

m, f = dispatch(reverse(funcs), 0)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Integer}
@test m.sig == Tuple{typeof(funcs[1]),Integer}
end

@testset "optional" begin
Expand All @@ -146,15 +148,15 @@ end

m, f = dispatch(funcs, 0)
@test f == funcs[2]
@test m.sig == Tuple{typeof(funcs[2]), Any}
@test m.sig == Tuple{typeof(funcs[2]),Any}

m, f = dispatch(funcs, 0, 0)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Any, Any}
@test m.sig == Tuple{typeof(funcs[1]),Any,Any}

m, f = dispatch(reverse(funcs), 0)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Any}
@test m.sig == Tuple{typeof(funcs[1]),Any}
end

@testset "Type{T}" begin
Expand All @@ -169,7 +171,7 @@ end

m, f = dispatch(funcs, Integer)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Type{Integer}}
@test m.sig == Tuple{typeof(funcs[1]),Type{Integer}}

m, f = dispatch(reverse(funcs), Type{Integer})
@test f === nothing
Expand All @@ -184,10 +186,10 @@ end

m, f = dispatch(funcs, Int)
@test f == funcs[1]
@test m.sig == Tuple{typeof(funcs[1]), Type{Int}}
@test m.sig == Tuple{typeof(funcs[1]),Type{Int}}

m, f = dispatch(reverse(funcs), Integer)
@test f == funcs[2]
@test m.sig == Tuple{typeof(funcs[2]), Type{<:Integer}}
@test m.sig == Tuple{typeof(funcs[2]),Type{<:Integer}}
end
end
4 changes: 2 additions & 2 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
function f end

p = @patch function f(x; y=x)
(x, x, y, y)
return (x, x, y, y)
end

apply(p) do
Expand All @@ -29,7 +29,7 @@ end
function f end

p = @patch function f(x; y=:foo)
(x, x, y, y)
return (x, x, y, y)
end

apply(p) do
Expand Down
6 changes: 4 additions & 2 deletions test/mock-in-patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
foo(x::Float64) = floor(x)

# Patching only the function that takes a scalar
#! format: off
patches = Patch[
@patch foo(x::Float64) = ceil(x)
]
#! format: on

@test (@mock foo(1.6)) == 1.0

Expand All @@ -36,8 +38,8 @@ end
f(args...) = 0

patches = [
@patch f(a::Function, b) = b
@patch f(b) = @mock f(() -> nothing, b)
@patch f(a::Function, b) = b
@patch f(b) = @mock f(() -> nothing, b)
]

apply(patches) do
Expand Down
4 changes: 3 additions & 1 deletion test/real-nested.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@testset "nested mock call" begin
readfile(filename) = (@mock isfile(filename)) ? read((@mock open(filename)), String) : ""
function readfile(filename)
return (@mock isfile(filename)) ? read((@mock open(filename)), String) : ""
end

# Testing with both generic and anonymous functions
patches = Patch[
Expand Down
2 changes: 1 addition & 1 deletion test/reuse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

@testset "optional arguments" begin
f(args::T...) where T = zero(T)
f(args::T...) where {T} = zero(T)

# Create a patch function that contains two methods: `f(x)` and `f(x, y)`
p1 = @patch f(x, y=0) = x + y
Expand Down
6 changes: 3 additions & 3 deletions test/targets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ end
@testset "constructor function" begin
@test Vector() == []

p = @patch Vector() = [1,2,3]
p = @patch Vector() = [1, 2, 3]
apply(p) do
# Call alternative
@test (@mock Vector()) == [1,2,3]
@test (@mock Base.Vector()) == [1,2,3]
@test (@mock Vector()) == [1, 2, 3]
@test (@mock Base.Vector()) == [1, 2, 3]

# Call original function
@test (@mock Base.Vector{Any}()) == Any[]
Expand Down

0 comments on commit 8dbab5e

Please sign in to comment.