-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathwith.spec.js
More file actions
147 lines (113 loc) · 3.82 KB
/
with.spec.js
File metadata and controls
147 lines (113 loc) · 3.82 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
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
import Core from '../lib/core';
import test from 'ava';
const Patterns = Core.Patterns;
const SpecialForms = Core.SpecialForms;
const Tuple = Core.Tuple;
const MatchError = Core.Patterns.MatchError;
const $ = Patterns.variable();
function map_fetch(map, key) {
if (key in map) {
return new Tuple(Symbol.for('ok'), map[key]);
}
return Symbol.for('error');
}
test('with', async (t) => {
/*
opts = %{width: 10, height: 15}
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height),
do: {:ok, width * height}
{:ok, 150}
*/
const opts = { width: 10, height: 15 };
const value = await SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[new Tuple(Symbol.for('ok'), $), width => map_fetch(opts, 'height')],
(width, height) => new Tuple(Symbol.for('ok'), width * height),
);
t.deepEqual(value, new Tuple(Symbol.for('ok'), 150));
});
test('with without match', async (t) => {
/*
opts = %{width: 10}
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height),
do: {:ok, width * height}
:error
*/
const opts = { width: 10 };
const value = await SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'height')],
(width, height) => new Tuple(Symbol.for('ok'), width * height),
);
t.deepEqual(value, Symbol.for('error'));
});
test('with bare expression', async (t) => {
/*
opts = %{width: 10}
with {:ok, width} <- Map.fetch(opts, :width),
double_width = width * 2,
{:ok, height} <- Map.fetch(opts, :height),
do: {:ok, double_width * height}
{:ok, 300}
*/
const opts = { width: 10, height: 15 };
const value = await SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[$, width => width * 2],
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'height')],
(width, double_width, height) => new Tuple(Symbol.for('ok'), double_width * height),
);
t.deepEqual(value, new Tuple(Symbol.for('ok'), 300));
});
test('with else', async (t) => {
/*
opts = %{width: 10}
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height) do
{:ok, width * height}
else
:error -> {:error, :wrong_data}
end
{:error, :wrong_data}
*/
const opts = { width: 10 };
const value = await SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'height')],
(width, height) => new Tuple(Symbol.for('ok'), width * height),
Patterns.defmatch(
Patterns.clause(
[Symbol.for('error')],
() => new Tuple(Symbol.for('error'), Symbol.for('wrong_data')),
),
),
);
t.deepEqual(value, new Tuple(Symbol.for('error'), Symbol.for('wrong_data')));
});
test('with else that don`t match', async (t) => {
/*
opts = %{width: 10}
with {:ok, width} <- Map.fetch(opts, :width),
{:ok, height} <- Map.fetch(opts, :height) do
{:ok, width * height}
else
:fail -> {:error, :wrong_data}
end
{:error, :wrong_data}
*/
const opts = { width: 10 };
const withFunctionPromise = SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'height')],
(width, height) => new Tuple(Symbol.for('ok'), width * height),
Patterns.defmatch(
Patterns.clause(
[Symbol.for('fail')],
() => new Tuple(Symbol.for('error'), Symbol.for('wrong_data')),
),
),
);
await t.throws(withFunctionPromise, MatchError);
});