-
Notifications
You must be signed in to change notification settings - Fork 4
/
maybe.spec.ts
153 lines (113 loc) · 4.02 KB
/
maybe.spec.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Maybe } from '@/src/maybe';
describe('Maybe', () => {
test('some creates a Maybe with a value', () => {
const value = 'val';
const sut = Maybe.some(value);
expect(sut).toHaveValue(value);
});
test('none constructs with no value', () => {
const sut = Maybe.none<string>();
expect(sut).toHaveNoValue();
});
describe('from', () => {
test('creates a Maybe with a value when supplied a value', () => {
const value = 'val';
const sut = Maybe.from(value);
expect(sut).toHaveValue(value);
});
test('creates a Maybe with no value when supplied null', () => {
const value: string | null = null;
const sut = Maybe.from<string>(value);
expect(sut).toHaveNoValue();
});
test('creates a Maybe with no value when supplied undefined', () => {
const sut = Maybe.from<string>(undefined);
expect(sut).toHaveNoValue();
});
test('creates a Maybe with no value when supplied no value', () => {
const sut = Maybe.from<string>();
expect(sut).toHaveNoValue();
});
});
describe('hasValue', () => {
test('is true when the Maybe has a value', () => {
const sut = Maybe.some(1);
expect(sut.hasValue).toBe(true);
});
test('is false when the Maybe has no value', () => {
const sut = Maybe.none();
expect(sut.hasValue).toBe(false);
});
test('will never equal hasNoValue', () => {
const sut = Maybe.some(1);
expect(sut.hasValue).not.toBe(sut.hasNoValue);
});
});
describe('hasNoValue', () => {
test('is true when the Maybe has no value', () => {
const sut = Maybe.none();
expect(sut.hasNoValue).toBe(true);
});
test('is false when the Maybe has a value', () => {
const sut = Maybe.some(1);
expect(sut.hasNoValue).toBe(false);
});
test('will never equal hasValue', () => {
const sut = Maybe.none();
expect(sut.hasNoValue).not.toBe(sut.hasValue);
});
});
describe('getValueOrDefault', () => {
test('returns the inner value for a Maybe with a value', () => {
const value = 1;
const defaultValue = 10;
const sut = Maybe.some(value);
expect(sut.getValueOrDefault(defaultValue)).toBe(value);
});
test('returns the default value for a Maybe with no value', () => {
const defaultValue = 10;
const sut = Maybe.none<number>();
expect(sut.getValueOrDefault(defaultValue)).toBe(defaultValue);
});
test('returns the result of the default value creator for a Maybe with no value', () => {
const defaultValueCreator = () => 10;
const sut = Maybe.none<number>();
expect(sut.getValueOrDefault(defaultValueCreator)).toBe(
defaultValueCreator()
);
});
});
describe('getValueOrThrow', () => {
test('returns the inner value for a Maybe with a value', () => {
const value = 1;
const sut = Maybe.some(value);
expect(sut.getValueOrThrow()).toBe(value);
});
test('throw an error for a Maybe with no value', () => {
const sut = Maybe.none();
expect(() => sut.getValueOrThrow()).toThrowError('No value');
});
});
describe('toString', () => {
test('returns a simple string expressing the state of the Maybe', () => {
const some = Maybe.some(1);
const none = Maybe.none<number>();
expect(some.toString()).toBe('Maybe.some');
expect(none.toString()).toBe('Maybe.none');
});
});
describe('equals', () => {
test('returns true when each Maybe has the same value', () => {
expect(Maybe.some(1).equals(Maybe.some(1))).toBe(true);
});
test('returns false when each Maybe has a different value', () => {
expect(Maybe.some(1).equals(Maybe.some(2))).toBe(false);
});
test('returns false when both Maybes have no value', () => {
expect(Maybe.none().equals(Maybe.none())).toBe(false);
});
test('returns false when one Maybe has a value and the other has no value', () => {
expect(Maybe.some(1).equals(Maybe.none())).toBe(false);
});
});
});