Skip to content

Commit f6a4fa9

Browse files
committed
organise
1 parent 7d9d23a commit f6a4fa9

File tree

9 files changed

+202
-198
lines changed

9 files changed

+202
-198
lines changed

src/MacroTools.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ using Compat
44
using Compat.Markdown
55
export @match, @capture
66

7-
include("match.jl")
8-
include("types.jl")
9-
include("union.jl")
10-
include("macro.jl")
7+
include("match/match.jl")
8+
include("match/types.jl")
9+
include("match/union.jl")
10+
include("match/macro.jl")
11+
1112
include("utils.jl")
1213
include("structdef.jl")
1314

@@ -16,7 +17,7 @@ include("examples/threading.jl")
1617
include("examples/forward.jl")
1718

1819
const animals = Symbol[]
19-
const animals_file = joinpath(dirname(@__FILE__), "..", "animals.txt")
20+
const animals_file = joinpath(@__DIR__, "..", "animals.txt")
2021

2122
function __init__()
2223
_animals = split(read(animals_file, String))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/match.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
let
2+
x = @match :(2+3) begin
3+
(a_+b_) => (a, b)
4+
(a_-b_) => (b, a)
5+
end
6+
@test x == (2, 3)
7+
end
8+
9+
let
10+
x = @match :(2-3) begin
11+
(a_+b_) => (a, b)
12+
(a_-b_) => (b, a)
13+
end
14+
@test x == (3, 2)
15+
end
16+
17+
let
18+
x = @match :(2/3) begin
19+
(a_+b_) => (a, b)
20+
(a_-b_) => (b, a)
21+
end
22+
@test x == nothing
23+
end
24+
25+
let
26+
x = @match :(2/3) begin
27+
(a_+b_) => (a, b)
28+
(a_-b_) => (b, a)
29+
_ => :default
30+
end
31+
@test x == :default
32+
end
33+
34+
let
35+
ex = :(mutable struct Foo
36+
x::Int
37+
y
38+
end)
39+
@capture(ex, mutable struct T_ fields__ end)
40+
@test T == :Foo
41+
@test fields == [:(x::Int), :y]
42+
43+
@capture(ex, mutable struct T_ fields__ end)
44+
@test T == :Foo
45+
@test fields == [:(x::Int), :y]
46+
end
47+
48+
let
49+
ex = :(f(x))
50+
@capture(ex, f_(xs__))
51+
@test f == :f
52+
@test xs == [:x]
53+
end
54+
55+
let
56+
ex = :(f(x, y, z))
57+
@capture(ex, f_(x_, xs__))
58+
@test f == :f
59+
@test x == :x
60+
@test xs == [:y, :z]
61+
end
62+
63+
let
64+
ex = quote
65+
function foo(a, b)
66+
return a+b
67+
end
68+
end
69+
@assert @capture(shortdef(ex), f_(args__) = body_)
70+
end
71+
72+
let
73+
ex = :(a = b)
74+
@capture(ex, a_ = b_)
75+
@test (a, b) == (:a, :b)
76+
end
77+
78+
let
79+
ex = :(f(a = b))
80+
@capture(ex, f(a_ = b_))
81+
@test (a, b) == (:a, :b)
82+
@capture(ex, f(x_))
83+
@test isexpr(x, :kw)
84+
end
85+
86+
let
87+
ex = :(@foo(a,b))
88+
@capture(ex, @foo(a_,b_))
89+
@test (a, b) == (:a, :b)
90+
end

test/runtests.jl

Lines changed: 5 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,10 @@
11
using MacroTools
2-
using Compat
3-
using Compat.Test
2+
using Compat, Compat.Test
43

5-
let
6-
x = @match :(2+3) begin
7-
(a_+b_) => (a, b)
8-
(a_-b_) => (b, a)
9-
end
10-
@test x == (2, 3)
11-
end
12-
13-
let
14-
x = @match :(2-3) begin
15-
(a_+b_) => (a, b)
16-
(a_-b_) => (b, a)
17-
end
18-
@test x == (3, 2)
19-
end
20-
21-
let
22-
x = @match :(2/3) begin
23-
(a_+b_) => (a, b)
24-
(a_-b_) => (b, a)
25-
end
26-
@test x == nothing
27-
end
4+
@testset "MacroTools" begin
285

