Skip to content

Commit

Permalink
Updates for Julia 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed May 19, 2018
1 parent 42631f9 commit eca43bf
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 116 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ os:
- osx
julia:
- 0.6
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.6.0-pre
julia 0.6
Compat 0.62.0
15 changes: 12 additions & 3 deletions src/DeepDiffs.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
19 changes: 6 additions & 13 deletions src/arrays.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type VectorDiff{T1, T2} <: DeepDiff
struct VectorDiff{T1, T2} <: DeepDiff
before::T1
after::T2
removed::Vector{Int}
Expand All @@ -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
Expand Down Expand Up @@ -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, ", ")
Expand All @@ -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
Expand Down
15 changes: 3 additions & 12 deletions src/dicts.jl
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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)
Expand Down
22 changes: 8 additions & 14 deletions src/strings.jl
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -33,23 +33,17 @@ 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')
ylines = split(diff.after, '\n')
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
Expand Down Expand Up @@ -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
Expand Down
102 changes: 30 additions & 72 deletions test/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DeepDiffs
using Base.Test
using Compat
using Compat.Test
using TestSetExtensions

@testset DottedTestSet "DeepDiff Tests" begin
Expand Down

0 comments on commit eca43bf

Please sign in to comment.