Migrate more tests to html runner

Implement LocalStorage named get/set (i.e. localStorage["hi"])
This commit is contained in:
Karl Seguin
2025-09-03 16:40:01 +08:00
parent a0374133cd
commit 0382c2775e
5 changed files with 189 additions and 161 deletions

View File

@@ -130,7 +130,7 @@ pub const Bottle = struct {
return @intCast(self.map.count());
}
pub fn _key(self: *Bottle, idx: u32) ?[]const u8 {
pub fn _key(self: *const Bottle, idx: u32) ?[]const u8 {
if (idx >= self.map.count()) return null;
var it = self.map.valueIterator();
@@ -142,7 +142,7 @@ pub const Bottle = struct {
unreachable;
}
pub fn _getItem(self: *Bottle, k: []const u8) ?[]const u8 {
pub fn _getItem(self: *const Bottle, k: []const u8) ?[]const u8 {
return self.map.get(k);
}
@@ -202,6 +202,14 @@ pub const Bottle = struct {
//
// So for now, we won't impement the feature.
}
pub fn named_get(self: *const Bottle, name: []const u8, _: *bool) ?[]const u8 {
return self._getItem(name);
}
pub fn named_set(self: *Bottle, name: []const u8, value: []const u8, _: *bool) !void {
try self._setItem(name, value);
}
};
// Tests

View File

@@ -501,163 +501,10 @@ const ValueIterable = iterator.Iterable(kv.ValueIterator, "URLSearchParamsValueI
const EntryIterable = iterator.Iterable(kv.EntryIterator, "URLSearchParamsEntryIterator");
const testing = @import("../../testing.zig");
test "Browser.URL" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();
try runner.testCases(&.{
.{ "var url = new URL('https://foo.bar/path?query#fragment')", "undefined" },
.{ "url.origin", "https://foo.bar" },
.{ "url.href", "https://foo.bar/path?query#fragment" },
.{ "url.protocol", "https:" },
.{ "url.username", "" },
.{ "url.password", "" },
.{ "url.host", "foo.bar" },
.{ "url.hostname", "foo.bar" },
.{ "url.port", "" },
.{ "url.pathname", "/path" },
.{ "url.search", "?query" },
.{ "url.hash", "#fragment" },
.{ "url.searchParams.get('query')", "" },
.{ "url.search = 'hello=world'", null },
.{ "url.searchParams.size", "1" },
.{ "url.searchParams.get('hello')", "world" },
.{ "url.search = '?over=9000'", null },
.{ "url.searchParams.size", "1" },
.{ "url.searchParams.get('over')", "9000" },
.{ "url.search = ''", null },
.{ "url.searchParams.size", "0" },
.{ " const url2 = new URL(url);", null },
.{ "url2.href", "https://foo.bar/path#fragment" },
.{ " try { new URL(document.createElement('a')); } catch (e) { e }", "TypeError: invalid argument" },
.{ " let a = document.createElement('a');", null },
.{ " a.href = 'https://www.lightpanda.io/over?9000=!!';", null },
.{ " const url3 = new URL(a);", null },
.{ "url3.href", "https://www.lightpanda.io/over?9000=%21%21" },
}, .{});
try runner.testCases(&.{
.{ "var url = new URL('https://foo.bar/path?a=~&b=%7E#fragment')", "undefined" },
.{ "url.searchParams.get('a')", "~" },
.{ "url.searchParams.get('b')", "~" },
.{ "url.searchParams.append('c', 'foo')", "undefined" },
.{ "url.searchParams.get('c')", "foo" },
.{ "url.searchParams.getAll('c').length", "1" },
.{ "url.searchParams.getAll('c')[0]", "foo" },
.{ "url.searchParams.size", "3" },
// search is dynamic
.{ "url.search", "?a=~&b=~&c=foo" },
// href is dynamic
.{ "url.href", "https://foo.bar/path?a=~&b=~&c=foo#fragment" },
.{ "url.searchParams.delete('c', 'foo')", "undefined" },
.{ "url.searchParams.get('c')", "null" },
.{ "url.searchParams.delete('a')", "undefined" },
.{ "url.searchParams.get('a')", "null" },
}, .{});
try runner.testCases(&.{
.{ "var url = new URL('over?9000', 'https://lightpanda.io')", null },
.{ "url.href", "https://lightpanda.io/over?9000" },
}, .{});
try runner.testCases(&.{
.{ "let sk = new URL('sveltekit-internal://')", null },
.{ "sk.protocol", "sveltekit-internal:" },
.{ "sk.host", "" },
.{ "sk.hostname", "" },
.{ "sk.href", "sveltekit-internal://" },
}, .{});
test "Browser: URL" {
try testing.htmlRunner("url/url.html");
}
test "Browser.URLSearchParams" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();
try runner.testCases(&.{
.{ "let usp = new URLSearchParams()", null },
.{ "usp.get('a')", "null" },
.{ "usp.has('a')", "false" },
.{ "usp.getAll('a')", "" },
.{ "usp.delete('a')", "undefined" },
.{ "usp.set('a', 1)", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1" },
.{ "usp.append('a', 2)", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1,2" },
.{ "usp.append('b', '3')", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1,2" },
.{ "usp.has('b')", "true" },
.{ "usp.get('b')", "3" },
.{ "usp.getAll('b')", "3" },
.{ "let acc = [];", null },
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "a,a,b" },
.{ "acc = [];", null },
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "1,2,3" },
.{ "acc = [];", null },
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },
.{ "acc = [];", null },
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },
.{ "usp.delete('a')", "undefined" },
.{ "usp.has('a')", "false" },
.{ "usp.has('b')", "true" },
.{ "acc = [];", null },
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "b" },
.{ "acc = [];", null },
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "3" },
.{ "acc = [];", null },
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "b,3" },
.{ "acc = [];", null },
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "b,3" },
}, .{});
try runner.testCases(&.{
.{ "usp = new URLSearchParams('?hello')", null },
.{ "usp.get('hello')", "" },
.{ "usp = new URLSearchParams('?abc=')", null },
.{ "usp.get('abc')", "" },
.{ "usp = new URLSearchParams('?abc=123&')", null },
.{ "usp.get('abc')", "123" },
.{ "usp.size", "1" },
.{ "var fd = new FormData()", null },
.{ "fd.append('a', '1')", null },
.{ "fd.append('a', '2')", null },
.{ "fd.append('b', '3')", null },
.{ "ups = new URLSearchParams(fd)", null },
.{ "ups.size", "3" },
.{ "ups.getAll('a')", "1,2" },
.{ "ups.getAll('b')", "3" },
.{ "fd.delete('a')", null }, // the two aren't linked, it created a copy
.{ "ups.size", "3" },
.{ "ups = new URLSearchParams({over: 9000, spice: 'flow'})", null },
.{ "ups.size", "2" },
.{ "ups.getAll('over')", "9000" },
.{ "ups.getAll('spice')", "flow" },
}, .{});
test "Browser: URLSearchParams" {
try testing.htmlRunner("url/url_search_params.html");
}