29-
let
30-
x = @match :(2/3) begin
31-
(a_+b_) => (a, b)
32-
(a_-b_) => (b, a)
33-
_ => :default
34-
end
35-
@test x == :default
36-
end
37-
38-
let
39-
ex = :(mutable struct Foo
40-
x::Int
41-
y
42-
end)
43-
@capture(ex, mutable struct T_ fields__ end)
44-
@test T == :Foo
45-
@test fields == [:(x::Int), :y]
46-
47-
@capture(ex, mutable struct T_ fields__ end)
48-
@test T == :Foo
49-
@test fields == [:(x::Int), :y]
50-
end
51-
52-
let
53-
ex = :(f(x))
54-
@capture(ex, f_(xs__))
55-
@test f == :f
56-
@test xs == [:x]
57-
end
58-
59-
let
60-
ex = :(f(x, y, z))
61-
@capture(ex, f_(x_, xs__))
62-
@test f == :f
63-
@test x == :x
64-
@test xs == [:y, :z]
65-
end
66-
67-
let
68-
ex = quote
69-
function foo(a, b)
70-
return a+b
71-
end
72-
end
73-
@assert @capture(shortdef(ex), f_(args__) = body_)
74-
end
75-
76-
let
77-
ex = :(a = b)
78-
@capture(ex, a_ = b_)
79-
@test (a, b) == (:a, :b)
80-
end
81-
82-
let
83-
ex = :(f(a = b))
84-
@capture(ex, f(a_ = b_))
85-
@test (a, b) == (:a, :b)
86-
@capture(ex, f(x_))
87-
@test isexpr(x, :kw)
88-
end
89-
90-
let
91-
ex = :(@foo(a,b))
92-
@capture(ex, @foo(a_,b_))
93-
@test (a, b) == (:a, :b)
94-
end
95-
96-
macro nothing_macro()
97-
end
98-
@test @expand(@nothing_macro) === nothing
99-
100-
macro splitcombine(fundef) # should be a no-op
101-
dict = splitdef(fundef)
102-
esc(MacroTools.combinedef(dict))
103-
end
104-
105-
# Macros for testing that splitcombine doesn't break
106-
# macrocalls in bodies
107-
macro zeroarg()
108-
:(1)
109-
end
110-
macro onearg(x)
111-
:(1+$(esc(x)))
112-
end
113-
114-
let
115-
# Ideally we'd compare the result against :(function f(x)::Int 10 end),
116-
# but it fails because of :line and :block differences
117-
@test longdef(:(f(x)::Int = 10)).head == :function
118-
@test longdef(:(f(x::T) where U where T = 2)).head == :function
119-
@test shortdef(:(function f(x)::Int 10 end)).head != :function
120-
@test map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end]) ==
121-
[(:a, :Any, false, 2), (:x, :Int, false, :nothing),
122-
(:y, :Any, false, nothing), (:args, :Any, true, nothing)]
123-
@test splitarg(:(::Int)) == (nothing, :Int, false, nothing)
124-
125-
@splitcombine foo(x) = x+2
126-
@test foo(10) == 12
127-
@splitcombine add(a, b=2; c=3, d=4)::Float64 = a+b+c+d
128-
@test add(1; d=10) === 16.0
129-
@splitcombine fparam(a::T) where {T} = T
130-
@test fparam([]) == Vector{Any}
131-
struct Orange end
132-
@splitcombine (::Orange)(x) = x+2
133-
@test Orange()(10) == 12
134-
@splitcombine fwhere(a::T) where T = T
135-
@test fwhere(10) == Int
136-
@splitcombine manywhere(x::T, y::Vector{U}) where T <: U where U = (T, U)
137-
@test manywhere(1, Number[2.0]) == (Int, Number)
138-
@splitcombine fmacro0() = @zeroarg
139-
@test fmacro0() == 1
140-
@splitcombine fmacro1() = @onearg 1
141-
@test fmacro1() == 2
6+
include("match.jl")
7+
include("split.jl")
8+
include("destruct.jl")
1429

143-
struct Foo{A, B}
144-
a::A
145-
b::B
146-
end
147-
# Parametric outer constructor
148-
@splitcombine Foo{A}(a::A) where A = Foo{A, A}(a,a)
149-
@test Foo{Int}(2) == Foo{Int, Int}(2, 2)
15010
end
151-
152-
include("destruct.jl")
153-
include("structdef.jl")

