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

Updates for Julia 0.7 #9

Merged
merged 4 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 14 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

branches:
only:
- master
- /release-.*/

notifications:
- provider: Email
Expand All @@ -10,6 +18,11 @@ notifications:

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# if there's a newer build queued for the same PR, cancel this one
- ps: if ($env:APPVEYOR_PULL_REQUEST_NUMBER -and $env:APPVEYOR_BUILD_NUMBER -ne ((Invoke-RestMethod `
https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/history?recordsNumber=50).builds | `
Where-Object pullRequestId -eq $env:APPVEYOR_PULL_REQUEST_NUMBER)[0].buildNumber) { `
throw "There are newer queued builds for this pull request, failing early." }
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
Expand All @@ -24,4 +37,4 @@ build_script:
Pkg.clone(pwd(), \"DeepDiffs\"); Pkg.build(\"DeepDiffs\")"

test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"DeepDiffs\")"
- C:\projects\julia\bin\julia --check-bounds=yes -e "Pkg.test(\"DeepDiffs\")"
22 changes: 19 additions & 3 deletions src/DeepDiffs.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
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

# Determine whether color is supported by the given stream
@static if VERSION >= v"0.7.0-DEV.3077"
hascolor(io::IO) = get(IOContext(io), :color, false)
else
hascolor(io::IO) = Base.have_color
end

"""
diff = deepdiff(obj1, obj2)

Expand All @@ -16,13 +33,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 hascolor(io)
printstyled(io, v, color=color)
else
print(io, prefix, v)
end
Expand Down
19 changes: 5 additions & 14 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 Expand Up @@ -101,7 +92,7 @@ function diffprint(io, d::DictDiff, indent=0)
print(io, inspace ^ indent, ")")
end

function prettyprint(io, d::Associative, linemarker, indent)
function prettyprint(io, d::AbstractDict, linemarker, indent)
println(io, "Dict(")
for p in d
print(io, linemarker, inspace ^ (indent+1))
Expand All @@ -117,7 +108,7 @@ function prettyprint(io, p::Pair, linemarker, indent)
prettyprint(io, p[2], linemarker, indent)
end

function prettyprint{T1, T2<:DictDiff}(io, p::Pair{T1, T2}, linemarker, indent)
function prettyprint(io, p::Pair{<:Any, <:DictDiff}, linemarker, indent)
prettyprint(io, p[1], linemarker, indent)
print(io, " => ")
diffprint(io, p[2], indent)
Expand Down
26 changes: 10 additions & 16 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 All @@ -68,7 +62,7 @@ function Base.show(io::IO, diff::StringDiff)

print(io, "\"")
visitall(diff.diff) do idx, state, last
if !Base.have_color
if !hascolor(io)
# check to see if we need to close a block
if laststate == :removed && state != :removed
print(io, "-}")
Expand All @@ -83,15 +77,15 @@ 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
laststate = state
end
if !Base.have_color
if !hascolor(io)
if laststate == :removed
print(io, "-}")
elseif laststate == :added
Expand Down
1 change: 0 additions & 1 deletion test/REQUIRE

This file was deleted.

Loading