Repository navigation
Expand file tree
/
Copy pathImportMapTest.cpp
More file actions
226 lines (206 loc) · 7.59 KB
/
Copy pathImportMapTest.cpp
File metadata and controls
226 lines (206 loc) · 7.59 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <cstdio>
#include <optional>
#include <string>
#include <vector>
#include "ns/loader/ImportMap.h"
using namespace nativescript::common;
using namespace nativescript::common::loader;
namespace {
int failures = 0;
void Check(bool ok, const std::string& what) {
if (!ok) {
failures++;
std::fprintf(stderr, "FAIL %s\n", what.c_str());
}
}
void Expect(const std::string& actual, const std::string& expected,
const std::string& what) {
if (actual != expected) {
failures++;
std::fprintf(stderr, "FAIL %s\n expected: %s\n actual: %s\n",
what.c_str(), expected.c_str(), actual.c_str());
}
}
void Expect(const std::optional<std::string>& actual,
const std::optional<std::string>& expected,
const std::string& what) {
Expect(actual.value_or("<nullopt>"), expected.value_or("<nullopt>"), what);
}
void Add(ImportMapSection& section, const std::string& key,
const std::string& target) {
std::string error;
Check(AddImportMapEntry(section, "imports", key, target, &error),
"add " + key + ": " + error);
}
void TestEntryValidation() {
struct Case {
std::string key;
std::string target;
std::string error;
};
const std::vector<Case> rejected = {
{"", "./x.mjs", "imports: a specifier key must not be empty"},
{"a", "", "imports: the target for 'a' must not be empty"},
{"a", std::string("./x\0.mjs", 8),
"imports: the target for 'a' must not contain U+0000"},
{"lib/", "./lib",
"imports: the target for 'lib/' must end with '/' because the "
"specifier key does"},
};
for (const Case& c : rejected) {
ImportMapSection section;
Add(section, "kept", "./kept.mjs");
std::string error;
Check(!AddImportMapEntry(section, "imports", c.key, c.target, &error),
"rejects '" + c.key + "'");
Expect(error, c.error, "error for '" + c.key + "'");
Check(section.entries.size() == 1, "a rejection leaves the section as is");
}
ImportMapSection section;
Add(section, "a", "./first.mjs");
Add(section, "a", "./second.mjs");
Expect(section.entries.at("a"), "./second.mjs", "a later duplicate wins");
std::string error;
Check(!AddImportMapEntry(section, "scope 'https://h/'", "", "./x", &error),
"scope label");
Expect(error, "scope 'https://h/': a specifier key must not be empty",
"the label names the section");
}
void TestScopePrefix() {
struct Case {
std::string key;
std::string prefix;
};
const std::vector<Case> cases = {
{"https://H.example", "https://h.example"},
{"https://h.example/", "https://h.example/"},
{"https://h.example/pkg/", "https://h.example/pkg/"},
{"https://h.example/pkg", "https://h.example/pkg"},
{"/data/app/vendor/", "/data/app/vendor/"},
{"./vendor/", "./vendor/"},
};
for (const Case& c : cases) {
Expect(ImportMapScopePrefix(c.key), c.prefix, "scope prefix of " + c.key);
}
}
ParsedImportMap SampleMap() {
ParsedImportMap map;
Add(map.imports, "a", "./a.mjs");
Add(map.imports, "lib/", "./lib/");
Add(map.imports, "lib/deep/", "./deep/");
Add(map.imports, "lib/exact", "./exact.mjs");
Add(map.imports, "only-top", "./top.mjs");
ImportMapSection vendor;
Add(vendor, "a", "./vendor-a.mjs");
Add(vendor, "lib/", "./vendor-lib/");
ImportMapSection vendorDeep;
Add(vendorDeep, "a", "./vendor-deep-a.mjs");
map.scopes.emplace_back(ImportMapScopePrefix("/r/vendor/"),
std::move(vendor));
map.scopes.emplace_back(ImportMapScopePrefix("/r/vendor/deep/"),
std::move(vendorDeep));
ImportMapSection pkg;
Add(pkg, "a", "./pkg-a.mjs");
map.scopes.emplace_back(ImportMapScopePrefix("/r/pkg"), std::move(pkg));
ImportMapSection host;
Add(host, "a", "./host-a.mjs");
map.scopes.emplace_back(ImportMapScopePrefix("https://H.example"),
std::move(host));
FinishImportMap(map);
return map;
}
void TestLookup() {
const ParsedImportMap map = SampleMap();
struct Case {
std::string specifier;
std::string referrer;
std::optional<std::string> target;
};
const std::vector<Case> cases = {
{"a", "/r/main.mjs", "./a.mjs"},
{"lib/x.mjs", "/r/main.mjs", "./lib/x.mjs"},
{"lib/deep/x.mjs", "/r/main.mjs", "./deep/x.mjs"},
{"lib/exact", "/r/main.mjs", "./exact.mjs"},
{"lib/", "/r/main.mjs", "./lib/"},
{"lib", "/r/main.mjs", std::nullopt},
{"b", "/r/main.mjs", std::nullopt},
{"a", "/r/vendor/x.mjs", "./vendor-a.mjs"},
{"lib/deep/x.mjs", "/r/vendor/x.mjs", "./vendor-lib/deep/x.mjs"},
{"a", "/r/vendor/deep/x.mjs", "./vendor-deep-a.mjs"},
{"lib/x.mjs", "/r/vendor/deep/x.mjs", "./vendor-lib/x.mjs"},
{"only-top", "/r/vendor/deep/x.mjs", "./top.mjs"},
{"a", "/r/vendorish/x.mjs", "./a.mjs"},
// A scope key without a trailing '/' names one module, not a prefix.
{"a", "/r/pkg", "./pkg-a.mjs"},
{"a", "/r/pkgs/x.mjs", "./a.mjs"},
{"a", "/r/pkg/x.mjs", "./a.mjs"},
// Q9: an http(s) key written with no path scopes the whole origin.
{"a", "https://h.example/x.mjs", "./host-a.mjs"},
{"a", "https://h.example", "./host-a.mjs"},
{"a", "", "./a.mjs"},
};
for (const Case& c : cases) {
Expect(LookupImportMap(map, c.specifier, c.referrer), c.target,
c.specifier + " from " + c.referrer);
}
auto scopeIndex = [&map](const std::string& prefix) {
for (size_t i = 0; i < map.scopes.size(); i++) {
if (map.scopes[i].first == prefix) {
return i;
}
}
return map.scopes.size();
};
Check(scopeIndex("/r/vendor/deep/") < scopeIndex("/r/vendor/"),
"the more specific scope is first");
Check(map.imports.prefixes.front().first == "lib/deep/",
"the longest prefix is first");
Check(ParsedImportMap().empty() && !map.empty(), "empty()");
}
// Scope keys that name one prefix are one scope, and the last one written
// replaces the others, as later JSON keys do.
void TestDuplicateScopes() {
ParsedImportMap map;
for (const std::string key : {"HTTPS://h.example/a/", "https://h.example/a/",
"https://H.example/a/"}) {
ImportMapSection section;
Add(section, "dep", "./from-" + key);
if (key == "HTTPS://h.example/a/") {
Add(section, "only-first", "./only-first.mjs");
}
map.scopes.emplace_back(ImportMapScopePrefix(key), std::move(section));
}
ImportMapSection other;
Add(other, "dep", "./other.mjs");
map.scopes.emplace_back(ImportMapScopePrefix("https://h.example/b/"),
std::move(other));
FinishImportMap(map);
Check(map.scopes.size() == 2, "one scope per prefix");
Expect(LookupImportMap(map, "dep", "https://h.example/a/x.mjs"),
std::string("./from-https://H.example/a/"), "the last one wins");
Expect(LookupImportMap(map, "only-first", "https://h.example/a/x.mjs"),
std::nullopt, "the last one replaces the whole scope");
}
void TestVolatile() {
LoaderVocabulary vocabulary;
Check(!IsVolatileUrl(vocabulary, "https://h/x.mjs"), "no patterns");
vocabulary.volatilePatterns = {"/@vite/", "hot-update"};
Check(IsVolatileUrl(vocabulary, "https://h/@vite/client"), "first pattern");
Check(IsVolatileUrl(vocabulary, "https://h/x.hot-update.mjs"),
"second pattern");
Check(!IsVolatileUrl(vocabulary, "https://h/x.mjs"), "no match");
}
} // namespace
int main() {
TestEntryValidation();
TestScopePrefix();
TestLookup();
TestDuplicateScopes();
TestVolatile();
if (failures != 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("ImportMapTest: ok\n");
return 0;
}