View File

@@ -3,10 +3,13 @@
<script id=localstorage>
testing.expectEqual(0, localStorage.length);
testing.expectEqual(null, localStorage.getItem('foo'));
testing.expectEqual(null, localStorage.key(0));
localStorage.setItem('foo', 'bar');
testing.expectEqual(1, localStorage.length)
testing.expectEqual('bar', localStorage.getItem('foo'));
testing.expectEqual('bar', localStorage.key(0));
testing.expectEqual(null, localStorage.key(1));
localStorage.removeItem('foo');
testing.expectEqual(0, localStorage.length)
@@ -19,7 +22,7 @@
localStorage.setItem('a', '1');
localStorage.setItem('b', '2');
localStorage.setItem('c', '3');
testing.expectEqual(3, localStorage.length)
testing.expectEqual(4, localStorage.length)
localStorage.clear();
localStorage.length", "0" },
testing.expectEqual(0, localStorage.length)
</script>

77
src/tests/url/url.html Normal file
View File

@@ -0,0 +1,77 @@
<script src="../testing.js"></script>
<script id=url>
var url = new URL('https://foo.bar/path?query#fragment');
testing.expectEqual("https://foo.bar", url.origin);
testing.expectEqual("https://foo.bar/path?query#fragment", url.href);
testing.expectEqual("https:", url.protocol);
testing.expectEqual("", url.username);
testing.expectEqual("", url.password);
testing.expectEqual("foo.bar", url.host);
testing.expectEqual("foo.bar", url.hostname);
testing.expectEqual("", url.port);
testing.expectEqual("/path", url.pathname);
testing.expectEqual("?query", url.search);
testing.expectEqual("#fragment", url.hash);
testing.expectEqual("", url.searchParams.get('query'));
url.search = 'hello=world';
testing.expectEqual(1, url.searchParams.size);
testing.expectEqual("world", url.searchParams.get('hello'));
url.search = '?over=9000';
testing.expectEqual(1, url.searchParams.size);
testing.expectEqual("9000", url.searchParams.get('over'));
url.search = '';
testing.expectEqual(0, url.searchParams.size);
const url2 = new URL(url);
testing.expectEqual("https://foo.bar/path#fragment", url2.href);
</script>
<script id="constructor">
testing.expectError("TypeError: invalid argument", () => {
new URL(document.createElement('a'));
});
let a = document.createElement('a');
a.href = 'https://www.lightpanda.io/over?9000=!!';
const url3 = new URL(a);
testing.expectEqual("https://www.lightpanda.io/over?9000=%21%21", url3.href);
</script>
<script id=searchParams>
url = new URL('https://foo.bar/path?a=~&b=%7E#fragment');
testing.expectEqual("~", url.searchParams.get('a'));
testing.expectEqual("~", url.searchParams.get('b'));
url.searchParams.append('c', 'foo');
testing.expectEqual("foo", url.searchParams.get('c'));
testing.expectEqual(1, url.searchParams.getAll('c').length);
testing.expectEqual("foo", url.searchParams.getAll('c')[0]);
testing.expectEqual(3, url.searchParams.size);
// search is dynamic
testing.expectEqual("?a=~&b=~&c=foo", url.search);
// href is dynamic
testing.expectEqual("https://foo.bar/path?a=~&b=~&c=foo#fragment", url.href);
url.searchParams.delete('c', 'foo');
testing.expectEqual(null, url.searchParams.get('c'));
url.searchParams.delete('a');
testing.expectEqual(null, url.searchParams.get('a'));
</script>
<script id=base>
url = new URL('over?9000', 'https://lightpanda.io');
testing.expectEqual("https://lightpanda.io/over?9000", url.href);
</script>
<script id="svelkit">
let sk = new URL('sveltekit-internal://');
testing.expectEqual("sveltekit-internal:", sk.protocol);
testing.expectEqual("", sk.host);
testing.expectEqual("", sk.hostname);
testing.expectEqual("sveltekit-internal://", sk.href);
</script>

