Skip to content

Commit

Permalink
move Test from Base to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Sep 29, 2017
1 parent 4a6e64d commit d759779
Show file tree
Hide file tree
Showing 75 changed files with 297 additions and 259 deletions.
97 changes: 23 additions & 74 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,26 @@ macro deprecate_binding(old, new, export_old=true)
Expr(:call, :deprecate, __module__, Expr(:quote, old)))
end

macro deprecate_moved(old, new, export_old=true)
macro deprecate_moved(old, new, export_old=true, default_package=false)
eold = esc(old)
return Expr(:toplevel,
:(function $eold(args...; kwargs...)
error($eold, " has been moved to the package ", $new, ".jl.\n",
"Run `Pkg.add(\"", $new, "\")` to install it, restart Julia,\n",
"and then run `using ", $new, "` to load it.")
end),
default_package ? :(function $eold(args...; kwargs...)
error($eold, " has been moved to the standard library package ", $new, ".\n",
"Restart Julia and then run `using ", $new, "` to load it.")
end) :
:(function $eold(args...; kwargs...)
error($eold, " has been moved to the package ", $new, ".jl.\n",
"Run `Pkg.add(\"", $new, "\")` to install it, restart Julia,\n",
"and then run `using ", $new, "` to load it.")
end),
export_old ? Expr(:export, eold) : nothing,
Expr(:call, :deprecate, __module__, Expr(:quote, old), 2))
end

# BEGIN 0.6-alpha deprecations (delete when 0.6 is released)

@deprecate isambiguous(m1::Method, m2::Method, b::Bool) isambiguous(m1, m2, ambiguous_bottom=b) false
# TODO: delete allow_bottom keyword code in Base.Test.detect_ambiguities
# TODO: delete allow_bottom keyword code in Test.detect_ambiguities

# END 0.6-alpha deprecations

Expand Down Expand Up @@ -996,73 +1000,6 @@ iteratoreltype(::Type{Task}) = EltypeUnknown()

isempty(::Task) = error("isempty not defined for Tasks")

@eval Base.Test begin
approx_full(x::AbstractArray) = x
approx_full(x::Number) = x
approx_full(x) = full(x)

function test_approx_eq(va, vb, Eps, astr, bstr)
va = approx_full(va)
vb = approx_full(vb)
la, lb = length(linearindices(va)), length(linearindices(vb))
if la != lb
error("lengths of ", astr, " and ", bstr, " do not match: ",
"\n ", astr, " (length $la) = ", va,
"\n ", bstr, " (length $lb) = ", vb)
end
diff = real(zero(eltype(va)))
for (xa, xb) = zip(va, vb)
if isfinite(xa) && isfinite(xb)
diff = max(diff, abs(xa-xb))
elseif !isequal(xa,xb)
error("mismatch of non-finite elements: ",
"\n ", astr, " = ", va,
"\n ", bstr, " = ", vb)
end
end

if !isnan(Eps) && !(diff <= Eps)
sdiff = string("|", astr, " - ", bstr, "| <= ", Eps)
error("assertion failed: ", sdiff,
"\n ", astr, " = ", va,
"\n ", bstr, " = ", vb,
"\n difference = ", diff, " > ", Eps)
end
end

array_eps(a::AbstractArray{Complex{T}}) where {T} = eps(float(maximum(x->(isfinite(x) ? abs(x) : T(NaN)), a)))
array_eps(a) = eps(float(maximum(x->(isfinite(x) ? abs(x) : oftype(x,NaN)), a)))

test_approx_eq(va, vb, astr, bstr) =
test_approx_eq(va, vb, 1E4*length(linearindices(va))*max(array_eps(va), array_eps(vb)), astr, bstr)

"""
@test_approx_eq_eps(a, b, tol)
Test two floating point numbers `a` and `b` for equality taking into account
a margin of tolerance given by `tol`.
"""
macro test_approx_eq_eps(a, b, c)
Base.depwarn(string("@test_approx_eq_eps is deprecated, use `@test ", a, "", b, " atol=", c, "` instead"),
Symbol("@test_approx_eq_eps"))
:(test_approx_eq($(esc(a)), $(esc(b)), $(esc(c)), $(string(a)), $(string(b))))
end
export @test_approx_eq_eps

