aboutsummaryrefslogtreecommitdiffstats
path: root/tests/core/result_spec.lua
blob: cd10acb554ece000182a4198e1913ffb2f0821c3 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
local Result = require "nvim-lsp-installer.core.result"
local match = require "luassert.match"

describe("result", function()
    it("should create success", function()
        local result = Result.success "Hello!"
        assert.is_true(result:is_success())
        assert.is_false(result:is_failure())
        assert.equals("Hello!", result:get_or_nil())
    end)

    it("should create failure", function()
        local result = Result.failure "Hello!"
        assert.is_true(result:is_failure())
        assert.is_false(result:is_success())
        assert.equals("Hello!", result:err_or_nil())
    end)

    it("should return value on get_or_throw()", function()
        local result = Result.success "Hello!"
        local val = result:get_or_throw()
        assert.equals("Hello!", val)
    end)

    it("should throw failure on get_or_throw()", function()
        local result = Result.failure "Hello!"
        local err = assert.has_error(function()
            result:get_or_throw()
        end)
        assert.equals("Hello!", err)
    end)

    it("should map() success", function()
        local result = Result.success "Hello"
        local mapped = result:map(function(x)
            return x .. " World!"
        end)
        assert.equals("Hello World!", mapped:get_or_nil())
        assert.is_nil(mapped:err_or_nil())
    end)

    it("should not map() failure", function()
        local result = Result.failure "Hello"
        local mapped = result:map(function(x)
            return x .. " World!"
        end)
        assert.equals("Hello", mapped:err_or_nil())
        assert.is_nil(mapped:get_or_nil())
    end)

    it("should raise exceptions in map()", function()
        local result = Result.success "failure"
        local err = assert.has_error(function()
            result:map(function()
                error "error"
            end)
        end)
        assert.equals("error", err)
    end)

    it("should map_catching() success", function()
        local result = Result.success "Hello"
        local mapped = result:map_catching(function(x)
            return x .. " World!"
        end)
        assert.equals("Hello World!", mapped:get_or_nil())
        assert.is_nil(mapped:err_or_nil())
    end)

    it("should not map_catching() failure", function()
        local result = Result.failure "Hello"
        local mapped = result:map_catching(function(x)
            return x .. " World!"
        end)
        assert.equals("Hello", mapped:err_or_nil())
        assert.is_nil(mapped:get_or_nil())
    end)

    it("should catch errors in map_catching()", function()
        local result = Result.success "value"
        local mapped = result:map_catching(function()
            error "This is an error"
        end)
        assert.is_false(mapped:is_success())
        assert.is_true(mapped:is_failure())
        assert.is_true(match.has_match "This is an error$"(mapped:err_or_nil()))
    end)

    it("should recover errors", function()
        local result = Result.failure("call an ambulance"):recover(function(err)
            return err .. ". but not for me!"
        end)
        assert.is_true(result:is_success())
        assert.equals("call an ambulance. but not for me!", result:get_or_nil())
    end)

    it("should catch errors in recover", function()
        local result = Result.failure("call an ambulance"):recover_catching(function(err)
            error("Oh no... " .. err, 2)
        end)
        assert.is_true(result:is_failure())
        assert.equals("Oh no... call an ambulance", result:err_or_nil())
    end)

    it("should return results in run_catching", function()
        local result = Result.run_catching(function()
            return "Hello world!"
        end)
        assert.is_true(result:is_success())
        assert.equals("Hello world!", result:get_or_nil())
    end)

    it("should return failures in run_catching", function()
        local result = Result.run_catching(function()
            error("Oh noes", 2)
        end)
        assert.is_true(result:is_failure())
        assert.equals("Oh noes", result:err_or_nil())
    end)
end)