-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreflection.jl
214 lines (147 loc) · 4.47 KB
/
reflection.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
module ReflectionTests
using Base.Test
using ExpressionPatterns
using ExpressionPatterns.Dispatch
using ExpressionPatterns.Dispatch.Reflection
# pattern preference
Dispatch.set_conflict_warnings(:no)
xpy = :(x+y)
ypx = :(y+x)
@macromethod f(x+y, z) = [x,y]
@macromethod f(z, x+y) = [x,y]
@metafunction f(x+y, z) = [x,y]
@metafunction f(z, x+y) = [x,y]
@test @f(x+y,y+x) == [:x, :y]
@test f(xpy,ypx) == [:x, :y]
@prefer @f(z, x+y) over @f(x+y, z)
@prefer f(z, x+y) over f(x+y, z)
@test @f(x+y,y+x) == [:y,:x]
@test f(xpy,ypx) == [:y,:x]
@prefer @f(x+y, z) over @f(z, x+y)
@prefer f(x+y, z) over f(z, x+y)
@test @f(x+y,y+x) == [:x, :y]
@test f(xpy,ypx) == [:x, :y]
# label preference
@macromethod g(x+y, z)[a] = [x,y]
@macromethod g(z, x+y)[b] = [x,y]
@metafunction g(x+y, z)[a] = [x,y]
@metafunction g(z, x+y)[b] = [x,y]
@test @g(x+y,y+x) == [:x, :y]
@test g(xpy,ypx) == [:x, :y]
@prefer b over a in @g
@prefer b over a in g
@test @g(x+y,y+x) == [:y, :x]
@test g(xpy,ypx) == [:y, :x]
@prefer a over b in @g
@prefer a over b in g
@test @g(x+y,y+x) == [:x, :y]
@test g(xpy,ypx) == [:x, :y]
# method removal
@macromethod h(x+y)[plus] = [x,y]
@macromethod h(x)[any] = [x]
@metafunction h(x+y)[plus] = [x,y]
@metafunction h(x)[any] = [x]
@test @h(x+y) == [:x, :y]
@test h(:(x+y)) == [:x, :y]
@remove plus from @h
@remove plus from h
@test @h(x+y) == [:(x+y)]
@test h(:(x+y)) == [:(x+y)]
@remove @h(x)
@remove h(x)
@test_throws MetaMethodError try @eval @h(x); catch err throw(err isa LoadError ? err.error : err) end
@test_throws MetaMethodError h(:x)
# which
MM = Dispatch.Applications.MACROMETHODS[ReflectionTests]
MF = Dispatch.Applications.METAFUNCTIONS[ReflectionTests]
@macromethod m(x) = [x]
@macromethod m(x+y) = [x,y]
@metafunction m(x) = [x]
@metafunction m(x+y) = [x,y]
const mmac = Symbol("@m")
const mfun = :m
@test MM[mmac].methods[1] == @whichmeta @m(x+y)
@test MM[mmac].methods[2] == @whichmeta @m(x)
@test MF[mfun].methods[1] == @whichmeta m(x+y)
@test MF[mfun].methods[2] == @whichmeta m(x)
# conflicts
@macromethod n(x,x+y)[a] ()
@macromethod n(x+y,x)[b] ()
let stdout = STDOUT
conflicts_io, = redirect_stdout()
@metaconflicts @n
print_with_color(:red, "[a]<x x + y>")
print_with_color(:yellow, " | ")
print_with_color(:red, "[b]<x + y x>")
println()
metaconflicts_result = readline(conflicts_io)
readline(conflicts_io)
expected = readline(conflicts_io)
redirect_stdout(stdout)
@test metaconflicts_result == expected
end
# Cross-module reflection
module A
using ..Dispatch
export @f, @g
@macromethod f(x) (x,)
@macromethod g(x) (x,)
end
module B
using ExpressionPatterns.Dispatch
using ExpressionPatterns.Dispatch.Reflection
@metamodule import ..A.@f
@macromethod f(x+y) (x,y)
@macromethod g(x+y) (x,y)
@macromethod h(x+y, z) (x,y)
@macromethod h(z, x+y) (x,y)
@macromethod m(x+y, z)[a] (x,y)
@macromethod m(z, x+y)[b] (x,y)
end
@test A.@f(x+y) == B.@f(x+y)
@test A.@g(x+y) != B.@g(x+y)
@test B.@h(x+y,y+x) == (:x,:y)
@prefer B.@h(z, x+y) over B.@h(x+y, z)
@test B.@h(x+y,y+x) == (:y,:x)
@test B.@m(x+y,y+x) == (:x,:y)
@prefer b over a in B.@m
@test B.@m(x+y,y+x) == (:y,:x)
@prefer a over b in B.@m
@test B.@m(x+y,y+x) == (:x,:y)
# Errors and warnings
macro test_warning(ex, expected_warning::String)
@gensym had_color
quote
let
token = gensym()
stderr = STDERR
nerr, = redirect_stderr()
$(esc(had_color)) = Base.have_color
eval($Base, :(have_color = false))
try
$(esc(ex))
println(STDERR)
println(STDERR, "$token")
warning = readline(nerr)
line = (eof(nerr) ? "" : readline(nerr))
while line != "$token"
if line != ""
warning = "$warning\n$line"
end
line = readline(nerr)
end
@test warning == $expected_warning
finally
redirect_stderr(stderr)
eval($Base, :(have_color = $$had_color))
end
end
end
end
@test_throws ArgumentError ExpressionPatterns.Dispatch.TableManipulation.set_conflict_warnings(:bleh)
ExpressionPatterns.Dispatch.TableManipulation.set_conflict_warnings(:yes)
@macromethod m()[a] ()
@test_warning (@prefer a over a in @m) "WARNING: Using `prefer` with non-conflicting methods. (pattern`()` and pattern`()` in macromethod m)"
@test_warning (@remove b from @m) "WARNING: Couldn't remove the method [b] from the macromethod m"
ExpressionPatterns.Dispatch.TableManipulation.set_conflict_warnings(:no)
end