"""
@test_approx_eq(a, b)
Deprecated. Test two floating point numbers `a` and `b` for equality taking into
account small numerical errors.
"""
macro test_approx_eq(a, b)
Base.depwarn(string("@test_approx_eq is deprecated, use `@test ", a, "", b, "` instead"),
Symbol("@test_approx_eq"))
:(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b))))
end
export @test_approx_eq
end

# Deprecate Array(T, dims...) in favor of proper type constructors
@deprecate Array(::Type{T}, d::NTuple{N,Int}) where {T,N} Array{T}(d)
@deprecate Array(::Type{T}, d::Int...) where {T} Array{T}(d...)
Expand Down Expand Up @@ -1429,6 +1366,18 @@ end
using .DSP
export conv, conv2, deconv, filt, filt!, xcorr

module Test
for f in [Symbol("@inferred"), Symbol("@test"), Symbol("@test_approx_eq"),
Symbol("@test_approx_eq_eps"), Symbol("@test_broken"), Symbol("@test_nowarn"),
Symbol("@test_skip"), Symbol("@test_throws"), Symbol("@test_warn"),
Symbol("@testset"), :GenericArray, :GenericDict, :GenericSet, :GenericString,
:detect_ambiguities, :detect_unbound_args]
@eval Base.@deprecate_moved $f "Test" true true
end
end
export Test
deprecate(@__MODULE__, :Test)

# PR #21709
@deprecate cov(x::AbstractVector, corrected::Bool) cov(x, corrected=corrected)
@deprecate cov(x::AbstractMatrix, vardim::Int, corrected::Bool) cov(x, vardim, corrected=corrected)
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export
Profile,
Dates,
Sys,
Test,
Libc,
Libdl,
Mmap,
Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ include("deepcopy.jl")
include("interactiveutil.jl")
include("summarysize.jl")
include("replutil.jl")
include("test.jl")
include("i18n.jl")
using .I18n

Expand Down Expand Up @@ -456,6 +455,7 @@ Base.init_load_path(ccall(:jl_get_julia_home, Any, ()))

# load some stdlib packages but don't put their names in Main
Base.require(:DelimitedFiles)
Base.require(:Test)

empty!(LOAD_PATH)

Expand Down
6 changes: 4 additions & 2 deletions doc/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ end
# make links for stdlib package docs
if Sys.iswindows()
cp("../stdlib/DelimitedFiles/docs/src/index.md", "src/stdlib/delimitedfiles.md")
cp("../stdlib/Test/docs/src/index.md", "src/stdlib/test.md")
else
symlink("../../../stdlib/DelimitedFiles/docs/src/index.md", "src/stdlib/delimitedfiles.md")
symlink("../../../stdlib/Test/docs/src/index.md", "src/stdlib/test.md")
end

const PAGES = [
Expand Down Expand Up @@ -124,11 +126,11 @@ const PAGES = [
],
]

using DelimitedFiles
using DelimitedFiles, Test

