Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
202 additions
and
198 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,90 @@ | ||
let | ||
x = @match :(2+3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == (2, 3) | ||
end | ||
|
||
let | ||
x = @match :(2-3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == (3, 2) | ||
end | ||
|
||
let | ||
x = @match :(2/3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == nothing | ||
end | ||
|
||
let | ||
x = @match :(2/3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
_ => :default | ||
end | ||
@test x == :default | ||
end | ||
|
||
let | ||
ex = :(mutable struct Foo | ||
x::Int | ||
y | ||
end) | ||
@capture(ex, mutable struct T_ fields__ end) | ||
@test T == :Foo | ||
@test fields == [:(x::Int), :y] | ||
|
||
@capture(ex, mutable struct T_ fields__ end) | ||
@test T == :Foo | ||
@test fields == [:(x::Int), :y] | ||
end | ||
|
||
let | ||
ex = :(f(x)) | ||
@capture(ex, f_(xs__)) | ||
@test f == :f | ||
@test xs == [:x] | ||
end | ||
|
||
let | ||
ex = :(f(x, y, z)) | ||
@capture(ex, f_(x_, xs__)) | ||
@test f == :f | ||
@test x == :x | ||
@test xs == [:y, :z] | ||
end | ||
|
||
let | ||
ex = quote | ||
function foo(a, b) | ||
return a+b | ||
end | ||
end | ||
@assert @capture(shortdef(ex), f_(args__) = body_) | ||
end | ||
|
||
let | ||
ex = :(a = b) | ||
@capture(ex, a_ = b_) | ||
@test (a, b) == (:a, :b) | ||
end | ||
|
||
let | ||
ex = :(f(a = b)) | ||
@capture(ex, f(a_ = b_)) | ||
@test (a, b) == (:a, :b) | ||
@capture(ex, f(x_)) | ||
@test isexpr(x, :kw) | ||
end | ||
|
||
let | ||
ex = :(@foo(a,b)) | ||
@capture(ex, @foo(a_,b_)) | ||
@test (a, b) == (:a, :b) | ||
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,153 +1,10 @@ | ||
using MacroTools | ||
using Compat | ||
using Compat.Test | ||
using Compat, Compat.Test | ||
|
||
let | ||
x = @match :(2+3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == (2, 3) | ||
end | ||
|
||
let | ||
x = @match :(2-3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == (3, 2) | ||
end | ||
|
||
let | ||
x = @match :(2/3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
end | ||
@test x == nothing | ||
end | ||
@testset "MacroTools" begin | ||
|
||
let | ||
x = @match :(2/3) begin | ||
(a_+b_) => (a, b) | ||
(a_-b_) => (b, a) | ||
_ => :default | ||
end | ||
@test x == :default | ||
end | ||
|
||
let | ||
ex = :(mutable struct Foo | ||
x::Int | ||
y | ||
end) | ||
@capture(ex, mutable struct T_ fields__ end) | ||
@test T == :Foo | ||
@test fields == [:(x::Int), :y] | ||
|
||
@capture(ex, mutable struct T_ fields__ end) | ||
@test T == :Foo | ||
@test fields == [:(x::Int), :y] | ||
end | ||
|
||
let | ||
ex = :(f(x)) | ||
@capture(ex, f_(xs__)) | ||
@test f == :f | ||
@test xs == [:x] | ||
end | ||
|
||
let | ||
ex = :(f(x, y, z)) | ||
@capture(ex, f_(x_, xs__)) | ||
@test f == :f | ||
@test x == :x | ||
@test xs == [:y, :z] | ||
end | ||
|
||
let | ||
ex = quote | ||
function foo(a, b) | ||
return a+b | ||
end | ||
end | ||
@assert @capture(shortdef(ex), f_(args__) = body_) | ||
end | ||
|
||
let | ||
ex = :(a = b) | ||
@capture(ex, a_ = b_) | ||
@test (a, b) == (:a, :b) | ||
end | ||
|
||
let | ||
ex = :(f(a = b)) | ||
@capture(ex, f(a_ = b_)) | ||
@test (a, b) == (:a, :b) | ||
@capture(ex, f(x_)) | ||
@test isexpr(x, :kw) | ||
end | ||
|
||
let | ||
ex = :(@foo(a,b)) | ||
@capture(ex, @foo(a_,b_)) | ||
@test (a, b) == (:a, :b) | ||
end | ||
|
||
macro nothing_macro() | ||
end | ||
@test @expand(@nothing_macro) === nothing | ||
|
||
macro splitcombine(fundef) # should be a no-op | ||
dict = splitdef(fundef) | ||
esc(MacroTools.combinedef(dict)) | ||
end | ||
|
||
# Macros for testing that splitcombine doesn't break | ||
# macrocalls in bodies | ||
macro zeroarg() | ||
:(1) | ||
end | ||
macro onearg(x) | ||
:(1+$(esc(x))) | ||
end | ||
|
||
let | ||
# Ideally we'd compare the result against :(function f(x)::Int 10 end), | ||
# but it fails because of :line and :block differences | ||
@test longdef(:(f(x)::Int = 10)).head == :function | ||
@test longdef(:(f(x::T) where U where T = 2)).head == :function | ||
@test shortdef(:(function f(x)::Int 10 end)).head != :function | ||
@test map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end]) == | ||
[(:a, :Any, false, 2), (:x, :Int, false, :nothing), | ||
(:y, :Any, false, nothing), (:args, :Any, true, nothing)] | ||
@test splitarg(:(::Int)) == (nothing, :Int, false, nothing) | ||
|
||
@splitcombine foo(x) = x+2 | ||
@test foo(10) == 12 | ||
@splitcombine add(a, b=2; c=3, d=4)::Float64 = a+b+c+d | ||
@test add(1; d=10) === 16.0 | ||
@splitcombine fparam(a::T) where {T} = T | ||
@test fparam([]) == Vector{Any} | ||
struct Orange end | ||
@splitcombine (::Orange)(x) = x+2 | ||
@test Orange()(10) == 12 | ||
@splitcombine fwhere(a::T) where T = T | ||
@test fwhere(10) == Int | ||
@splitcombine manywhere(x::T, y::Vector{U}) where T <: U where U = (T, U) | ||
@test manywhere(1, Number[2.0]) == (Int, Number) | ||
@splitcombine fmacro0() = @zeroarg | ||
@test fmacro0() == 1 | ||
@splitcombine fmacro1() = @onearg 1 | ||
@test fmacro1() == 2 | ||
include("match.jl") | ||
include("split.jl") | ||
include("destruct.jl") | ||
|
||
struct Foo{A, B} | ||
a::A | ||
b::B | ||
end | ||
# Parametric outer constructor | ||
@splitcombine Foo{A}(a::A) where A = Foo{A, A}(a,a) | ||
@test Foo{Int}(2) == Foo{Int, Int}(2, 2) | ||
end | ||
|
||
include("destruct.jl") | ||
include("structdef.jl") |
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,101 @@ | ||
using MacroTools: splitstructdef, combinestructdef | ||
|
||
macro nothing_macro() | ||
end | ||
@test @expand(@nothing_macro) === nothing | ||
|
||
macro splitcombine(fundef) # should be a no-op | ||
dict = splitdef(fundef) | ||
esc(MacroTools.combinedef(dict)) | ||
end | ||
|
||
# Macros for testing that splitcombine doesn't break | ||
# macrocalls in bodies | ||
macro zeroarg() | ||
:(1) | ||
end | ||
macro onearg(x) | ||
:(1+$(esc(x))) | ||
end | ||
|
||
let | ||
# Ideally we'd compare the result against :(function f(x)::Int 10 end), | ||
# but it fails because of :line and :block differences | ||
@test longdef(:(f(x)::Int = 10)).head == :function | ||
@test longdef(:(f(x::T) where U where T = 2)).head == :function | ||
@test shortdef(:(function f(x)::Int 10 end)).head != :function | ||
@test map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end]) == | ||
[(:a, :Any, false, 2), (:x, :Int, false, :nothing), | ||
(:y, :Any, false, nothing), (:args, :Any, true, nothing)] | ||
@test splitarg(:(::Int)) == (nothing, :Int, false, nothing) | ||
|
||
@splitcombine foo(x) = x+2 | ||
@test foo(10) == 12 | ||
@splitcombine add(a, b=2; c=3, d=4)::Float64 = a+b+c+d | ||
@test add(1; d=10) === 16.0 | ||
@splitcombine fparam(a::T) where {T} = T | ||
@test fparam([]) == Vector{Any} | ||
struct Orange end | ||
@splitcombine (::Orange)(x) = x+2 | ||
@test Orange()(10) == 12 | ||
@splitcombine fwhere(a::T) where T = T | ||
@test fwhere(10) == Int | ||
@splitcombine manywhere(x::T, y::Vector{U}) where T <: U where U = (T, U) | ||
@test manywhere(1, Number[2.0]) == (Int, Number) | ||
@splitcombine fmacro0() = @zeroarg | ||
@test fmacro0() == 1 | ||
@splitcombine fmacro1() = @onearg 1 | ||
@test fmacro1() == 2 | ||
|
||
struct Foo{A, B} | ||
a::A | ||
b::B | ||
end | ||
# Parametric outer constructor | ||
@splitcombine Foo{A}(a::A) where A = Foo{A, A}(a,a) | ||
@test Foo{Int}(2) == Foo{Int, Int}(2, 2) | ||
end | ||
|
||
@testset "combinestructdef, splitstructdef" begin | ||
ex = :(struct S end) | ||
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! == | ||
:(struct S <: Any end) | ||
|
||
@test splitstructdef(ex) == Dict( | ||
:constructors => Any[], | ||
:mutable => false, | ||
:params => Any[], | ||
:name => :S, | ||
:fields => Any[], | ||
:supertype => :Any) | ||
|
||
ex = :(mutable struct T end) | ||
@test splitstructdef(ex)[:mutable] === true | ||
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! == | ||
:(mutable struct T <: Any end) | ||
|
||
ex = :(struct S{A,B} <: AbstractS{B} | ||
a::A | ||
end) | ||
@test splitstructdef(ex) == Dict( | ||
:constructors => Any[], | ||
:mutable => false, | ||
:params => Any[:A, :B], | ||
:name => :S, | ||
:fields => Any[(:a, :A)], | ||
:supertype => :(AbstractS{B}),) | ||
|
||
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! == | ||
ex |> Base.remove_linenums! | ||
|
||
ex = :(struct S{A} <: Foo; S(a::A) where {A} = new{A}() end) | ||
@test ex |> splitstructdef |> combinestructdef |> | ||
Base.remove_linenums! |> MacroTools.flatten == | ||
ex |> Base.remove_linenums! |> MacroTools.flatten | ||
|
||
constructors = splitstructdef(ex)[:constructors] | ||
@test length(constructors) == 1 | ||
@test first(constructors) == | ||
:((S(a::A) where A) = new{A}()) |> MacroTools.flatten | ||
|
||
end |
Oops, something went wrong.