Skip to content

Commit

Permalink
wpt: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and ronag committed Oct 22, 2022
1 parent 8ee8f01 commit 54437e2
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/wpt/start-xhr.mjs
@@ -0,0 +1,7 @@
import { WPTRunner } from './runner/runner/runner.mjs'
import { once } from 'events'

const runner = new WPTRunner('xhr', 'http://localhost:3333')
runner.run()

await once(runner, 'completion')
1 change: 1 addition & 0 deletions test/wpt/status/xhr.status.json
@@ -0,0 +1 @@
{}
37 changes: 37 additions & 0 deletions test/wpt/tests/xhr/append.any.js
@@ -0,0 +1,37 @@
// META: title=FormData.append

test(function() {
assert_equals(create_formdata(['key', 'value1']).get('key'), "value1");
}, 'testFormDataAppend1');
test(function() {
assert_equals(create_formdata(['key', 'value2'], ['key', 'value1']).get('key'), "value2");
}, 'testFormDataAppend2');
test(function() {
assert_equals(create_formdata(['key', undefined]).get('key'), "undefined");
}, 'testFormDataAppendUndefined1');
test(function() {
assert_equals(create_formdata(['key', undefined], ['key', 'value1']).get('key'), "undefined");
}, 'testFormDataAppendUndefined2');
test(function() {
assert_equals(create_formdata(['key', null]).get('key'), "null");
}, 'testFormDataAppendNull1');
test(function() {
assert_equals(create_formdata(['key', null], ['key', 'value1']).get('key'), "null");
}, 'testFormDataAppendNull2');
test(function() {
var before = new Date(new Date().getTime() - 2000); // two seconds ago, in case there's clock drift
var fd = create_formdata(['key', new Blob(), 'blank.txt']).get('key');
assert_equals(fd.name, "blank.txt");
assert_equals(fd.type, "");
assert_equals(fd.size, 0);
assert_greater_than_equal(fd.lastModified, before);
assert_less_than_equal(fd.lastModified, new Date());
}, 'testFormDataAppendEmptyBlob');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.append.apply(fd, arguments[i]);
};
return fd;
}
6 changes: 6 additions & 0 deletions test/wpt/tests/xhr/constructor.any.js
@@ -0,0 +1,6 @@
// META: title=FormData: constructor

test(() => {
assert_throws_js(TypeError, () => { new FormData(null); });
assert_throws_js(TypeError, () => { new FormData("string"); });
}, "Constructors should throw a type error");
26 changes: 26 additions & 0 deletions test/wpt/tests/xhr/delete.any.js
@@ -0,0 +1,26 @@
// META: title=FormData: delete

test(function() {
var fd = create_formdata(['key', 'value1'], ['key', 'value2']);
fd.delete('key');
assert_equals(fd.get('key'), null);
}, 'testFormDataDelete');
test(function() {
var fd = create_formdata(['key', 'value1'], ['key', 'value2']);
fd.delete('nil');
assert_equals(fd.get('key'), 'value1');
}, 'testFormDataDeleteNonExistentKey');
test(function() {
var fd = create_formdata(['key1', 'value1'], ['key2', 'value2']);
fd.delete('key1');
assert_equals(fd.get('key1'), null);
assert_equals(fd.get('key2'), 'value2');
}, 'testFormDataDeleteOtherKey');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.append.apply(fd, arguments[i]);
};
return fd;
}
56 changes: 56 additions & 0 deletions test/wpt/tests/xhr/foreach.any.js
@@ -0,0 +1,56 @@
// META: title=FormData: foreach

var fd = new FormData();
fd.append('n1', 'v1');
fd.append('n2', 'v2');
fd.append('n3', 'v3');
fd.append('n1', 'v4');
fd.append('n2', 'v5');
fd.append('n3', 'v6');
fd.delete('n2');

var file = new File(['hello'], "hello.txt");
fd.append('f1', file);

var expected_keys = ['n1', 'n3', 'n1', 'n3', 'f1'];
var expected_values = ['v1', 'v3', 'v4', 'v6', file];
test(function() {
var mykeys = [], myvalues = [];
for(var entry of fd) {
assert_equals(entry.length, 2,
'Default iterator should yield key/value pairs');
mykeys.push(entry[0]);
myvalues.push(entry[1]);
}
assert_array_equals(mykeys, expected_keys,
'Default iterator should see duplicate keys');
assert_array_equals(myvalues, expected_values,
'Default iterator should see non-deleted values');
}, 'Iterator should return duplicate keys and non-deleted values');
test(function() {
var mykeys = [], myvalues = [];
for(var entry of fd.entries()) {
assert_equals(entry.length, 2,
'entries() iterator should yield key/value pairs');
mykeys.push(entry[0]);
myvalues.push(entry[1]);
}
assert_array_equals(mykeys, expected_keys,
'entries() iterator should see duplicate keys');
assert_array_equals(myvalues, expected_values,
'entries() iterator should see non-deleted values');
}, 'Entries iterator should return duplicate keys and non-deleted values');
test(function() {
var mykeys = [];
for(var entry of fd.keys())
mykeys.push(entry);
assert_array_equals(mykeys, expected_keys,
'keys() iterator should see duplicate keys');
}, 'Keys iterator should return duplicates');
test(function() {
var myvalues = [];
for(var entry of fd.values())
myvalues.push(entry);
assert_array_equals(myvalues, expected_values,
'values() iterator should see non-deleted values');
}, 'Values iterator should return non-deleted values');
28 changes: 28 additions & 0 deletions test/wpt/tests/xhr/get.any.js
@@ -0,0 +1,28 @@
// META: title=FormData: get and getAll

