Skip to content

Commit

Permalink
Introduce IdlInterfaceMember#toString to improve test names. (#13142)
Browse files Browse the repository at this point in the history
This deduplicates some code, and improves the output. This ensures that unions
and generics don't show up as [object Object], and it makes the output for
other arguments more accurate.
  • Loading branch information
Ms2ger authored Feb 20, 2020
1 parent 791f628 commit 06ef87a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
58 changes: 37 additions & 21 deletions resources/idlharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2406,10 +2406,7 @@ IdlInterface.prototype.test_member_operation = function(member)
if (!shouldRunSubTest(this.name)) {
return;
}
var a_test = subsetTestByKey(this.name, async_test, this.name + " interface: operation " + member.name +
"(" + member.arguments.map(
function(m) {return m.idlType.idlType; } ).join(", ")
+")");
var a_test = subsetTestByKey(this.name, async_test, this.name + " interface: operation " + member);
a_test.step(function()
{
// This function tests WebIDL as of 2015-12-29.
Expand Down Expand Up @@ -2883,11 +2880,6 @@ IdlInterface.prototype.test_interface_of = function(desc, obj, exception, expect
|| member.type == "operation")
&& member.name)
{
var described_name = member.name;
if (member.type == "operation")
{
described_name += "(" + member.arguments.map(arg => arg.idlType.idlType).join(", ") + ")";
}
subsetTestByKey(this.name, test, function()
{
assert_equals(exception, null, "Unexpected exception when evaluating object");
Expand Down Expand Up @@ -2927,16 +2919,17 @@ IdlInterface.prototype.test_interface_of = function(desc, obj, exception, expect
assert_equals(typeof obj[member.name], "function");
}
}
}.bind(this), this.name + " interface: " + desc + ' must inherit property "' + described_name + '" with the proper type');
}.bind(this), this.name + " interface: " + desc + ' must inherit property "' + member + '" with the proper type');
}
// TODO: This is wrong if there are multiple operations with the same
// identifier.
// TODO: Test passing arguments of the wrong type.
if (member.type == "operation" && member.name && member.arguments.length)
{
var a_test = subsetTestByKey(this.name, async_test, this.name + " interface: calling " + member.name +
"(" + member.arguments.map(function(m) { return m.idlType.idlType; }).join(", ") +
") on " + desc + " with too few arguments must throw TypeError");
var description =
this.name + " interface: calling " + member + " on " + desc +
" with too few arguments must throw TypeError";
var a_test = subsetTestByKey(this.name, async_test, description);
a_test.step(function()
{
assert_equals(exception, null, "Unexpected exception when evaluating object");
Expand Down Expand Up @@ -3150,6 +3143,36 @@ IdlInterfaceMember.prototype.is_to_json_regular_operation = function() {
return this.type == "operation" && this.special !== "static" && this.name == "toJSON";
};

IdlInterfaceMember.prototype.toString = function() {
function formatType(type) {
var result;
if (type.generic) {
result = type.generic + "<" + type.idlType.map(formatType).join(", ") + ">";
} else if (type.union) {
result = "(" + type.subtype.map(formatType).join(" or ") + ")";
} else {
result = type.idlType;
}
if (type.nullable) {
result += "?"
}
return result;
}

if (this.type === "operation") {
var args = this.arguments.map(function(m) {
return [
m.optional ? "optional " : "",
formatType(m.idlType),
m.variadic ? "..." : "",
].join("");
}).join(", ");
return this.name + "(" + args + ")";
}

return this.name;
}

/// Internal helper functions ///
function create_suitable_object(type)
{
Expand Down Expand Up @@ -3294,17 +3317,10 @@ IdlNamespace.prototype.test_member_operation = function(member)
if (!shouldRunSubTest(this.name)) {
return;
}
var args = member.arguments.map(function(a) {
var s = a.idlType.idlType;
if (a.variadic) {
s += '...';
}
return s;
}).join(", ");
var a_test = subsetTestByKey(
this.name,
async_test,
this.name + ' namespace: operation ' + member.name + '(' + args + ')');
this.name + ' namespace: operation ' + member);
a_test.step(function() {
assert_own_property(
self[this.name],
Expand Down
36 changes: 36 additions & 0 deletions resources/test/tests/unit/IdlInterfaceMember/toString.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE HTML>
<html>
<head>
<title>IdlInterfaceMember.prototype.toString()</title>
</head>
<body>
<div id="log"></div>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/WebIDLParser.js"></script>
<script src="/resources/idlharness.js"></script>
<script src="../../../idl-helper.js"></script>
<script>
"use strict";
const tests = [
["long x", "long"],
["long? x", "long?"],
["Promise<long> x", "Promise<long>"],
["Promise<long?> x", "Promise<long?>"],
["sequence<long> x", "sequence<long>"],
["(long or DOMString) x", "(long or DOMString)"],
["long x, boolean y", "long, boolean"],
["long x, optional boolean y", "long, optional boolean"],
["long... args", "long..."],
["sequence<long>... args", "sequence<long>..."],
["(long or DOMString)... args", "(long or DOMString)..."],
];
for (const [input, output] of tests) {
test(function() {
var m = memberFrom(`void foo(${input})`);
assert_equals(m.toString(), `foo(${output})`);
}, `toString for ${input}`);
}
</script>
</body>
</html>

0 comments on commit 06ef87a

Please sign in to comment.