Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use is_juliarepr more in show.jl testing #54304

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 24 additions & 23 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ end
@test repr(:(∓ 1)) == ":(∓1)"
@test repr(:(± 1)) == ":(±1)"

eval_parse_repr(x) = eval(Meta.parse(repr(x)))
eval_parse_repr_test(x) = @test eval_parse_repr(x) == x
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the line numbers reported by a test failure worse.

julia> using Test

julia> @test 1==2
Test Failed at REPL[2]:1 <---- Just entered
  Expression: 1 == 2
   Evaluated: 1 == 2

ERROR: There was an error during testing

julia> @test 1==3
Test Failed at REPL[3]:1 <---- Just entered
  Expression: 1 == 3
   Evaluated: 1 == 3

ERROR: There was an error during testing

julia> test_eq_1(x) = @test x == 1
test_eq_1 (generic function with 1 method)

julia> test_eq_1(2)
Test Failed at REPL[4]:1 <----- where function defined
  Expression: x == 1
   Evaluated: 2 == 1

ERROR: There was an error during testing

julia> test_eq_1(3)
Test Failed at REPL[4]:1 <----- where function defined
  Expression: x == 1
   Evaluated: 3 == 1

ERROR: There was an error during testing

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just make it return a bool and add back the @test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have either line numbers or display the actual/expected values. I prefer displaying the actual/expected values (this) because we can always get line numbers from stack traces.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? As is, this is just worse than before. Finding the line number in the stacktrace is often not easy.

Just inline it back if it is an issue. Going around DRY-ing test code is seldom a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

julia> f(x) = x == 5
f (generic function with 1 method)

julia> @test f(4)
Test Failed at REPL[15]:1
  Expression: f(4)

ERROR: There was an error during testing

julia> g(x) = @test x == 5
g (generic function with 1 method)

julia> g(4)
Test Failed at REPL[16]:1
  Expression: x == 5
   Evaluated: 4 == 5

ERROR: There was an error during testing

for ex in [Expr(:call, :f, Expr(:(=), :x, 1)),
Expr(:ref, :f, Expr(:(=), :x, 1)),
Expr(:vect, 1, 2, Expr(:kw, :x, 1)),
Expand All @@ -260,7 +262,7 @@ for ex in [Expr(:call, :f, Expr(:(=), :x, 1)),
Expr(:call, :*, 0, :x01),
Expr(:call, :*, 0, :b01),
Expr(:call, :*, 0, :o01)]
@test eval(Meta.parse(repr(ex))) == ex
@test eval_parse_repr(ex) == ex
end

@test repr(Expr(:using, :Foo)) == ":(\$(Expr(:using, :Foo)))"
Expand Down Expand Up @@ -294,8 +296,8 @@ end
@test_repr "3:4:5"
let ex4 = Expr(:call, :(:), 1, 2, 3, 4),
ex1 = Expr(:call, :(:), 1)
@test eval(Meta.parse(repr(ex4))) == ex4
@test eval(Meta.parse(repr(ex1))) == ex1
@test eval_parse_repr(ex4) == ex4
@test eval_parse_repr(ex1) == ex1
end

# Complex
Expand Down Expand Up @@ -1716,36 +1718,35 @@ let x = TypeVar(:_, Number), y = TypeVar(:_, Number)
end


is_juliarepr(x) = eval(Meta.parse(repr(x))) == x
@testset "unionall types" begin
X = TypeVar(gensym())
Y = TypeVar(gensym(), Ref, Ref)
x, y, z = TypeVar(:a), TypeVar(:a), TypeVar(:a)
struct TestTVUpper{A<:Integer} end

# named typevars
@test is_juliarepr(Ref{A} where A)
@test is_juliarepr(Ref{A} where A>:Ref)
@test is_juliarepr(Ref{A} where A<:Ref)
@test is_juliarepr(Ref{A} where Ref<:A<:Ref)
@test is_juliarepr(TestTVUpper{<:Real})
@test is_juliarepr(TestTVUpper{<:Integer})
@test is_juliarepr(TestTVUpper{<:Signed})
eval_parse_repr_test(Ref{A} where A)
eval_parse_repr_test(Ref{A} where A>:Ref)
eval_parse_repr_test(Ref{A} where A<:Ref)
eval_parse_repr_test(Ref{A} where Ref<:A<:Ref)
eval_parse_repr_test(TestTVUpper{<:Real})
eval_parse_repr_test(TestTVUpper{<:Integer})
eval_parse_repr_test(TestTVUpper{<:Signed})

# typearg order
@test is_juliarepr(UnionAll(X, Pair{X,<:Any}))
@test is_juliarepr(UnionAll(X, Pair{<:Any,X}))
eval_parse_repr_test(UnionAll(X, Pair{X,<:Any}))
eval_parse_repr_test(UnionAll(X, Pair{<:Any,X}))

# duplicates
@test is_juliarepr(UnionAll(X, Pair{X,X}))
eval_parse_repr_test(UnionAll(X, Pair{X,X}))

# nesting
@test is_juliarepr(UnionAll(X, Ref{Ref{X}}))
@test is_juliarepr(Union{T, Int} where T)
@test is_juliarepr(Pair{A, <:A} where A)
eval_parse_repr_test(UnionAll(X, Ref{Ref{X}}))
eval_parse_repr_test(Union{T, Int} where T)
eval_parse_repr_test(Pair{A, <:A} where A)

# renumbered typevars with same names
@test is_juliarepr(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z}))))
eval_parse_repr_test(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z}))))

# shortened typevar printing
@test repr(Ref{<:Any}) == "Ref"
Expand All @@ -1762,9 +1763,9 @@ is_juliarepr(x) = eval(Meta.parse(repr(x))) == x
@test endswith(repr(TestTVUpper{<:Signed}), "TestTVUpper{<:Signed}")

# exception for tuples
@test is_juliarepr(Tuple)
@test is_juliarepr(Tuple{})
@test is_juliarepr(Tuple{<:Any})
@test eval_parse_repr(Tuple) == Tuple
@test eval_parse_repr(Tuple{}) == Tuple{}
@test eval_parse_repr(Tuple{<:Any}) == Tuple{<:Any}
end

@testset "showarg" begin
Expand Down Expand Up @@ -2199,7 +2200,7 @@ end

# issue #30927
Z = Array{Float64}(undef,0,0)
@test eval(Meta.parse(repr(Z))) == Z
@test eval_parse_repr(Z) == Z

@testset "show undef" begin
# issue #33204 - Parseable `repr` for `undef`
Expand Down Expand Up @@ -2256,7 +2257,7 @@ end

@testset "0-dimensional Array. Issue #31481" begin
for x in (zeros(Int32), collect('b'), fill(nothing), BitArray(0))
@test eval(Meta.parse(repr(x))) == x
@test eval_parse_repr(x) == x
end
@test showstr(zeros(Int32)) == "fill(0)"
@test showstr(collect('b')) == "fill('b')"
Expand Down