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):
], [
'resources/WebIDLParser.js',
'http/tests/w3c/resources/WebIDLParser.js',
], [
'resources/testharness-helpers.js',
'http/tests/resources/testharness-helpers.js',
]]
def _absolute_path(s):
......
......@@ -185,3 +185,72 @@ function prepopulated_cache_test(entries, test_function, 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) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request.url)
.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 with URL');
......@@ -23,7 +23,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request)
.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 with Request');
......@@ -31,7 +31,7 @@ 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))
.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 with new Request');
......@@ -40,7 +40,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a.request,
{ignoreSearch: true})
.then(function(result) {
assert_object_in_array(
assert_response_in_array(
result,
[
entries.a.response,
......@@ -57,7 +57,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.a_with_query.request,
{ignoreSearch: true})
.then(function(result) {
assert_object_in_array(
assert_response_in_array(
result,
[
entries.a.response,
......@@ -72,7 +72,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.cat.request.url + '#mouse')
.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 with URL containing fragment');
......@@ -90,7 +90,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.match(entries.secret_cat.request.url)
.then(function(result) {
assert_object_equals_fixed(
assert_response_equals(
result, entries.secret_cat.response,
'Cache.match should not ignore embedded credentials');
});
......@@ -99,7 +99,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.match('http://example.com/c')
.then(function(result) {
assert_object_in_array(
assert_response_in_array(
result,
[
entries.vary_wildcard.response,
......@@ -126,7 +126,7 @@ cache_test(function(cache) {
return cache.match(request.url);
})
.then(function(result) {
assert_object_equals_fixed(
assert_response_equals(
result, response,
'Cache.match should return a Response object that has the same ' +
'properties as the stored response.');
......@@ -182,7 +182,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
var response = entries.non_2xx_response.response;
return cache.match(entries.non_2xx_response.request.url)
.then(function(result) {
assert_object_equals_fixed(
assert_response_equals(
result, entries.non_2xx_response.response,
'Cache.match should return a Response object that has the ' +
'same properties as a stored non-2xx response.');
......@@ -193,7 +193,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
var response = entries.error_response.response;
return cache.match(entries.error_response.request.url)
.then(function(result) {
assert_object_equals_fixed(
assert_response_equals(
result, entries.error_response.response,
'Cache.match should return a Response object that has the ' +
'same properties as a stored network error response.');
......
......@@ -7,7 +7,7 @@ if (self.importScripts) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll('not-present-in-the-cache')
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result, [],
'Cache.matchAll should resolve with an empty array on failure.');
});
......@@ -16,7 +16,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request.url)
.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 with URL');
......@@ -24,7 +24,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request)
.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 with Request');
......@@ -32,7 +32,7 @@ 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))
.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 with new Request');
......@@ -41,7 +41,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a.request,
{ignoreSearch: true})
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.a.response,
......@@ -58,7 +58,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a_with_query.request,
{ignoreSearch: true})
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.a.response,
......@@ -73,7 +73,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.cat.request.url + '#mouse')
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.cat.response,
......@@ -85,7 +85,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll('http')
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result, [],
'Cache.matchAll should treat query as a URL and not ' +
'just a string fragment.');
......@@ -95,7 +95,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.secret_cat.request.url)
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result, [entries.secret_cat.response],
'Cache.matchAll should not ignore embedded credentials');
});
......@@ -104,7 +104,7 @@ prepopulated_cache_test(simple_entries, function(cache, entries) {
prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.matchAll('http://example.com/c')
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.vary_wildcard.response,
......@@ -121,7 +121,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
{headers: {'Cookies': 'none-of-the-above'}}));
})
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.vary_wildcard.response
......@@ -137,7 +137,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
{headers: {'Cookies': 'is-for-cookie'}}));
})
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[entries.vary_cookie_is_cookie.response],
'Cache.matchAll should match the entire header if a vary header ' +
......@@ -149,7 +149,7 @@ prepopulated_cache_test(vary_entries, function(cache, entries) {
return cache.matchAll('http://example.com/c',
{ignoreVary: true})
.then(function(result) {
assert_array_equivalent(
assert_response_array_equivalent(
result,
[
entries.vary_cookie_is_cookie.response,
......
......@@ -30,7 +30,7 @@ cache_test(function(cache) {
return cache.match(test_url);
})
.then(function(result) {
assert_object_equals_fixed(result, response,
assert_response_equals(result, response,
'Cache.put should update the cache with ' +
'new request and response.');
return result.text();
......@@ -75,7 +75,7 @@ cache_test(function(cache) {
return cache.match(test_url);
})
.then(function(result) {
assert_object_equals_fixed(result, response,
assert_response_equals(result, response,
'Cache.put should update the cache with ' +
'new Request and Response.');
});
......@@ -118,7 +118,7 @@ cache_test(function(cache) {
return cache.match(test_url);
})
.then(function(result) {
assert_object_equals_fixed(result, response,
assert_response_equals(result, response,
'Cache.put should update the cache with ' +
'new request and response.');
return result.text();
......@@ -142,7 +142,7 @@ cache_test(function(cache) {
return cache.match(test_url);
})
.then(function(result) {
assert_object_equals_fixed(result, alternate_response,
assert_response_equals(result, alternate_response,
'Cache.put should replace existing ' +
'response with new response.');
return result.text();
......@@ -168,7 +168,7 @@ cache_test(function(cache) {
return cache.match(test_url);
})
.then(function(result) {
assert_object_equals_fixed(result, alternate_response,
assert_response_equals(result, alternate_response,
'Cache.put should replace existing ' +
'response with new response.');
return result.text();
......@@ -248,7 +248,7 @@ cache_test(function(cache) {
return cache.match(new URL('relative-url', location.href).href);
})
.then(function(result) {
assert_object_equals_fixed(result, response,
assert_response_equals(result, response,
'Cache.put should accept a relative URL ' +
'as the request.');
});
......
......@@ -30,7 +30,7 @@ cache_test(function(cache) {
return self.caches.match(transaction.request);
})
.then(function(response) {
assert_object_equals_fixed(response, transaction.response,
assert_response_equals(response, transaction.response,
'The response should not have changed.');
});
}, 'CacheStorageMatch with no cache name provided');
......@@ -49,7 +49,7 @@ cache_test(function(cache) {
return self.caches.match(transaction.request);
})
.then(function(response) {
assert_object_equals_fixed(response, transaction.response,
assert_response_equals(response, transaction.response,
'The response should not have changed.');
});
}, 'CacheStorageMatch from one of many caches');
......@@ -70,7 +70,7 @@ promise_test(function(test) {
return self.caches.match(transaction.request, {cacheName: 'x'});
})
.then(function(response) {
assert_object_equals_fixed(response, transaction.response,
assert_response_equals(response, transaction.response,
'The response should not have changed.');
})
.then(function() {
......@@ -89,7 +89,7 @@ cache_test(function(cache) {
return self.caches.match(transaction.request);
})
.then(function(response) {
assert_object_equals_fixed(response, transaction.response,
assert_response_equals(response, transaction.response,
'The response should not have changed.');
});
}, 'CacheStorageMatch a string request');
......
......@@ -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
// |attribute_name| following the conditions specified by WebIDL, but it's
// acceptable that the attribute |attribute_name| is an own property of the
......
......@@ -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
// |attribute_name| following the conditions specified by WebIDL, but it's
// 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