|
| 1 | +@testitem "of_type" begin |
| 2 | + source = QueryOperators.query(Any[1, "a", 2, "b", 3.0]) |
| 3 | + |
| 4 | + ints = QueryOperators.of_type(source, Int) |
| 5 | + |
| 6 | + @test collect(ints) == [1, 2] |
| 7 | + @test eltype(ints) == Int |
| 8 | + |
| 9 | + strings = QueryOperators.of_type(source, String) |
| 10 | + @test collect(strings) == ["a", "b"] |
| 11 | + @test eltype(strings) == String |
| 12 | +end |
| 13 | + |
| 14 | +@testitem "of_type keeps subtypes" begin |
| 15 | + source = QueryOperators.query(Any[1, 2.0, "a"]) |
| 16 | + |
| 17 | + numbers = QueryOperators.of_type(source, Number) |
| 18 | + |
| 19 | + @test collect(numbers) == [1, 2.0] |
| 20 | + @test eltype(numbers) == Number |
| 21 | +end |
| 22 | + |
| 23 | +@testitem "of_type over a Union element type" begin |
| 24 | + source = QueryOperators.query(Union{Int,String}[1, "a", 2]) |
| 25 | + |
| 26 | + @test collect(QueryOperators.of_type(source, Int)) == [1, 2] |
| 27 | +end |
| 28 | + |
| 29 | +@testitem "of_type matching nothing or everything" begin |
| 30 | + source = QueryOperators.query(Any[1, 2, 3]) |
| 31 | + |
| 32 | + @test collect(QueryOperators.of_type(source, String)) == String[] |
| 33 | + @test collect(QueryOperators.of_type(source, Int)) == [1, 2, 3] |
| 34 | +end |
| 35 | + |
| 36 | +@testitem "of_type on an empty source" begin |
| 37 | + source = QueryOperators.query(Any[]) |
| 38 | + |
| 39 | + @test collect(QueryOperators.of_type(source, Int)) == Int[] |
| 40 | +end |
| 41 | + |
| 42 | +@testitem "cast" begin |
| 43 | + source = QueryOperators.query([1, 2, 3]) |
| 44 | + |
| 45 | + res = QueryOperators.cast(source, Float64) |
| 46 | + |
| 47 | + @test collect(res) == [1.0, 2.0, 3.0] |
| 48 | + @test eltype(res) == Float64 |
| 49 | + @test Base.IteratorSize(typeof(res)) == Base.HasLength() |
| 50 | + @test length(res) == 3 |
| 51 | +end |
| 52 | + |
| 53 | +@testitem "cast widens an Any source to a concrete type" begin |
| 54 | + source = QueryOperators.query(Any[1, 2, 3]) |
| 55 | + |
| 56 | + res = QueryOperators.cast(source, Int) |
| 57 | + |
| 58 | + @test collect(res) == [1, 2, 3] |
| 59 | + @test eltype(res) == Int |
| 60 | +end |
| 61 | + |
| 62 | +@testitem "cast fails on an element that cannot be converted" begin |
| 63 | + source = QueryOperators.query(Any[1, "a"]) |
| 64 | + |
| 65 | + res = QueryOperators.cast(source, Int) |
| 66 | + |
| 67 | + @test_throws MethodError collect(res) |
| 68 | +end |
| 69 | + |
| 70 | +@testitem "cast fails on a lossy conversion" begin |
| 71 | + source = QueryOperators.query([1.5]) |
| 72 | + |
| 73 | + @test_throws InexactError collect(QueryOperators.cast(source, Int)) |
| 74 | +end |
| 75 | + |
| 76 | +@testitem "cast on an empty source" begin |
| 77 | + source = QueryOperators.query(Int[]) |
| 78 | + |
| 79 | + @test collect(QueryOperators.cast(source, Float64)) == Float64[] |
| 80 | +end |
| 81 | + |
| 82 | +@testitem "of_type and cast compose with other operators" begin |
| 83 | + source = QueryOperators.query(Any[1, "a", 2, "b", 3]) |
| 84 | + |
| 85 | + res = QueryOperators.cast(QueryOperators.of_type(source, Int), Float64) |
| 86 | + |
| 87 | + @test collect(res) == [1.0, 2.0, 3.0] |
| 88 | + @test eltype(res) == Float64 |
| 89 | +end |
0 commit comments