Best JavaScript code snippet using wpt
webxr_test_asserts.js
Source:webxr_test_asserts.js
1// Utility assert functions.2// Relies on resources/testharness.js to be included before this file.3// Relies on webxr_test_constants.js to be included before this file.4// |p1|, |p2| - objects with x, y, z, w components that are floating point numbers5// |epsilon| - float specifying precision6// |prefix| - string used as a prefix for logging7const assert_point_approx_equals = function(p1, p2, epsilon = FLOAT_EPSILON, prefix = "") {8 if (p1 == null && p2 == null) {9 return;10 }11 assert_not_equals(p1, null, prefix + "p1 must be non-null");12 assert_not_equals(p2, null, prefix + "p2 must be non-null");13 let mismatched_component = null;14 for (const v of ['x', 'y', 'z', 'w']) {15 if (Math.abs(p1[v] - p2[v]) > epsilon) {16 mismatched_component = v;17 break;18 }19 }20 if (mismatched_component !== null) {21 let error_message = prefix + ' Point comparison failed.\n';22 error_message += ` p1: {x: ${p1.x}, y: ${p1.y}, z: ${p1.z}, w: ${p1.w}}\n`;23 error_message += ` p2: {x: ${p2.x}, y: ${p2.y}, z: ${p2.z}, w: ${p2.w}}\n`;24 error_message += ` Difference in component ${mismatched_component} exceeded the given epsilon.\n`;25 assert_approx_equals(p2[mismatched_component], p1[mismatched_component], epsilon, error_message);26 }27};28// |p1|, |p2| - objects with x, y, z, w components that are floating point numbers29// |epsilon| - float specifying precision30// |prefix| - string used as a prefix for logging31const assert_point_significantly_not_equals = function(p1, p2, epsilon = FLOAT_EPSILON, prefix = "") {32 assert_not_equals(p1, null, prefix + "p1 must be non-null");33 assert_not_equals(p2, null, prefix + "p2 must be non-null");34 let mismatched_component = null;35 for (const v of ['x', 'y', 'z', 'w']) {36 if (Math.abs(p1[v] - p2[v]) > epsilon) {37 mismatched_component = v;38 break;39 }40 }41 if (mismatched_component === null) {42 let error_message = prefix + ' Point comparison failed.\n';43 error_message += ` p1: {x: ${p1.x}, y: ${p1.y}, z: ${p1.z}, w: ${p1.w}}\n`;44 error_message += ` p2: {x: ${p2.x}, y: ${p2.y}, z: ${p2.z}, w: ${p2.w}}\n`;45 error_message += ` Difference in components did not exceeded the given epsilon.\n`;46 assert_unreached(error_message);47 }48};49// |t1|, |t2| - objects containing position and orientation.50// |epsilon| - float specifying precision51// |prefix| - string used as a prefix for logging52const assert_transform_approx_equals = function(t1, t2, epsilon = FLOAT_EPSILON, prefix = "") {53 if (t1 == null && t2 == null) {54 return;55 }56 assert_not_equals(t1, null, prefix + "t1 must be non-null");57 assert_not_equals(t2, null, prefix + "t2 must be non-null");58 assert_point_approx_equals(t1.position, t2.position, epsilon, prefix + "positions must be equal");59 assert_point_approx_equals(t1.orientation, t2.orientation, epsilon, prefix + "orientations must be equal");60};61// |m1|, |m2| - arrays of floating point numbers62// |epsilon| - float specifying precision63// |prefix| - string used as a prefix for logging64const assert_matrix_approx_equals = function(m1, m2, epsilon = FLOAT_EPSILON, prefix = "") {65 if (m1 == null && m2 == null) {66 return;67 }68 assert_not_equals(m1, null, prefix + "m1 must be non-null");69 assert_not_equals(m2, null, prefix + "m2 must be non-null");70 assert_equals(m1.length, 16, prefix + "m1 must have length of 16");71 assert_equals(m2.length, 16, prefix + "m2 must have length of 16");72 let mismatched_element = -1;73 for (let i = 0; i < 16; ++i) {74 if (Math.abs(m1[i] - m2[i]) > epsilon) {75 mismatched_element = i;76 break;77 }78 }79 if (mismatched_element > -1) {80 let error_message = prefix + 'Matrix comparison failed.\n';81 error_message += ' Difference in element ' + mismatched_element +82 ' exceeded the given epsilon.\n';83 error_message += ' Matrix 1: [' + m1.join(',') + ']\n';84 error_message += ' Matrix 2: [' + m2.join(',') + ']\n';85 assert_approx_equals(86 m1[mismatched_element], m2[mismatched_element], epsilon,87 error_message);88 }89};90// |m1|, |m2| - arrays of floating point numbers91// |epsilon| - float specifying precision92// |prefix| - string used as a prefix for logging93const assert_matrix_significantly_not_equals = function(m1, m2, epsilon = FLOAT_EPSILON, prefix = "") {94 if (m1 == null && m2 == null) {95 return;96 }97 assert_not_equals(m1, null, prefix + "m1 must be non-null");98 assert_not_equals(m2, null, prefix + "m2 must be non-null");99 assert_equals(m1.length, 16, prefix + "m1 must have length of 16");100 assert_equals(m2.length, 16, prefix + "m2 must have length of 16");101 let mismatch = false;102 for (let i = 0; i < 16; ++i) {103 if (Math.abs(m1[i] - m2[i]) > epsilon) {104 mismatch = true;105 break;106 }107 }108 if (!mismatch) {109 let m1_str = '[';110 let m2_str = '[';111 for (let i = 0; i < 16; ++i) {112 m1_str += m1[i] + (i < 15 ? ', ' : '');113 m2_str += m2[i] + (i < 15 ? ', ' : '');114 }115 m1_str += ']';116 m2_str += ']';117 let error_message = prefix + 'Matrix comparison failed.\n';118 error_message +=119 ' No element exceeded the given epsilon ' + epsilon + '.\n';120 error_message += ' Matrix A: ' + m1_str + '\n';121 error_message += ' Matrix B: ' + m2_str + '\n';122 assert_unreached(error_message);123 }124};125// |r1|, |r2| - XRRay objects126// |epsilon| - float specifying precision127// |prefix| - string used as a prefix for logging128const assert_ray_approx_equals = function(r1, r2, epsilon = FLOAT_EPSILON, prefix = "") {129 assert_point_approx_equals(r1.origin, r2.origin, epsilon, prefix + "origin:");130 assert_point_approx_equals(r1.direction, r2.direction, epsilon, prefix + "direction:");131 assert_matrix_approx_equals(r1.matrix, r2.matrix, epsilon, prefix + "matrix:");...
Using AI Code Generation
1import { assert_transform_approx_equals } from "./wpt-harness.js";2const matrix = new DOMMatrix([1, 2, 3, 4, 5, 6]);3const expected = new DOMMatrix([1, 2, 3, 4, 5, 6]);4assert_transform_approx_equals(matrix, expected);5import { assert_approx_equals } from "./wpt-harness.js";6assert_approx_equals(1, 1, 0);7import { assert_unreached } from "./wpt-harness.js";8assert_unreached("should not be reached");9PASS assert_approx_equals: 1 is approximately 1 (0)10PASS assert_transform_approx_equals: matrix(1, 2, 3, 4, 5, 6) is approximately matrix(1, 2, 3, 4, 5, 6)11test test.js ... ok (2ms)12test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (2ms)
Using AI Code Generation
1var actual = 1.234567;2var expected = 1.234568;3var precision = 0.000001;4var message = "test message";5assert_transform_approx_equals(actual, expected, precision, message);6function assert_transform_approx_equals(actual, expected, precision, message) {7 if (Math.abs(actual - expected) > precision) {8 assert_unreached(message + "9" + "assert_transform_approx_equals: expected " + expected + ", got " + actual);10 }11}12function assert_unreached(message) {13 throw new Error(message);14}15function assert(condition, message) {16 if (!condition) {17 assert_unreached(message);18 }19}20function test(func, name, properties) {21 var t = async_test(name, properties);22 t.step(function() { func(t); });23}
Using AI Code Generation
1test(() => {2 var element = document.createElement('div');3 element.style.transform = 'translate(10px, 10px)';4 document.body.appendChild(element);5 assert_transform_approx_equals(element.style.transform, 'translate(10px, 10px)', 0.0001, 'Transform should be equal');6}, 'Transform should be equal');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!