|
83 | 83 | @test (@splitcombine function (x::T, y::Vector{U}) where T <: U where U
|
84 | 84 | (T, U)
|
85 | 85 | end)(1, Number[2.0]) == (Int, Number)
|
| 86 | + |
| 87 | + # Test for lambda expressions with keyword arguments |
| 88 | + @test (@splitcombine (a::Int; b=2) -> a + b)(1) === 3 |
| 89 | + @test (@splitcombine (a::Int; b::Float64=2.0) -> Float64(a) + b)(1) === 3.0 |
| 90 | + @test (@splitcombine (a::Int, x; b=2, c=3) -> a + b + c + x)(1, 4) === 10 |
| 91 | + @test (@splitcombine (a::Int, x=2) -> a + x)(1) === 3 |
| 92 | + @test (@splitcombine (a::Int, x=2; y) -> a + x + y)(1; y=3) === 6 |
| 93 | + @test (@splitcombine (a, x::Int=2; y) -> a + x + y)(1; y=3) === 6 |
| 94 | + @test (@splitcombine (a::Int, x::Int=2; y) -> a + x + y)(1; y=3) === 6 |
| 95 | + |
| 96 | + # With tuple unpacking |
| 97 | + @test (@splitcombine (((a, b)::Tuple{Int, Int}, c; d=1) -> a + b + c + d))((1, 2), 3; d=4) === 10 |
| 98 | + @test (@splitcombine ((c, (a, b); d=1) -> a + b + c + d))(3, (1, 2); d=4) === 10 |
| 99 | + @test (@splitcombine ((c, (a, b); d) -> a + b + c + d))(3, (1, 2); d=4) === 10 |
| 100 | + |
| 101 | + # Test for single varargs argument in lambda |
| 102 | + @test splitdef(Meta.parse("(args...) -> 0"))[:args] == [:(args...)] |
| 103 | + @test (@splitcombine (args...) -> sum(args))(1, 2, 3) == 6 |
| 104 | + @test (@splitcombine (args::Int...) -> sum(args))(1, 2, 3) == 6 |
| 105 | + @test (@splitcombine (args::Int...; y=2) -> sum(args) + y)(1, 2, 3) == 8 |
| 106 | + @test (@splitcombine (arg, args::Int...; y=2) -> arg + sum(args) + y)(1, 2, 3) == 8 |
| 107 | + @test (@splitcombine (::Int...) -> 1)(1, 2, 3) === 1 |
| 108 | + |
| 109 | + # Splatted keyword arguments |
| 110 | + @test (@splitcombine (a::Int; kws...) -> a + sum(values(kws)))(1; b=2, c=3) == 6 |
| 111 | + @test (@splitcombine (; kws...) -> sum(values(kws)))(b=2, c=3) == 5 |
| 112 | + @test (@splitcombine (a::Int; b, kws...) -> a + b + sum(values(kws)))(1; b=2, c=3) == 6 |
| 113 | + @test (@splitcombine (a::Int; b=2, kws...) -> a + b + sum(values(kws)))(1; c=3) == 6 |
| 114 | + |
| 115 | + # Both splatted positional and keyword arguments |
| 116 | + @test (@splitcombine (a::Int, args::Int...; kws...) -> a + sum(args) + sum(values(kws)))(1, 2, 3; b=4, c=5) == 15 |
| 117 | + @test (@splitcombine (a, ::Int...; b, kws...) -> a + sum(values(kws)))(1, 2, 3; b=4, c=5) == 1 + 5 |
| 118 | + |
| 119 | + # Issue with longdef |
| 120 | + ex = longdef(:((a::Int; b=2) -> a + b)) |
| 121 | + any_kw(ex) = ex isa Expr ? (any_kw(ex.head) || any(any_kw, ex.args)) : ex == :kw |
| 122 | + @test any_kw(ex) |
| 123 | + ## ^Ensure we get a :kw expression in the output AST |
| 124 | + @test eval(ex) isa Function |
| 125 | + ## Shouldn't have issues evaluating |
86 | 126 | end
|
87 | 127 |
|
88 | 128 | @testset "combinestructdef, splitstructdef" begin
|
|
0 commit comments