-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathwith.spec.js
More file actions
157 lines (123 loc) · 4.04 KB
/
with.spec.js
File metadata and controls
157 lines (123 loc) · 4.04 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
148
149
150
151
152
153
154
155
156
157
import Core from '../lib/core';
import chai from 'chai';
const Patterns = Core.Patterns;
const SpecialForms = Core.SpecialForms;
const Tuple = Core.Tuple;
const MatchError = Core.Patterns.MatchError;
const expect = chai.expect;
const $ = Patterns.variable();
function map_fetch(map, key) {
if (key in map) {
return new Tuple(Symbol.for('ok'), map[key]);
}
return Symbol.for('error');
}
describe('with', () => {
it('normal', () => {
/*
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 = 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),
);
expect(value).to.eql(new Tuple(Symbol.for('ok'), 150));
});
it('without match', () => {
/*
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 = 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),
);
expect(value).to.eql(Symbol.for('error'));
});
it('bare expression', () => {
/*
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 = SpecialForms._with(
[new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, 'width')],
[$, width => width * 2],
[
new Tuple(Symbol.for('ok'), $),
(width, double_width) => map_fetch(opts, 'height'),
],
(width, double_width, height) =>
new Tuple(Symbol.for('ok'), double_width * height),
);
expect(value).to.eql(new Tuple(Symbol.for('ok'), 300));
});
it('with else', () => {
/*
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 = 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),
Patterns.defmatch(
Patterns.clause(
[Symbol.for('error')],
() => new Tuple(Symbol.for('error'), Symbol.for('wrong_data')),
),
),
);
expect(value).to.eql(
new Tuple(Symbol.for('error'), Symbol.for('wrong_data')),
);
});
it('with else that don`t match', () => {
/*
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 withFunction = SpecialForms._with.bind(
null,
[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),
Patterns.defmatch(
Patterns.clause(
[Symbol.for('fail')],
() => new Tuple(Symbol.for('error'), Symbol.for('wrong_data')),
),
),
);
expect(withFunction).to.throw(MatchError, 'No match for: Symbol(error)');
});
});