forked from denoland/deno_std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_entries_test.ts
140 lines (131 loc) · 2.72 KB
/
map_entries_test.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";
import { mapEntries } from "./map_entries.ts";
function mapEntriesTest<T, O>(
input: [Record<string, T>, (entry: [string, T]) => [string, O]],
expected: Record<string, O>,
message?: string,
) {
const actual = mapEntries(...input);
assertEquals(actual, expected, message);
}
Deno.test({
name: "[collections/mapEntries] no mutation",
fn() {
const object = { a: 5, b: true };
mapEntries(object, ([key, value]) => [`${key}a`, value]);
assertEquals(object, { a: 5, b: true });
},
});
Deno.test({
name: "[collections/mapEntries] empty input",
fn() {
mapEntriesTest(
[{}, (it) => it],
{},
);
},
});
Deno.test({
name: "[collections/mapEntries] identity",
fn() {
mapEntriesTest(
[
{
foo: true,
bar: "lorem",
1: -5,
},
(it) => it,
],
{
foo: true,
bar: "lorem",
1: -5,
},
);
},
});
Deno.test({
name: "[collections/mapEntries] to constant entry",
fn() {
mapEntriesTest(
[
{ test: "foo", "": [] },
() => ["b", 5],
],
{ b: 5 },
);
},
});
Deno.test({
name: "[collections/mapEntries] overlapping keys",
fn() {
mapEntriesTest(
[
{
"Anna": 22,
"Kim": 24,
"Karen": 33,
"Claudio": 11,
"Karl": 45,
},
([name, age]) => [name.charAt(0), age - 10],
],
{
"A": 12,
"K": 35,
"C": 1,
},
);
mapEntriesTest(
[
{
"ad04": "Kim",
"e5f1": "Marija",
"10a8": "Kim",
},
([key, value]) => [value, key],
],
{
"Kim": "10a8",
"Marija": "e5f1",
},
);
},
});
Deno.test({
name: "[collections/mapEntries] normal mappers",
fn() {
mapEntriesTest(
[
{
"Mine": "Aliens",
"Umse": "Feiert das",
"ZM": "36 Grad",
},
([artist, track]) => [artist, `${artist} - ${track}`],
],
{
"Mine": "Mine - Aliens",
"Umse": "Umse - Feiert das",
"ZM": "ZM - 36 Grad",
},
);
mapEntriesTest(
[
{
"ad04": { name: "Object A", type: "Asset" },
"e5f1": { name: "Onboarding", type: "Process" },
"10a8": { name: "Marija", type: "Employee" },
},
([_, value]) => [value.type, value],
],
{
"Asset": { name: "Object A", type: "Asset" },
"Process": { name: "Onboarding", type: "Process" },
"Employee": { name: "Marija", type: "Employee" },
},
);
},
});