makedocs(
build = joinpath(pwd(), "_build/html/en"),
modules = [Base, Core, BuildSysImg, DelimitedFiles],
modules = [Base, Core, BuildSysImg, DelimitedFiles, Test],
clean = false,
doctest = "doctest" in ARGS,
linkcheck = "linkcheck" in ARGS,
Expand Down
1 change: 1 addition & 0 deletions doc/src/stdlib/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
delimitedfiles.md
test.md
2 changes: 1 addition & 1 deletion examples/embedding/embedding-test.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# tests the output of the embedding example is correct
using Base.Test
using Test

if Sys.iswindows()
# libjulia needs to be in the same directory as the embedding executable or in path
Expand Down
2 changes: 1 addition & 1 deletion examples/juliatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ tndict[AbstractArray.name] = AbstractArrayT.T.T.name
tndict[Array.name] = ArrayT.T.T.name
tndict[Pair.name] = PairT.T.T.name

using Base.Test
using Test

issub_strict(x,y) = issub(x,y) && !issub(y,x)

Expand Down
2 changes: 1 addition & 1 deletion stdlib/DelimitedFiles/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Base.Test
using Test
using DelimitedFiles

isequaldlm(m1, m2, t) = isequal(m1, m2) && (eltype(m1) == eltype(m2) == t)
Expand Down
44 changes: 22 additions & 22 deletions doc/src/stdlib/test.md → stdlib/Test/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```@meta
DocTestSetup = quote
using Base.Test
using Test
end
```

Expand All @@ -18,22 +18,22 @@ Base.runtests

## Basic Unit Tests

The `Base.Test` module provides simple *unit testing* functionality. Unit testing is a way to
The `Test` module provides simple *unit testing* functionality. Unit testing is a way to
see if your code is correct by checking that the results are what you expect. It can be helpful
to ensure your code still works after you make changes, and can be used when developing as a way
of specifying the behaviors your code should have when complete.

Simple unit testing can be performed with the `@test` and `@test_throws` macros:

```@docs
Base.Test.@test
Base.Test.@test_throws
Test.@test
Test.@test_throws
```

For example, suppose we want to check our new function `foo(x)` works as expected:

```jldoctest testfoo
julia> using Base.Test
julia> using Test
julia> foo(x) = length(x)^2
foo (generic function with 1 method)
Expand Down Expand Up @@ -100,7 +100,7 @@ be run, and at the end of the test set a summary will be printed. If any of the
or could not be evaluated due to an error, the test set will then throw a `TestSetException`.

```@docs
Base.Test.@testset
Test.@testset
```

We can put our tests for the `foo(x)` function in a test set:
Expand Down Expand Up @@ -181,9 +181,9 @@ ERROR: There was an error during testing
```

```@docs
Base.Test.@inferred
Base.Test.@test_warn
Base.Test.@test_nowarn
Test.@inferred
Test.@test_warn
Test.@test_nowarn
```

## Broken Tests
Expand All @@ -193,14 +193,14 @@ the test as `Broken` if the test continues to fail and alerts the user via an `E
succeeds.

```@docs
Base.Test.@test_broken
Test.@test_broken
```

`@test_skip` is also available to skip a test without evaluation, but counting the skipped test
in the test set reporting. The test will not run but gives a `Broken` `Result`.

```@docs
Base.Test.@test_skip
Test.@test_skip
```

## Creating Custom `AbstractTestSet` Types
Expand All @@ -210,32 +210,32 @@ methods. The subtype should have a one-argument constructor taking a description
any options passed in as keyword arguments.

```@docs
Base.Test.record
Base.Test.finish
Test.record
Test.finish
```

`Base.Test` takes responsibility for maintaining a stack of nested testsets as they are executed,
`Test` takes responsibility for maintaining a stack of nested testsets as they are executed,
but any result accumulation is the responsibility of the `AbstractTestSet` subtype. You can access
this stack with the `get_testset` and `get_testset_depth` methods. Note that these functions are
not exported.

```@docs
Base.Test.get_testset
Base.Test.get_testset_depth
Test.get_testset
Test.get_testset_depth
```

`Base.Test` also makes sure that nested `@testset` invocations use the same `AbstractTestSet`
`Test` also makes sure that nested `@testset` invocations use the same `AbstractTestSet`
subtype as their parent unless it is set explicitly. It does not propagate any properties of the
testset. Option inheritance behavior can be implemented by packages using the stack infrastructure
that `Base.Test` provides.
that `Test` provides.

Defining a basic `AbstractTestSet` subtype might look like:

```julia
import Base.Test: record, finish
using Base.Test: AbstractTestSet, Result, Pass, Fail, Error
using Base.Test: get_testset_depth, get_testset
struct CustomTestSet <: Base.Test.AbstractTestSet
import Test: record, finish
using Test: AbstractTestSet, Result, Pass, Fail, Error
using Test: get_testset_depth, get_testset
struct CustomTestSet <: Test.AbstractTestSet
description::AbstractString
foo::Int
results::Vector
Expand Down
Loading

0 comments on commit d759779

Please sign in to comment.