-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
some.jl
84 lines (65 loc) · 2.65 KB
/
some.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file is a part of Julia. License is MIT: https://julialang.org/license
## promote()
@test promote_type(Some{Int}, Some{Float64}) === Some{Float64}
## convert()
# These conversions must fail to prevent ambiguities
# when a value to wrap is already a Some or a Nothing
@test_throws MethodError convert(Some, 1)
@test_throws MethodError convert(Union{Some, Nothing}, 1)
@test_throws MethodError convert(Some{Int}, 1)
@test_throws MethodError convert(Union{Some{Int}, Nothing}, 1)
@test convert(Some, Some(1)) === convert(Union{Some, Nothing}, Some(1)) === Some(1)
@test convert(Some{Int}, Some(1)) === convert(Union{Some{Int}, Nothing}, Some(1)) === Some(1)
@test convert(Some{Int}, Some(1.0)) === Some(1)
@test convert(Union{Some{Int}, Nothing}, Some(1.0)) === Some(1)
@test_throws MethodError convert(Some, nothing)
@test_throws MethodError convert(Some{Int}, nothing)
@test convert(Some, Some(nothing)) === Some(nothing)
@test convert(Some{Nothing}, Some(nothing)) === Some(nothing)
@test convert(Union{Some, Nothing}, nothing) === nothing
@test convert(Union{Some{Int}, Nothing}, nothing) === nothing
@test convert(Union{Int, Nothing}, nothing) === nothing
@test convert(Union{Int, Nothing}, 1) === 1
@test convert(Union{Int, Nothing}, 1.0) === 1
@test convert(Nothing, nothing) === nothing
@test_throws MethodError convert(Nothing, 1)
## show()
@test sprint(show, Some(1)) == "Some(1)"
@test sprint(show, Some(Some(1))) == "Some(Some(1))"
@test repr([Some(1)]) == "Some{$Int}[1]"
@test repr([Some(Some(1))]) == "Some{Some{$Int}}[Some(1)]"
## == and isequal nothing
@test Some(1) != nothing
@test Some(nothing) != nothing
@test !isequal(Some(1), nothing)
@test !isequal(Some(nothing), nothing)
# coalesce()
for v in (nothing, missing)
@test coalesce(1) === 1
@test coalesce(v) === v
@test coalesce(v, 1) === 1
@test coalesce(1, v) === 1
@test coalesce(v, v) === v
@test coalesce(v, 1, 2) === 1
@test coalesce(1, v, 2) === 1
@test coalesce(v, v, 2) === 2
@test coalesce(v, v, v) === v
@test coalesce(Some(1)) === 1
@test coalesce(Some(v)) === v
@test coalesce(Some(1), 0) === 1
@test coalesce(Some(v), 0) === v
@test coalesce(v, Some(v)) === v
@test coalesce(Some(1), v) === 1
@test coalesce(v, Some(1)) === 1
@test coalesce(v, Some(1), v) === 1
@test coalesce(v, Some(1), Some(2)) === 1
@test coalesce(Some(1), v, Some(2)) === 1
@test coalesce(v, missing) === missing
@test coalesce(v, nothing) === nothing
@test coalesce(v, missing, v) === v
@test coalesce(v, nothing, v) === v
end
# notnothing()
using Base: notnothing
@test notnothing(1) === 1
@test_throws ArgumentError notnothing(nothing)