test.js
1.81 KB
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
var test = require('tape')
var Result = require('./index.js')
test('can create Ok', function t(assert) {
var v = Result.Ok(42)
assert.equal(!!v, true)
assert.equal(v.v, 42)
assert.end()
})
test('can check for Ok', function t(assert) {
var result = Result.Ok(42)
assert.equal(Result.isOk(result), true)
assert.equal(Result.Ok(result), 42)
assert.end()
})
test('check for Ok fails for Err', function t(assert) {
var result = Result.Err(new Error('foo'))
assert.equal(Result.isOk(result), false)
assert.equal(Result.Ok(result), undefined)
assert.end()
})
test('can create Err', function t(assert) {
var err = Result.Err(new Error('foo'))
assert.equal(!!err, true)
assert.equal(err.err.message, 'foo')
assert.end()
})
test('can check for Err', function t(assert) {
var result = Result.Err(new Error('foo'))
assert.equal(Result.isErr(result), true)
assert.equal(Result.Err(result).message, 'foo')
assert.end()
})
test('check for Err fails for Ok', function t(assert) {
var result = Result.Ok(42)
assert.equal(Result.isErr(result), false)
assert.equal(Result.Err(result), undefined)
assert.end()
})
test('Ok can support null value', function t(assert) {
var result = Result.Ok(null)
assert.equal(!!result, true)
assert.equal(result.v, null)
assert.equal(Result.isOk(result), true)
assert.equal(Result.Ok(result), null)
assert.equal(Result.isErr(result), false)
assert.equal(Result.Err(result), undefined)
assert.end()
})
test('Ok throws if you box undefined', function t(assert) {
assert.throws(function () {
Result.Ok(undefined)
}, /Cannot box `undefined` in Result.Ok/)
assert.end()
})
test('Err throws if you box non-error', function t(assert) {
assert.throws(function () {
Result.Err(42)
}, /Cannot box a non-error in Result.Err/)
assert.end()
})