using Base.Test
abstract AbstractPathComp
immutable RelPathRoot <: AbstractPathComp; end
const Iᵣ=RelPathRoot()
immutable RelPathComp <: AbstractPathComp
v::Symbol
end
macro p_str(x)
:(RelPathComp(Symbol($x)))
end
const φ=p".."
immutable AbsolutePathRoot <: AbstractPathComp
v::Symbol
end
macro root_str(x)
:(AbsolutePathRoot(Symbol($x)))
end
@root_str (macro with 1 method)
normparts() = RelPathComp[]
normparts(x)=RelPathComp[x]
normparts(ir::RelPathRoot) = RelPathRoot[ir]
normparts(a::AbsolutePathRoot) = AbsolutePathRoot[a]
function normparts(x...)::Vector{RelPathComp}
#This is going to be a bit slow since julia does not have TCO
#But for explanitive purposes
@assert x[1]!=φ
if x[2]==φ
return normparts(x[3:end]...)
else
[x[1]; normparts(x[2:end]...)]
end
end
function normparts(ir::RelPathRoot, x...)::Vector{AbstractPathComp}
x=collect(x)
if !all(x.==φ)
ii = findfirst(x.!=φ)
return [ir; x[1:ii-1]; normparts(x[ii:end]...)]
else
return [ir; x]
end
end
function normparts(a::AbsolutePathRoot, x...)::Vector{AbstractPathComp}
normed_parts = normparts(Iᵣ, x...)[2:end] #Run it as if relpath
if !all(normed_parts.==φ)
jj = findfirst(normed_parts.!=φ)
return [a; normed_parts[jj:end]]
else
[a]
end
end
normparts (generic function with 7 methods)
@testset "Relative Paths" begin
@test normparts(Iᵣ) == [Iᵣ]
@test normparts(Iᵣ, φ,φ,φ) == [Iᵣ, φ,φ,φ]
@test normparts(Iᵣ, φ,φ,φ,p"foo") == [Iᵣ, φ,φ,φ,p"foo"]
@test normparts(Iᵣ,p"foo",p"bar") == [Iᵣ,p"foo",p"bar"]
@test normparts(Iᵣ, φ,φ,φ,p"foo",p"bar") == [Iᵣ, φ,φ,φ,p"foo",p"bar"]
@test normparts(Iᵣ, φ,φ,φ,p"foo",φ,p"bar") == [Iᵣ, φ,φ,φ,p"bar"]
end
@testset "Absolute Paths" begin
@test normparts(root"C:") == [root"C:"]
@test normparts(root"C:", φ,φ,φ) == [root"C:"]
@test normparts(root"C:", φ,φ,φ,p"foo") == [root"C:",p"foo"]
@test normparts(root"C:", φ,φ,p"foo",φ,p"bar") == [root"C:",p"bar"]
@test normparts(root"C:", φ,φ,p"foo",φ,p"bar") == [root"C:",p"bar"]
@test normparts(root"C:", φ,φ,φ,p"foo",φ,p"bar") == [root"C:",p"bar"]
end;
Test Summary: | Pass Total Relative Paths | 6 6 Test Summary: | Pass Total Absolute Paths | 6 6