Skip to content

Commit b07ceb2

Browse files
committed
marks: wrap the dict into a Marks type
In prevision of introducing "hard" marks, which are marks attached statically to a `@testset`, independent of subjects.
1 parent ca7caf2 commit b07ceb2

File tree

5 files changed

+39
-31
lines changed

5 files changed

+39
-31
lines changed

src/ReTest.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ mutable struct TestsetExpr
7171
parent::Maybe{TestsetExpr}
7272
children::Vector{TestsetExpr}
7373
strings::Vector{Union{String,Missing}}
74-
# Union to avoid creating a vector in most cases
75-
marks::Dict{String, Union{Symbol, Vector{Symbol}}} # TODO: should be a MultiDict
74+
marks::Marks
7675
# loopvalues & loopiters: when successful in evaluating loop values in resolve!,
7776
# we "flatten" the nested for loops into a single loop, with loopvalues
7877
# containing tuples of values, and loopiters the tuples of variables to which the
@@ -88,7 +87,7 @@ mutable struct TestsetExpr
8887

8988
TestsetExpr(source, mod, desc, options, loops, parent, children=TestsetExpr[]) =
9089
new(0, source, mod, desc, options, loops, parent, children, String[],
91-
Dict{String, Union{Symbol, Vector{Symbol}}}())
90+
Marks())
9291
end
9392

9493
isfor(ts::TestsetExpr) = ts.loops !== nothing

src/marks.jl

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
struct Marks
2+
# Union to avoid creating a vector in most cases
3+
soft::Dict{String, Union{Symbol, Vector{Symbol}}} # TODO: should be a MultiDict
4+
5+
Marks() = new(Dict{String, Union{Symbol, Vector{Symbol}}}())
6+
end
7+
18
const _pass = :__pass__
29
const _fail = :__fail__
310

411
# marks contains at most one of _pass, _fail
512

613
# return true (pass), false (fail), or nothing (unrun)
7-
function pastresult(marks::Dict, subject)
8-
ms = get(marks, subject, Symbol())
14+
function pastresult(marks::Marks, subject)
15+
ms = get(marks.soft, subject, Symbol())
916
sym::Symbol = ms isa Symbol ? ms : isempty(ms) ? Symbol() : ms[1]
1017
sym === _pass ? true : sym === _fail ? false : nothing
1118
end
1219

13-
function setresult!(marks::Dict, subject, success::Bool)
14-
ms = get(marks, subject, Symbol())
20+
function setresult!(marks::Marks, subject, success::Bool)
21+
soft = marks.soft
22+
ms = get(soft, subject, Symbol())
1523
res = success ? _pass : _fail
1624
if ms isa Symbol
1725
# ms could be Symbol() from get's default or delmark!
1826
if ms (_pass, _fail, Symbol())
19-
marks[subject] = res
27+
soft[subject] = res
2028
else
2129
# res always in first position
22-
marks[subject] = [res, ms]
30+
soft[subject] = [res, ms]
2331
end
2432
else # ms isa Vector
2533
if !isempty(ms) && ms[1] (_pass, _fail)
@@ -30,8 +38,8 @@ function setresult!(marks::Dict, subject, success::Bool)
3038
end
3139
end
3240

33-
function markiter(marks, subject, skipres::Bool)
34-
ms = get(marks, subject, Symbol())
41+
function markiter(marks::Marks, subject, skipres::Bool)
42+
ms = get(marks.soft, subject, Symbol())
3543
if ms isa Symbol
3644
if ms === Symbol() || skipres && ms (_pass, _fail)
3745
()
@@ -47,16 +55,17 @@ function markiter(marks, subject, skipres::Bool)
4755
end
4856
end
4957

50-
function addmark!(marks, subject, m::Symbol)
51-
ms = get(marks, subject, Symbol())
58+
function addmark!(marks::Marks, subject, m::Symbol)
59+
soft = marks.soft
60+
ms = get(soft, subject, Symbol())
5261
if ms isa Symbol
5362
if ms === m
5463
false
5564
elseif ms === Symbol()
56-
marks[subject] = m
65+
soft[subject] = m
5766
true
5867
else
59-
marks[subject] = [ms, m]
68+
soft[subject] = [ms, m]
6069
true
6170
end
6271
elseif findfirst(==(m), ms) === nothing
@@ -67,11 +76,12 @@ function addmark!(marks, subject, m::Symbol)
6776
end
6877
end
6978

70-
function delmark!(marks, subject, m::Symbol)
71-
ms = get(marks, subject, Symbol())
79+
function delmark!(marks::Marks, subject, m::Symbol)
80+
soft = marks.soft
81+
ms = get(soft, subject, Symbol())
7282
if ms isa Symbol
7383
if ms === m
74-
marks[subject] = Symbol()
84+
soft[subject] = Symbol()
7585
end
7686
else
7787
p = findfirst(==(m), ms)
@@ -82,8 +92,8 @@ function delmark!(marks, subject, m::Symbol)
8292
nothing
8393
end
8494