View File

@@ -0,0 +1,93 @@
<script src="../testing.js"></script>
<script id=url_search_params>
let usp = new URLSearchParams();
usp.get('a')
testing.expectEqual(false, usp.has('a'));
testing.expectEqual([], usp.getAll('a'));
usp.delete('a');
usp.set('a', 1);
testing.expectEqual(true, usp.has('a'));
testing.expectEqual("1", usp.get('a'));
testing.expectEqual(["1"], usp.getAll('a'));
usp.append('a', 2);
testing.expectEqual(true, usp.has('a'));
testing.expectEqual("1", usp.get('a'));
testing.expectEqual(['1','2'], usp.getAll('a'));
usp.append('b', '3');
testing.expectEqual(true, usp.has('a'));
testing.expectEqual("1", usp.get('a'));
testing.expectEqual(['1','2'], usp.getAll('a'));
testing.expectEqual(true, usp.has('b'));
testing.expectEqual("3", usp.get('b'));
testing.expectEqual(['3'], usp.getAll('b'));
let acc = [];
for (const key of usp.keys()) { acc.push(key) };
testing.expectEqual(['a', 'a', 'b'], acc);
acc = [];
for (const value of usp.values()) { acc.push(value) };
testing.expectEqual(['1', '2', '3'], acc);
acc = [];
for (const entry of usp.entries()) { acc.push(entry) };
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
acc = [];
for (const entry of usp) { acc.push(entry) };
testing.expectEqual([['a', '1'], ['a', '2'], ['b', '3']], acc);
usp.delete('a');
testing.expectEqual(false, usp.has('a'));
testing.expectEqual(true, usp.has('b'));
acc = [];
for (const key of usp.keys()) { acc.push(key) };
testing.expectEqual(['b'], acc);
acc = [];
for (const value of usp.values()) { acc.push(value) };
testing.expectEqual(['3'], acc);
acc = [];
for (const entry of usp.entries()) { acc.push(entry) };
testing.expectEqual([['b', '3']], acc);
acc = [];
for (const entry of usp) { acc.push(entry) };
testing.expectEqual([['b', '3']], acc);
</script>
<script id=parsed>
usp = new URLSearchParams('?hello');
testing.expectEqual('', usp.get('hello'));
usp = new URLSearchParams('?abc=');
testing.expectEqual('', usp.get('abc'));
usp = new URLSearchParams('?abc=123&');
testing.expectEqual('123', usp.get('abc'));
testing.expectEqual(1, usp.size);
var fd = new FormData();
fd.append('a', '1');
fd.append('a', '2');
fd.append('b', '3');
ups = new URLSearchParams(fd);
testing.expectEqual(3, ups.size);
testing.expectEqual(['1', '2'], ups.getAll('a'));
testing.expectEqual(['3'], ups.getAll('b'));
fd.delete('a'); // the two aren't linked, it created a copy
testing.expectEqual(3, ups.size);
ups = new URLSearchParams({over: 9000, spice: 'flow'});
testing.expectEqual(2, ups.size);
testing.expectEqual(['9000'], ups.getAll('over'));
testing.expectEqual(['flow'], ups.getAll('spice'));
</script>