Commit 8ecb4688 authored by jsbell@chromium.org's avatar jsbell@chromium.org

Cache Storage: replace assert_object_equals w/ assert_response_equals

In web-platform-tests there's a move away from the generic (and
fragile, buggy) assert_object_equals(). Upstream, the cache storage
tests moved to dedicated assert_response_equals() (and friends);
mirror that in Blink.

R=jkarlin@chromium.org

Review URL: https://codereview.chromium.org/1295633003

git-svn-id: svn://svn.chromium.org/blink/trunk@200998 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 6d5d9574
...@@ -49,6 +49,9 @@ def _CheckIdenticalFiles(input_api, output_api): ...@@ -49,6 +49,9 @@ def _CheckIdenticalFiles(input_api, output_api):
], [ ], [
'resources/WebIDLParser.js', 'resources/WebIDLParser.js',
'http/tests/w3c/resources/WebIDLParser.js', 'http/tests/w3c/resources/WebIDLParser.js',
], [
'resources/testharness-helpers.js',
'http/tests/resources/testharness-helpers.js',
]] ]]
def _absolute_path(s): def _absolute_path(s):
......
...@@ -185,3 +185,72 @@ function prepopulated_cache_test(entries, test_function, description) { ...@@ -185,3 +185,72 @@ function prepopulated_cache_test(entries, test_function, description) {
}); });
}, description); }, description);
} }
// Helper for testing with Headers objects. Compares Headers instances
// by serializing |expected| and |actual| to arrays and comparing.
function assert_header_equals(actual, expected, description) {
assert_class_string(actual, "Headers", description);
var header;
var actual_headers = [];
var expected_headers = [];
for (header of actual)
actual_headers.push(header[0] + ": " + header[1]);
for (header of expected)
expected_headers.push(header[0] + ": " + header[1]);
assert_array_equals(actual_headers, expected_headers,
description + " Headers differ.");
}
// Helper for testing with Response objects. Compares simple
// attributes defined on the interfaces, as well as the headers. It
// does not compare the response bodies.
function assert_response_equals(actual, expected, description) {
assert_class_string(actual, "Response", description);
["type", "url", "status", "ok", "statusText"].forEach(function(attribute) {
assert_equals(actual[attribute], expected[attribute],
description + " Attributes differ: " + attribute + ".");
});
assert_header_equals(actual.headers, expected.headers, description);
}
// Assert that the two arrays |actual| and |expected| contain the same
// set of Responses as determined by assert_response_equals. The order
// is not significant.
//
// |expected| is assumed to not contain any duplicates.
function assert_response_array_equivalent(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
expected.forEach(function(expected_element) {
// assert_response_in_array treats the first argument as being
// 'actual', and the second as being 'expected array'. We are
// switching them around because we want to be resilient
// against the |actual| array containing duplicates.
assert_response_in_array(expected_element, actual, description);
});
}
// Asserts that two arrays |actual| and |expected| contain the same
// set of Responses as determined by assert_response_equals(). The
// corresponding elements must occupy corresponding indices in their
// respective arrays.
function assert_response_array_equals(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
actual.forEach(function(value, index) {
assert_response_equals(value, expected[index],
description + " : object[" + index + "]");
});
}
// Equivalent to assert_in_array, but uses assert_response_equals.
function assert_response_in_array(actual, expected_array, description) {
assert_true(expected_array.some(function(element) {
try {
assert_response_equals(actual, element);
return true;
} catch (e) {
return false;
}
}), description);
}
...@@ -15,7 +15,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -15,7 +15,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request.url) return cache.match(entries.a.request.url)
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, entries.a.response, assert_response_equals(result, entries.a.response,
'Cache.match should match by URL.'); 'Cache.match should match by URL.');
}); });
}, 'Cache.match with URL'); }, 'Cache.match with URL');
...@@ -23,7 +23,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -23,7 +23,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request) return cache.match(entries.a.request)
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, entries.a.response, assert_response_equals(result, entries.a.response,
'Cache.match should match by Request.'); 'Cache.match should match by Request.');
}); });
}, 'Cache.match with Request'); }, 'Cache.match with Request');
...@@ -31,7 +31,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -31,7 +31,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(new Request(entries.a.request.url)) return cache.match(new Request(entries.a.request.url))
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, entries.a.response, assert_response_equals(result, entries.a.response,
'Cache.match should match by Request.'); 'Cache.match should match by Request.');
}); });
}, 'Cache.match with new Request'); }, 'Cache.match with new Request');
...@@ -40,7 +40,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -40,7 +40,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request, return cache.match(entries.a.request,
{ignoreSearch: true}) {ignoreSearch: true})
.then(function(result) { .then(function(result) {
assert_object_in_array( assert_response_in_array(
result, result,
[ [
entries.a.response, entries.a.response,
...@@ -57,7 +57,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -57,7 +57,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a_with_query.request, return cache.match(entries.a_with_query.request,
{ignoreSearch: true}) {ignoreSearch: true})
.then(function(result) { .then(function(result) {
assert_object_in_array( assert_response_in_array(
result, result,
[ [
entries.a.response, entries.a.response,
...@@ -72,7 +72,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -72,7 +72,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.cat.request.url + '#mouse') return cache.match(entries.cat.request.url + '#mouse')
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, entries.cat.response, assert_response_equals(result, entries.cat.response,
'Cache.match should ignore URL fragment.'); 'Cache.match should ignore URL fragment.');
}); });
}, 'Cache.match with URL containing fragment'); }, 'Cache.match with URL containing fragment');
...@@ -90,7 +90,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -90,7 +90,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.secret_cat.request.url) return cache.match(entries.secret_cat.request.url)
.then(function(result) { .then(function(result) {
assert_object_equals_fixed( assert_response_equals(
result, entries.secret_cat.response, result, entries.secret_cat.response,
'Cache.match should not ignore embedded credentials'); 'Cache.match should not ignore embedded credentials');
}); });
...@@ -99,7 +99,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -99,7 +99,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(vary_entries, function(cache, entries) { prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.match('http://example.com/c') return cache.match('http://example.com/c')
.then(function(result) { .then(function(result) {
assert_object_in_array( assert_response_in_array(
result, result,
[ [
entries.vary_wildcard.response, entries.vary_wildcard.response,
...@@ -126,7 +126,7 @@ cache_test(function(cache) { ...@@ -126,7 +126,7 @@ cache_test(function(cache) {
return cache.match(request.url); return cache.match(request.url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed( assert_response_equals(
result, response, result, response,
'Cache.match should return a Response object that has the same ' + 'Cache.match should return a Response object that has the same ' +
'properties as the stored response.'); 'properties as the stored response.');
...@@ -182,7 +182,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -182,7 +182,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
var response = entries.non_2xx_response.response; var response = entries.non_2xx_response.response;
return cache.match(entries.non_2xx_response.request.url) return cache.match(entries.non_2xx_response.request.url)
.then(function(result) { .then(function(result) {
assert_object_equals_fixed( assert_response_equals(
result, entries.non_2xx_response.response, result, entries.non_2xx_response.response,
'Cache.match should return a Response object that has the ' + 'Cache.match should return a Response object that has the ' +
'same properties as a stored non-2xx response.'); 'same properties as a stored non-2xx response.');
...@@ -193,7 +193,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -193,7 +193,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
var response = entries.error_response.response; var response = entries.error_response.response;
return cache.match(entries.error_response.request.url) return cache.match(entries.error_response.request.url)
.then(function(result) { .then(function(result) {
assert_object_equals_fixed( assert_response_equals(
result, entries.error_response.response, result, entries.error_response.response,
'Cache.match should return a Response object that has the ' + 'Cache.match should return a Response object that has the ' +
'same properties as a stored network error response.'); 'same properties as a stored network error response.');
......
...@@ -7,7 +7,7 @@ if (self.importScripts) { ...@@ -7,7 +7,7 @@ if (self.importScripts) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll('not-present-in-the-cache') return cache.matchAll('not-present-in-the-cache')
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, [], result, [],
'Cache.matchAll should resolve with an empty array on failure.'); 'Cache.matchAll should resolve with an empty array on failure.');
}); });
...@@ -16,7 +16,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -16,7 +16,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request.url) return cache.matchAll(entries.a.request.url)
.then(function(result) { .then(function(result) {
assert_array_equivalent(result, [entries.a.response], assert_response_array_equivalent(result, [entries.a.response],
'Cache.matchAll should match by URL.'); 'Cache.matchAll should match by URL.');
}); });
}, 'Cache.matchAll with URL'); }, 'Cache.matchAll with URL');
...@@ -24,7 +24,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -24,7 +24,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request) return cache.matchAll(entries.a.request)
.then(function(result) { .then(function(result) {
assert_array_equivalent(result, [entries.a.response], assert_response_array_equivalent(result, [entries.a.response],
'Cache.matchAll should match by Request.'); 'Cache.matchAll should match by Request.');
}); });
}, 'Cache.matchAll with Request'); }, 'Cache.matchAll with Request');
...@@ -32,7 +32,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -32,7 +32,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(new Request(entries.a.request.url)) return cache.matchAll(new Request(entries.a.request.url))
.then(function(result) { .then(function(result) {
assert_array_equivalent(result, [entries.a.response], assert_response_array_equivalent(result, [entries.a.response],
'Cache.matchAll should match by Request.'); 'Cache.matchAll should match by Request.');
}); });
}, 'Cache.matchAll with new Request'); }, 'Cache.matchAll with new Request');
...@@ -41,7 +41,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -41,7 +41,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request, return cache.matchAll(entries.a.request,
{ignoreSearch: true}) {ignoreSearch: true})
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.a.response, entries.a.response,
...@@ -58,7 +58,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -58,7 +58,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a_with_query.request, return cache.matchAll(entries.a_with_query.request,
{ignoreSearch: true}) {ignoreSearch: true})
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.a.response, entries.a.response,
...@@ -73,7 +73,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -73,7 +73,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.cat.request.url + '#mouse') return cache.matchAll(entries.cat.request.url + '#mouse')
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.cat.response, entries.cat.response,
...@@ -85,7 +85,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -85,7 +85,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll('http') return cache.matchAll('http')
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, [], result, [],
'Cache.matchAll should treat query as a URL and not ' + 'Cache.matchAll should treat query as a URL and not ' +
'just a string fragment.'); 'just a string fragment.');
...@@ -95,7 +95,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -95,7 +95,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) { prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.secret_cat.request.url) return cache.matchAll(entries.secret_cat.request.url)
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, [entries.secret_cat.response], result, [entries.secret_cat.response],
'Cache.matchAll should not ignore embedded credentials'); 'Cache.matchAll should not ignore embedded credentials');
}); });
...@@ -104,7 +104,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) { ...@@ -104,7 +104,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(vary_entries, function(cache, entries) { prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.matchAll('http://example.com/c') return cache.matchAll('http://example.com/c')
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.vary_wildcard.response, entries.vary_wildcard.response,
...@@ -121,7 +121,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) { ...@@ -121,7 +121,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
{headers: {'Cookies': 'none-of-the-above'}})); {headers: {'Cookies': 'none-of-the-above'}}));
}) })
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.vary_wildcard.response entries.vary_wildcard.response
...@@ -137,7 +137,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) { ...@@ -137,7 +137,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
{headers: {'Cookies': 'is-for-cookie'}})); {headers: {'Cookies': 'is-for-cookie'}}));
}) })
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[entries.vary_cookie_is_cookie.response], [entries.vary_cookie_is_cookie.response],
'Cache.matchAll should match the entire header if a vary header ' + 'Cache.matchAll should match the entire header if a vary header ' +
...@@ -149,7 +149,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) { ...@@ -149,7 +149,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.matchAll('http://example.com/c', return cache.matchAll('http://example.com/c',
{ignoreVary: true}) {ignoreVary: true})
.then(function(result) { .then(function(result) {
assert_array_equivalent( assert_response_array_equivalent(
result, result,
[ [
entries.vary_cookie_is_cookie.response, entries.vary_cookie_is_cookie.response,
......
...@@ -30,9 +30,9 @@ cache_test(function(cache) { ...@@ -30,9 +30,9 @@ cache_test(function(cache) {
return cache.match(test_url); return cache.match(test_url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, response, assert_response_equals(result, response,
'Cache.put should update the cache with ' + 'Cache.put should update the cache with ' +
'new request and response.'); 'new request and response.');
return result.text(); return result.text();
}) })
.then(function(body) { .then(function(body) {
...@@ -75,9 +75,9 @@ cache_test(function(cache) { ...@@ -75,9 +75,9 @@ cache_test(function(cache) {
return cache.match(test_url); return cache.match(test_url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, response, assert_response_equals(result, response,
'Cache.put should update the cache with ' + 'Cache.put should update the cache with ' +
'new Request and Response.'); 'new Request and Response.');
}); });
}, 'Cache.put with a Response containing an empty URL'); }, 'Cache.put with a Response containing an empty URL');
...@@ -118,9 +118,9 @@ cache_test(function(cache) { ...@@ -118,9 +118,9 @@ cache_test(function(cache) {
return cache.match(test_url); return cache.match(test_url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, response, assert_response_equals(result, response,
'Cache.put should update the cache with ' + 'Cache.put should update the cache with ' +
'new request and response.'); 'new request and response.');
return result.text(); return result.text();
}) })
.then(function(body) { .then(function(body) {
...@@ -142,9 +142,9 @@ cache_test(function(cache) { ...@@ -142,9 +142,9 @@ cache_test(function(cache) {
return cache.match(test_url); return cache.match(test_url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, alternate_response, assert_response_equals(result, alternate_response,
'Cache.put should replace existing ' + 'Cache.put should replace existing ' +
'response with new response.'); 'response with new response.');
return result.text(); return result.text();
}) })
.then(function(body) { .then(function(body) {
...@@ -168,9 +168,9 @@ cache_test(function(cache) { ...@@ -168,9 +168,9 @@ cache_test(function(cache) {
return cache.match(test_url); return cache.match(test_url);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, alternate_response, assert_response_equals(result, alternate_response,
'Cache.put should replace existing ' + 'Cache.put should replace existing ' +
'response with new response.'); 'response with new response.');
return result.text(); return result.text();
}) })
.then(function(body) { .then(function(body) {
...@@ -248,9 +248,9 @@ cache_test(function(cache) { ...@@ -248,9 +248,9 @@ cache_test(function(cache) {
return cache.match(new URL('relative-url', location.href).href); return cache.match(new URL('relative-url', location.href).href);
}) })
.then(function(result) { .then(function(result) {
assert_object_equals_fixed(result, response, assert_response_equals(result, response,
'Cache.put should accept a relative URL ' + 'Cache.put should accept a relative URL ' +
'as the request.'); 'as the request.');
}); });
}, 'Cache.put with a relative URL'); }, 'Cache.put with a relative URL');
......
...@@ -30,8 +30,8 @@ cache_test(function(cache) { ...@@ -30,8 +30,8 @@ cache_test(function(cache) {
return self.caches.match(transaction.request); return self.caches.match(transaction.request);
}) })
.then(function(response) { .then(function(response) {
assert_object_equals_fixed(response, transaction.response, assert_response_equals(response, transaction.response,
'The response should not have changed.'); 'The response should not have changed.');
}); });
}, 'CacheStorageMatch with no cache name provided'); }, 'CacheStorageMatch with no cache name provided');
...@@ -49,8 +49,8 @@ cache_test(function(cache) { ...@@ -49,8 +49,8 @@ cache_test(function(cache) {
return self.caches.match(transaction.request); return self.caches.match(transaction.request);
}) })
.then(function(response) { .then(function(response) {
assert_object_equals_fixed(response, transaction.response, assert_response_equals(response, transaction.response,
'The response should not have changed.'); 'The response should not have changed.');
}); });
}, 'CacheStorageMatch from one of many caches'); }, 'CacheStorageMatch from one of many caches');
...@@ -70,8 +70,8 @@ promise_test(function(test) { ...@@ -70,8 +70,8 @@ promise_test(function(test) {
return self.caches.match(transaction.request, {cacheName: 'x'}); return self.caches.match(transaction.request, {cacheName: 'x'});
}) })
.then(function(response) { .then(function(response) {
assert_object_equals_fixed(response, transaction.response, assert_response_equals(response, transaction.response,
'The response should not have changed.'); 'The response should not have changed.');
}) })
.then(function() { .then(function() {
return self.caches.match(transaction.request, {cacheName: 'y'}); return self.caches.match(transaction.request, {cacheName: 'y'});
...@@ -89,8 +89,8 @@ cache_test(function(cache) { ...@@ -89,8 +89,8 @@ cache_test(function(cache) {
return self.caches.match(transaction.request); return self.caches.match(transaction.request);
}) })
.then(function(response) { .then(function(response) {
assert_object_equals_fixed(response, transaction.response, assert_response_equals(response, transaction.response,
'The response should not have changed.'); 'The response should not have changed.');
}); });
}, 'CacheStorageMatch a string request'); }, 'CacheStorageMatch a string request');
......
...@@ -32,78 +32,6 @@ function assert_promise_rejects(promise, code, description) { ...@@ -32,78 +32,6 @@ function assert_promise_rejects(promise, code, description) {
}); });
} }
// Equivalent to testharness.js's assert_object_equals(), but correctly
// tests that property ownership is the same.
// TODO(jsbell): Upstream this to assert_object_equals and remove.
function assert_object_equals_fixed(actual, expected, description)
{
function check_equal(actual, expected, stack)
{
stack.push(actual);
var p;
for (p in actual) {
assert_equals(expected.hasOwnProperty(p), actual.hasOwnProperty(p),
"different property ownership: " + p + " - " + description);
if (typeof actual[p] === "object" && actual[p] !== null) {
if (stack.indexOf(actual[p]) === -1) {
check_equal(actual[p], expected[p], stack);
}
} else {
assert_equals(actual[p], expected[p], description);
}
}
for (p in expected) {
assert_equals(expected.hasOwnProperty(p), actual.hasOwnProperty(p),
"different property ownership: " + p + " - " + description);
}
stack.pop();
}
check_equal(actual, expected, []);
}
// Equivalent to assert_in_array, but uses the object-aware equivalence relation
// provided by assert_object_equals_fixed().
function assert_object_in_array(actual, expected_array, description) {
assert_true(expected_array.some(function(element) {
try {
assert_object_equals_fixed(actual, element);
return true;
} catch (e) {
return false;
}
}), description);
}
// Assert that the two arrays |actual| and |expected| contain the same set of
// elements as determined by assert_object_equals_fixed. The order is not significant.
//
// |expected| is assumed to not contain any duplicates as determined by
// assert_object_equals_fixed().
function assert_array_equivalent(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
expected.forEach(function(expected_element) {
// assert_in_array treats the first argument as being 'actual', and the
// second as being 'expected array'. We are switching them around because
// we want to be resilient against the |actual| array containing
// duplicates.
assert_object_in_array(expected_element, actual, description);
});
}
// Asserts that two arrays |actual| and |expected| contain the same set of
// elements as determined by assert_object_equals(). The corresponding elements
// must occupy corresponding indices in their respective arrays.
function assert_array_objects_equals(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
actual.forEach(function(value, index) {
assert_object_equals(value, expected[index],
description + ' : object[' + index + ']');
});
}
// Asserts that |object| that is an instance of some interface has the attribute // Asserts that |object| that is an instance of some interface has the attribute
// |attribute_name| following the conditions specified by WebIDL, but it's // |attribute_name| following the conditions specified by WebIDL, but it's
// acceptable that the attribute |attribute_name| is an own property of the // acceptable that the attribute |attribute_name| is an own property of the
......
...@@ -32,97 +32,6 @@ function assert_promise_rejects(promise, code, description) { ...@@ -32,97 +32,6 @@ function assert_promise_rejects(promise, code, description) {
}); });
} }
// Asserts that two objects |actual| and |expected| are weakly equal under the
// following definition:
//
// |a| and |b| are weakly equal if any of the following are true:
// 1. If |a| is not an 'object', and |a| === |b|.
// 2. If |a| is an 'object', and all of the following are true:
// 2.1 |a.p| is weakly equal to |b.p| for all own properties |p| of |a|.
// 2.2 Every own property of |b| is an own property of |a|.
//
// This is a replacement for the the version of assert_object_equals() in
// testharness.js. The latter doesn't handle own properties correctly. I.e. if
// |a.p| is not an own property, it still requires that |b.p| be an own
// property.
//
// Note that |actual| must not contain cyclic references.
self.assert_object_equals = function(actual, expected, description) {
var object_stack = [];
function _is_equal(actual, expected, prefix) {
if (typeof actual !== 'object') {
assert_equals(actual, expected, prefix);
return;
}
assert_true(typeof expected === 'object', prefix);
assert_equals(object_stack.indexOf(actual), -1,
prefix + ' must not contain cyclic references.');
object_stack.push(actual);
Object.getOwnPropertyNames(expected).forEach(function(property) {
assert_own_property(actual, property, prefix);
_is_equal(actual[property], expected[property],
prefix + '.' + property);
});
Object.getOwnPropertyNames(actual).forEach(function(property) {
assert_own_property(expected, property, prefix);
});
object_stack.pop();
}
function _brand(object) {
return Object.prototype.toString.call(object).match(/^\[object (.*)\]$/)[1];
}
_is_equal(actual, expected,
(description ? description + ': ' : '') + _brand(expected));
};
// Equivalent to assert_in_array, but uses a weaker equivalence relation
// (assert_object_equals) than '==='.
function assert_object_in_array(actual, expected_array, description) {
assert_true(expected_array.some(function(element) {
try {
assert_object_equals(actual, element);
return true;
} catch (e) {
return false;
}
}), description);
}
// Assert that the two arrays |actual| and |expected| contain the same set of
// elements as determined by assert_object_equals. The order is not significant.
//
// |expected| is assumed to not contain any duplicates as determined by
// assert_object_equals().
function assert_array_equivalent(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
expected.forEach(function(expected_element) {
// assert_in_array treats the first argument as being 'actual', and the
// second as being 'expected array'. We are switching them around because
// we want to be resilient against the |actual| array containing
// duplicates.
assert_object_in_array(expected_element, actual, description);
});
}
// Asserts that two arrays |actual| and |expected| contain the same set of
// elements as determined by assert_object_equals(). The corresponding elements
// must occupy corresponding indices in their respective arrays.
function assert_array_objects_equals(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
actual.forEach(function(value, index) {
assert_object_equals(value, expected[index],
description + ' : object[' + index + ']');
});
}
// Asserts that |object| that is an instance of some interface has the attribute // Asserts that |object| that is an instance of some interface has the attribute
// |attribute_name| following the conditions specified by WebIDL, but it's // |attribute_name| following the conditions specified by WebIDL, but it's
// acceptable that the attribute |attribute_name| is an own property of the // acceptable that the attribute |attribute_name| is an own property of the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment