Skip to content

Commit 0382c27

Browse files
committed
Migrate more tests to html runner
Implement LocalStorage named get/set (i.e. localStorage["hi"])
1 parent a037413 commit 0382c27

File tree

5 files changed

+189
-161
lines changed

5 files changed

+189
-161
lines changed

src/browser/storage/storage.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub const Bottle = struct {
130130
return @intCast(self.map.count());
131131
}
132132

133-
pub fn _key(self: *Bottle, idx: u32) ?[]const u8 {
133+
pub fn _key(self: *const Bottle, idx: u32) ?[]const u8 {
134134
if (idx >= self.map.count()) return null;
135135

136136
var it = self.map.valueIterator();
@@ -142,7 +142,7 @@ pub const Bottle = struct {
142142
unreachable;
143143
}
144144

145-
pub fn _getItem(self: *Bottle, k: []const u8) ?[]const u8 {
145+
pub fn _getItem(self: *const Bottle, k: []const u8) ?[]const u8 {
146146
return self.map.get(k);
147147
}
148148

@@ -202,6 +202,14 @@ pub const Bottle = struct {
202202
//
203203
// So for now, we won't impement the feature.
204204
}
205+
206+
pub fn named_get(self: *const Bottle, name: []const u8, _: *bool) ?[]const u8 {
207+
return self._getItem(name);
208+
}
209+
210+
pub fn named_set(self: *Bottle, name: []const u8, value: []const u8, _: *bool) !void {
211+
try self._setItem(name, value);
212+
}
205213
};
206214

207215
// Tests

src/browser/url/url.zig

Lines changed: 4 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -501,163 +501,10 @@ const ValueIterable = iterator.Iterable(kv.ValueIterator, "URLSearchParamsValueI
501501
const EntryIterable = iterator.Iterable(kv.EntryIterator, "URLSearchParamsEntryIterator");
502502

503503
const testing = @import("../../testing.zig");
504-
test "Browser.URL" {
505-
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
506-
defer runner.deinit();
507-
508-
try runner.testCases(&.{
509-
.{ "var url = new URL('https://foo.bar/path?query#fragment')", "undefined" },
510-
.{ "url.origin", "https://foo.bar" },
511-
.{ "url.href", "https://foo.bar/path?query#fragment" },
512-
.{ "url.protocol", "https:" },
513-
.{ "url.username", "" },
514-
.{ "url.password", "" },
515-
.{ "url.host", "foo.bar" },
516-
.{ "url.hostname", "foo.bar" },
517-
.{ "url.port", "" },
518-
.{ "url.pathname", "/path" },
519-
.{ "url.search", "?query" },
520-
.{ "url.hash", "#fragment" },
521-
.{ "url.searchParams.get('query')", "" },
522-
523-
.{ "url.search = 'hello=world'", null },
524-
.{ "url.searchParams.size", "1" },
525-
.{ "url.searchParams.get('hello')", "world" },
526-
527-
.{ "url.search = '?over=9000'", null },
528-
.{ "url.searchParams.size", "1" },
529-
.{ "url.searchParams.get('over')", "9000" },
530-
531-
.{ "url.search = ''", null },
532-
.{ "url.searchParams.size", "0" },
533-
534-
.{ " const url2 = new URL(url);", null },
535-
.{ "url2.href", "https://foo.bar/path#fragment" },
536-
537-
.{ " try { new URL(document.createElement('a')); } catch (e) { e }", "TypeError: invalid argument" },
538-
539-
.{ " let a = document.createElement('a');", null },
540-
.{ " a.href = 'https://www.lightpanda.io/over?9000=!!';", null },
541-
.{ " const url3 = new URL(a);", null },
542-
.{ "url3.href", "https://www.lightpanda.io/over?9000=%21%21" },
543-
}, .{});
544-
545-
try runner.testCases(&.{
546-
.{ "var url = new URL('https://foo.bar/path?a=~&b=%7E#fragment')", "undefined" },
547-
.{ "url.searchParams.get('a')", "~" },
548-
.{ "url.searchParams.get('b')", "~" },
549-
.{ "url.searchParams.append('c', 'foo')", "undefined" },
550-
.{ "url.searchParams.get('c')", "foo" },
551-
.{ "url.searchParams.getAll('c').length", "1" },
552-
.{ "url.searchParams.getAll('c')[0]", "foo" },
553-
.{ "url.searchParams.size", "3" },
554-
555-
// search is dynamic
556-
.{ "url.search", "?a=~&b=~&c=foo" },
557-
// href is dynamic
558-
.{ "url.href", "https://foo.bar/path?a=~&b=~&c=foo#fragment" },
559-
560-
.{ "url.searchParams.delete('c', 'foo')", "undefined" },
561-
.{ "url.searchParams.get('c')", "null" },
562-
.{ "url.searchParams.delete('a')", "undefined" },
563-
.{ "url.searchParams.get('a')", "null" },
564-
}, .{});
565-
566-
try runner.testCases(&.{
567-
.{ "var url = new URL('over?9000', 'https://lightpanda.io')", null },
568-
.{ "url.href", "https://lightpanda.io/over?9000" },
569-
}, .{});
570-
571-
try runner.testCases(&.{
572-
.{ "let sk = new URL('sveltekit-internal://')", null },
573-
.{ "sk.protocol", "sveltekit-internal:" },
574-
.{ "sk.host", "" },
575-
.{ "sk.hostname", "" },
576-
.{ "sk.href", "sveltekit-internal://" },
577-
}, .{});
504+
test "Browser: URL" {
505+
try testing.htmlRunner("url/url.html");
578506
}
579507

580-
test "Browser.URLSearchParams" {
581-
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
582-
defer runner.deinit();
583-
try runner.testCases(&.{
584-
.{ "let usp = new URLSearchParams()", null },
585-
.{ "usp.get('a')", "null" },
586-
.{ "usp.has('a')", "false" },
587-
.{ "usp.getAll('a')", "" },
588-
.{ "usp.delete('a')", "undefined" },
589-
590-
.{ "usp.set('a', 1)", "undefined" },
591-
.{ "usp.has('a')", "true" },
592-
.{ "usp.get('a')", "1" },
593-
.{ "usp.getAll('a')", "1" },
594-
595-
.{ "usp.append('a', 2)", "undefined" },
596-
.{ "usp.has('a')", "true" },
597-
.{ "usp.get('a')", "1" },
598-
.{ "usp.getAll('a')", "1,2" },
599-
600-
.{ "usp.append('b', '3')", "undefined" },
601-
.{ "usp.has('a')", "true" },
602-
.{ "usp.get('a')", "1" },
603-
.{ "usp.getAll('a')", "1,2" },
604-
.{ "usp.has('b')", "true" },
605-
.{ "usp.get('b')", "3" },
606-
.{ "usp.getAll('b')", "3" },
607-
608-
.{ "let acc = [];", null },
609-
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "a,a,b" },
610-
611-
.{ "acc = [];", null },
612-
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "1,2,3" },
613-
614-
.{ "acc = [];", null },
615-
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },
616-
617-
.{ "acc = [];", null },
618-
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },
619-
620-
.{ "usp.delete('a')", "undefined" },
621-
.{ "usp.has('a')", "false" },
622-
.{ "usp.has('b')", "true" },
623-
624-
.{ "acc = [];", null },
625-
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "b" },
626-
627-
.{ "acc = [];", null },
628-
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "3" },
629-
630-
.{ "acc = [];", null },
631-
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "b,3" },
632-
633-
.{ "acc = [];", null },
634-
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "b,3" },
635-
}, .{});
636-
637-
try runner.testCases(&.{
638-
.{ "usp = new URLSearchParams('?hello')", null },
639-
.{ "usp.get('hello')", "" },
640-
641-
.{ "usp = new URLSearchParams('?abc=')", null },
642-
.{ "usp.get('abc')", "" },
643-
644-
.{ "usp = new URLSearchParams('?abc=123&')", null },
645-
.{ "usp.get('abc')", "123" },
646-
.{ "usp.size", "1" },
647-
648-
.{ "var fd = new FormData()", null },
649-
.{ "fd.append('a', '1')", null },
650-
.{ "fd.append('a', '2')", null },
651-
.{ "fd.append('b', '3')", null },
652-
.{ "ups = new URLSearchParams(fd)", null },
653-
.{ "ups.size", "3" },
654-
.{ "ups.getAll('a')", "1,2" },
655-
.{ "ups.getAll('b')", "3" },
656-
.{ "fd.delete('a')", null }, // the two aren't linked, it created a copy
657-
.{ "ups.size", "3" },
658-
.{ "ups = new URLSearchParams({over: 9000, spice: 'flow'})", null },
659-
.{ "ups.size", "2" },
660-
.{ "ups.getAll('over')", "9000" },
661-
.{ "ups.getAll('spice')", "flow" },
662-
}, .{});
508+
test "Browser: URLSearchParams" {
509+
try testing.htmlRunner("url/url_search_params.html");
663510
}

src/tests/storage/local_storage.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
<script id=localstorage>
44
testing.expectEqual(0, localStorage.length);
55
testing.expectEqual(null, localStorage.getItem('foo'));
6+
testing.expectEqual(null, localStorage.key(0));
67

78
localStorage.setItem('foo', 'bar');
89
testing.expectEqual(1, localStorage.length)
910
testing.expectEqual('bar', localStorage.getItem('foo'));
11+
testing.expectEqual('bar', localStorage.key(0));
12+
testing.expectEqual(null, localStorage.key(1));
1013

1114
localStorage.removeItem('foo');
1215
testing.expectEqual(0, localStorage.length)
@@ -19,7 +22,7 @@
1922
localStorage.setItem('a', '1');
2023
localStorage.setItem('b', '2');
2124
localStorage.setItem('c', '3');
22-
testing.expectEqual(3, localStorage.length)
25+
testing.expectEqual(4, localStorage.length)
2326
localStorage.clear();
24-
localStorage.length", "0" },
27+
testing.expectEqual(0, localStorage.length)
2528
</script>

src/tests/url/url.html

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<script src="../testing.js"></script>
2+
<script id=url>
3+
var url = new URL('https://foo.bar/path?query#fragment');
4+
testing.expectEqual("https://foo.bar", url.origin);
5+
testing.expectEqual("https://foo.bar/path?query#fragment", url.href);
6+
testing.expectEqual("https:", url.protocol);
7+
testing.expectEqual("", url.username);
8+
testing.expectEqual("", url.password);
9+
testing.expectEqual("foo.bar", url.host);
10+
testing.expectEqual("foo.bar", url.hostname);
11+
testing.expectEqual("", url.port);
12+
testing.expectEqual("/path", url.pathname);
13+
testing.expectEqual("?query", url.search);
14+
testing.expectEqual("#fragment", url.hash);
15+
testing.expectEqual("", url.searchParams.get('query'));
16+
17+
url.search = 'hello=world';
18+
testing.expectEqual(1, url.searchParams.size);
19+
testing.expectEqual("world", url.searchParams.get('hello'));
20+
21+
url.search = '?over=9000';
22+
testing.expectEqual(1, url.searchParams.size);
23+
testing.expectEqual("9000", url.searchParams.get('over'));
24+
25+
url.search = '';
26+
testing.expectEqual(0, url.searchParams.size);
27+
28+
const url2 = new URL(url);
29+
testing.expectEqual("https://foo.bar/path#fragment", url2.href);
30+
</script>
31+
32+
<script id="constructor">
33+
testing.expectError("TypeError: invalid argument", () => {
34+
new URL(document.createElement('a'));
35+
});
36+
37+
let a = document.createElement('a');
38+
a.href = 'https://www.lightpanda.io/over?9000=!!';
39+
const url3 = new URL(a);
40+
testing.expectEqual("https://www.lightpanda.io/over?9000=%21%21", url3.href);
41+
</script>
42+
43+
<script id=searchParams>
44+
url = new URL('https://foo.bar/path?a=~&b=%7E#fragment');
45+
testing.expectEqual("~", url.searchParams.get('a'));
46+
testing.expectEqual("~", url.searchParams.get('b'));
47+
48+
url.searchParams.append('c', 'foo');
49+
testing.expectEqual("foo", url.searchParams.get('c'));
50+
testing.expectEqual(1, url.searchParams.getAll('c').length);
51+
testing.expectEqual("foo", url.searchParams.getAll('c')[0]);
52+
testing.expectEqual(3, url.searchParams.size);
53+
54+
// search is dynamic
55+
testing.expectEqual("?a=~&b=~&c=foo", url.search);
56+
57+
// href is dynamic
58+
testing.expectEqual("https://foo.bar/path?a=~&b=~&c=foo#fragment", url.href);
59+
60+
url.searchParams.delete('c', 'foo');
61+
testing.expectEqual(null, url.searchParams.get('c'));
62+
url.searchParams.delete('a');
63+
testing.expectEqual(null, url.searchParams.get('a'));
64+
</script>
65+
66+
<script id=base>
67+
url = new URL('over?9000', 'https://lightpanda.io');
68+
testing.expectEqual("https://lightpanda.io/over?9000", url.href);
69+
</script>
70+
71+
<script id="svelkit">
72+
let sk = new URL('sveltekit-internal://');
73+
testing.expectEqual("sveltekit-internal:", sk.protocol);
74+
testing.expectEqual("", sk.host);
75+
testing.expectEqual("", sk.hostname);
76+
testing.expectEqual("sveltekit-internal://", sk.href);
77+
</script>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<script src="../testing.js"></script>
2+
<script id=url_search_params>
3+
let usp = new URLSearchParams();
4+
usp.get('a')
5+
testing.expectEqual(false, usp.has('a'));
6+
testing.expectEqual([], usp.getAll('a'));
7+
usp.delete('a');
8+
9+
usp.set('a', 1);
10+
testing.expectEqual(true, usp.has('a'));
11+
testing.expectEqual("1", usp.get('a'));
12+
testing.expectEqual(["1"], usp.getAll('a'));
13+
14+
usp.append('a', 2);
15+
testing.expectEqual(true, usp.has('a'));
16+
testing.expectEqual("1", usp.get('a'));
17+
testing.expectEqual(['1','2'], usp.getAll('a'));
18+
19+
usp.append('b', '3');
20+
testing.expectEqual(true, usp.has('a'));
21+
testing.expectEqual("1", usp.get('a'));
22+
testing.expectEqual(['1','2'], usp.getAll('a'));
23+
testing.expectEqual(true, usp.has('b'));
24+
testing.expectEqual("3", usp.get('b'));
25+
testing.expectEqual(['3'], usp.getAll('b'));
26+
27+
let acc = [];
28+
for (const key of usp.keys()) { acc.push(key) };
29+
testing.expectEqual(['a', 'a', 'b'], acc);
30+
31+
acc = [];
32+
for (const value of usp.values()) { acc.push(value) };
33+
testing.expectEqual(['1', '2', '3'], acc);
34+
35+
acc = [];
36+
for (const entry of usp.entries()) { acc.push(entry) };
37+
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
38+
39+
acc = [];
40+
for (const entry of usp) { acc.push(entry) };
41+
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
42+
43+
usp.delete('a');
44+
testing.expectEqual(false, usp.has('a'));
45+
testing.expectEqual(true, usp.has('b'));
46+
47+
acc = [];
48+
for (const key of usp.keys()) { acc.push(key) };
49+
testing.expectEqual(['b'], acc);
50+
51+
acc = [];
52+
for (const value of usp.values()) { acc.push(value) };
53+
testing.expectEqual(['3'], acc);
54+
55+
acc = [];
56+
for (const entry of usp.entries()) { acc.push(entry) };
57+
testing.expectEqual([['b', '3']], acc);
58+
59+
acc = [];
60+
for (const entry of usp) { acc.push(entry) };
61+
testing.expectEqual([['b', '3']], acc);
62+
</script>
63+
64+
<script id=parsed>
65+
usp = new URLSearchParams('?hello');
66+
testing.expectEqual('', usp.get('hello'));
67+
68+
usp = new URLSearchParams('?abc=');
69+
testing.expectEqual('', usp.get('abc'));
70+
71+
usp = new URLSearchParams('?abc=123&');
72+
testing.expectEqual('123', usp.get('abc'));
73+
testing.expectEqual(1, usp.size);
74+
75+
var fd = new FormData();
76+
fd.append('a', '1');
77+
fd.append('a', '2');
78+
fd.append('b', '3');
79+
ups = new URLSearchParams(fd);
80+
81+
testing.expectEqual(3, ups.size);
82+
testing.expectEqual(['1', '2'], ups.getAll('a'));
83+
84+
testing.expectEqual(['3'], ups.getAll('b'));
85+
86+
fd.delete('a'); // the two aren't linked, it created a copy
87+
testing.expectEqual(3, ups.size);
88+
89+
ups = new URLSearchParams({over: 9000, spice: 'flow'});
90+
testing.expectEqual(2, ups.size);
91+
testing.expectEqual(['9000'], ups.getAll('over'));
92+
testing.expectEqual(['flow'], ups.getAll('spice'));
93+
</script>

0 commit comments

Comments
 (0)