From eca43bf78222d5e103cef30948392369a8a0782c Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Fri, 18 May 2018 20:15:35 -0700 Subject: [PATCH] Updates for Julia 0.7 --- .travis.yml | 1 + REQUIRE | 3 +- src/DeepDiffs.jl | 15 +++++-- src/arrays.jl | 19 +++------ src/dicts.jl | 15 ++----- src/strings.jl | 22 ++++------ test/display.jl | 102 ++++++++++++++--------------------------------- test/runtests.jl | 3 +- 8 files changed, 64 insertions(+), 116 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e73104..03c88c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ os: - osx julia: - 0.6 + - nightly notifications: email: false # uncomment the following lines to override the default test script diff --git a/REQUIRE b/REQUIRE index f194848..913499f 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1,2 @@ -julia 0.6.0-pre +julia 0.6 +Compat 0.62.0 diff --git a/src/DeepDiffs.jl b/src/DeepDiffs.jl index 8bf1884..f28b047 100644 --- a/src/DeepDiffs.jl +++ b/src/DeepDiffs.jl @@ -1,8 +1,18 @@ module DeepDiffs +using Compat + export deepdiff, added, removed, changed, before, after export SimpleDiff, VectorDiff, StringDiff, DictDiff +# Helper function for comparing two instances of a type for equality by field +function fieldequal(x::T, y::T) where T + for f in fieldnames(T) + getfield(x, f) == getfield(y, f) || return false + end + true +end + """ diff = deepdiff(obj1, obj2) @@ -16,13 +26,12 @@ function deepdiff end abstract type DeepDiff end # fallback diff that just stores two values -type SimpleDiff{T1, T2} <: DeepDiff +struct SimpleDiff{T1, T2} <: DeepDiff before::T1 after::T2 end -import Base: == -==(lhs::SimpleDiff, rhs::SimpleDiff) = lhs.before == rhs.before && lhs.after == rhs.after +Base.:(==)(lhs::SimpleDiff, rhs::SimpleDiff) = fieldequal(lhs, rhs) before(d::SimpleDiff) = d.before after(d::SimpleDiff) = d.after diff --git a/src/arrays.jl b/src/arrays.jl index f2a2b63..6473ac6 100644 --- a/src/arrays.jl +++ b/src/arrays.jl @@ -1,4 +1,4 @@ -type VectorDiff{T1, T2} <: DeepDiff +struct VectorDiff{T1, T2} <: DeepDiff before::T1 after::T2 removed::Vector{Int} @@ -11,14 +11,7 @@ removed(diff::VectorDiff) = diff.removed added(diff::VectorDiff) = diff.added changed(diff::VectorDiff) = Int[] -function ==(d1::VectorDiff, d2::VectorDiff) - d1.before == d2.before || return false - d1.after == d2.after || return false - d1.removed == d2.removed || return false - d1.added == d2.added || return false - - true -end +Base.:(==)(d1::VectorDiff, d2::VectorDiff) = fieldequal(d1, d2) # diffing an array is an application of the Longest Common Subsequence problem: # https://en.wikipedia.org/wiki/Longest_common_subsequence_problem @@ -103,10 +96,10 @@ function Base.show(io::IO, diff::VectorDiff) visitall(diff) do idx, state, last if state == :removed printitem(io, from[idx], :red, "(-)") - last || print_with_color(:red, io, ", ") + last || printstyled(io, ", ", color=:red) elseif state == :added printitem(io, to[idx], :green, "(+)") - last || print_with_color(:green, io, ", ") + last || printstyled(io, ", ", color=:green) else printitem(io, from[idx]) last || print(io, ", ") @@ -117,8 +110,8 @@ end # prefix is printed if we're not using color function printitem(io, v, color=:normal, prefix="") - if(Base.have_color) - print_with_color(color, io, string(v)) + if Base.have_color + printstyled(io, v, color=color) else print(io, prefix, v) end diff --git a/src/dicts.jl b/src/dicts.jl index d5331d0..08bd343 100644 --- a/src/dicts.jl +++ b/src/dicts.jl @@ -1,4 +1,4 @@ -type DictDiff{T1, KT1, T2, KT2} <: DeepDiff +struct DictDiff{T1, KT1, T2, KT2} <: DeepDiff before::T1 after::T2 removed::Set{KT1} @@ -13,18 +13,9 @@ removed(diff::DictDiff) = diff.removed added(diff::DictDiff) = diff.added changed(diff::DictDiff) = diff.changed -function ==(lhs::DictDiff, rhs::DictDiff) - lhs.before == rhs.before || return false - lhs.after == rhs.after || return false - lhs.removed == rhs.removed || return false - lhs.added == rhs.added || return false - lhs.changed == rhs.changed || return false - lhs.unchanged == rhs.unchanged || return false +Base.:(==)(lhs::DictDiff, rhs::DictDiff) = fieldequal(lhs, rhs) - true -end - -function deepdiff(X::Associative, Y::Associative) +function deepdiff(X::AbstractDict, Y::AbstractDict) xkeys = Set(keys(X)) ykeys = Set(keys(Y)) bothkeys = intersect(xkeys, ykeys) diff --git a/src/strings.jl b/src/strings.jl index c303e33..b232c5e 100644 --- a/src/strings.jl +++ b/src/strings.jl @@ -1,19 +1,19 @@ # used for single-line strings -type StringDiff{T1, T2} <: DeepDiff +struct StringDiff{T1, T2} <: DeepDiff before::T1 after::T2 diff::VectorDiff end # used for multi-line strings -type StringLineDiff{T1, T2} <: DeepDiff +struct StringLineDiff{T1, T2} <: DeepDiff before::T1 after::T2 diff::VectorDiff end function deepdiff(X::AbstractString, Y::AbstractString) - if contains(X, "\n") || contains(Y, "\n") + if occursin("\n", X) || occursin("\n", Y) # we'll compare hashes of each line rather than the text itself, because # these comparisons are done many times xhashes = map(hash, split(X, '\n')) @@ -33,13 +33,7 @@ added(diff::AllStringDiffs) = added(diff.diff) removed(diff::AllStringDiffs) = removed(diff.diff) changed(diff::AllStringDiffs) = [] -function =={T<:AllStringDiffs}(d1::T, d2::T) - d1.before == d2.before || return false - d1.after == d2.after || return false - d1.diff == d2.diff || return false - - true -end +Base.:(==)(d1::T, d2::T) where {T<:AllStringDiffs} = fieldequal(d1, d2) function Base.show(io::IO, diff::StringLineDiff) xlines = split(diff.before, '\n') @@ -47,9 +41,9 @@ function Base.show(io::IO, diff::StringLineDiff) println(io, "\"\"\"") visitall(diff.diff) do idx, state, last if state == :removed - print_with_color(:red, io, "- ", escape_string(xlines[idx])) + printstyled(io, "- ", escape_string(xlines[idx]), color=:red) elseif state == :added - print_with_color(:green, io, "+ ", escape_string(ylines[idx])) + printstyled(io, "+ ", escape_string(ylines[idx]), color=:green) else print(io, " ", escape_string(xlines[idx])) end @@ -83,9 +77,9 @@ function Base.show(io::IO, diff::StringDiff) end end if state == :removed - print_with_color(:red, io, string(xchars[idx])) + printstyled(io, string(xchars[idx]), color=:red) elseif state == :added - print_with_color(:green, io, string(ychars[idx])) + printstyled(io, string(ychars[idx]), color=:green) else print(io, xchars[idx]) end diff --git a/test/display.jl b/test/display.jl index fde0d6b..24f2014 100644 --- a/test/display.jl +++ b/test/display.jl @@ -17,16 +17,9 @@ buf = IOBuffer() eval(Base, :(have_color=true)) - # in 0.6 colored output is no longer bold as of PR #18628 - if VERSION < v"0.6.0-dev.1574" - expected1 = """ - [2, 3, 4, 1, 2, 7, 3, 5]""" - expected2 = """[1, 2]""" - else - expected1 = """ - [2, 3, 4, 1, 2, 7, 3, 5]""" - expected2 = """[1, 2]""" - end + expected1 = """ + [2, 3, 4, 1, 2, 7, 3, 5]""" + expected2 = """[1, 2]""" @testset "Color Diffs" begin display(TextDisplay(buf), d1) @test String(take!(buf)) == expected1 @@ -86,48 +79,25 @@ eval(Base, :(have_color=true)) buf = IOBuffer() display(TextDisplay(buf), d) - # in 0.6 colored output is no longer bold as of PR #18628 - if VERSION < v"0.6.0-dev.1574" - expected = """ - Dict( - :a => "a", - :dict1 => Dict( - :c => 3, - :a => 1, - :b => 2, - ), - - :c => "c", -  :list => [1, 2, 4, 3], - :b => "bd", - :dict2 => Dict( - :a => 1, - - :b => 2, - - :c => 3, - + :c => 4, -  ), - + :e => "e", - )""" - else - expected = """ - Dict( - :a => "a", - :dict1 => Dict( - :c => 3, - :a => 1, - :b => 2, - ), - - :c => "c", -  :list => [1, 2, 4, 3], - :b => "bd", - :dict2 => Dict( - :a => 1, - - :b => 2, - - :c => 3, - + :c => 4, -  ), - + :e => "e", - )""" - end + expected = """ + Dict( + :a => "a", + :dict1 => Dict( + :c => 3, + :a => 1, + :b => 2, + ), + - :c => "c", +  :list => [1, 2, 4, 3], + :b => "bd", + :dict2 => Dict( + :a => 1, + - :b => 2, + - :c => 3, + + :c => 4, +  ), + + :e => "e", + )""" # This test is broken because the specifics of how the ANSI color # codes are printed change based on the order, which changes with # different julia versions. @@ -187,26 +157,14 @@ buf = IOBuffer() @testset "Color Display" begin eval(Base, :(have_color=true)) - # in 0.6 colored output is no longer bold as of PR #18628 - if VERSION < v"0.6.0-dev.1574" - expected = """ - \"\"\" - differences can - - be hard to find - - in - + be hurd to find - multiline - output\"\"\"""" - else - expected = """ - \"\"\" - differences can - - be hard to find - - in - + be hurd to find - multiline - output\"\"\"""" - end + expected = """ + \"\"\" + differences can + - be hard to find + - in + + be hurd to find + multiline + output\"\"\"""" display(TextDisplay(buf), diff) @test String(take!(buf)) == expected end diff --git a/test/runtests.jl b/test/runtests.jl index 51d2ab0..20848d4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using DeepDiffs -using Base.Test +using Compat +using Compat.Test using TestSetExtensions @testset DottedTestSet "DeepDiff Tests" begin