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

rework default_rendermode and add tests for render #66

Open
wants to merge 4 commits into
base: jc/test
Choose a base branch
from

Conversation

johnnychen94
Copy link
Member

@johnnychen94 johnnychen94 commented Aug 1, 2020

This is a sub PR of #61

The previous default_rendermode(format, data) is problematic that the render pipeline is showing the original data instead of the encoded data.

render(rendermode, raw_actual)

For example, setting default_rendermode(::SHA256, img) to BeforeAfterLimited doesn't make much sense to me.

@@ -1,2 +1,2 @@
This is a
Copy link
Member Author

Choose a reason for hiding this comment

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

This was used to check CRLF/LF difference, but given that #64 properly handles this test case, we no longer need it.

The new reference is automatically generated by rm -rf test/references.

The previous `default_rendermode(format, data)` is problematic that
the render pipeline is showing the original data instead of
the encoded data. That said, `default_rendermode(::SHA256, img)` is wrongly
set to `BeforeAfterFull` -- we shoud never print the whole image array.
Comment on lines 1 to 6
- REFERENCE -------------------
Gray{Normed{UInt8,8}}[Gray{N0f8}(0.0), Gray{N0f8}(0.102), Gray{N0f8}(0.2), Gray{N0f8}(0.298), Gray{N0f8}(0.4), Gray{N0f8}(0.502), Gray{N0f8}(0.6), Gray{N0f8}(0.698), Gray{N0f8}(0.8), Gray{N0f8}(0.902)]
-------------------------------
- ACTUAL ----------------------
Gray{Normed{UInt8,8}}[Gray{N0f8}(0.902), Gray{N0f8}(0.8), Gray{N0f8}(0.698), Gray{N0f8}(0.6), Gray{N0f8}(0.502), Gray{N0f8}(0.4), Gray{N0f8}(0.298), Gray{N0f8}(0.2), Gray{N0f8}(0.102), Gray{N0f8}(0.0)]
-------------------------------
Copy link
Member Author

Choose a reason for hiding this comment

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

How each pixel is rendered may change from versions to versions, in that case, the test will fail, but we may just regenerate these.

Cref: JuliaGraphics/ColorTypes.jl#202

It seems that images are not properly encoded by ImageInTerminal:
I get different results in macOS and Ubuntu
Comment on lines +3 to +26
function string_check(ref, actual)
# a over-verbose collection of patterns that we want to ignore during test
patterns = [
# Julia v1.6
"Normed{UInt8,8}" => "N0f8",
r"Array{(\w+),2}" => s"Matrix{\1}",
r"Array{(\w+),1}" => s"Vector{\1}",

# https://github.com/JuliaGraphics/ColorTypes.jl/pull/206
# r"Gray{\w+}\(([\w\.]+)\)" => s"\1",
# r"RGB{\w+}\(([\w\.,]+)\)" => s"RGB(\1)",
]

for p in patterns
actual = replace(actual, p)
ref = replace(ref, p)
end

# Julia v1.4
ref = join(map(strip, split(ref, "\n")), "\n")
actual = join(map(strip, split(actual, "\n")), "\n")

isequal(ref, actual)
end
Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps we can make this the default equality checking function for AbstractArray{<:Number} types. I'm not sure.

Copy link

@kimikage kimikage Aug 10, 2020

Choose a reason for hiding this comment

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

It doesn't fully support nested parameters, but I think [^,] is slightly better than \w.

julia> occursin(r"Array{(\w+),1}", "Array{Base.TwicePrecision{Float32},1}")
false

julia> occursin(r"Array{([^,]+),1}", "Array{Base.TwicePrecision{Float32},1}")
true

Also, \b may be helpful.

julia> replace("MyLittleArray{Float32,2}", r"Array{(\w+),2}" => s"Matrix{\1}")
"MyLittleMatrix{Float32}"

julia> replace("MyLittleArray{Float32,2}", r"\bArray{(\w+),2}" => s"Matrix{\1}")
"MyLittleArray{Float32,2}"

Of course, since the function is only for testing, my suggestion is not important. TBH, I don't really like modifying "reference"s. I prefer the approach by means of multiple references (#47)

function render_item(::BeforeAfterLimited, item)
show(IOContext(stdout, :limit=>true, :displaysize=>(20,80)), "text/plain", item)
println()
render_item(mode::RenderMode, item) = render_item(stdout, mode, item)
Copy link
Member

Choose a reason for hiding this comment

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

I think we might not actually need this one anymore (or in the first place)
since render takes care of making sure we alrways have a IO arg

Suggested change
render_item(mode::RenderMode, item) = render_item(stdout, mode, item)

end

# We set the fallback as limited mode because it is not safe/efficient to fully render anything unless
# * we have prior information that it is not long -- numbers
Copy link
Member

Choose a reason for hiding this comment

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

but if it is not long, then BeforeAfterLimitted and BeforeAfter will be the same

if rendermode === nothing
rendermode = default_rendermode(F, raw_actual)
end
rendermode === nothing && (rendermode = default_rendermode(T))
Copy link
Member

Choose a reason for hiding this comment

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

Can't we just change line 97 to be:
rendermode = default_rendermode(T)?

for (x, xname) in (
# (img1d_1, "img1d_1"),
# (img1d_2, "img1d_2"),
# (img1d_3, "img1d_3"),
Copy link
Member

Choose a reason for hiding this comment

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

we should leave a comment saying why these are commented out


end

# `render_item` is repeatly called by `render` so we can skip it
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# `render_item` is repeatly called by `render` so we can skip it
# `render_item` is an internal helper called by `render` so we don't test it directly

using ReferenceTests: Diff, BeforeAfterFull, BeforeAfterImage, BeforeAfterLimited
using ReferenceTests: render, render_item

refdir = joinpath(refroot, "render")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
refdir = joinpath(refroot, "render")
const refdir = joinpath(refroot, "render")

"""
default_rendermode(::DataFormat, actual)
default_rendermode(actual)
Copy link
Member

Choose a reason for hiding this comment

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

It nolonger gets the actual, only its type?

Suggested change
default_rendermode(actual)
default_rendermode(typeof(actual))

Comment on lines +61 to +65
default_rendermode(::Type) = BeforeAfterLimited()
default_rendermode(::Type{T}) where T<:Number = BeforeAfterFull()
default_rendermode(::Type{T}) where T<:AbstractString = Diff()
default_rendermode(::Type{T}) where T<:AbstractArray{<:AbstractString} = Diff()
default_rendermode(::Type{T}) where T<:AbstractArray{<:Colorant} = BeforeAfterImage()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
default_rendermode(::Type) = BeforeAfterLimited()
default_rendermode(::Type{T}) where T<:Number = BeforeAfterFull()
default_rendermode(::Type{T}) where T<:AbstractString = Diff()
default_rendermode(::Type{T}) where T<:AbstractArray{<:AbstractString} = Diff()
default_rendermode(::Type{T}) where T<:AbstractArray{<:Colorant} = BeforeAfterImage()
default_rendermode(::Type) = BeforeAfterLimited()
default_rendermode(::Type{<:Number}) = BeforeAfterFull()
default_rendermode(::Type{<:AbstractString}) = Diff()
default_rendermode(::Type{<:AbstractArray{<:AbstractString}}) = Diff()
default_rendermode(::Type{<:AbstractArray{<:Colorant}}) = BeforeAfterImage()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants