Skip to content

Commit fd0fec2

Browse files
Stephan SahmStephan Sahm
authored andcommitted
adds extensive tests
1 parent 2efc14f commit fd0fec2

8 files changed

Lines changed: 284 additions & 35 deletions

File tree

test/Const.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@ end == Const(3)
1212
@test repr(Const(4)) == "Const(4)"
1313

1414
@test collect(Const(4)) == []
15+
@test length(Const(:hi)) == 0
16+
17+
@test @capture_out(for i in Const(true)
18+
println("i = $i")
19+
end) == ""
20+
@test @capture_out(foreach(println, Const(5))) == ""
1521

1622
@test eltype(Const(nothing)) == Any
1723

1824
@test Iterators.flatten(Const(5)) == Const(5)
1925

20-
@test @capture_out(foreach(println, Const(5))) == ""
26+
@test convert(Const{Float32}, Const(1)) == Const{Float32}(Float32(1))
27+
@test convert(Const{Number}, Const(1)) == Const{Number}(1)
28+
29+
@test promote_type(Const{Int}, Const{Number}) == Const{Number}
30+
@test promote_type(Const{Int}, Const{String}) == Const
31+
@test promote_type(Const{Int}, Const) == Const
32+
@test promote_type(Const, Const) == Const

test/ContextManager.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ cm(i=4, prefix="") = @ContextManager function(cont)
1010
result
1111
end
1212
@test eltype(cm()) == Int
13+
@test eltype(typeof(cm())) == Int
14+
@test eltype(ContextManager) == Any
15+
16+
17+
empty!(logging)
18+
@test run(cm(1)) == 1
19+
@test logging == ["before 1", "after 1"]
20+
21+
empty!(logging)
22+
@test run(x -> 3x, cm(2)) == 6
23+
@test logging == ["before 2", "after 2"]
24+
25+
empty!(logging)
26+
@test foreach(x -> push!(logging, "x = $x"), cm(2)) == nothing
27+
@test logging == ["before 2", "x = 2", "after 2"]
28+
1329

1430
# FunctorApplicativeMonad
1531
# =======================

test/Either.jl

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1+
@test Either{String, Int}(4) == Identity(4)
2+
@test Either{String, Int}("left") == Const("left")
3+
@test Either{Symbol}(true) == Identity(true)
4+
@test Either{Symbol}(:fallback) == Const(:fallback)
5+
6+
@test @either(true ? 4 : "else") == Identity(4)
7+
@test @either(false ? 4 : "else") == Const("else")
8+
@test @either(if true
9+
4
10+
else
11+
"else"
12+
end) == Identity(4)
13+
@test @either(if false
14+
4
15+
else
16+
"else"
17+
end) == Const("else")
18+
19+
@test iseither(Identity(4))
20+
@test iseither(Const("hi"))
21+
@test !iseither(Base.Some(nothing))
22+
123
@test isright(either("hi", true, 3))
224
@test isright(Either{String}(3))
325
@test !isleft(Either{String}(3))
26+
427
@test getright(Either{String}(3)) == 3
28+
@test_throws MethodError getright(Either{String}("hi"))
29+
30+
@test getleft(Either{String}("hi")) == "hi"
531
@test_throws MethodError getleft(Either{String}(3))
32+
633
@test getrightOption(Either{String}(3)) == Option(3)
7-
@test getleftOption(Either{String}(3)) == Option{String}(nothing)
34+
@test getrightOption(Either{String}("fallback")) == Option()
35+
36+
@test getleftOption(Either{String}(3)) == Option()
37+
@test getleftOption(Either{String}("fallback")) == Option("fallback")
38+
39+
@test getOption(Either{String}(3)) == Option(3)
40+
@test getOption(Either{String}("fallback")) == Option()
41+
842

943
@test eltype(Either{Int, String}) == String
1044
@test eltype(Either{<:Any, String}) == String
45+
@test eltype(Either) == Any
46+
47+
48+
@test flip_left_right(Const(4)) == Identity(4)
49+
@test flip_left_right(Identity("hi")) == Const("hi")
50+
h = Identity(:hellp)
51+
@test flip_left_right(flip_left_right(h)) == h

test/Identity.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ end == Identity(3 + 4)
99

1010
@test eltype(Identity("hello")) == String
1111
@test eltype(Identity{Int}) == Int
12+
@test eltype(Identity) == Any
1213

1314
@test collect(Identity(1)) == [1]
15+
@test length(Identity([1,2,3,4])) == 1
16+
17+
@test get(Identity(42)) == 42
1418

1519
@test isidentity(Identity(4))
1620
@test !isidentity(Const(3))
@@ -20,3 +24,14 @@ end == Identity(3 + 4)
2024
@test Iterators.flatten(Identity(Const(5))) == Const(5)
2125

2226
@test @capture_out(foreach(print, Identity(5))) == "5"
27+
@test @capture_out(for i in Identity(5)
28+
print(5)
29+
end) == "5"
30+
31+
@test convert(Identity{Float32}, Identity(1)) == Identity{Float32}(Float32(1))
32+
@test convert(Identity{Number}, Identity(1)) == Identity{Number}(1)
33+
34+
@test promote_type(Identity{Int}, Identity{Number}) == Identity{Number}
35+
@test promote_type(Identity{Int}, Identity{String}) == Identity
36+
@test promote_type(Identity{Int}, Identity) == Identity
37+
@test promote_type(Identity, Identity) == Identity

test/Option.jl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,31 @@ using DataTypesBasic
66
@test isconst(Option(nothing))
77
@test !isconst(Option(5))
88

9-
@test iftrue(2==3) do
9+
@test iftrue(false) do
1010
54
11-
end == Option{Int}()
12-
@test iffalse(2==3, 54) == Option{Int}(54)
11+
end == Option()
12+
@test iftrue(true) do
13+
54
14+
end == Option(54)
15+
@test iftrue(false, :Something) == Option()
16+
@test iftrue(true, :Something) == Option(:Something)
17+
18+
@test iffalse(false, 54) == Option(54)
19+
@test iffalse(true, 54) == Option()
20+
@test iffalse(() -> "hi", false) == Option("hi")
21+
@test iffalse(() -> "hi", true) == Option()
1322

1423
@test eltype(Option{Int}) == Int
1524
@test eltype(Option) == Any
25+
26+
@test isoption(Identity(3))
27+
@test isoption(Option())
28+
@test !isoption(Const("anything but nothing"))
29+
30+
@test issome(Option(3))
31+
@test !issome(Option())
32+
@test_throws MethodError issome(Const("other"))
33+
34+
@test !isnone(Option(3))
35+
@test isnone(Option())
36+
@test_throws MethodError isnone(Const("other"))

test/Try.jl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1+
using DataTypesBasic
2+
using Test
3+
4+
@test Try(4) == Identity(4)
5+
@test Try(ErrorException("hi")) == Const(ErrorException("hi"))
6+
7+
@test istry(Identity(4))
8+
@test istry(Const(ErrorException("hi")))
9+
@test istry(@Try error("an error"))
10+
@test istry(@Try :value)
11+
@test istry(@TryCatch ErrorException error("an error"))
12+
@test istry(@TryCatch ErrorException :value)
13+
114
@test issuccess(@Try 5)
215
@test !issuccess(@Try error("hi"))
16+
@test_throws MethodError issuccess(Const("other"))
17+
318
@test isexception(@Try error("hi"))
419
@test !isexception(@Try 5)
20+
@test_throws MethodError isexception(Const("other"))
521

6-
@test (@Try error("hi")) isa Const{<:Thrown}
22+
@test Thrown(ErrorException("one"), []) == Thrown(ErrorException("one"), [])
23+
@test repr(Thrown(ErrorException("some"), [])) == """Thrown(ErrorException("some"))"""
724

825

26+
27+
@test (@Try error("hi")) isa Const{<:Thrown}
28+
@test (@TryCatch ErrorException error("hi")) isa Const{<:Thrown}
29+
@test_throws ArgumentError (@TryCatch ErrorException throw(ArgumentError("bad")))
30+
931
me = MultipleExceptions(ErrorException("hi"))
1032
@test me.exceptions == (ErrorException("hi"),)
1133
@test MultipleExceptions(me, ErrorException("ho")).exceptions == (ErrorException("hi"), ErrorException("ho"))
1234

35+
e1 = Thrown(ErrorException("one"), [])
36+
e2 = AssertionError("error")
37+
@test merge(e1, e2) == MultipleExceptions(e1, e2)
38+
@test merge(e2, e1) == MultipleExceptions(e2, e1)
39+
@test merge(e1, e1) == MultipleExceptions(e1, e1)
40+
m12 = MultipleExceptions(e1, e2)
41+
@test merge(e2, m12) == MultipleExceptions(e2, e1, e2)
42+
@test merge(m12, e2) == MultipleExceptions(e1, e2, e2)
43+
@test merge(m12, m12) == MultipleExceptions(e1, e2, e1, e2)
44+
1345

1446
@test eltype(Try{Int}) == Int
1547
@test eltype(Identity{Int}) == Int
1648
@test eltype(Thrown) == Any
1749
@test eltype(Const{Thrown}) == Any
50+
@test eltype(Try) == Any

test/convert.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Test
22
using DataTypesBasic
33

4+
@test convert(Const, []) == Const(nothing)
5+
@test_throws AssertionError convert(Const, [1,2,3])
6+
47
@test convert(Vector, Identity("hello")) == ["hello"]
58
@test convert(Vector, nothing) == []
69
@test convert(Vector, Const(43)) == []
@@ -15,3 +18,25 @@ using DataTypesBasic
1518
@test_throws MethodError convert(ContextManager, nothing)
1619
@test_throws MethodError convert(ContextManager, Const(:error))
1720
@test_throws MethodError convert(ContextManager, [1,2,3])
21+
22+
23+
@test convert(Either{String, Float32}, Identity(1)) == Identity(Float32(1))
24+
@test convert(Either{String, Int}, Identity(1)) == Identity(1)
25+
@test_throws MethodError convert(Either{String, Int}, Identity("hi"))
26+
27+
@test convert(Either{Float32, Int}, Const(1)) == Const(Float32(1))
28+
@test convert(Either{Int, Int}, Const(1)) == Const(Float32(1))
29+
@test_throws MethodError convert(Either{String, Int}, Const(1))
30+
31+
32+
33+
@test convert(Either{String}, Identity(1)) == Identity(1)
34+
@test convert(Either{String}, Const("hi")) == Const("hi")
35+
@test convert(Either{Float32}, Const(1)) == Const(Float32(1))
36+
@test_throws MethodError convert(Either{Float32}, Const("hi"))
37+
38+
39+
@test convert(Either{<:Any, String}, Const(1)) == Const(1)
40+
@test convert(Either{<:Any, String}, Identity("hi")) == Identity("hi")
41+
@test convert(Either{<:Any, Float32}, Identity(1)) == Identity(Float32(1))
42+
@test_throws MethodError convert(Either{<:Any, Float32}, Identity("hi"))

0 commit comments

Comments
 (0)