test(function() {
assert_equals(create_formdata(['key', 'value1'], ['key', 'value2']).get('key'), "value1");
}, 'testFormDataGet');
test(function() {
assert_equals(create_formdata(['key', 'value1'], ['key', 'value2']).get('nil'), null);
}, 'testFormDataGetNull1');
test(function() {
assert_equals(create_formdata().get('key'), null);
}, 'testFormDataGetNull2');
test(function() {
assert_array_equals(create_formdata(['key', 'value1'], ['key', 'value2']).getAll('key'), ["value1", "value2"]);
}, 'testFormDataGetAll');
test(function() {
assert_array_equals(create_formdata(['key', 'value1'], ['key', 'value2']).getAll('nil'), []);
}, 'testFormDataGetAllEmpty1');
test(function() {
assert_array_equals(create_formdata().getAll('key'), []);
}, 'testFormDataGetAllEmpty2');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.append.apply(fd, arguments[i]);
};
return fd;
}
19 changes: 19 additions & 0 deletions test/wpt/tests/xhr/has.any.js
@@ -0,0 +1,19 @@
// META: title=FormData: has

test(function() {
assert_equals(create_formdata(['key', 'value1'], ['key', 'value2']).has('key'), true);
}, 'testFormDataHas');
test(function() {
assert_equals(create_formdata(['key', 'value1'], ['key', 'value2']).has('nil'), false);
}, 'testFormDataHasEmpty1');
test(function() {
assert_equals(create_formdata().has('key'), false);
}, 'testFormDataHasEmpty2');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.append.apply(fd, arguments[i]);
};
return fd;
}
50 changes: 50 additions & 0 deletions test/wpt/tests/xhr/set-blob.any.js
@@ -0,0 +1,50 @@
// META: title=formData.set(blob) and formData.set(file)

"use strict";

const formData = new FormData();

test(() => {
formData.set("blob-1", new Blob());
const blob1 = formData.get("blob-1");
assert_equals(blob1.constructor.name, "File");
assert_equals(blob1.name, "blob");
assert_equals(blob1.type, "");
assert_less_than(Math.abs(blob1.lastModified - Date.now()), 200, "lastModified should be now");
}, "blob without type");

test(() => {
formData.set("blob-2", new Blob([], { type: "text/plain" }));
const blob2 = formData.get("blob-2");
assert_equals(blob2.constructor.name, "File");
assert_equals(blob2.name, "blob");
assert_equals(blob2.type, "text/plain");
assert_less_than(Math.abs(blob2.lastModified - Date.now()), 200, "lastModified should be now");
}, "blob with type");

test(() => {
formData.set("blob-3", new Blob(), "custom name");
const blob3 = formData.get("blob-3");
assert_equals(blob3.constructor.name, "File");
assert_equals(blob3.name, "custom name");
assert_equals(blob3.type, "");
assert_less_than(Math.abs(blob3.lastModified - Date.now()), 200, "lastModified should be now");
}, "blob with custom name");

test(() => {
formData.set("file-1", new File([], "name"));
const file1 = formData.get("file-1");
assert_equals(file1.constructor.name, "File");
assert_equals(file1.name, "name");
assert_equals(file1.type, "");
assert_less_than(Math.abs(file1.lastModified - Date.now()), 200, "lastModified should be now");
}, "file without lastModified or custom name");

test(() => {
formData.set("file-2", new File([], "name", { lastModified: 123 }), "custom name");
const file2 = formData.get("file-2");
assert_equals(file2.constructor.name, "File");
assert_equals(file2.name, "custom name");
assert_equals(file2.type, "");
assert_equals(file2.lastModified, 123, "lastModified should be 123");
}, "file with lastModified and custom name");
36 changes: 36 additions & 0 deletions test/wpt/tests/xhr/set.any.js
@@ -0,0 +1,36 @@
// META: title=FormData: set

test(function() {
assert_equals(create_formdata(['key', 'value1']).get('key'), "value1");
}, 'testFormDataSet1');
test(function() {
assert_equals(create_formdata(['key', 'value2'], ['key', 'value1']).get('key'), "value1");
}, 'testFormDataSet2');
test(function() {
assert_equals(create_formdata(['key', undefined]).get('key'), "undefined");
}, 'testFormDataSetUndefined1');
test(function() {
assert_equals(create_formdata(['key', undefined], ['key', 'value1']).get('key'), "value1");
}, 'testFormDataSetUndefined2');
test(function() {
assert_equals(create_formdata(['key', null]).get('key'), "null");
}, 'testFormDataSetNull1');
test(function() {
assert_equals(create_formdata(['key', null], ['key', 'value1']).get('key'), "value1");
}, 'testFormDataSetNull2');
test(function() {
var fd = new FormData();
fd.set('key', new Blob([]), 'blank.txt');
var file = fd.get('key');

assert_true(file instanceof File);
assert_equals(file.name, 'blank.txt');
}, 'testFormDataSetEmptyBlob');

function create_formdata() {
var fd = new FormData();
for (var i = 0; i < arguments.length; i++) {
fd.set.apply(fd, arguments[i]);
};
return fd;
}

0 comments on commit 54437e2

Please sign in to comment.