Skip to content

Commit 3453826

Browse files
committed
refactor: smaller testitem
1 parent a1fc807 commit 3453826

File tree

1 file changed

+78
-41
lines changed

1 file changed

+78
-41
lines changed

test/test_parse.jl

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
using DynamicExpressions
2-
using DynamicExpressions: DynamicExpressions as DE
3-
using DynamicExpressions.ParseModule: parse_expression
4-
using Test
5-
using Suppressor
1+
@testitem "custom operator" begin
2+
using DynamicExpressions
63

7-
function my_custom_op(x, y)
8-
return x + y
9-
end
4+
function my_custom_op(x, y)
5+
return x + y
6+
end
107

11-
operators = OperatorEnum(;
12-
binary_operators=[+, -, *, /, my_custom_op],
13-
unary_operators=[cos, sin],
14-
define_helper_functions=false,
15-
)
8+
operators = OperatorEnum(;
9+
binary_operators=[+, -, *, /, my_custom_op],
10+
unary_operators=[cos, sin],
11+
define_helper_functions=false,
12+
)
1613

17-
# Clear operators
18-
OperatorEnum(; binary_operators=[/])
14+
# Clears operators (which should not affect the outcome!)
15+
OperatorEnum(; binary_operators=[/])
1916

20-
let ex = @parse_expression(
17+
ex = @parse_expression(
2118
$(my_custom_op)(x, sin(y) + 0.3),
2219
operators = operators,
2320
variable_names = ["x", "y"],
@@ -46,36 +43,54 @@ let ex = @parse_expression(
4643
end
4744
end
4845

49-
# Can also parse just a float
50-
let ex = @parse_expression(1.0, operators = operators, variable_names = [])
46+
@testitem "Can also parse just a float" begin
47+
using DynamicExpressions
48+
operators = OperatorEnum() # Tests empty operators
49+
ex = @parse_expression(1.0, operators = operators, variable_names = [])
5150
@test typeof(ex) <: Expression
5251
@test ex.tree.val == 1.0
5352
@test typeof(ex.tree) <: Node{Float64}
5453
end
5554

56-
# Or an int
57-
let ex = @parse_expression(1, operators = operators, variable_names = [])
55+
# # Or an int
56+
# let ex = @parse_expression(1, operators = operators, variable_names = [])
57+
@testitem "Or an int" begin
58+
using DynamicExpressions
59+
operators = OperatorEnum()
60+
ex = @parse_expression(1, operators = operators, variable_names = [])
5861
@test typeof(ex.tree) <: Node{Int64}
5962
end
6063

6164
# Or just a variable
62-
let ex = @parse_expression(x, operators = operators, variable_names = ["x"])
65+
@testitem "Or just a variable" begin
66+
using DynamicExpressions
67+
operators = OperatorEnum()
68+
ex = @parse_expression(x, operators = operators, variable_names = ["x"])
6369
@test typeof(ex.tree) <: Node{Float32}
6470
end
6571

6672
# Or, with custom node types
67-
let ex = @parse_expression(
73+
@testitem "Or, with custom node types" begin
74+
using DynamicExpressions
75+
operators = OperatorEnum()
76+
ex = @parse_expression(
6877
x, operators = operators, variable_names = ["x"], node_type = GraphNode
6978
)
7079
@test typeof(ex.tree) <: GraphNode{Float32}
7180
end
72-
let node_type = GraphNode,
73-
ex = @parse_expression(x, operators = operators, variable_names = ["x"], node_type)
7481

82+
83+
@testitem "With GraphNode" begin
84+
using DynamicExpressions
85+
86+
node_type = GraphNode
87+
operators = OperatorEnum()
88+
ex = @parse_expression(x, operators = operators, variable_names = ["x"], node_type)
7589
@test typeof(ex.tree) <: GraphNode{Float32}
7690
end
7791

78-
# Should work with symbols for variable names too
92+
@testitem "Should work with symbols for variable names too" begin
93+
using DynamicExpressions
7994
let ex = @parse_expression(
8095
cos(exp(α)),
8196
operators = OperatorEnum(; unary_operators=[cos, exp]),
@@ -85,8 +100,10 @@ let ex = @parse_expression(
85100
s = sprint((io, e) -> show(io, MIME("text/plain"), e), ex)
86101
@test s == "cos(exp(α))"
87102
end
103+
end
88104

89-
# This also works for parsing mixed types
105+
@testitem "This also works for parsing mixed types" begin
106+
using DynamicExpressions
90107
let v = [1, 2, 3],
91108
ex = @parse_expression(
92109
$v * tan(cos(5 + x)),
@@ -164,7 +181,7 @@ let v = [1, 2, 3],
164181
@test ex([1.0]) [1, 2, 3] * tan(cos(5 + 1.0))
165182
end
166183

167-
# Also check with tuple inputs
184+
@testitem "Also check with tuple inputs" begin
168185
let tu = (1.0, 2.0im),
169186
ex = @parse_expression(
170187
x * $tu - cos(y),
@@ -177,7 +194,11 @@ let tu = (1.0, 2.0im),
177194
@test s == "(x * ((1.0, 0.0 + 2.0im))) - cos(y)"
178195
@test typeof(ex.tree) <: Node{Tuple{Float64,ComplexF64}}
179196
end
197+
end
180198

199+
@testitem "interpolating custom function" begin
200+
using DynamicExpressions
201+
using Suppressor
181202
show_type(x) = (show(typeof(x)); x)
182203

183204
let
@@ -193,8 +214,10 @@ let
193214
end
194215
@test contains(logged_out, "Node{Float32}")
195216
end
217+
end
196218

197-
# Helpful errors for missing operator
219+
@testitem "Helpful errors for missing operator" begin
220+
using DynamicExpressions
198221
let operators = OperatorEnum(; unary_operators=[sin])
199222
@test_throws ArgumentError @parse_expression(
200223
cos(x), operators = operators, variable_names = [:x]
@@ -248,8 +271,15 @@ let operators = OperatorEnum(; unary_operators=[sin])
248271
)
249272
end
250273
end
274+
end
251275

252-
# Helpful error for missing function in scope
276+
@testitem "Helpful error for missing function in scope" begin
277+
using DynamicExpressions
278+
operators = OperatorEnum(;
279+
binary_operators=[+, -, *, /],
280+
unary_operators=[cos, sin],
281+
define_helper_functions=false,
282+
)
253283
my_badly_scoped_function(x) = x
254284
@test_throws ArgumentError begin
255285
ex = @parse_expression(
@@ -269,9 +299,14 @@ if VERSION >= v"1.9"
269299
)
270300
end
271301
end
302+
end
272303

273-
# Helpful error for missing variable name
274-
let
304+
@testitem "Helpful error for missing variable name" begin
305+
using DynamicExpressions
306+
operators = OperatorEnum(
307+
binary_operators=[+, -, *, /],
308+
unary_operators=[cos, sin],
309+
)
275310
@test_throws ArgumentError @parse_expression(
276311
x + y, operators = operators, variable_names = ["x"],
277312
)
@@ -283,14 +318,14 @@ let
283318
end
284319
end
285320

286-
# Helpful error for bad keyword
287-
let
321+
@testitem "Helpful error for bad keyword" begin
322+
using DynamicExpressions
288323
@test_throws LoadError @eval @parse_expression(x, variable_names = [:x], bad_arg = true)
289324
end
290325

291-
# Call function explicitly to get coverage
292-
let
293-
operators = OperatorEnum(; binary_operators=[+, *], unary_operators=[sin])
326+
@testitem "Call function explicitly to get coverage" begin
327+
import DynamicExpressions as DE
328+
operators = DE.OperatorEnum(; binary_operators=[+, *], unary_operators=[sin])
294329
d = 1
295330
ex = DE.parse_expression(
296331
:(a + $d * b * b * b * b + 1.5 * c + identity(sin(a)));
@@ -299,11 +334,12 @@ let
299334
variable_names=[:a, :b, :c],
300335
calling_module=@__MODULE__,
301336
)
302-
@test string_tree(ex) == "((a + ((((1.0 * b) * b) * b) * b)) + (1.5 * c)) + sin(a)"
337+
@test DE.string_tree(ex) == "((a + ((((1.0 * b) * b) * b) * b)) + (1.5 * c)) + sin(a)"
303338
end
304339

305-
# Test parsing of kws
306-
let
340+
@testitem "Test parsing of kws" begin
341+
using DynamicExpressions
342+
import DynamicExpressions as DE
307343
kws = [
308344
:(operators = OperatorEnum(; binary_operators=[+, *], unary_operators=[sin])),
309345
:(variable_names = [:x, :y]),
@@ -343,6 +379,9 @@ end
343379
s = sprint((io, e) -> show(io, MIME"text/plain"(), e), ex)
344380
@test s == "(x * x) - cos((0.3 * y) - 0.9)"
345381
end
382+
383+
@testitem "Misc tests" begin
384+
using DynamicExpressions
346385
ex = parse_expression(
347386
:(x);
348387
operators=OperatorEnum(; binary_operators=[+, -, *, /]),
@@ -351,5 +390,3 @@ end
351390
)
352391
@test string_tree(ex) == "x"
353392
end
354-
355-
# TODO: Test parsing with custom operators

0 commit comments

Comments
 (0)