forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith.spec.js
More file actions
94 lines (66 loc) · 2.26 KB
/
with.spec.js
File metadata and controls
94 lines (66 loc) · 2.26 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
import Core from "../lib/core";
const Patterns = Core.Patterns;
const SpecialForms = Core.SpecialForms;
const Tuple = Core.Tuple;
import Enum from "../lib/enum";
import chai from 'chai';
var 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}
*/
let opts = { width: 10, height: 15 };
let 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
*/
let opts = { width: 10 };
let 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}
*/
let opts = { width: 10, height: 15 };
let 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));
});
});