Best JavaScript code snippet using wpt
testharness.js
Source:testharness.js
...2740 };2741 function code_unit_str(char) {2742 return 'U+' + char.charCodeAt(0).toString(16);2743 }2744 function sanitize_unpaired_surrogates(str) {2745 return str.replace(2746 /([\ud800-\udbff]+)(?![\udc00-\udfff])|(^|[^\ud800-\udbff])([\udc00-\udfff]+)/g,2747 function(_, low, prefix, high) {2748 var output = prefix || ""; // prefix may be undefined2749 var string = low || high; // only one of these alternates can match2750 for (var i = 0; i < string.length; i++) {2751 output += code_unit_str(string[i]);2752 }2753 return output;2754 });2755 }2756 function sanitize_all_unpaired_surrogates(tests) {2757 forEach (tests,2758 function (test)2759 {2760 var sanitized = sanitize_unpaired_surrogates(test.name);2761 if (test.name !== sanitized) {2762 test.name = sanitized;2763 delete test._structured_clone;2764 }2765 });2766 }2767 Tests.prototype.notify_complete = function() {2768 var this_obj = this;2769 var duplicates;2770 if (this.status.status === null) {2771 duplicates = this.find_duplicates();2772 // Some transports adhere to UTF-8's restriction on unpaired2773 // surrogates. Sanitize the titles so that the results can be2774 // consistently sent via all transports.2775 sanitize_all_unpaired_surrogates(this.tests);2776 // Test names are presumed to be unique within test files--this2777 // allows consumers to use them for identification purposes.2778 // Duplicated names violate this expectation and should therefore2779 // be reported as an error.2780 if (duplicates.length) {2781 this.status.status = this.status.ERROR;2782 this.status.message =2783 duplicates.length + ' duplicate test name' +2784 (duplicates.length > 1 ? 's' : '') + ': "' +2785 duplicates.join('", "') + '"';2786 } else {2787 this.status.status = this.status.OK;2788 }2789 }2790 forEach (this.all_done_callbacks,2791 function(callback)2792 {2793 callback(this_obj.tests, this_obj.status, this_obj.asserts_run);2794 });2795 };2796 /*2797 * Constructs a RemoteContext that tracks tests from a specific worker.2798 */2799 Tests.prototype.create_remote_worker = function(worker) {2800 var message_port;2801 if (is_service_worker(worker)) {2802 message_port = navigator.serviceWorker;2803 worker.postMessage({type: "connect"});2804 } else if (is_shared_worker(worker)) {2805 message_port = worker.port;2806 message_port.start();2807 } else {2808 message_port = worker;2809 }2810 return new RemoteContext(worker, message_port);2811 };2812 /*2813 * Constructs a RemoteContext that tracks tests from a specific window.2814 */2815 Tests.prototype.create_remote_window = function(remote) {2816 remote.postMessage({type: "getmessages"}, "*");2817 return new RemoteContext(2818 remote,2819 window,2820 function(msg) {2821 return msg.source === remote;2822 }2823 );2824 };2825 Tests.prototype.fetch_tests_from_worker = function(worker) {2826 if (this.phase >= this.phases.COMPLETE) {2827 return;2828 }2829 var remoteContext = this.create_remote_worker(worker);2830 this.pending_remotes.push(remoteContext);2831 return remoteContext.done;2832 };2833 function fetch_tests_from_worker(port) {2834 return tests.fetch_tests_from_worker(port);2835 }2836 expose(fetch_tests_from_worker, 'fetch_tests_from_worker');2837 Tests.prototype.fetch_tests_from_window = function(remote) {2838 if (this.phase >= this.phases.COMPLETE) {2839 return;2840 }2841 this.pending_remotes.push(this.create_remote_window(remote));2842 };2843 function fetch_tests_from_window(window) {2844 tests.fetch_tests_from_window(window);2845 }2846 expose(fetch_tests_from_window, 'fetch_tests_from_window');2847 function timeout() {2848 if (tests.timeout_length === null) {2849 tests.timeout();2850 }2851 }2852 expose(timeout, 'timeout');2853 function add_start_callback(callback) {2854 tests.start_callbacks.push(callback);2855 }2856 function add_test_state_callback(callback) {2857 tests.test_state_callbacks.push(callback);2858 }2859 function add_result_callback(callback) {2860 tests.test_done_callbacks.push(callback);2861 }2862 function add_completion_callback(callback) {2863 tests.all_done_callbacks.push(callback);2864 }2865 expose(add_start_callback, 'add_start_callback');2866 expose(add_test_state_callback, 'add_test_state_callback');2867 expose(add_result_callback, 'add_result_callback');2868 expose(add_completion_callback, 'add_completion_callback');2869 function remove(array, item) {2870 var index = array.indexOf(item);2871 if (index > -1) {2872 array.splice(index, 1);2873 }2874 }2875 function remove_start_callback(callback) {2876 remove(tests.start_callbacks, callback);2877 }2878 function remove_test_state_callback(callback) {2879 remove(tests.test_state_callbacks, callback);2880 }2881 function remove_result_callback(callback) {2882 remove(tests.test_done_callbacks, callback);2883 }2884 function remove_completion_callback(callback) {2885 remove(tests.all_done_callbacks, callback);2886 }2887 /*2888 * Output listener2889 */2890 function Output() {2891 this.output_document = document;2892 this.output_node = null;2893 this.enabled = settings.output;2894 this.phase = this.INITIAL;2895 }2896 Output.prototype.INITIAL = 0;2897 Output.prototype.STARTED = 1;2898 Output.prototype.HAVE_RESULTS = 2;2899 Output.prototype.COMPLETE = 3;2900 Output.prototype.setup = function(properties) {2901 if (this.phase > this.INITIAL) {2902 return;2903 }2904 //If output is disabled in testharnessreport.js the test shouldn't be2905 //able to override that2906 this.enabled = this.enabled && (properties.hasOwnProperty("output") ?2907 properties.output : settings.output);2908 };2909 Output.prototype.init = function(properties) {2910 if (this.phase >= this.STARTED) {2911 return;2912 }2913 if (properties.output_document) {2914 this.output_document = properties.output_document;2915 } else {2916 this.output_document = document;2917 }2918 this.phase = this.STARTED;2919 };2920 Output.prototype.resolve_log = function() {2921 var output_document;2922 if (this.output_node) {2923 return;2924 }2925 if (typeof this.output_document === "function") {2926 output_document = this.output_document.apply(undefined);2927 } else {2928 output_document = this.output_document;2929 }2930 if (!output_document) {2931 return;2932 }2933 var node = output_document.getElementById("log");2934 if (!node) {2935 if (output_document.readyState === "loading") {2936 return;2937 }2938 node = output_document.createElementNS("http://www.w3.org/1999/xhtml", "div");2939 node.id = "log";2940 if (output_document.body) {2941 output_document.body.appendChild(node);2942 } else {2943 var root = output_document.documentElement;2944 var is_html = (root &&2945 root.namespaceURI == "http://www.w3.org/1999/xhtml" &&2946 root.localName == "html");2947 var is_svg = (output_document.defaultView &&2948 "SVGSVGElement" in output_document.defaultView &&2949 root instanceof output_document.defaultView.SVGSVGElement);2950 if (is_svg) {2951 var foreignObject = output_document.createElementNS("http://www.w3.org/2000/svg", "foreignObject");2952 foreignObject.setAttribute("width", "100%");2953 foreignObject.setAttribute("height", "100%");2954 root.appendChild(foreignObject);2955 foreignObject.appendChild(node);2956 } else if (is_html) {2957 root.appendChild(output_document.createElementNS("http://www.w3.org/1999/xhtml", "body"))2958 .appendChild(node);2959 } else {2960 root.appendChild(node);2961 }2962 }2963 }2964 this.output_document = output_document;2965 this.output_node = node;2966 };2967 Output.prototype.show_status = function() {2968 if (this.phase < this.STARTED) {2969 this.init();2970 }2971 if (!this.enabled || this.phase === this.COMPLETE) {2972 return;2973 }2974 this.resolve_log();2975 if (this.phase < this.HAVE_RESULTS) {2976 this.phase = this.HAVE_RESULTS;2977 }2978 var done_count = tests.tests.length - tests.num_pending;2979 if (this.output_node && !tests.hide_test_state) {2980 if (done_count < 100 ||2981 (done_count < 1000 && done_count % 100 === 0) ||2982 done_count % 1000 === 0) {2983 this.output_node.textContent = "Running, " +2984 done_count + " complete, " +2985 tests.num_pending + " remain";2986 }2987 }2988 };2989 Output.prototype.show_results = function (tests, harness_status, asserts_run) {2990 if (this.phase >= this.COMPLETE) {2991 return;2992 }2993 if (!this.enabled) {2994 return;2995 }2996 if (!this.output_node) {2997 this.resolve_log();2998 }2999 this.phase = this.COMPLETE;3000 var log = this.output_node;3001 if (!log) {3002 return;3003 }3004 var output_document = this.output_document;3005 while (log.lastChild) {3006 log.removeChild(log.lastChild);3007 }3008 var stylesheet = output_document.createElementNS(xhtml_ns, "style");3009 stylesheet.textContent = stylesheetContent;3010 var heads = output_document.getElementsByTagName("head");3011 if (heads.length) {3012 heads[0].appendChild(stylesheet);3013 }3014 var status_number = {};3015 forEach(tests,3016 function(test) {3017 var status = test.format_status();3018 if (status_number.hasOwnProperty(status)) {3019 status_number[status] += 1;3020 } else {3021 status_number[status] = 1;3022 }3023 });3024 function status_class(status)3025 {3026 return status.replace(/\s/g, '').toLowerCase();3027 }3028 var summary_template = ["section", {"id":"summary"},3029 ["h2", {}, "Summary"],3030 function()3031 {3032 var status = harness_status.format_status();3033 var rv = [["section", {},3034 ["p", {},3035 "Harness status: ",3036 ["span", {"class":status_class(status)},3037 status3038 ],3039 ]3040 ]];3041 if (harness_status.status === harness_status.ERROR) {3042 rv[0].push(["pre", {}, harness_status.message]);3043 if (harness_status.stack) {3044 rv[0].push(["pre", {}, harness_status.stack]);3045 }3046 }3047 return rv;3048 },3049 ["p", {}, "Found ${num_tests} tests"],3050 function() {3051 var rv = [["div", {}]];3052 var i = 0;3053 while (Test.prototype.status_formats.hasOwnProperty(i)) {3054 if (status_number.hasOwnProperty(Test.prototype.status_formats[i])) {3055 var status = Test.prototype.status_formats[i];3056 rv[0].push(["div", {},3057 ["label", {},3058 ["input", {type:"checkbox", checked:"checked"}],3059 status_number[status] + " ",3060 ["span", {"class":status_class(status)}, status]]]);3061 }3062 i++;3063 }3064 return rv;3065 },3066 ];3067 log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));3068 forEach(output_document.querySelectorAll("section#summary label"),3069 function(element)3070 {3071 on_event(element, "click",3072 function(e)3073 {3074 if (output_document.getElementById("results") === null) {3075 e.preventDefault();3076 return;3077 }3078 var result_class = element.querySelector("span[class]").getAttribute("class");3079 var style_element = output_document.querySelector("style#hide-" + result_class);3080 var input_element = element.querySelector("input");3081 if (!style_element && !input_element.checked) {3082 style_element = output_document.createElementNS(xhtml_ns, "style");3083 style_element.id = "hide-" + result_class;3084 style_element.textContent = "table#results > tbody > tr.overall-"+result_class+"{display:none}";3085 output_document.body.appendChild(style_element);3086 } else if (style_element && input_element.checked) {3087 style_element.parentNode.removeChild(style_element);3088 }3089 });3090 });3091 // This use of innerHTML plus manual escaping is not recommended in3092 // general, but is necessary here for performance. Using textContent3093 // on each individual <td> adds tens of seconds of execution time for3094 // large test suites (tens of thousands of tests).3095 function escape_html(s)3096 {3097 return s.replace(/\&/g, "&")3098 .replace(/</g, "<")3099 .replace(/"/g, """)3100 .replace(/'/g, "'");3101 }3102 function has_assertions()3103 {3104 for (var i = 0; i < tests.length; i++) {3105 if (tests[i].properties.hasOwnProperty("assert")) {3106 return true;3107 }3108 }3109 return false;3110 }3111 function get_assertion(test)3112 {3113 if (test.properties.hasOwnProperty("assert")) {3114 if (Array.isArray(test.properties.assert)) {3115 return test.properties.assert.join(' ');3116 }3117 return test.properties.assert;3118 }3119 return '';3120 }3121 var asserts_run_by_test = new Map();3122 asserts_run.forEach(assert => {3123 if (!asserts_run_by_test.has(assert.test)) {3124 asserts_run_by_test.set(assert.test, []);3125 }3126 asserts_run_by_test.get(assert.test).push(assert);3127 });3128 function get_asserts_output(test) {3129 var asserts = asserts_run_by_test.get(test);3130 if (!asserts) {3131 return "No asserts ran";3132 }3133 rv = "<table>";3134 rv += asserts.map(assert => {3135 var output_fn = "<strong>" + escape_html(assert.assert_name) + "</strong>(";3136 var prefix_len = output_fn.length;3137 var output_args = assert.args;3138 var output_len = output_args.reduce((prev, current) => prev+current, prefix_len);3139 if (output_len[output_len.length - 1] > 50) {3140 output_args = output_args.map((x, i) =>3141 (i > 0 ? " ".repeat(prefix_len) : "" )+ x + (i < output_args.length - 1 ? ",\n" : ""));3142 } else {3143 output_args = output_args.map((x, i) => x + (i < output_args.length - 1 ? ", " : ""));3144 }3145 output_fn += escape_html(output_args.join(""));3146 output_fn += ')';3147 var output_location;3148 if (assert.stack) {3149 output_location = assert.stack.split("\n", 1)[0].replace(/@?\w+:\/\/[^ "\/]+(?::\d+)?/g, " ");3150 }3151 return "<tr class='overall-" +3152 status_class(Test.prototype.status_formats[assert.status]) + "'>" +3153 "<td class='" +3154 status_class(Test.prototype.status_formats[assert.status]) + "'>" +3155 Test.prototype.status_formats[assert.status] + "</td>" +3156 "<td><pre>" +3157 output_fn +3158 (output_location ? "\n" + escape_html(output_location) : "") +3159 "</pre></td></tr>";3160 }3161 ).join("\n");3162 rv += "</table>";3163 return rv;3164 }3165 log.appendChild(document.createElementNS(xhtml_ns, "section"));3166 var assertions = has_assertions();3167 var html = "<h2>Details</h2><table id='results' " + (assertions ? "class='assertions'" : "" ) + ">" +3168 "<thead><tr><th>Result</th><th>Test Name</th>" +3169 (assertions ? "<th>Assertion</th>" : "") +3170 "<th>Message</th></tr></thead>" +3171 "<tbody>";3172 for (var i = 0; i < tests.length; i++) {3173 var test = tests[i];3174 html += '<tr class="overall-' +3175 status_class(test.format_status()) +3176 '">' +3177 '<td class="' +3178 status_class(test.format_status()) +3179 '">' +3180 test.format_status() +3181 "</td><td>" +3182 escape_html(test.name) +3183 "</td><td>" +3184 (assertions ? escape_html(get_assertion(test)) + "</td><td>" : "") +3185 escape_html(test.message ? tests[i].message : " ") +3186 (tests[i].stack ? "<pre>" +3187 escape_html(tests[i].stack) +3188 "</pre>": "");3189 if (!(test instanceof RemoteTest)) {3190 html += "<details><summary>Asserts run</summary>" + get_asserts_output(test) + "</details>"3191 }3192 html += "</td></tr>";3193 }3194 html += "</tbody></table>";3195 try {3196 log.lastChild.innerHTML = html;3197 } catch (e) {3198 log.appendChild(document.createElementNS(xhtml_ns, "p"))3199 .textContent = "Setting innerHTML for the log threw an exception.";3200 log.appendChild(document.createElementNS(xhtml_ns, "pre"))3201 .textContent = html;3202 }3203 };3204 /*3205 * Template code3206 *3207 * A template is just a JavaScript structure. An element is represented as:3208 *3209 * [tag_name, {attr_name:attr_value}, child1, child2]3210 *3211 * the children can either be strings (which act like text nodes), other templates or3212 * functions (see below)3213 *3214 * A text node is represented as3215 *3216 * ["{text}", value]3217 *3218 * String values have a simple substitution syntax; ${foo} represents a variable foo.3219 *3220 * It is possible to embed logic in templates by using a function in a place where a3221 * node would usually go. The function must either return part of a template or null.3222 *3223 * In cases where a set of nodes are required as output rather than a single node3224 * with children it is possible to just use a list3225 * [node1, node2, node3]3226 *3227 * Usage:3228 *3229 * render(template, substitutions) - take a template and an object mapping3230 * variable names to parameters and return either a DOM node or a list of DOM nodes3231 *3232 * substitute(template, substitutions) - take a template and variable mapping object,3233 * make the variable substitutions and return the substituted template3234 *3235 */3236 function is_single_node(template)3237 {3238 return typeof template[0] === "string";3239 }3240 function substitute(template, substitutions)3241 {3242 if (typeof template === "function") {3243 var replacement = template(substitutions);3244 if (!replacement) {3245 return null;3246 }3247 return substitute(replacement, substitutions);3248 }3249 if (is_single_node(template)) {3250 return substitute_single(template, substitutions);3251 }3252 return filter(map(template, function(x) {3253 return substitute(x, substitutions);3254 }), function(x) {return x !== null;});3255 }3256 function substitute_single(template, substitutions)3257 {3258 var substitution_re = /\$\{([^ }]*)\}/g;3259 function do_substitution(input) {3260 var components = input.split(substitution_re);3261 var rv = [];3262 for (var i = 0; i < components.length; i += 2) {3263 rv.push(components[i]);3264 if (components[i + 1]) {3265 rv.push(String(substitutions[components[i + 1]]));3266 }3267 }3268 return rv;3269 }3270 function substitute_attrs(attrs, rv)3271 {3272 rv[1] = {};3273 for (var name in template[1]) {3274 if (attrs.hasOwnProperty(name)) {3275 var new_name = do_substitution(name).join("");3276 var new_value = do_substitution(attrs[name]).join("");3277 rv[1][new_name] = new_value;3278 }3279 }3280 }3281 function substitute_children(children, rv)3282 {3283 for (var i = 0; i < children.length; i++) {3284 if (children[i] instanceof Object) {3285 var replacement = substitute(children[i], substitutions);3286 if (replacement !== null) {3287 if (is_single_node(replacement)) {3288 rv.push(replacement);3289 } else {3290 extend(rv, replacement);3291 }3292 }3293 } else {3294 extend(rv, do_substitution(String(children[i])));3295 }3296 }3297 return rv;3298 }3299 var rv = [];3300 rv.push(do_substitution(String(template[0])).join(""));3301 if (template[0] === "{text}") {3302 substitute_children(template.slice(1), rv);3303 } else {3304 substitute_attrs(template[1], rv);3305 substitute_children(template.slice(2), rv);3306 }3307 return rv;3308 }3309 function make_dom_single(template, doc)3310 {3311 var output_document = doc || document;3312 var element;3313 if (template[0] === "{text}") {3314 element = output_document.createTextNode("");3315 for (var i = 1; i < template.length; i++) {3316 element.data += template[i];3317 }3318 } else {3319 element = output_document.createElementNS(xhtml_ns, template[0]);3320 for (var name in template[1]) {3321 if (template[1].hasOwnProperty(name)) {3322 element.setAttribute(name, template[1][name]);3323 }3324 }3325 for (var i = 2; i < template.length; i++) {3326 if (template[i] instanceof Object) {3327 var sub_element = make_dom(template[i]);3328 element.appendChild(sub_element);3329 } else {3330 var text_node = output_document.createTextNode(template[i]);3331 element.appendChild(text_node);3332 }3333 }3334 }3335 return element;3336 }3337 function make_dom(template, substitutions, output_document)3338 {3339 if (is_single_node(template)) {3340 return make_dom_single(template, output_document);3341 }3342 return map(template, function(x) {3343 return make_dom_single(x, output_document);3344 });3345 }3346 function render(template, substitutions, output_document)3347 {3348 return make_dom(substitute(template, substitutions), output_document);3349 }3350 /*3351 * Utility functions3352 */3353 function assert(expected_true, function_name, description, error, substitutions)3354 {3355 if (expected_true !== true) {3356 var msg = make_message(function_name, description,3357 error, substitutions);3358 throw new AssertionError(msg);3359 }3360 }3361 function AssertionError(message)3362 {3363 if (typeof message == "string") {3364 message = sanitize_unpaired_surrogates(message);3365 }3366 this.message = message;3367 this.stack = get_stack();3368 }3369 expose(AssertionError, "AssertionError");3370 AssertionError.prototype = Object.create(Error.prototype);3371 const get_stack = function() {3372 var stack = new Error().stack;3373 // IE11 does not initialize 'Error.stack' until the object is thrown.3374 if (!stack) {3375 try {3376 throw new Error();3377 } catch (e) {3378 stack = e.stack;...
aflprep_testharness.js
Source:aflprep_testharness.js
...2436 };2437 function code_unit_str(char) {2438 return 'U+' + char.charCodeAt(0).toString(16);2439 }2440 function sanitize_unpaired_surrogates(str) {2441 return str.replace(2442 function(_, low, prefix, high) {2443 for (var i = 0; i < string.length; i++) {2444 output += code_unit_str(string[i]);2445 }2446 return output;2447 });2448 }2449 function sanitize_all_unpaired_surrogates(tests) {2450 forEach (tests,2451 function (test)2452 {2453 var sanitized = sanitize_unpaired_surrogates(test.name);2454 if (test.name !== sanitized) {2455 test.name = sanitized;2456 delete test._structured_clone;2457 }2458 });2459 }2460 Tests.prototype.notify_complete = function() {2461 var this_obj = this;2462 var duplicates;2463 if (this.status.status === null) {2464 duplicates = this.find_duplicates();2465 sanitize_all_unpaired_surrogates(this.tests);2466 if (duplicates.length) {2467 this.status.status = this.status.ERROR;2468 this.status.message =2469 duplicates.length + ' duplicate test name' +2470 (duplicates.length > 1 ? 's' : '') + ': "' +2471 duplicates.join('", "') + '"';2472 } else {2473 this.status.status = this.status.OK;2474 }2475 }2476 forEach (this.all_done_callbacks,2477 function(callback)2478 {2479 callback(this_obj.tests, this_obj.status, this_obj.asserts_run);2480 });2481 };2482 * Constructs a RemoteContext that tracks tests from a specific worker.2483 Tests.prototype.create_remote_worker = function(worker) {2484 var message_port;2485 if (is_service_worker(worker)) {2486 message_port = navigator.serviceWorker;2487 worker.postMessage({type: "connect"});2488 } else if (is_shared_worker(worker)) {2489 message_port = worker.port;2490 message_port.start();2491 } else {2492 message_port = worker;2493 }2494 return new RemoteContext(worker, message_port);2495 };2496 * Constructs a RemoteContext that tracks tests from a specific window.2497 Tests.prototype.create_remote_window = function(remote) {2498 remote.postMessage({type: "getmessages"}, "*");2499 return new RemoteContext(2500 remote,2501 window,2502 function(msg) {2503 return msg.source === remote;2504 }2505 );2506 };2507 Tests.prototype.fetch_tests_from_worker = function(worker) {2508 if (this.phase >= this.phases.COMPLETE) {2509 return;2510 }2511 var remoteContext = this.create_remote_worker(worker);2512 this.pending_remotes.push(remoteContext);2513 return remoteContext.done;2514 };2515 function fetch_tests_from_worker(port) {2516 return tests.fetch_tests_from_worker(port);2517 }2518 expose(fetch_tests_from_worker, 'fetch_tests_from_worker');2519 Tests.prototype.fetch_tests_from_window = function(remote) {2520 if (this.phase >= this.phases.COMPLETE) {2521 return;2522 }2523 this.pending_remotes.push(this.create_remote_window(remote));2524 };2525 function fetch_tests_from_window(window) {2526 tests.fetch_tests_from_window(window);2527 }2528 expose(fetch_tests_from_window, 'fetch_tests_from_window');2529 function timeout() {2530 if (tests.timeout_length === null) {2531 tests.timeout();2532 }2533 }2534 expose(timeout, 'timeout');2535 function add_start_callback(callback) {2536 tests.start_callbacks.push(callback);2537 }2538 function add_test_state_callback(callback) {2539 tests.test_state_callbacks.push(callback);2540 }2541 function add_result_callback(callback) {2542 tests.test_done_callbacks.push(callback);2543 }2544 function add_completion_callback(callback) {2545 tests.all_done_callbacks.push(callback);2546 }2547 expose(add_start_callback, 'add_start_callback');2548 expose(add_test_state_callback, 'add_test_state_callback');2549 expose(add_result_callback, 'add_result_callback');2550 expose(add_completion_callback, 'add_completion_callback');2551 function remove(array, item) {2552 var index = array.indexOf(item);2553 if (index > -1) {2554 array.splice(index, 1);2555 }2556 }2557 function remove_start_callback(callback) {2558 remove(tests.start_callbacks, callback);2559 }2560 function remove_test_state_callback(callback) {2561 remove(tests.test_state_callbacks, callback);2562 }2563 function remove_result_callback(callback) {2564 remove(tests.test_done_callbacks, callback);2565 }2566 function remove_completion_callback(callback) {2567 remove(tests.all_done_callbacks, callback);2568 }2569 * Output listener2570 function Output() {2571 this.output_document = document;2572 this.output_node = null;2573 this.enabled = settings.output;2574 this.phase = this.INITIAL;2575 }2576 Output.prototype.INITIAL = 0;2577 Output.prototype.STARTED = 1;2578 Output.prototype.HAVE_RESULTS = 2;2579 Output.prototype.COMPLETE = 3;2580 Output.prototype.setup = function(properties) {2581 if (this.phase > this.INITIAL) {2582 return;2583 }2584 this.enabled = this.enabled && (properties.hasOwnProperty("output") ?2585 properties.output : settings.output);2586 };2587 Output.prototype.init = function(properties) {2588 if (this.phase >= this.STARTED) {2589 return;2590 }2591 if (properties.output_document) {2592 this.output_document = properties.output_document;2593 } else {2594 this.output_document = document;2595 }2596 this.phase = this.STARTED;2597 };2598 Output.prototype.resolve_log = function() {2599 var output_document;2600 if (this.output_node) {2601 return;2602 }2603 if (typeof this.output_document === "function") {2604 output_document = this.output_document.apply(undefined);2605 } else {2606 output_document = this.output_document;2607 }2608 if (!output_document) {2609 return;2610 }2611 var node = output_document.getElementById("log");2612 if (!node) {2613 if (output_document.readyState === "loading") {2614 return;2615 }2616 node.id = "log";2617 if (output_document.body) {2618 output_document.body.appendChild(node);2619 } else {2620 var root = output_document.documentElement;2621 var is_html = (root &&2622 root.localName == "html");2623 var is_svg = (output_document.defaultView &&2624 "SVGSVGElement" in output_document.defaultView &&2625 root instanceof output_document.defaultView.SVGSVGElement);2626 if (is_svg) {2627 foreignObject.setAttribute("width", "100%");2628 foreignObject.setAttribute("height", "100%");2629 root.appendChild(foreignObject);2630 foreignObject.appendChild(node);2631 } else if (is_html) {2632 .appendChild(node);2633 } else {2634 root.appendChild(node);2635 }2636 }2637 }2638 this.output_document = output_document;2639 this.output_node = node;2640 };2641 Output.prototype.show_status = function() {2642 if (this.phase < this.STARTED) {2643 this.init();2644 }2645 if (!this.enabled || this.phase === this.COMPLETE) {2646 return;2647 }2648 this.resolve_log();2649 if (this.phase < this.HAVE_RESULTS) {2650 this.phase = this.HAVE_RESULTS;2651 }2652 var done_count = tests.tests.length - tests.num_pending;2653 if (this.output_node && !tests.hide_test_state) {2654 if (done_count < 100 ||2655 (done_count < 1000 && done_count % 100 === 0) ||2656 done_count % 1000 === 0) {2657 this.output_node.textContent = "Running, " +2658 done_count + " complete, " +2659 tests.num_pending + " remain";2660 }2661 }2662 };2663 Output.prototype.show_results = function (tests, harness_status, asserts_run) {2664 if (this.phase >= this.COMPLETE) {2665 return;2666 }2667 if (!this.enabled) {2668 return;2669 }2670 if (!this.output_node) {2671 this.resolve_log();2672 }2673 this.phase = this.COMPLETE;2674 var log = this.output_node;2675 if (!log) {2676 return;2677 }2678 var output_document = this.output_document;2679 while (log.lastChild) {2680 log.removeChild(log.lastChild);2681 }2682 var stylesheet = output_document.createElementNS(xhtml_ns, "style");2683 stylesheet.textContent = stylesheetContent;2684 var heads = output_document.getElementsByTagName("head");2685 if (heads.length) {2686 heads[0].appendChild(stylesheet);2687 }2688 var status_number = {};2689 forEach(tests,2690 function(test) {2691 var status = test.format_status();2692 if (status_number.hasOwnProperty(status)) {2693 status_number[status] += 1;2694 } else {2695 status_number[status] = 1;2696 }2697 });2698 function status_class(status)2699 {2700 }2701 var summary_template = ["section", {"id":"summary"},2702 ["h2", {}, "Summary"],2703 function()2704 {2705 var status = harness_status.format_status();2706 var rv = [["section", {},2707 ["p", {},2708 "Harness status: ",2709 ["span", {"class":status_class(status)},2710 status2711 ],2712 ]2713 ]];2714 if (harness_status.status === harness_status.ERROR) {2715 rv[0].push(["pre", {}, harness_status.message]);2716 if (harness_status.stack) {2717 rv[0].push(["pre", {}, harness_status.stack]);2718 }2719 }2720 return rv;2721 },2722 ["p", {}, "Found ${num_tests} tests"],2723 function() {2724 var rv = [["div", {}]];2725 var i = 0;2726 while (Test.prototype.status_formats.hasOwnProperty(i)) {2727 if (status_number.hasOwnProperty(Test.prototype.status_formats[i])) {2728 var status = Test.prototype.status_formats[i];2729 rv[0].push(["div", {},2730 ["label", {},2731 ["input", {type:"checkbox", checked:"checked"}],2732 status_number[status] + " ",2733 ["span", {"class":status_class(status)}, status]]]);2734 }2735 i++;2736 }2737 return rv;2738 },2739 ];2740 log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));2741 forEach(output_document.querySelectorAll("section#summary label"),2742 function(element)2743 {2744 on_event(element, "click",2745 function(e)2746 {2747 if (output_document.getElementById("results") === null) {2748 e.preventDefault();2749 return;2750 }2751 var result_class = element.querySelector("span[class]").getAttribute("class");2752 var style_element = output_document.querySelector("style#hide-" + result_class);2753 var input_element = element.querySelector("input");2754 if (!style_element && !input_element.checked) {2755 style_element = output_document.createElementNS(xhtml_ns, "style");2756 style_element.id = "hide-" + result_class;2757 style_element.textContent = "table#results > tbody > tr.overall-"+result_class+"{display:none}";2758 output_document.body.appendChild(style_element);2759 } else if (style_element && input_element.checked) {2760 style_element.parentNode.removeChild(style_element);2761 }2762 });2763 });2764 function escape_html(s)2765 {2766 }2767 function has_assertions()2768 {2769 for (var i = 0; i < tests.length; i++) {2770 if (tests[i].properties.hasOwnProperty("assert")) {2771 return true;2772 }2773 }2774 return false;2775 }2776 function get_assertion(test)2777 {2778 if (test.properties.hasOwnProperty("assert")) {2779 if (Array.isArray(test.properties.assert)) {2780 return test.properties.assert.join(' ');2781 }2782 return test.properties.assert;2783 }2784 return '';2785 }2786 var asserts_run_by_test = new Map();2787 asserts_run.forEach(assert => {2788 if (!asserts_run_by_test.has(assert.test)) {2789 asserts_run_by_test.set(assert.test, []);2790 }2791 asserts_run_by_test.get(assert.test).push(assert);2792 });2793 function get_asserts_output(test) {2794 var asserts = asserts_run_by_test.get(test);2795 if (!asserts) {2796 return "No asserts ran";2797 }2798 rv = "<table>";2799 rv += asserts.map(assert => {2800 var prefix_len = output_fn.length;2801 var output_args = assert.args;2802 var output_len = output_args.reduce((prev, current) => prev+current, prefix_len);2803 if (output_len[output_len.length - 1] > 50) {2804 output_args = output_args.map((x, i) =>2805 (i > 0 ? " ".repeat(prefix_len) : "" )+ x + (i < output_args.length - 1 ? ",\n" : ""));2806 } else {2807 output_args = output_args.map((x, i) => x + (i < output_args.length - 1 ? ", " : ""));2808 }2809 output_fn += escape_html(output_args.join(""));2810 output_fn += ')';2811 var output_location;2812 if (assert.stack) {2813 }2814 return "<tr class='overall-" +2815 status_class(Test.prototype.status_formats[assert.status]) + "'>" +2816 "<td class='" +2817 status_class(Test.prototype.status_formats[assert.status]) + "'>" +2818 "<td><pre>" +2819 output_fn +2820 (output_location ? "\n" + escape_html(output_location) : "") +2821 }2822 ).join("\n");2823 return rv;2824 }2825 log.appendChild(document.createElementNS(xhtml_ns, "section"));2826 var assertions = has_assertions();2827 "<tbody>";2828 for (var i = 0; i < tests.length; i++) {2829 var test = tests[i];2830 html += '<tr class="overall-' +2831 status_class(test.format_status()) +2832 '">' +2833 '<td class="' +2834 status_class(test.format_status()) +2835 '">' +2836 test.format_status() +2837 escape_html(test.name) +2838 escape_html(test.message ? tests[i].message : " ") +2839 (tests[i].stack ? "<pre>" +2840 escape_html(tests[i].stack) +2841 if (!(test instanceof RemoteTest)) {2842 }2843 }2844 try {2845 log.lastChild.innerHTML = html;2846 } catch (e) {2847 log.appendChild(document.createElementNS(xhtml_ns, "p"))2848 .textContent = "Setting innerHTML for the log threw an exception.";2849 log.appendChild(document.createElementNS(xhtml_ns, "pre"))2850 .textContent = html;2851 }2852 };2853 * Template code2854 *2855 * A template is just a JavaScript structure. An element is represented as:2856 *2857 * [tag_name, {attr_name:attr_value}, child1, child2]2858 *2859 * the children can either be strings (which act like text nodes), other templates or2860 * functions (see below)2861 *2862 * A text node is represented as2863 *2864 * ["{text}", value]2865 *2866 * String values have a simple substitution syntax; ${foo} represents a variable foo.2867 *2868 * It is possible to embed logic in templates by using a function in a place where a2869 * node would usually go. The function must either return part of a template or null.2870 *2871 * In cases where a set of nodes are required as output rather than a single node2872 * with children it is possible to just use a list2873 * [node1, node2, node3]2874 *2875 * Usage:2876 *2877 * render(template, substitutions) - take a template and an object mapping2878 * variable names to parameters and return either a DOM node or a list of DOM nodes2879 *2880 * substitute(template, substitutions) - take a template and variable mapping object,2881 * make the variable substitutions and return the substituted template2882 *2883 function is_single_node(template)2884 {2885 return typeof template[0] === "string";2886 }2887 function substitute(template, substitutions)2888 {2889 if (typeof template === "function") {2890 var replacement = template(substitutions);2891 if (!replacement) {2892 return null;2893 }2894 return substitute(replacement, substitutions);2895 }2896 if (is_single_node(template)) {2897 return substitute_single(template, substitutions);2898 }2899 return filter(map(template, function(x) {2900 return substitute(x, substitutions);2901 }), function(x) {return x !== null;});2902 }2903 function substitute_single(template, substitutions)2904 {2905 function do_substitution(input) {2906 var components = input.split(substitution_re);2907 var rv = [];2908 for (var i = 0; i < components.length; i += 2) {2909 rv.push(components[i]);2910 if (components[i + 1]) {2911 rv.push(String(substitutions[components[i + 1]]));2912 }2913 }2914 return rv;2915 }2916 function substitute_attrs(attrs, rv)2917 {2918 rv[1] = {};2919 for (var name in template[1]) {2920 if (attrs.hasOwnProperty(name)) {2921 var new_name = do_substitution(name).join("");2922 var new_value = do_substitution(attrs[name]).join("");2923 rv[1][new_name] = new_value;2924 }2925 }2926 }2927 function substitute_children(children, rv)2928 {2929 for (var i = 0; i < children.length; i++) {2930 if (children[i] instanceof Object) {2931 var replacement = substitute(children[i], substitutions);2932 if (replacement !== null) {2933 if (is_single_node(replacement)) {2934 rv.push(replacement);2935 } else {2936 extend(rv, replacement);2937 }2938 }2939 } else {2940 extend(rv, do_substitution(String(children[i])));2941 }2942 }2943 return rv;2944 }2945 var rv = [];2946 rv.push(do_substitution(String(template[0])).join(""));2947 if (template[0] === "{text}") {2948 substitute_children(template.slice(1), rv);2949 } else {2950 substitute_attrs(template[1], rv);2951 substitute_children(template.slice(2), rv);2952 }2953 return rv;2954 }2955 function make_dom_single(template, doc)2956 {2957 var output_document = doc || document;2958 var element;2959 if (template[0] === "{text}") {2960 element = output_document.createTextNode("");2961 for (var i = 1; i < template.length; i++) {2962 element.data += template[i];2963 }2964 } else {2965 element = output_document.createElementNS(xhtml_ns, template[0]);2966 for (var name in template[1]) {2967 if (template[1].hasOwnProperty(name)) {2968 element.setAttribute(name, template[1][name]);2969 }2970 }2971 for (var i = 2; i < template.length; i++) {2972 if (template[i] instanceof Object) {2973 var sub_element = make_dom(template[i]);2974 element.appendChild(sub_element);2975 } else {2976 var text_node = output_document.createTextNode(template[i]);2977 element.appendChild(text_node);2978 }2979 }2980 }2981 return element;2982 }2983 function make_dom(template, substitutions, output_document)2984 {2985 if (is_single_node(template)) {2986 return make_dom_single(template, output_document);2987 }2988 return map(template, function(x) {2989 return make_dom_single(x, output_document);2990 });2991 }2992 function render(template, substitutions, output_document)2993 {2994 return make_dom(substitute(template, substitutions), output_document);2995 }2996 * Utility functions2997 function assert(expected_true, function_name, description, error, substitutions)2998 {2999 if (expected_true !== true) {3000 var msg = make_message(function_name, description,3001 error, substitutions);3002 throw new AssertionError(msg);3003 }3004 }3005 function AssertionError(message)3006 {3007 if (typeof message == "string") {3008 message = sanitize_unpaired_surrogates(message);3009 }3010 this.message = message;3011 this.stack = get_stack();3012 }3013 expose(AssertionError, "AssertionError");3014 AssertionError.prototype = Object.create(Error.prototype);3015 const get_stack = function() {3016 var stack = new Error().stack;3017 if (!stack) {3018 try {3019 throw new Error();3020 } catch (e) {3021 stack = e.stack;3022 }...
Using AI Code Generation
1var wptexturize = require('wptexturize');2var text = "This is a test \ud83d\udc4d";3var result = wptexturize.sanitize_unpaired_surrogates(text);4console.log(result);5var wptexturize = require('wptexturize');6var text = 'This is a test';7var result = wptexturize(text);
Using AI Code Generation
1var texturize = require('wptexturize');2var output = texturize.sanitize_unpaired_surrogates(input);3var texturize = require('wptexturize');4var output = texturize.sanitize_unpaired_surrogates(input);5var texturize = require('wptexturize');6var output = texturize.sanitize_unpaired_surrogates(input);7var texturize = require('wptexturize');8var output = texturize.sanitize_unpaired_surrogates(input);9var texturize = require('wptexturize');10var output = texturize.sanitize_unpaired_surrogates(input);
Using AI Code Generation
1var string = "hello \uDC00world";2var result = wpt.sanitize_unpaired_surrogates(string);3var string = "hello \uDC00world";4var result = wpt.sanitize_unpaired_surrogates(string);5var string = "hello \uDC00world";6var result = wpt.sanitize_unpaired_surrogates(string);7var string = "hello \uDC00world";8var result = wpt.sanitize_unpaired_surrogates(string);9var string = "hello \uDC00world";10var result = wpt.sanitize_unpaired_surrogates(string);11var string = "hello \uDC00world";12var result = wpt.sanitize_unpaired_surrogates(string);13var string = "hello \uDC00world";14var result = wpt.sanitize_unpaired_surrogates(string);15var string = "hello \uDC00world";16var result = wpt.sanitize_unpaired_surrogates(string);17var string = "hello \uDC00world";18var result = wpt.sanitize_unpaired_surrogates(string);19var string = "hello \uDC00world";
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!!