Skip to content

Commit 47446ba

Browse files
committed
Test for custom code
1 parent 22925a6 commit 47446ba

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/@aml/@aml_create/get_struct_xml_.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ check function for the aml struct
66
function get_struct_xmlchecker(struct_function, args_var)
77
# aml Function
88
if !ismissing(struct_function[1])
9-
F=struct_function[1]
9+
F = struct_function[1]
1010
struct_xmlchecker = quote
1111
if !( ($(esc(F)))($(esc.(args_var)...)) )
1212
error("struct criteria function ($($(esc(F)))) isn't meet")

test/customcode.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Definition
2+
@aml mutable struct Student "student"
3+
4+
# add custom code to the begining of creator function
5+
# the following automatically fills IQ based on the name
6+
@creator begin
7+
if occursin("smart", name)
8+
name = replace(name, "-smart" => "") # remove smart from name
9+
IQ = "smart"
10+
elseif occursin("genius", name)
11+
name = replace(name, "-genius" => "") # remove smart from name
12+
IQ = "genius"
13+
else
14+
error("Give a smart student.")
15+
end
16+
end
17+
18+
name::String, "~"
19+
GPA::Float64, "~"
20+
IQ::UN{String} = nothing, att"~" # default to nothing, but filled automatically by first @cretor macro
21+
# add custom code to the end of extractor function
22+
23+
log::UN{String} = nothing, "~"
24+
25+
@extractor begin
26+
if GPA > 4.0
27+
log = "A genius with a GPA of $GPA is found" # setting fields using @extractor only changes the field and not the xml code
28+
end
29+
end
30+
end
31+
32+
@aml mutable struct MathClass "math-class"
33+
@creator begin
34+
GPAsum = 0.0
35+
for student in students
36+
GPAsum = GPAsum + student.GPA
37+
end
38+
average = GPAsum / length(students) # fill average field
39+
end
40+
41+
students::Vector{Student}, "student"
42+
average::UN{Float64} = nothing, "average" # calculated automatically
43+
end
44+
45+
################################################################
46+
# Creation
47+
smarts = [Student(name = "Jack-smart", GPA = 2.0), Student(name = "Sara-genius", GPA = 5.0)]
48+
mathclass = MathClass(students = smarts)
49+
50+
@testset "Custom Code - Creation" begin
51+
@test mathclass.students[1].name == "Jack"
52+
@test mathclass.students[2].name == "Sara"
53+
@test mathclass.average == 3.5
54+
@test stripall(@capture_out(pprint(mathclass))) == stripall("""
55+
<math-class>
56+
<student IQ="smart">
57+
<name>Jack</name>
58+
<GPA>2.0</GPA>
59+
</student>
60+
<student IQ="genius">
61+
<name>Sara</name>
62+
<GPA>5.0</GPA>
63+
</student>
64+
<average>3.5</average>
65+
</math-class>
66+
""")
67+
end
68+
69+
################################################################
70+
# Extraction
71+
72+
@testset "Custom Code - Extraction" begin
73+
xml = parsexml("""
74+
<math-class>
75+
<student>
76+
<name>Jack</name>
77+
<GPA>2.0</GPA>
78+
</student>
79+
<student>
80+
<name>Sara</name>
81+
<GPA>5.0</GPA>
82+
</student>
83+
<average>3.5</average>
84+
</math-class>
85+
""")
86+
87+
mathclass = MathClass(xml)
88+
89+
@test mathclass.students[2].log == "A genius with a GPA of 5.0 is found"
90+
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ using Test, Suppressor, DataFrames
33

44
stripall(x::String) = replace(x, r"\s|\n"=>"")
55

6+
##
67
include("struct_definition.jl")
78
include("creator.jl")
89
include("extractor.jl")
910
include("tables.jl")
1011
include("xmlutils.jl")
1112
# include("musicxml/musicxml.jl")
13+
include("customcode.jl")

0 commit comments

Comments
 (0)