checker.js (1825B)
1 function checkContainer(actual, expected) { 2 if (!actual) return true; 3 if (!expected) return false; 4 return actual.id == expected.id && actual.src == expected.src; 5 } 6 7 function checkAttribution(attribution, expected) { 8 assert_own_property(attribution, 'url'); 9 assert_own_property(attribution, 'scope'); 10 let found = false; 11 for (const e of expected) { 12 if (attribution.url === e.url && 13 attribution.scope === e.scope && 14 checkContainer(attribution.container, e.container)) { 15 found = true; 16 e.found = true; 17 } 18 } 19 assert_true(found, JSON.stringify(attribution) + 20 ' is not found in ' + JSON.stringify(expected) + '.'); 21 } 22 23 function checkBreakdown(breakdown, expected) { 24 assert_own_property(breakdown, 'bytes'); 25 assert_greater_than_equal(breakdown.bytes, 0); 26 assert_own_property(breakdown, 'types'); 27 for (const memoryType of breakdown.types) { 28 assert_equals(typeof memoryType, 'string'); 29 } 30 assert_own_property(breakdown, 'attribution'); 31 for (const attribution of breakdown.attribution) { 32 checkAttribution(attribution, expected); 33 } 34 } 35 36 function isEmptyBreakdownEntry(entry) { 37 return entry.bytes === 0 && entry.attribution.length === 0 && 38 entry.types.length === 0; 39 } 40 41 function checkMeasureMemory(result, expected) { 42 assert_own_property(result, 'bytes'); 43 assert_own_property(result, 'breakdown'); 44 let bytes = 0; 45 for (let breakdown of result.breakdown) { 46 checkBreakdown(breakdown, expected); 47 bytes += breakdown.bytes; 48 } 49 assert_equals(bytes, result.bytes); 50 for (const e of expected) { 51 if (e.required) { 52 assert_true(e.found, 53 JSON.stringify(e) + ' did not appear in the result.'); 54 } 55 } 56 assert_true(result.breakdown.some(isEmptyBreakdownEntry), 57 'The result must include an empty breakdown entry.'); 58 }