Repository: JuliaLang/julia
File: stdlib/Test/src/Test.jl (Julia)

Code before:
    new_error = Error(t.test_type, t.orig_expr, t.value, t.backtrace, context, t.source)
    record(c.parent_ts, new_error)
end

#-----------------------------------------------------------------------

"""
    DefaultTestSet

If using the DefaultTestSet, the test results will be recorded. If there
are any `Fail`s or `Error`s, an exception will be thrown only at the end,
along with a summary of the test results.
"""
mutable struct DefaultTestSet <: AbstractTestSet
    description::String
    results::Vector{Any}
    n_passed::Int
    anynonpass::Bool
    verbose::Bool
    showtiming::Bool
    time_start::Float64
    time_end::Union{Float64,Nothing}
    failfast::Bool
    file::Union{String,Nothing}
    rng::Union{Nothing,AbstractRNG}
end
function DefaultTestSet(desc::AbstractString; verbose::Bool = false, showtiming::Bool = true, failfast::Union{Nothing,Bool} = nothing, source = nothing, rng = nothing)
    if isnothing(failfast)
        # pass failfast state into child testsets
        parent_ts = get_testset()
        if parent_ts isa DefaultTestSet
            failfast = parent_ts.failfast
        else
            failfast = false
        end
    end
    return DefaultTestSet(String(desc)::String, [], 0, false, verbose, showtiming, time(), nothing, failfast, extract_file(source), rng)
end
extract_file(source::LineNumberNode) = extract_file(source.file)
extract_file(file::Symbol) = string(file)
extract_file(::Nothing) = nothing

struct FailFastError <: Exception end

# For a broken result, simply store the result
record(ts::DefaultTestSet, t::Broken) = (push!(ts.results, t); t)
# For a passed result, do not store the result since it uses a lot of memory, unless
# `record_passes()` is true. i.e. set env var `JULIA_TEST_RECORD_PASSES=true` before running any testsets
function record(ts::DefaultTestSet, t::Pass)
    ts.n_passed += 1
    if record_passes()

Review comment (refactor):
e.g.
```suggestion
    # Warning: `results_lock` must be held before accessing any of the following fields
    rng::Union{Nothing,AbstractRNG}
    n_passed::Int
    time_end::Float64
```

Code after:
    new_error = Error(t.test_type, t.orig_expr, t.value, t.backtrace, context, t.source)
    record(c.parent_ts, new_error)
end

#-----------------------------------------------------------------------

"""
    DefaultTestSet

If using the DefaultTestSet, the test results will be recorded. If there
are any `Fail`s or `Error`s, an exception will be thrown only at the end,
along with a summary of the test results.
"""
mutable struct DefaultTestSet <: AbstractTestSet
    const description::String
    const verbose::Bool
    const showtiming::Bool
    const failfast::Bool
    const file::Union{String,Nothing}
    const time_start::Float64

    # Warning: Not thread-safe
    rng::Union{Nothing,AbstractRNG}

    @atomic n_passed::Int
    @atomic time_end::Float64

    # Memoized test result state over `results` - Computed only once the test set is finished
    # 0x0: Unknown
    # 0x1: All passed
    # 0x2: Some failed
    @atomic anynonpass::UInt8

    results_lock::ReentrantLock
    results::Vector{Any}
end
function DefaultTestSet(desc::AbstractString; verbose::Bool = false, showtiming::Bool = true, failfast::Union{Nothing,Bool} = nothing, source = nothing, rng = nothing)
    if isnothing(failfast)
        # pass failfast state into child testsets
        parent_ts = get_testset()
        if parent_ts isa DefaultTestSet
            failfast = parent_ts.failfast
        else
            failfast = false
        end
    end
    return DefaultTestSet(String(desc)::String,
        verbose, showtiming, failfast, extract_file(source),
        time(), rng, 0, 0., 0x00, ReentrantLock(), Any[])
end
extract_file(source::LineNumberNode) = extract_file(source.file)