test/split.jl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using MacroTools: splitstructdef, combinestructdef
2+
3+
macro nothing_macro()
4+
end
5+
@test @expand(@nothing_macro) === nothing
6+
7+
macro splitcombine(fundef) # should be a no-op
8+
dict = splitdef(fundef)
9+
esc(MacroTools.combinedef(dict))
10+
end
11+
12+
# Macros for testing that splitcombine doesn't break
13+
# macrocalls in bodies
14+
macro zeroarg()
15+
:(1)
16+
end
17+
macro onearg(x)
18+
:(1+$(esc(x)))
19+
end
20+
21+
let
22+
# Ideally we'd compare the result against :(function f(x)::Int 10 end),
23+
# but it fails because of :line and :block differences
24+
@test longdef(:(f(x)::Int = 10)).head == :function
25+
@test longdef(:(f(x::T) where U where T = 2)).head == :function
26+
@test shortdef(:(function f(x)::Int 10 end)).head != :function
27+
@test map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end]) ==
28+
[(:a, :Any, false, 2), (:x, :Int, false, :nothing),
29+
(:y, :Any, false, nothing), (:args, :Any, true, nothing)]
30+
@test splitarg(:(::Int)) == (nothing, :Int, false, nothing)
31+
32+
@splitcombine foo(x) = x+2
33+
@test foo(10) == 12
34+
@splitcombine add(a, b=2; c=3, d=4)::Float64 = a+b+c+d
35+
@test add(1; d=10) === 16.0
36+
@splitcombine fparam(a::T) where {T} = T
37+
@test fparam([]) == Vector{Any}
38+
struct Orange end
39+
@splitcombine (::Orange)(x) = x+2
40+
@test Orange()(10) == 12
41+
@splitcombine fwhere(a::T) where T = T
42+
@test fwhere(10) == Int
43+
@splitcombine manywhere(x::T, y::Vector{U}) where T <: U where U = (T, U)
44+
@test manywhere(1, Number[2.0]) == (Int, Number)
45+
@splitcombine fmacro0() = @zeroarg
46+
@test fmacro0() == 1
47+
@splitcombine fmacro1() = @onearg 1
48+
@test fmacro1() == 2
49+
50+
struct Foo{A, B}
51+
a::A
52+
b::B
53+
end
54+
# Parametric outer constructor
55+
@splitcombine Foo{A}(a::A) where A = Foo{A, A}(a,a)
56+
@test Foo{Int}(2) == Foo{Int, Int}(2, 2)
57+
end
58+
59+
@testset "combinestructdef, splitstructdef" begin
60+
ex = :(struct S end)
61+
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! ==
62+
:(struct S <: Any end)
63+
64+
@test splitstructdef(ex) == Dict(
65+
:constructors => Any[],
66+
:mutable => false,
67+
:params => Any[],
68+
:name => :S,
69+
:fields => Any[],
70+
:supertype => :Any)
71+
72+
ex = :(mutable struct T end)
73+
@test splitstructdef(ex)[:mutable] === true
74+
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! ==
75+
:(mutable struct T <: Any end)
76+
77+
ex = :(struct S{A,B} <: AbstractS{B}
78+
a::A
79+
end)
80+
@test splitstructdef(ex) == Dict(
81+
:constructors => Any[],
82+
:mutable => false,
83+
:params => Any[:A, :B],
84+
:name => :S,
85+
:fields => Any[(:a, :A)],
86+
:supertype => :(AbstractS{B}),)
87+
88+
@test ex |> splitstructdef |> combinestructdef |> Base.remove_linenums! ==
89+
ex |> Base.remove_linenums!
90+
91+
ex = :(struct S{A} <: Foo; S(a::A) where {A} = new{A}() end)
92+
@test ex |> splitstructdef |> combinestructdef |>
93+
Base.remove_linenums! |> MacroTools.flatten ==
94+
ex |> Base.remove_linenums! |> MacroTools.flatten
95+
96+
constructors = splitstructdef(ex)[:constructors]
97+
@test length(constructors) == 1
98+
@test first(constructors) ==
99+
:((S(a::A) where A) = new{A}()) |> MacroTools.flatten
100+
101+
end

0 commit comments

Comments
 (0)