85-
function hasmark(marks, subject, m::Symbol)
86-
ms = get(marks, subject, Symbol())
95+
function hasmark(marks::Marks, subject, m::Symbol)
96+
ms = get(marks.soft, subject, Symbol())
8797
if ms isa Symbol
8898
ms === m
8999
else

src/patterns.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ matches(d::Depth, _, ts) = d.d == tsdepth(ts)
144144
matches(::Pass, subj::AbstractString, ts) = something(pastresult(ts.marks, subj), false)
145145
matches(::Fail, subj::AbstractString, ts) = !something(pastresult(ts.marks, subj), true)
146146
# TODO: test method below
147-
# instead of `isempty(ts.marks)`, a tighter test would be to check that no value in marks
147+
# instead of `isempty(ts.marks.soft)`, a tighter test would be to check that no value in marks
148148
# contains the pass/fail marks; but this would be a bit more expensive, as values have to be
149149
# iterated over
150150
matches(::Union{Pass,Fail}, ::Missing, ts) =
151-
isempty(ts.marks) ? false : missing
151+
isempty(ts.marks.soft) ? false : missing
152152

153153
matches(i::Iter, subj, ts) =
154154
if ts.iter isa Int

src/testset.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using Distributed: myid, nworkers
1414

1515
import InlineTest: @testset
1616

17-
using ..ReTest: Pattern, matches, setresult!
17+
using ..ReTest: Pattern, Marks, matches, setresult!
1818

1919
# mostly copied from Test stdlib
2020
# changed from Test: pass nothing as file in ip_has_file_and_func
@@ -65,7 +65,7 @@ mutable struct ReTestSet <: AbstractTestSet
6565
subject::String
6666
id::Int64
6767
overall::Bool # TODO: could be conveyed by having self.mod == ""
68-
marks::Dict{String, Union{Symbol, Vector{Symbol}}} # used in matches()
68+
marks::Marks # used in matches()
6969
iter::Int # used in matches()
7070
results::Vector
7171
n_passed::Int
@@ -77,8 +77,7 @@ end
7777

7878
function ReTestSet(mod, desc::String, id::Integer=0;
7979
overall=false, verbose=true, parent=nothing,
80-
marks=Dict{String, Union{Symbol, Vector{Symbol}}}(),
81-
iter=1)
80+
marks=Marks(), iter=1)
8281
parentsubj = parent === nothing ? "" : parent.subject
8382
subject = string(parentsubj, '/', desc)
8483
ReTestSet(mod, parent, desc, subject, id, overall, marks, iter, [],
@@ -474,13 +473,13 @@ end
474473

475474
# non-inline testset with regex filtering support
476475
macro testset(mod::Module, isfinal::Bool, pat::Pattern, id::Int64, desc::Union{String,Expr},
477-
options, marks::Dict, stats::Bool, chan, body)
476+
options, marks::Marks, stats::Bool, chan, body)
478477
Testset.testset_beginend(mod, isfinal, pat, id, desc,
479478
options, marks, stats, chan, body, __source__)
480479
end
481480

482481
macro testset(mod::Module, isfinal::Bool, pat::Pattern, id::Int64, desc::Union{String,Expr},
483-
options, marks::Dict, stats::Bool, chan, loops, body)
482+
options, marks::Marks, stats::Bool, chan, loops, body)
484483
Testset.testset_forloop(mod, isfinal, pat, id, desc,
485484
options, marks, stats, chan, loops, body, __source__)
486485
end
@@ -489,7 +488,7 @@ end
489488
Generate the code for a `@testset` with a `begin`/`end` argument
490489
"""
491490
function testset_beginend(mod::Module, isfinal::Bool, pat::Pattern, id::Int64, desc, options,
492-
marks::Dict, stats::Bool, chan, tests, source)
491+
marks::Marks, stats::Bool, chan, tests, source)
493492
# Generate a block of code that initializes a new testset, adds
494493
# it to the task local storage, evaluates the test(s), before
495494
# finally removing the testset and giving it a chance to take
@@ -548,7 +547,7 @@ end
548547
Generate the code for a `@testset` with a `for` loop argument
549548
"""
550549
function testset_forloop(mod::Module, isfinal::Bool, pat::Pattern, id::Int64,
551-
desc::Union{String,Expr}, options, marks::Dict, stats, chan,
550+
desc::Union{String,Expr}, options, marks::Marks, stats, chan,
552551
loops, tests, source)
553552

554553
desc = esc(desc)

test/test_patterns.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct MockTestset
1010
parent
1111
iter
1212

13-
MockTestset() = new(rand(1:typemax(Int)), Dict(), nothing, 1)
13+
MockTestset() = new(rand(1:typemax(Int)), ReTest.Marks(), nothing, 1)
1414
end
1515

1616
ReTest.tsdepth(::MockTestset) = 1

0 commit comments

Comments
 (0)