Skip to content

Commit

Permalink
split out TestHelpers into separate parts (#28292)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 27, 2018
1 parent 4b8c697 commit ca7a692
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 120 deletions.
4 changes: 2 additions & 2 deletions stdlib/LibGit2/test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using Test
using Random, Serialization, Sockets

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
import .Main.TestHelpers: with_fake_pty
isdefined(Main, :FakePTYs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FakePTYs.jl"))
import .Main.FakePTYs: with_fake_pty

function challenge_prompt(code::Expr, challenges; timeout::Integer=60, debug::Bool=true)
input_code = tempname()
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ end
end

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
using .Main.OffsetArrays

@testset "offset axes" begin
s = Base.Slice(-3:3)'
Expand Down
6 changes: 4 additions & 2 deletions stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,10 @@ end

# dimensional correctness:
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
using .Main.TestHelpers: Furlong
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
using .Main.Furlongs
LinearAlgebra.sylvester(a::Furlong,b::Furlong,c::Furlong) = -c / (a + b)

let A = UpperTriangular([Furlong(1) Furlong(4); Furlong(0) Furlong(1)])
@test sqrt(A) == Furlong{1//2}.(UpperTriangular([1 2; 0 1]))
end
Expand Down
4 changes: 0 additions & 4 deletions stdlib/REPL/test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import REPL.LineEdit: edit_insert, buffer, content, setmark, getmark, region
include("FakeTerminals.jl")
import .FakeTerminals.FakeTerminal

const BASE_TEST_PATH = joinpath(@__DIR__, "..", "..", "..", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
import .Main.TestHelpers

# no need to have animation in tests
REPL.GlobalOptions.region_animation_duration=0.001

Expand Down
6 changes: 3 additions & 3 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import REPL.LineEdit
using Markdown

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
import .Main.TestHelpers
isdefined(Main, :FakePTYs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FakePTYs.jl"))
import .Main.FakePTYs: with_fake_pty

# For curmod_*
include(joinpath(BASE_TEST_PATH, "testenv.jl"))
Expand Down Expand Up @@ -719,7 +719,7 @@ ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 1)
let exename = Base.julia_cmd()
# Test REPL in dumb mode
if !Sys.iswindows()
TestHelpers.with_fake_pty() do slave, master
with_fake_pty() do slave, master
nENV = copy(ENV)
nENV["TERM"] = "dumb"
p = run(setenv(`$exename --startup-file=no -q`,nENV),slave,slave,slave,wait=false)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ using Test, SparseArrays
using Test: guardsrand

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
using .Main.OffsetArrays

using Random
using Random.DSFMT
Expand Down
4 changes: 2 additions & 2 deletions stdlib/Statistics/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ end

# dimensional correctness
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
using .Main.TestHelpers: Furlong
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
using .Main.Furlongs

Statistics.middle(x::Furlong{p}) where {p} = Furlong{p}(middle(x.val))
Statistics.middle(x::Furlong{p}, y::Furlong{p}) where {p} = Furlong{p}(middle(x.val, y.val))
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include $(JULIAHOME)/Make.inc
TESTGROUPS = unicode strings compiler
TESTS = all stdlib $(TESTGROUPS) \
$(patsubst $(STDLIBDIR)/%/,%,$(dir $(wildcard $(STDLIBDIR)/*/.))) \
$(filter-out TestHelpers runtests testdefs, \
$(filter-out runtests testdefs, \
$(patsubst $(SRCDIR)/%.jl,%,$(wildcard $(SRCDIR)/*.jl))) \
$(foreach group,$(TESTGROUPS), \
$(patsubst $(SRCDIR)/%.jl,%,$(wildcard $(SRCDIR)/$(group)/*.jl)))
Expand Down
4 changes: 2 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Array test
isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays
using SparseArrays

using Random, LinearAlgebra
Expand Down
4 changes: 2 additions & 2 deletions test/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ include("testenv.jl")

using Test, Serialization

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using Main.TestHelpers
isdefined(Main, :MacroCalls) || @eval Main include("testhelpers/MacroCalls.jl")
using Main.MacroCalls

@test_throws MethodError convert(Enum, 1.0)

Expand Down
4 changes: 2 additions & 2 deletions test/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Base.CoreLogging: BelowMinLevel, Debug, Info, Warn, Error,
import Test: collect_test_logs, TestLogger
using Base.Printf: @sprintf

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using Main.TestHelpers
isdefined(Main, :MacroCalls) || @eval Main include("testhelpers/MacroCalls.jl")
using Main.MacroCalls

#-------------------------------------------------------------------------------
@testset "Logging" begin
Expand Down
4 changes: 2 additions & 2 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,8 @@ end
end
end

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers: Furlong
isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
using .Main.Furlongs
@test hypot(Furlong(0), Furlong(0)) == Furlong(0.0)
@test hypot(Furlong(3), Furlong(4)) == Furlong(5.0)
@test hypot(Furlong(NaN), Furlong(Inf)) == Furlong(Inf)
Expand Down
6 changes: 3 additions & 3 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays
using DelimitedFiles
using Random
using LinearAlgebra
using Statistics

const OAs_name = join(fullname(OAs), ".")
const OAs_name = join(fullname(OffsetArrays), ".")

let
# Basics
Expand Down
14 changes: 8 additions & 6 deletions test/ranges.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Dates, Random
isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
isdefined(Main, :PhysQuantities) || @eval Main include("testhelpers/PhysQuantities.jl")
using .Main.PhysQuantities

# Compare precision in a manner sensitive to subnormals, which lose
# precision compared to widening.
Expand Down Expand Up @@ -187,10 +188,10 @@ end
@test isnan(Float64(x0/0))
@test isnan(Float64(x0/0.0))

x = Base.TwicePrecision(Main.TestHelpers.PhysQuantity{1}(4.0))
@test x.hi*2 === Main.TestHelpers.PhysQuantity{1}(8.0)
x = Base.TwicePrecision(PhysQuantity{1}(4.0))
@test x.hi*2 === PhysQuantity{1}(8.0)
@test_throws ErrorException("Int is incommensurate with PhysQuantity") x*2 # not a MethodError for convert
@test x.hi/2 === Main.TestHelpers.PhysQuantity{1}(2.0)
@test x.hi/2 === PhysQuantity{1}(2.0)
@test_throws ErrorException("Int is incommensurate with PhysQuantity") x/2
end
@testset "ranges" begin
Expand Down Expand Up @@ -1224,8 +1225,9 @@ Base.rem(x, y::NotReal) = rem(x, y.val)
Base.isless(x, y::NotReal) = isless(x, y.val)
@test (:)(1, NotReal(1), 5) isa StepRange{Int,NotReal}

isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers: Furlong
isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
using .Main.Furlongs

@testset "dimensional correctness" begin
@test length(Vector(Furlong(2):Furlong(10))) == 9
@test length(range(Furlong(2), length=9)) == 9
Expand Down
4 changes: 2 additions & 2 deletions test/reduce.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Random
isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

# fold(l|r) & mapfold(l|r)
@test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160)
Expand Down
4 changes: 2 additions & 2 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Test
isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

A = Int64[1, 2, 3, 4]
B = Complex{Int64}[5+6im, 7+8im, 9+10im]
Expand Down
4 changes: 2 additions & 2 deletions test/sets.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Set tests
isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using .Main.TestHelpers.OAs
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

using Dates

Expand Down
39 changes: 39 additions & 0 deletions test/testhelpers/FakePTYs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

module FakePTYs

function open_fake_pty()
@static if Sys.iswindows()
error("Unable to create a fake PTY in Windows")
end

O_RDWR = Base.Filesystem.JL_O_RDWR
O_NOCTTY = Base.Filesystem.JL_O_NOCTTY

fdm = ccall(:posix_openpt, Cint, (Cint,), O_RDWR|O_NOCTTY)
fdm == -1 && error("Failed to open PTY master")
rc = ccall(:grantpt, Cint, (Cint,), fdm)
rc != 0 && error("grantpt failed")
rc = ccall(:unlockpt, Cint, (Cint,), fdm)
rc != 0 && error("unlockpt")

fds = ccall(:open, Cint, (Ptr{UInt8}, Cint),
ccall(:ptsname, Ptr{UInt8}, (Cint,), fdm), O_RDWR|O_NOCTTY)

# slave
slave = RawFD(fds)
master = Base.TTY(RawFD(fdm); readable = true)
slave, master
end

function with_fake_pty(f)
slave, master = open_fake_pty()
try
f(slave, master)
finally
ccall(:close,Cint,(Cint,),slave) # XXX: this causes the kernel to throw away all unread data on the pty
close(master)
end
end

end
11 changes: 7 additions & 4 deletions test/dimensionful.jl → test/testhelpers/Furlongs.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

module Furlongs

export Furlong

# Here we implement a minimal dimensionful type Furlong, which is used
# to test dimensional correctness of various functions in Base. Furlong
# is exported by the TestHelpers module.
# to test dimensional correctness of various functions in Base.

# represents a quantity in furlongs^p
struct Furlong{p,T<:Number} <: Number
Expand Down Expand Up @@ -34,8 +37,6 @@ canonical_p(p) = isinteger(p) ? Int(p) : Rational{Int}(p)
Base.abs(x::Furlong{p}) where {p} = Furlong{p}(abs(x.val))
@generated Base.abs2(x::Furlong{p}) where {p} = :(Furlong{$(canonical_p(2p))}(abs2(x.val)))
@generated Base.inv(x::Furlong{p}) where {p} = :(Furlong{$(canonical_p(-p))}(inv(x.val)))
import LinearAlgebra: sylvester
sylvester(a::Furlong,b::Furlong,c::Furlong) = -c / (a + b)

for f in (:isfinite, :isnan, :isreal, :isinf)
@eval Base.$f(x::Furlong) = $f(x.val)
Expand Down Expand Up @@ -76,3 +77,5 @@ for op in (:rem, :mod)
end
end
Base.sqrt(x::Furlong) = _div(sqrt(x.val), x, Val(2))

end
15 changes: 15 additions & 0 deletions test/testhelpers/MacroCalls.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module MacroCalls

export @macrocall

macro macrocall(ex)
@assert Meta.isexpr(ex, :macrocall)
ex.head = :call
for i in 2:length(ex.args)
ex.args[i] = QuoteNode(ex.args[i])
end
insert!(ex.args, 3, __module__)
return esc(ex)
end

end
Loading

0 comments on commit ca7a692

Please sign in to comment.