|
| 1 | +module TestPkg |
| 2 | + |
| 3 | +using FilePaths |
| 4 | + |
| 5 | +# Test for basic definitions taking abstract paths |
| 6 | +FilePaths.@compat function typical(x::AbstractPath, y::AbstractPath) |
| 7 | + return relative(x, y) |
| 8 | +end |
| 9 | + |
| 10 | +# Test for definition taking parameterized paths |
| 11 | +FilePaths.@compat function parameterized(x::P, y::P) where P <: AbstractPath |
| 12 | + return relative(x, y) |
| 13 | +end |
| 14 | + |
| 15 | +# Test for definition with a path return type |
| 16 | +# (indicating that we should convert it back to a string) |
| 17 | +FilePaths.@compat function rtype(x::P, y::P)::P where P <: AbstractPath |
| 18 | + return relative(x, y) |
| 19 | +end |
| 20 | + |
| 21 | +# Test for a mixed input definition |
| 22 | +# z is an unused variable to make sure untyped variables don't break the macro |
| 23 | +FilePaths.@compat function mixed(x::P, y::String, z)::P where P <: AbstractPath |
| 24 | + return x / y |
| 25 | +end |
| 26 | + |
| 27 | +# Test kwargs |
| 28 | +FilePaths.@compat function kwargs(x::AbstractPath; y::AbstractPath=cwd())::AbstractPath |
| 29 | + return relative(x, y) |
| 30 | +end |
| 31 | + |
| 32 | +# Test optional args |
| 33 | +FilePaths.@compat function optargs(x::AbstractPath, y::AbstractPath=cwd())::AbstractPath |
| 34 | + return relative(x, y) |
| 35 | +end |
| 36 | + |
| 37 | +# Test for inline definitions |
| 38 | +FilePaths.@compat inline(x::AbstractPath, y::AbstractPath) = relative(x, y) |
| 39 | + |
| 40 | +end |
| 41 | + |
| 42 | +@testset "@compat" begin |
| 43 | + cd(abs(parent(Path(@__FILE__)))) do |
| 44 | + reg = Sys.iswindows() ? "..\\src\\FilePaths.jl" : "../src/FilePaths.jl" |
| 45 | + @test ispath(reg) |
| 46 | + p = Path(reg) |
| 47 | + |
| 48 | + @testset "typical" begin |
| 49 | + # Passing paths should should work just like calling relative |
| 50 | + @test TestPkg.typical(p, home()) == relative(p, home()) |
| 51 | + |
| 52 | + # Passing strings should now also work just like calling relative |
| 53 | + @test TestPkg.typical(string(p), string(home())) == relative(p, home()) |
| 54 | + |
| 55 | + # Mixing strings and paths should also work here |
| 56 | + @test TestPkg.typical(string(p), home()) == relative(p, home()) |
| 57 | + end |
| 58 | + |
| 59 | + @testset "parameterized" begin |
| 60 | + # Passing paths should should work just like calling relative |
| 61 | + @test TestPkg.parameterized(p, home()) == relative(p, home()) |
| 62 | + |
| 63 | + # Passing strings should now also work just like calling relative |
| 64 | + @test TestPkg.parameterized(string(p), string(home())) == relative(p, home()) |
| 65 | + |
| 66 | + # Mixing strings and paths should error because x and y need to be the same type |
| 67 | + @test_throws MethodError TestPkg.parameterized(string(p), home()) |
| 68 | + end |
| 69 | + |
| 70 | + @testset "rtype" begin |
| 71 | + # Passing paths should should work just like calling relative |
| 72 | + @test TestPkg.rtype(p, home()) == relative(p, home()) |
| 73 | + |
| 74 | + # Passing strings should also convert the output to a string |
| 75 | + @test TestPkg.rtype(string(p), string(home())) == string(relative(p, home())) |
| 76 | + |
| 77 | + # Mixing strings and paths should error because x and y need to be the same type |
| 78 | + @test_throws MethodError TestPkg.rtype(string(p), home()) |
| 79 | + end |
| 80 | + |
| 81 | + @testset "mixed" begin |
| 82 | + # Passing in a path as the first argument should return the same output as p / home() |
| 83 | + prefix = parent(p) |
| 84 | + suffix = basename(p) |
| 85 | + @test TestPkg.mixed(prefix, suffix, 2) == p |
| 86 | + |
| 87 | + # Passing 2 strings should also convert the output to a string |
| 88 | + @test TestPkg.mixed(string(prefix), suffix, 2) == string(p) |
| 89 | + |
| 90 | + # Passing a path where we explicitly want a string will error |
| 91 | + @test_throws MethodError TestPkg.mixed(prefix, Path(suffix), 2) |
| 92 | + end |
| 93 | + |
| 94 | + @testset "optargs" begin |
| 95 | + # Passing paths should should work just like calling relative |
| 96 | + @test TestPkg.optargs(p, home()) == relative(p, home()) |
| 97 | + |
| 98 | + # Passing strings should also return the relative path as a string |
| 99 | + @test TestPkg.optargs(string(p), string(home())) == string(relative(p, home())) |
| 100 | + |
| 101 | + # With optional arguments we should be able to mix strings on either argument |
| 102 | + @test TestPkg.optargs(p, string(home())) == string(relative(p, home())) |
| 103 | + @test TestPkg.optargs(string(p), home()) == string(relative(p, home())) |
| 104 | + |
| 105 | + # Use default |
| 106 | + @test TestPkg.optargs(p) == relative(p, cwd()) |
| 107 | + @test TestPkg.optargs(string(p)) == string(relative(p, cwd())) |
| 108 | + end |
| 109 | + |
| 110 | + @testset "inline" begin |
| 111 | + # Passing paths should should work just like calling relative |
| 112 | + @test TestPkg.inline(p, home()) == relative(p, home()) |
| 113 | + |
| 114 | + # Passing strings should now also work just like calling relative |
| 115 | + @test TestPkg.inline(string(p), string(home())) == relative(p, home()) |
| 116 | + |
| 117 | + # Mixing strings and paths should also work here |
| 118 | + @test TestPkg.inline(string(p), home()) == relative(p, home()) |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments