Skip to content

Commit 7fdc857

Browse files
authored
Merge pull request #1004 from lightpanda-io/migrate_some_tests_2
Migrate some tests 2
2 parents 055f697 + 0382c27 commit 7fdc857

File tree

7 files changed

+239
-207
lines changed

7 files changed

+239
-207
lines changed

src/browser/polyfill/webcomponents.zig

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,6 @@ pub const pre =
3737
;
3838

3939
const testing = @import("../../testing.zig");
40-
test "Browser.webcomponents" {
41-
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "<div id=main></div>" });
42-
defer runner.deinit();
43-
44-
try @import("polyfill.zig").preload(testing.allocator, runner.page.main_context);
45-
46-
try runner.testCases(&.{
47-
.{
48-
\\ class LightPanda extends HTMLElement {
49-
\\ constructor() {
50-
\\ super();
51-
\\ }
52-
\\ connectedCallback() {
53-
\\ this.append('connected');
54-
\\ }
55-
\\ }
56-
\\ window.customElements.define("lightpanda-test", LightPanda);
57-
\\ const main = document.getElementById('main');
58-
\\ main.appendChild(document.createElement('lightpanda-test'));
59-
,
60-
null,
61-
},
62-
63-
.{ "main.innerHTML", "<lightpanda-test>connected</lightpanda-test>" },
64-
.{ "document.createElement('lightpanda-test').dataset", "[object DataSet]" },
65-
.{ "document.createElement('lightpanda-test.x').dataset", "[object DataSet]" },
66-
}, .{});
40+
test "Browser: Polyfill.WebComponents" {
41+
try testing.htmlRunner("polyfill/webcomponents.html");
6742
}

src/browser/storage/storage.zig

Lines changed: 13 additions & 23 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,35 +202,25 @@ 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
208216
// -----
209217

210218
const testing = @import("../../testing.zig");
211-
test "Browser.Storage.LocalStorage" {
212-
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
213-
defer runner.deinit();
214-
215-
try runner.testCases(&.{
216-
.{ "localStorage.length", "0" },
217-
218-
.{ "localStorage.setItem('foo', 'bar')", "undefined" },
219-
.{ "localStorage.length", "1" },
220-
.{ "localStorage.getItem('foo')", "bar" },
221-
.{ "localStorage.removeItem('foo')", "undefined" },
222-
.{ "localStorage.length", "0" },
223-
224-
// .{ "localStorage['foo'] = 'bar'", "undefined" },
225-
// .{ "localStorage['foo']", "bar" },
226-
// .{ "localStorage.length", "1" },
227-
228-
.{ "localStorage.clear()", "undefined" },
229-
.{ "localStorage.length", "0" },
230-
}, .{});
219+
test "Browser: Storage.LocalStorage" {
220+
try testing.htmlRunner("storage/local_storage.html");
231221
}
232222

233-
test "storage bottle" {
223+
test "Browser: Storage.Bottle" {
234224
var bottle = Bottle.init(std.testing.allocator);
235225
defer bottle.deinit();
236226

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
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script src="../testing.js"></script>
2+
3+
<div id=main></div>
4+
5+
<script id=webcomponents>
6+
class LightPanda extends HTMLElement {
7+
constructor() {
8+
super();
9+
}
10+
connectedCallback() {
11+
this.append('connected');
12+
}
13+
}
14+
15+
window.customElements.define("lightpanda-test", LightPanda);
16+
const main = document.getElementById('main');
17+
main.appendChild(document.createElement('lightpanda-test'));
18+
19+
testing.expectEqual('<lightpanda-test>connected</lightpanda-test>', main.innerHTML)
20+
testing.expectEqual('[object DataSet]', document.createElement('lightpanda-test').dataset.toString());
21+
testing.expectEqual('[object DataSet]', document.createElement('lightpanda-test.x').dataset.toString());
22+
</script>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script src="../testing.js"></script>
2+
3+
<script id=localstorage>
4+
testing.expectEqual(0, localStorage.length);
5+
testing.expectEqual(null, localStorage.getItem('foo'));
6+
testing.expectEqual(null, localStorage.key(0));
7+
8+
localStorage.setItem('foo', 'bar');
9+
testing.expectEqual(1, localStorage.length)
10+
testing.expectEqual('bar', localStorage.getItem('foo'));
11+
testing.expectEqual('bar', localStorage.key(0));
12+
testing.expectEqual(null, localStorage.key(1));
13+
14+
localStorage.removeItem('foo');
15+
testing.expectEqual(0, localStorage.length)
16+
testing.expectEqual(null, localStorage.getItem('foo'));
17+
18+
localStorage['foo'] = 'bar';
19+
testing.expectEqual(1, localStorage.length);
20+
testing.expectEqual('bar', localStorage['foo']);
21+
22+
localStorage.setItem('a', '1');
23+
localStorage.setItem('b', '2');
24+
localStorage.setItem('c', '3');
25+
testing.expectEqual(4, localStorage.length)
26+
localStorage.clear();
27+
testing.expectEqual(0, localStorage.length)
28+
</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>

0 commit comments

Comments
 (0)