Best JavaScript code snippet using jest
file_extensions.js
Source:file_extensions.js
1const fs = require("fs");2const { dialog } = require("electron");3import {4 Document,5 Packer,6 Paragraph,7 TextRun,8 AlignmentType,9 HeadingLevel,10 TableOfContents,11 SectionType,12} from "docx";13import i18n from "../i18n.js";14export function txt(15 secciones_report,16 getMain,17 assets,18 activities,19 threats,20 vulnerabilities,21 recommentations,22 titletxt,23 message,24 locale25) {26 return new Promise(function (resolve, reject) {27 try {28 var options = {29 title: titletxt,30 message: message,31 //nameFieldLabel: "Project Name:"32 // defaultPath: directory to show (optional)33 };34 let path = dialog.showSaveDialogSync(options);35 i18n.locale = locale;36 if (path) {37 path = path + ".txt";38 var fileContents = [];39 let keys = Object.keys(secciones_report);40 fileContents.push(i18n.t("header.report") + "\n");41 keys.forEach((key) => {42 let item = secciones_report[key];43 const n = getMain[item.interest][0].tasks.length;44 let wrote = 0;45 for (var i = 0; i < n; i++) {46 wrote += 1;47 if (i == 0) {48 fileContents.push(item.title + "\n");49 }50 let description = getMain[item.interest][0].tasks[i].description;51 if (description === null) {52 description = "";53 }54 let title = getMain[item.interest][0].tasks[i].title;55 const j = i + 1;56 fileContents.push(57 " " + j + ") " + i18n.t("global.name") + ": " + title + "\n"58 );59 fileContents.push(60 " " + i18n.t("global.description") + ": " + description + "\n"61 );62 switch (item.name) {63 case "Assets":64 for (var k = 0; k < assets.length; k++) {65 if (66 assets[k].id ==67 getMain[item.interest][0].tasks[i].identifier68 ) {69 if (assets[k].asset_category_name === undefined) {70 assets[k].asset_category_name = i18n.t("global.none");71 }72 fileContents.push(73 " " +74 i18n.t("global.asset_category") +75 ": " +76 assets[k].asset_category_name +77 "\n"78 );79 }80 }81 break;82 case "Activities":83 for (var k = 0; k < activities.length; k++) {84 if (85 activities[k].id ==86 getMain[item.interest][0].tasks[i].identifier87 ) {88 if (activities[k].asset_name === undefined) {89 activities[k].asset_name = [];90 }91 var related_assets = 0;92 for (var z = 0; z < activities[k].asset_name.length; z++) {93 for (94 var p = 0;95 p < getMain.report_assets[0].tasks.length;96 p++97 ) {98 if (99 getMain.report_assets[0].tasks[p].title ==100 activities[k].asset_name[z]101 ) {102 related_assets += 1;103 if (related_assets == 1) {104 fileContents.push(105 " " + i18n.t("global.assets") + ":\n"106 );107 }108 fileContents.push(109 " - " + activities[k].asset_name[z] + "\n"110 );111 }112 }113 }114 if (related_assets == 0) {115 fileContents.push(116 " " +117 i18n.t("global.assets") +118 ": " +119 i18n.t("global.none") +120 "\n"121 );122 }123 }124 }125 break;126 case "Threats":127 for (var k = 0; k < threats.length; k++) {128 if (129 threats[k].id ==130 getMain[item.interest][0].tasks[i].identifier131 ) {132 if (threats[k].threat_type_name === undefined) {133 threats[k].threat_type_name = i18n.t("global.none");134 }135 fileContents.push(136 " " +137 i18n.t("global.threat_type") +138 ": " +139 threats[k].threat_type_name +140 "\n"141 );142 var related_asset = 0;143 for (144 var p = 0;145 p < getMain.report_assets[0].tasks.length;146 p++147 ) {148 if (149 getMain.report_assets[0].tasks[p].title ==150 threats[k].asset_name151 ) {152 related_asset = 1;153 fileContents.push(154 " " +155 i18n.t("global.asset") +156 ": " +157 threats[k].asset_name +158 "\n"159 );160 }161 }162 if (related_asset == 0) {163 fileContents.push(164 " " +165 i18n.t("global.asset") +166 ": " +167 i18n.t("global.none") +168 "\n"169 );170 }171 fileContents.push(172 " " +173 i18n.t("global.impact") +174 ": " +175 threats[k].impact +176 "\n"177 );178 fileContents.push(179 " " +180 i18n.t("global.likelihood") +181 ": " +182 threats[k].likelihood +183 "\n"184 );185 }186 }187 break;188 case "Vulnerabilities":189 let reportAct = getMain.report_activities[0].tasks;190 for (var k = 0; k < vulnerabilities.length; k++) {191 if (192 vulnerabilities[k].id ==193 getMain[item.interest][0].tasks[i].identifier194 ) {195 // Adding related activity196 var related_activity = 0;197 if (reportAct === undefined) {198 reportAct = [];199 }200 for (var p = 0; p < reportAct.length; p++) {201 if (202 reportAct[p].title ==203 vulnerabilities[k].assessment_activity_name204 ) {205 related_activity = 1;206 fileContents.push(207 " " +208 i18n.t("global.assessment_activity") +209 ": " +210 vulnerabilities[k].assessment_activity_name +211 "\n"212 );213 }214 }215 if (related_activity == 0) {216 fileContents.push(217 " " +218 i18n.t("global.assessment_activity") +219 ": " +220 i18n.t("global.none") +221 "\n"222 );223 }224 // Adding related asset225 let reportAssets = getMain.report_assets[0].tasks;226 var related_asset = 0;227 if (reportAssets === undefined) {228 reportAssets = [];229 }230 for (var p = 0; p < reportAssets.length; p++) {231 if (232 reportAssets[p].title == vulnerabilities[k].asset_name233 ) {234 related_asset = 1;235 fileContents.push(236 " " +237 i18n.t("global.asset") +238 ": " +239 vulnerabilities[k].asset_name +240 "\n"241 );242 }243 }244 if (related_asset == 0) {245 fileContents.push(246 " " +247 i18n.t("global.asset") +248 ": " +249 i18n.t("global.none") +250 "\n"251 );252 }253 // Adding related threats254 let reportThreats = getMain.report_threats[0].tasks;255 let reportThreatName = vulnerabilities[k].threat_name;256 var related_threats = 0;257 if (reportThreats === undefined) {258 reportThreats = [];259 }260 if (reportThreatName === undefined) {261 reportThreatName = [];262 }263 for (var z = 0; z < reportThreatName.length; z++) {264 for (var p = 0; p < reportThreats.length; p++) {265 if (reportThreats[p].title == reportThreatName[z]) {266 related_threats += 1;267 if (related_threats == 1) {268 fileContents.push(269 " " + i18n.t("global.threats") + ":\n"270 );271 }272 fileContents.push(273 " - " + reportThreatName[z] + "\n"274 );275 }276 }277 }278 if (related_threats == 0) {279 fileContents.push(280 " " +281 i18n.t("global.threats") +282 ": " +283 i18n.t("global.none") +284 "\n"285 );286 }287 }288 }289 break;290 case "Recommendations":291 for (var k = 0; k < recommentations.length; k++) {292 if (293 recommentations[k].id ==294 getMain[item.interest][0].tasks[i].identifier295 ) {296 if (recommentations[k].implementation_cost === null) {297 recommentations[k].implementation_cost = "";298 }299 if (recommentations[k].implementation_time === null) {300 recommentations[k].implementation_time = "";301 }302 fileContents.push(303 " " +304 i18n.t("global.implementation_cost") +305 ": " +306 recommentations[k].implementation_cost +307 "\n"308 );309 fileContents.push(310 " " +311 i18n.t("global.implementation_time") +312 ": " +313 recommentations[k].implementation_time +314 "\n"315 );316 let reportVulnerabilityName =317 recommentations[k].vulnerability_name;318 let reportVulnerabilitiesTasks =319 getMain.report_vulnerabilities[0].tasks;320 var related_vulnerabilities = 0;321 if (reportVulnerabilityName === undefined) {322 reportVulnerabilityName = [];323 }324 if (reportVulnerabilitiesTasks === undefined) {325 reportVulnerabilitiesTasks = [];326 }327 for (var z = 0; z < reportVulnerabilityName.length; z++) {328 for (329 var p = 0;330 p < reportVulnerabilitiesTasks.length;331 p++332 ) {333 if (334 reportVulnerabilitiesTasks[p].title ==335 reportVulnerabilityName[z]336 ) {337 related_vulnerabilities += 1;338 if (related_vulnerabilities == 1) {339 fileContents.push(340 " " + i18n.t("global.vulnerabilities") + ":\n"341 );342 }343 fileContents.push(344 " - " + reportVulnerabilityName[z] + "\n"345 );346 }347 }348 }349 if (related_vulnerabilities == 0) {350 fileContents.push(351 " " +352 i18n.t("global.vulnerabilities") +353 ": " +354 i18n.t("global.none") +355 "\n"356 );357 }358 }359 }360 break;361 default:362 }363 if (i != n - 1) {364 fileContents.push("\n");365 }366 }367 if (wrote > 0) {368 fileContents.push("\n");369 }370 });371 try {372 fs.writeFile(path, fileContents.join(""), "utf-8", (err) => {373 if (err) {374 /* eslint-disable no-console */375 console.log(err);376 reject(err);377 }378 });379 } catch (e) {380 console.log(err);381 reject(err);382 }383 } else {384 resolve();385 }386 resolve(path);387 } catch (err) {388 reject(err);389 }390 });391}392export function md(393 secciones_report,394 getMain,395 assets,396 activities,397 threats,398 vulnerabilities,399 recommentations,400 titlemd,401 message,402 locale403) {404 return new Promise(function (resolve, reject) {405 try {406 var options = {407 title: titlemd,408 message: message,409 //nameFieldLabel: "Project Name:"410 // defaultPath: directory to show (optional)411 };412 let path = dialog.showSaveDialogSync(options);413 i18n.locale = locale;414 if (path) {415 path = path + ".md";416 var title = "# " + i18n.t("header.report") + "\n\n";417 var separator = "---\n";418 var fileContents = [];419 var toc = [];420 let keys = Object.keys(secciones_report);421 keys.forEach((key) => {422 let item = secciones_report[key];423 const n = getMain[item.interest][0].tasks.length;424 let wrote = 0;425 for (var i = 0; i < n; i++) {426 wrote += 1;427 if (i == 0) {428 fileContents.push("## " + item.title + "\n\n");429 toc.push(430 "- [" +431 item.title +432 "](#" +433 item.title.toLowerCase().split(" ").join("-") +434 ")\n"435 );436 }437 let description = getMain[item.interest][0].tasks[i].description;438 if (description === null) {439 description = "";440 }441 let title = getMain[item.interest][0].tasks[i].title;442 const j = i + 1;443 fileContents.push("1. #### " + title + "\n");444 toc.push(445 " * [" +446 title +447 "](#" +448 title.toLowerCase().split(" ").join("-") +449 ")\n"450 );451 fileContents.push(452 "\t * **" +453 i18n.t("global.description") +454 ":** " +455 description +456 "\n"457 );458 switch (item.name) {459 case "Assets":460 for (var k = 0; k < assets.length; k++) {461 if (462 assets[k].id ==463 getMain[item.interest][0].tasks[i].identifier464 ) {465 if (assets[k].asset_category_name === undefined) {466 assets[k].asset_category_name = i18n.t("global.none");467 }468 fileContents.push(469 "\t * **" +470 i18n.t("global.asset_category") +471 ":** " +472 assets[k].asset_category_name +473 "\n"474 );475 }476 }477 break;478 case "Activities":479 for (var k = 0; k < activities.length; k++) {480 if (481 activities[k].id ==482 getMain[item.interest][0].tasks[i].identifier483 ) {484 if (activities[k].asset_name === undefined) {485 activities[k].asset_name = [];486 }487 var related_assets = 0;488 for (var z = 0; z < activities[k].asset_name.length; z++) {489 for (490 var p = 0;491 p < getMain.report_assets[0].tasks.length;492 p++493 ) {494 if (495 getMain.report_assets[0].tasks[p].title ==496 activities[k].asset_name[z]497 ) {498 related_assets += 1;499 if (related_assets == 1) {500 fileContents.push(501 "\t * **" + i18n.t("global.assets") + ":**\n"502 );503 }504 fileContents.push(505 "\t - [" +506 activities[k].asset_name[z] +507 "](#" +508 activities[k].asset_name[z]509 .toLowerCase()510 .split(" ")511 .join("-") +512 ")\n"513 );514 }515 }516 }517 if (related_assets == 0) {518 fileContents.push(519 "\t * **" +520 i18n.t("global.assets") +521 ":** " +522 i18n.t("global.none") +523 "\n"524 );525 }526 }527 }528 break;529 case "Threats":530 for (var k = 0; k < threats.length; k++) {531 if (532 threats[k].id ==533 getMain[item.interest][0].tasks[i].identifier534 ) {535 if (threats[k].threat_type_name === undefined) {536 threats[k].threat_type_name = i18n.t("global.none");537 }538 fileContents.push(539 "\t * **" +540 i18n.t("global.threat_type") +541 ":** " +542 threats[k].threat_type_name +543 "\n"544 );545 var related_asset = 0;546 for (547 var p = 0;548 p < getMain.report_assets[0].tasks.length;549 p++550 ) {551 if (552 getMain.report_assets[0].tasks[p].title ==553 threats[k].asset_name554 ) {555 related_asset = 1;556 fileContents.push(557 "\t * **" +558 i18n.t("global.asset") +559 ":** " +560 "[" +561 threats[k].asset_name +562 "](#" +563 threats[k].asset_name564 .toLowerCase()565 .split(" ")566 .join("-") +567 ")\n"568 );569 }570 }571 if (related_asset == 0) {572 fileContents.push(573 "\t * **" +574 i18n.t("global.asset") +575 ":** " +576 i18n.t("global.none") +577 "\n"578 );579 }580 fileContents.push(581 "\t * **" +582 i18n.t("global.impact") +583 ":** " +584 threats[k].impact +585 "\n"586 );587 fileContents.push(588 "\t * **" +589 i18n.t("global.likelihood") +590 ":** " +591 threats[k].likelihood +592 "\n"593 );594 }595 }596 break;597 case "Vulnerabilities":598 let reportAct = getMain.report_activities[0].tasks;599 for (var k = 0; k < vulnerabilities.length; k++) {600 if (601 vulnerabilities[k].id ==602 getMain[item.interest][0].tasks[i].identifier603 ) {604 // Adding related activity605 var related_activity = 0;606 if (reportAct === undefined) {607 reportAct = [];608 }609 for (var p = 0; p < reportAct.length; p++) {610 if (611 reportAct[p].title ==612 vulnerabilities[k].assessment_activity_name613 ) {614 related_activity = 1;615 fileContents.push(616 "\t * **" +617 i18n.t("global.assessment_activity") +618 ":** [" +619 vulnerabilities[k].assessment_activity_name +620 "](#" +621 vulnerabilities[k].assessment_activity_name622 .toLowerCase()623 .split(" ")624 .join("-") +625 ")\n"626 );627 }628 }629 if (related_activity == 0) {630 fileContents.push(631 "\t * **" +632 i18n.t("global.assessment_activity") +633 ":** " +634 i18n.t("global.none") +635 "\n"636 );637 }638 // Adding related asset639 let reportAssets = getMain.report_assets[0].tasks;640 var related_asset = 0;641 if (reportAssets === undefined) {642 reportAssets = [];643 }644 for (var p = 0; p < reportAssets.length; p++) {645 if (646 reportAssets[p].title == vulnerabilities[k].asset_name647 ) {648 related_asset = 1;649 fileContents.push(650 "\t * **" +651 i18n.t("global.asset") +652 ":** [" +653 vulnerabilities[k].asset_name +654 "](#" +655 vulnerabilities[k].asset_name656 .toLowerCase()657 .split(" ")658 .join("-") +659 ")\n"660 );661 }662 }663 if (related_asset == 0) {664 fileContents.push(665 "\t * **" +666 i18n.t("global.asset") +667 ":** " +668 i18n.t("global.none") +669 "\n"670 );671 }672 let reportThreats = getMain.report_threats[0].tasks;673 let reportThreatName = vulnerabilities[k].threat_name;674 var related_threats = 0;675 if (reportThreats === undefined) {676 reportThreats = [];677 }678 if (reportThreatName === undefined) {679 reportThreatName = [];680 }681 for (var z = 0; z < reportThreatName.length; z++) {682 for (var p = 0; p < reportThreats.length; p++) {683 if (reportThreats[p].title == reportThreatName[z]) {684 related_threats += 1;685 if (related_threats == 1) {686 fileContents.push(687 "\t * **" + i18n.t("global.threats") + ":**\n"688 );689 }690 fileContents.push(691 "\t - [" +692 reportThreatName[z] +693 "](#" +694 reportThreatName[z]695 .toLowerCase()696 .split(" ")697 .join("-") +698 ")\n"699 );700 }701 }702 }703 if (related_threats == 0) {704 fileContents.push(705 "\t * **" +706 i18n.t("global.threats") +707 ":** " +708 i18n.t("global.none") +709 "\n"710 );711 }712 }713 }714 break;715 case "Recommendations":716 for (var k = 0; k < recommentations.length; k++) {717 if (718 recommentations[k].id ==719 getMain[item.interest][0].tasks[i].identifier720 ) {721 if (recommentations[k].implementation_cost === null) {722 recommentations[k].implementation_cost = "";723 }724 if (recommentations[k].implementation_time === null) {725 recommentations[k].implementation_time = "";726 }727 fileContents.push(728 "\t * **" +729 i18n.t("global.implementation_cost") +730 ":** " +731 recommentations[k].implementation_cost +732 "\n"733 );734 fileContents.push(735 "\t * **" +736 i18n.t("global.implementation_time") +737 ":** " +738 recommentations[k].implementation_time +739 "\n"740 );741 let reportVulnerabilityName =742 recommentations[k].vulnerability_name;743 let reportVulnerabilitiesTasks =744 getMain.report_vulnerabilities[0].tasks;745 var related_vulnerabilities = 0;746 if (reportVulnerabilityName === undefined) {747 reportVulnerabilityName = [];748 }749 if (reportVulnerabilitiesTasks === undefined) {750 reportVulnerabilitiesTasks = [];751 }752 for (var z = 0; z < reportVulnerabilityName.length; z++) {753 for (754 var p = 0;755 p < reportVulnerabilitiesTasks.length;756 p++757 ) {758 if (759 reportVulnerabilitiesTasks[p].title ==760 reportVulnerabilityName[z]761 ) {762 related_vulnerabilities += 1;763 if (related_vulnerabilities == 1) {764 fileContents.push(765 "\t * **" +766 i18n.t("global.vulnerabilities") +767 ":**\n"768 );769 }770 fileContents.push(771 "\t - [" +772 reportVulnerabilityName[z] +773 "](#" +774 reportVulnerabilityName[z]775 .toLowerCase()776 .split(" ")777 .join("-") +778 ")\n"779 );780 }781 }782 }783 if (related_vulnerabilities == 0) {784 fileContents.push(785 "\t * **" +786 i18n.t("global.vulnerabilities") +787 ":** " +788 i18n.t("global.none") +789 "\n"790 );791 }792 }793 }794 break;795 default:796 }797 if (i != n - 1) {798 fileContents.push("\n");799 }800 }801 if (wrote > 0) {802 fileContents.push("\n");803 }804 });805 try {806 fs.writeFile(807 path,808 title.concat(809 toc.join("").concat(separator.concat(fileContents.join("")))810 ),811 "utf-8",812 (err) => {813 if (err) {814 /* eslint-disable no-console */815 console.log(err);816 reject(err);817 }818 }819 );820 } catch (e) {821 console.log(err);822 reject(err);823 }824 } else {825 resolve();826 }827 resolve(path);828 } catch (err) {829 reject(err);830 }831 });832}833export function json(834 secciones_report,835 getMain,836 assets,837 activities,838 threats,839 vulnerabilities,840 recommentations,841 titlejson,842 message,843 locale844) {845 return new Promise(function (resolve, reject) {846 try {847 var options = {848 title: titlejson,849 message: message,850 //nameFieldLabel: "Project Name:"851 // defaultPath: directory to show (optional)852 };853 let path = dialog.showSaveDialogSync(options);854 i18n.locale = locale;855 if (path) {856 path = path + ".json";857 var reportJson = {858 reportTitle: "",859 sections: [],860 };861 reportJson["reportTitle"] = i18n.t("header.report");862 var fileContents = [];863 var toc = [];864 let keys = Object.keys(secciones_report);865 var sectionsFlag = 0;866 keys.forEach((key) => {867 let item = secciones_report[key];868 const n = getMain[item.interest][0].tasks.length;869 let wrote = 0;870 var jsonSection = {871 sectionName: "",872 entries: [],873 };874 for (var i = 0; i < n; i++) {875 var jsonEntry = {};876 wrote += 1;877 sectionsFlag += 1;878 if (sectionsFlag == 1) {879 reportJson["sections"] = [];880 }881 if (i == 0) {882 jsonSection["sectionName"] = item.title;883 }884 let description = getMain[item.interest][0].tasks[i].description;885 if (description === null) {886 description = "";887 }888 jsonEntry["description"] = description;889 jsonEntry["title"] = getMain[item.interest][0].tasks[i].title;890 const j = i + 1;891 switch (item.name) {892 case "Assets":893 for (var k = 0; k < assets.length; k++) {894 if (895 assets[k].id ==896 getMain[item.interest][0].tasks[i].identifier897 ) {898 if (assets[k].asset_category_name === undefined) {899 assets[k].asset_category_name = i18n.t("global.none");900 }901 jsonEntry["assetCategoryName"] =902 assets[k].asset_category_name;903 }904 }905 break;906 case "Activities":907 for (var k = 0; k < activities.length; k++) {908 if (909 activities[k].id ==910 getMain[item.interest][0].tasks[i].identifier911 ) {912 var related_assets = 0;913 if (activities[k].asset_name === undefined) {914 activities[k].asset_name = [];915 }916 for (var z = 0; z < activities[k].asset_name.length; z++) {917 for (918 var p = 0;919 p < getMain.report_assets[0].tasks.length;920 p++921 ) {922 if (923 getMain.report_assets[0].tasks[p].title ==924 activities[k].asset_name[z]925 ) {926 related_assets += 1;927 if (related_assets == 1) {928 fileContents.push(929 "\t * **" + i18n.t("global.assets") + ":**\n"930 );931 jsonEntry["relatedAssets"] = [];932 }933 jsonEntry["relatedAssets"].push({934 assetName: activities[k].asset_name[z],935 });936 }937 }938 }939 if (related_assets == 0) {940 jsonEntry["relatedAssets"] = i18n.t("global.none");941 }942 }943 }944 break;945 case "Threats":946 for (var k = 0; k < threats.length; k++) {947 if (948 threats[k].id ==949 getMain[item.interest][0].tasks[i].identifier950 ) {951 if (threats[k].threat_type_name === undefined) {952 threats[k].threat_type_name = i18n.t("global.none");953 }954 jsonEntry["threadType"] = threats[k].threat_type_name;955 var related_asset = 0;956 for (957 var p = 0;958 p < getMain.report_assets[0].tasks.length;959 p++960 ) {961 if (962 getMain.report_assets[0].tasks[p].title ==963 threats[k].asset_name964 ) {965 related_asset = 1;966 jsonEntry["relatedAsset"] = threats[k].asset_name;967 }968 }969 if (related_asset == 0) {970 jsonEntry["relatedAsset"] = i18n.t("global.none");971 }972 jsonEntry["impact"] = threats[k].impact;973 jsonEntry["likelihood"] = threats[k].likelihood;974 }975 }976 break;977 case "Vulnerabilities":978 let reportAct = getMain.report_activities[0].tasks;979 for (var k = 0; k < vulnerabilities.length; k++) {980 if (981 vulnerabilities[k].id ==982 getMain[item.interest][0].tasks[i].identifier983 ) {984 // Adding related activity985 var related_activity = 0;986 if (reportAct === undefined) {987 reportAct = [];988 }989 for (var p = 0; p < reportAct.length; p++) {990 if (991 reportAct[p].title ==992 vulnerabilities[k].assessment_activity_name993 ) {994 related_activity = 1;995 jsonEntry["relatedAcivity"] =996 vulnerabilities[k].assessment_activity_name;997 }998 }999 if (related_activity == 0) {1000 jsonEntry["relatedAcivity"] = i18n.t("global.none");1001 }1002 // Adding related asset1003 let reportAssets = getMain.report_assets[0].tasks;1004 var related_asset = 0;1005 if (reportAssets === undefined) {1006 reportAssets = [];1007 }1008 for (var p = 0; p < reportAssets.length; p++) {1009 if (1010 reportAssets[p].title == vulnerabilities[k].asset_name1011 ) {1012 related_asset = 1;1013 jsonEntry["relatedAsset"] =1014 vulnerabilities[k].asset_name;1015 }1016 }1017 if (related_asset == 0) {1018 jsonEntry["relatedAsset"] = i18n.t("global.none");1019 }1020 let reportThreats = getMain.report_threats[0].tasks;1021 let reportThreatName = vulnerabilities[k].threat_name;1022 var related_threats = 0;1023 if (reportThreats === undefined) {1024 reportThreats = [];1025 }1026 if (reportThreatName === undefined) {1027 reportThreatName = [];1028 }1029 for (var z = 0; z < reportThreatName.length; z++) {1030 for (var p = 0; p < reportThreats.length; p++) {1031 if (reportThreats[p].title == reportThreatName[z]) {1032 related_threats += 1;1033 if (related_threats == 1) {1034 jsonEntry["relatedThreats"] = [];1035 }1036 jsonEntry["relatedThreats"].push({1037 threatName: reportThreatName[z],1038 });1039 }1040 }1041 }1042 if (related_threats == 0) {1043 jsonEntry["relatedThreats"] = i18n.t("global.none");1044 }1045 }1046 }1047 break;1048 case "Recommendations":1049 for (var k = 0; k < recommentations.length; k++) {1050 if (1051 recommentations[k].id ==1052 getMain[item.interest][0].tasks[i].identifier1053 ) {1054 if (recommentations[k].implementation_cost === null) {1055 recommentations[k].implementation_cost = "";1056 }1057 if (recommentations[k].implementation_time === null) {1058 recommentations[k].implementation_time = "";1059 }1060 jsonEntry["implementationCost"] =1061 recommentations[k].implementation_cost;1062 jsonEntry["implementationTime"] =1063 recommentations[k].implementation_time;1064 let reportVulnerabilityName =1065 recommentations[k].vulnerability_name;1066 let reportVulnerabilitiesTasks =1067 getMain.report_vulnerabilities[0].tasks;1068 var related_vulnerabilities = 0;1069 if (reportVulnerabilityName === undefined) {1070 reportVulnerabilityName = [];1071 }1072 if (reportVulnerabilitiesTasks === undefined) {1073 reportVulnerabilitiesTasks = [];1074 }1075 for (var z = 0; z < reportVulnerabilityName.length; z++) {1076 for (1077 var p = 0;1078 p < reportVulnerabilitiesTasks.length;1079 p++1080 ) {1081 if (1082 reportVulnerabilitiesTasks[p].title ==1083 reportVulnerabilityName[z]1084 ) {1085 related_vulnerabilities += 1;1086 if (related_vulnerabilities == 1) {1087 jsonEntry["relatedVulnerabilities"] = [];1088 }1089 jsonEntry["relatedVulnerabilities"].push({1090 vulnerabilityName: reportVulnerabilityName[z],1091 });1092 }1093 }1094 }1095 if (related_vulnerabilities == 0) {1096 jsonEntry["relatedVulnerabilities"] =1097 i18n.t("global.none");1098 }1099 }1100 }1101 break;1102 default:1103 }1104 jsonSection["entries"].push(jsonEntry);1105 }1106 reportJson["sections"].push(jsonSection);1107 });1108 const data = JSON.stringify(reportJson);1109 try {1110 fs.writeFile(path, data, "utf-8", (err) => {1111 if (err) {1112 /* eslint-disable no-console */1113 console.log(err);1114 reject(err);1115 }1116 });1117 } catch (e) {1118 console.log(err);1119 reject(err);1120 }1121 } else {1122 resolve();1123 }1124 resolve(path);1125 } catch (err) {1126 reject(err);1127 }1128 });1129}1130export function docx(1131 secciones_report,1132 getMain,1133 assets,1134 activities,1135 threats,1136 vulnerabilities,1137 recommentations,1138 titletxt,1139 message,1140 locale1141) {1142 return new Promise(function (resolve, reject) {1143 try {1144 var options = {1145 title: titletxt,1146 message: message,1147 //nameFieldLabel: "Project Name:"1148 // defaultPath: directory to show (optional)1149 };1150 let path = dialog.showSaveDialogSync(options);1151 i18n.locale = locale;1152 if (path) {1153 path = path + ".docx";1154 var fileContents = [];1155 var docxChildren = [];1156 docxChildren.push(1157 new Paragraph({1158 text: i18n.t("header.report"),1159 heading: HeadingLevel.TITLE,1160 })1161 );1162 docxChildren.push(1163 new TableOfContents("Summary", {1164 hyperlink: true,1165 headingStyleRange: "1-5",1166 })1167 );1168 let keys = Object.keys(secciones_report);1169 keys.forEach((key) => {1170 let item = secciones_report[key];1171 const n = getMain[item.interest][0].tasks.length;1172 let wrote = 0;1173 for (var i = 0; i < n; i++) {1174 wrote += 1;1175 if (i == 0) {1176 docxChildren.push(1177 new Paragraph({1178 text: item.title,1179 heading: HeadingLevel.HEADING_1,1180 pageBreakBefore: true,1181 numbering: {1182 reference: "rawrr-numbering",1183 level: 0,1184 },1185 })1186 );1187 }1188 let description = getMain[item.interest][0].tasks[i].description;1189 if (description === null) {1190 description = "";1191 }1192 let title = getMain[item.interest][0].tasks[i].title;1193 const j = i + 1;1194 docxChildren.push(1195 new Paragraph({1196 text: title,1197 heading: HeadingLevel.HEADING_2,1198 numbering: {1199 reference: "rawrr-numbering",1200 level: 1,1201 },1202 })1203 );1204 docxChildren.push(1205 new Paragraph({1206 text: i18n.t("global.description") + ": " + description,1207 numbering: {1208 reference: "rawrr-numbering",1209 level: 2,1210 },1211 })1212 );1213 switch (item.name) {1214 case "Assets":1215 for (var k = 0; k < assets.length; k++) {1216 if (1217 assets[k].id ==1218 getMain[item.interest][0].tasks[i].identifier1219 ) {1220 if (assets[k].asset_category_name === undefined) {1221 assets[k].asset_category_name = i18n.t("global.none");1222 }1223 docxChildren.push(1224 new Paragraph({1225 text:1226 i18n.t("global.asset_category") +1227 ": " +1228 assets[k].asset_category_name,1229 numbering: {1230 reference: "rawrr-numbering",1231 level: 2,1232 },1233 })1234 );1235 }1236 }1237 break;1238 case "Activities":1239 for (var k = 0; k < activities.length; k++) {1240 if (1241 activities[k].id ==1242 getMain[item.interest][0].tasks[i].identifier1243 ) {1244 if (activities[k].asset_name === undefined) {1245 activities[k].asset_name = [];1246 }1247 var related_assets = 0;1248 for (var z = 0; z < activities[k].asset_name.length; z++) {1249 for (1250 var p = 0;1251 p < getMain.report_assets[0].tasks.length;1252 p++1253 ) {1254 if (1255 getMain.report_assets[0].tasks[p].title ==1256 activities[k].asset_name[z]1257 ) {1258 related_assets += 1;1259 if (related_assets == 1) {1260 docxChildren.push(1261 new Paragraph({1262 text: i18n.t("global.assets") + ":",1263 numbering: {1264 reference: "rawrr-numbering",1265 level: 2,1266 },1267 })1268 );1269 }1270 docxChildren.push(1271 new Paragraph({1272 text: activities[k].asset_name[z],1273 numbering: {1274 reference: "rawrr-numbering",1275 level: 3,1276 },1277 })1278 );1279 }1280 }1281 }1282 if (related_assets == 0) {1283 docxChildren.push(1284 new Paragraph({1285 text:1286 i18n.t("global.assets") +1287 ": " +1288 i18n.t("global.none"),1289 numbering: {1290 reference: "rawrr-numbering",1291 level: 2,1292 },1293 })1294 );1295 }1296 }1297 }1298 break;1299 case "Threats":1300 for (var k = 0; k < threats.length; k++) {1301 if (1302 threats[k].id ==1303 getMain[item.interest][0].tasks[i].identifier1304 ) {1305 if (threats[k].threat_type_name === undefined) {1306 threats[k].threat_type_name = i18n.t("global.none");1307 }1308 docxChildren.push(1309 new Paragraph({1310 text:1311 i18n.t("global.threat_type") +1312 ": " +1313 threats[k].threat_type_name,1314 numbering: {1315 reference: "rawrr-numbering",1316 level: 2,1317 },1318 })1319 );1320 var related_asset = 0;1321 for (1322 var p = 0;1323 p < getMain.report_assets[0].tasks.length;1324 p++1325 ) {1326 if (1327 getMain.report_assets[0].tasks[p].title ==1328 threats[k].asset_name1329 ) {1330 related_asset = 1;1331 docxChildren.push(1332 new Paragraph({1333 text:1334 i18n.t("global.asset") +1335 ": " +1336 threats[k].asset_name,1337 numbering: {1338 reference: "rawrr-numbering",1339 level: 2,1340 },1341 })1342 );1343 }1344 }1345 if (related_asset == 0) {1346 docxChildren.push(1347 new Paragraph({1348 text:1349 i18n.t("global.asset") +1350 ": " +1351 i18n.t("global.none"),1352 numbering: {1353 reference: "rawrr-numbering",1354 level: 2,1355 },1356 })1357 );1358 }1359 docxChildren.push(1360 new Paragraph({1361 text:1362 i18n.t("global.impact") + ": " + threats[k].impact,1363 numbering: {1364 reference: "rawrr-numbering",1365 level: 2,1366 },1367 })1368 );1369 docxChildren.push(1370 new Paragraph({1371 text:1372 i18n.t("global.likelihood") +1373 ": " +1374 threats[k].likelihood,1375 numbering: {1376 reference: "rawrr-numbering",1377 level: 2,1378 },1379 })1380 );1381 }1382 }1383 break;1384 case "Vulnerabilities":1385 let reportAct = getMain.report_activities[0].tasks;1386 for (var k = 0; k < vulnerabilities.length; k++) {1387 if (1388 vulnerabilities[k].id ==1389 getMain[item.interest][0].tasks[i].identifier1390 ) {1391 // Adding related activity1392 var related_activity = 0;1393 if (reportAct === undefined) {1394 reportAct = [];1395 }1396 for (var p = 0; p < reportAct.length; p++) {1397 if (1398 reportAct[p].title ==1399 vulnerabilities[k].assessment_activity_name1400 ) {1401 related_activity = 1;1402 docxChildren.push(1403 new Paragraph({1404 text:1405 i18n.t("global.assessment_activity") +1406 ": " +1407 vulnerabilities[k].assessment_activity_name,1408 numbering: {1409 reference: "rawrr-numbering",1410 level: 2,1411 },1412 })1413 );1414 }1415 }1416 if (related_activity == 0) {1417 docxChildren.push(1418 new Paragraph({1419 text:1420 i18n.t("global.likelihood") +1421 ": " +1422 threats[k].likelihood,1423 numbering: {1424 reference: "rawrr-numbering",1425 level: 2,1426 },1427 })1428 );1429 fileContents.push(1430 " " +1431 i18n.t("global.assessment_activity") +1432 ": " +1433 i18n.t("global.none") +1434 "\n"1435 );1436 }1437 // Adding related asset1438 let reportAssets = getMain.report_assets[0].tasks;1439 var related_asset = 0;1440 if (reportAssets === undefined) {1441 reportAssets = [];1442 }1443 for (var p = 0; p < reportAssets.length; p++) {1444 if (1445 reportAssets[p].title == vulnerabilities[k].asset_name1446 ) {1447 related_asset = 1;1448 docxChildren.push(1449 new Paragraph({1450 text:1451 i18n.t("global.asset") +1452 ": " +1453 vulnerabilities[k].asset_name,1454 numbering: {1455 reference: "rawrr-numbering",1456 level: 2,1457 },1458 })1459 );1460 }1461 }1462 if (related_asset == 0) {1463 docxChildren.push(1464 new Paragraph({1465 text:1466 i18n.t("global.asset") +1467 ": " +1468 i18n.t("global.none"),1469 numbering: {1470 reference: "rawrr-numbering",1471 level: 2,1472 },1473 })1474 );1475 }1476 // Adding related threats1477 let reportThreats = getMain.report_threats[0].tasks;1478 let reportThreatName = vulnerabilities[k].threat_name;1479 var related_threats = 0;1480 if (reportThreats === undefined) {1481 reportThreats = [];1482 }1483 if (reportThreatName === undefined) {1484 reportThreatName = [];1485 }1486 for (var z = 0; z < reportThreatName.length; z++) {1487 for (var p = 0; p < reportThreats.length; p++) {1488 if (reportThreats[p].title == reportThreatName[z]) {1489 related_threats += 1;1490 if (related_threats == 1) {1491 docxChildren.push(1492 new Paragraph({1493 text: i18n.t("global.threats") + ":",1494 numbering: {1495 reference: "rawrr-numbering",1496 level: 2,1497 },1498 })1499 );1500 }1501 docxChildren.push(1502 new Paragraph({1503 text: reportThreatName[z],1504 numbering: {1505 reference: "rawrr-numbering",1506 level: 3,1507 },1508 })1509 );1510 }1511 }1512 }1513 if (related_threats == 0) {1514 docxChildren.push(1515 new Paragraph({1516 text:1517 i18n.t("global.threats") +1518 ": " +1519 i18n.t("global.none"),1520 numbering: {1521 reference: "rawrr-numbering",1522 level: 2,1523 },1524 })1525 );1526 }1527 }1528 }1529 break;1530 case "Recommendations":1531 for (var k = 0; k < recommentations.length; k++) {1532 if (1533 recommentations[k].id ==1534 getMain[item.interest][0].tasks[i].identifier1535 ) {1536 if (recommentations[k].implementation_cost === null) {1537 recommentations[k].implementation_cost = "";1538 }1539 if (recommentations[k].implementation_time === null) {1540 recommentations[k].implementation_time = "";1541 }1542 docxChildren.push(1543 new Paragraph({1544 text:1545 i18n.t("global.implementation_cost") +1546 ": " +1547 recommentations[k].implementation_cost,1548 numbering: {1549 reference: "rawrr-numbering",1550 level: 2,1551 },1552 })1553 );1554 docxChildren.push(1555 new Paragraph({1556 text:1557 i18n.t("global.implementation_time") +1558 ": " +1559 recommentations[k].implementation_time,1560 numbering: {1561 reference: "rawrr-numbering",1562 level: 2,1563 },1564 })1565 );1566 let reportVulnerabilityName =1567 recommentations[k].vulnerability_name;1568 let reportVulnerabilitiesTasks =1569 getMain.report_vulnerabilities[0].tasks;1570 var related_vulnerabilities = 0;1571 if (reportVulnerabilityName === undefined) {1572 reportVulnerabilityName = [];1573 }1574 if (reportVulnerabilitiesTasks === undefined) {1575 reportVulnerabilitiesTasks = [];1576 }1577 for (var z = 0; z < reportVulnerabilityName.length; z++) {1578 for (1579 var p = 0;1580 p < reportVulnerabilitiesTasks.length;1581 p++1582 ) {1583 if (1584 reportVulnerabilitiesTasks[p].title ==1585 reportVulnerabilityName[z]1586 ) {1587 related_vulnerabilities += 1;1588 if (related_vulnerabilities == 1) {1589 docxChildren.push(1590 new Paragraph({1591 text: i18n.t("global.vulnerabilities") + ":",1592 numbering: {1593 reference: "rawrr-numbering",1594 level: 2,1595 },1596 })1597 );1598 }1599 docxChildren.push(1600 new Paragraph({1601 text: reportVulnerabilityName[z],1602 numbering: {1603 reference: "rawrr-numbering",1604 level: 3,1605 },1606 })1607 );1608 }1609 }1610 }1611 if (related_vulnerabilities == 0) {1612 docxChildren.push(1613 new Paragraph({1614 text:1615 i18n.t("global.vulnerabilities") +1616 ": " +1617 i18n.t("global.none"),1618 numbering: {1619 reference: "rawrr-numbering",1620 level: 2,1621 },1622 })1623 );1624 }1625 }1626 }1627 break;1628 default:1629 }1630 if (i != n - 1) {1631 fileContents.push("\n");1632 }1633 }1634 if (wrote > 0) {1635 fileContents.push("\n");1636 }1637 });1638 try {1639 const doc = new Document({1640 numbering: {1641 config: [1642 {1643 reference: "rawrr-numbering",1644 levels: [1645 {1646 level: 0,1647 format: "upperRoman",1648 text: "%1",1649 alignment: AlignmentType.START,1650 style: {1651 paragraph: {1652 indent: { left: 260, hanging: 260 },1653 },1654 },1655 },1656 {1657 level: 1,1658 format: "decimal",1659 text: "%2.",1660 alignment: AlignmentType.DISTRIBUTE,1661 style: {1662 paragraph: {1663 indent: { left: 520, hanging: 260 },1664 },1665 },1666 },1667 {1668 level: 2,1669 format: "lowerLetter",1670 text: "%3)",1671 alignment: AlignmentType.DISTRIBUTE,1672 style: {1673 paragraph: {1674 indent: { left: 780, hanging: 260 },1675 },1676 },1677 },1678 {1679 level: 3,1680 format: "upperLetter",1681 text: "%4)",1682 alignment: AlignmentType.START,1683 style: {1684 paragraph: {1685 indent: { left: 1050, hanging: 260 },1686 },1687 },1688 },1689 ],1690 },1691 ],1692 },1693 features: {1694 updateFields: true,1695 },1696 sections: [1697 {1698 children: docxChildren,1699 },1700 ],1701 });1702 // Used to export the file into a .docx file1703 Packer.toBuffer(doc).then((buffer) => {1704 fs.writeFileSync(path, buffer);1705 });1706 } catch (e) {1707 console.log(err);1708 reject(err);1709 }1710 } else {1711 resolve();1712 }1713 resolve(path);1714 } catch (err) {1715 reject(err);1716 }1717 });...
connection_checker_test.mocha.js
Source:connection_checker_test.mocha.js
...114 });115 runTwoBlockTests();116 clearSiblingTests();117 siblingTest('Exact types multiple inputs', function() {118 const dog = this.getMain('dog');119 const dogOut = this.getInnerOutput('dog');120 this.assertCanConnect(dog.in1, dogOut);121 });122 siblingTest('Sibling types multiple inputs', function() {123 const dog = this.getMain('dog');124 const catOut = this.getInnerOutput('cat');125 this.assertCannotConnect(dog.in1, catOut);126 });127 runSiblingTests();128 });129 suite('Simple generics', function() {130 // Both explicit is the other suite.131 clearTwoBlockTests();132 twoBlockTest('Outer explicit, inner unbound', function() {133 const dogIn = this.getOuterInput('dog');134 const tOut = this.getInnerOutput('t');135 this.assertCanConnect(dogIn, tOut);136 });137 twoBlockTest('Outer explicit, inner bound sub', function() {138 const mammalIn = this.getOuterInput('mammal');139 const tOut = this.getInnerOutput('t');140 this.bindConnection(tOut, 'dog');141 this.assertCanConnect(mammalIn, tOut);142 });143 twoBlockTest('Outer explicit, inner bound super', function() {144 const dogIn = this.getOuterInput('dog');145 const tOut = this.getInnerOutput('t');146 this.bindConnection(tOut, 'mammal');147 this.assertCannotConnect(dogIn, tOut);148 });149 twoBlockTest('Outer explicit, inner bound different case', function() {150 const dogIn = this.getOuterInput('dog');151 const tOut = this.getInnerOutput('t');152 this.bindConnection(tOut, 'DOG');153 this.assertCanConnect(dogIn, tOut);154 });155 twoBlockTest('Outer unbound, inner explicit', function() {156 const tIn = this.getOuterInput('t');157 const dogOut = this.getInnerOutput('dog');158 this.assertCanConnect(tIn, dogOut);159 });160 twoBlockTest('Outer unbound, inner unbound', function() {161 const tIn = this.getOuterInput('t');162 const tOut = this.getInnerOutput('t');163 this.assertCanConnect(tIn, tOut);164 });165 twoBlockTest('Outer unbound, inner bound', function() {166 const tIn = this.getOuterInput('t');167 const tOut = this.getInnerOutput('t');168 this.bindConnection(tOut, 'dog');169 this.assertCanConnect(tIn, tOut);170 });171 twoBlockTest('Outer bound, child explicit sub', function() {172 const tIn = this.getOuterInput('t');173 const dogOut = this.getInnerOutput('dog');174 this.bindConnection(tIn, 'mammal');175 this.assertCanConnect(tIn, dogOut);176 });177 twoBlockTest('Outer bound, child explicit super', function() {178 const tIn = this.getOuterInput('t');179 const dogOut = this.getInnerOutput('mammal');180 this.bindConnection(tIn, 'dog');181 this.assertCannotConnect(tIn, dogOut);182 });183 twoBlockTest('Outer bound, child unbound', function() {184 const tIn = this.getOuterInput('t');185 const tOut = this.getInnerOutput('t');186 this.bindConnection(tIn, 'dog');187 this.assertCanConnect(tIn, tOut);188 });189 twoBlockTest('Outer bound, child bound sub', function() {190 const tIn = this.getOuterInput('t');191 const tOut = this.getInnerOutput('t');192 this.bindConnection(tIn, 'mammal');193 this.bindConnection(tIn, 'dog');194 this.assertCanConnect(tIn, tOut);195 });196 twoBlockTest('Outer bound, child bound super', function() {197 const tIn = this.getOuterInput('t');198 const tOut = this.getInnerOutput('t');199 this.bindConnection(tIn, 'dog');200 this.bindConnection(tIn, 'mammal');201 this.assertCanConnect(tIn, tOut);202 });203 twoBlockTest('Outer bound different case, inner explicit', function() {204 const tIn = this.getOuterInput('t');205 const dogOut = this.getInnerOutput('dog');206 this.bindConnection(tIn, 'DOG');207 this.assertCanConnect(tIn, dogOut);208 });209 runTwoBlockTests();210 });211 suite('Multiple inputs on generics', function() {212 setup(function() {213 const hierarchy = {214 'typeA': {},215 'typeB': {},216 'typeC': {217 'fulfills': ['typeB', 'typeA'],218 },219 'typeD': {220 'fulfills': ['typeB', 'typeA'],221 },222 'typeE': {},223 };224 const types = Object.keys(hierarchy);225 this.multiTypeBlocks = createBlockDefs(types);226 Blockly.defineBlocksWithJsonArray(this.multiTypeBlocks);227 this.checker.init(hierarchy);228 });229 teardown(function() {230 for (const block of this.multiTypeBlocks) {231 delete Blockly.Blocks[block.type];232 }233 });234 clearSiblingTests();235 siblingTest('No outer, compat inners', function() {236 const t = this.getMain('t');237 const typeCOut = this.getInnerOutput('typeC');238 const typeDOut = this.getInnerOutput('typeD');239 t.in1.connect(typeCOut);240 this.assertCanConnect(t.in2, typeDOut);241 });242 siblingTest('No outer, half compat inners', function() {243 const t = this.getMain('t');244 const t2 = this.getMain('t');245 const typeAOut = this.getInnerOutput('typeA');246 const typeCOut = this.getInnerOutput('typeC');247 const typeDOut = this.getInnerOutput('typeD');248 t.in1.connect(typeAOut);249 t2.in1.connect(typeCOut);250 t2.in2.connect(typeDOut);251 this.assertCanConnect(t.in2, t2.out);252 });253 siblingTest('No outer, incompat inners', function() {254 const t = this.getMain('t');255 const typeAOut = this.getInnerOutput('typeA');256 const typeEOut = this.getInnerOutput('typeE');257 t.in1.connect(typeAOut);258 this.assertCannotConnect(t.in2, typeEOut);259 });260 siblingTest('Outer unbound, compat inners', function() {261 const tIn = this.getOuterInput('t');262 const t = this.getMain('t');263 const typeCOut = this.getInnerOutput('typeC');264 const typeDOut = this.getInnerOutput('typeD');265 tIn.connect(t.out);266 t.in1.connect(typeCOut);267 this.assertCanConnect(t.in2, typeDOut);268 });269 siblingTest('Outer unbound, half compat inners', function() {270 const tIn = this.getOuterInput('t');271 const t = this.getMain('t');272 const t2 = this.getMain('t');273 const typeAOut = this.getInnerOutput('typeA');274 const typeCOut = this.getInnerOutput('typeC');275 const typeDOut = this.getInnerOutput('typeD');276 tIn.connect(t.out);277 t.in1.connect(typeAOut);278 t2.in1.connect(typeCOut);279 t2.in2.connect(typeDOut);280 this.assertCanConnect(t.in2, t2.out);281 });282 siblingTest('Outer unbound, incompat inners', function() {283 const tIn = this.getOuterInput('t');284 const t = this.getMain('t');285 const typeAOut = this.getInnerOutput('typeA');286 const typeEOut = this.getInnerOutput('typeE');287 tIn.connect(t.out);288 t.in1.connect(typeAOut);289 this.assertCannotConnect(t.in2, typeEOut);290 });291 // The following tests make sure the other branch is run.292 siblingTest('Outer bound, second inner incompat', function() {293 const tIn = this.getOuterInput('t');294 const t = this.getMain('t');295 const typeCOut = this.getInnerOutput('typeC');296 const typeDOut = this.getInnerOutput('typeD');297 this.bindConnection(tIn, 'typeC');298 tIn.connect(t.out);299 t.in1.connect(typeCOut);300 this.assertCannotConnect(t.in2, typeDOut);301 });302 siblingTest('Outer explicit, second inner incompat', function() {303 const typeCIn = this.getOuterInput('typeC');304 const t = this.getMain('t');305 const typeCOut = this.getInnerOutput('typeC');306 const typeDOut = this.getInnerOutput('typeD');307 typeCIn.connect(t.out);308 t.in1.connect(typeCOut);309 this.assertCannotConnect(t.in2, typeDOut);310 });311 siblingTest('Main bound, second inner incompat', function() {312 const t = this.getMain('t');313 const typeCOut = this.getInnerOutput('typeC');314 const typeDOut = this.getInnerOutput('typeD');315 this.bindConnection(t.out, 'typeC');316 t.in1.connect(typeCOut);317 this.assertCannotConnect(t.in2, typeDOut);318 });319 runSiblingTests();320 });321 suite('Multiple explicit types on generics', function() {322 setup(function() {323 const hierarchy = {324 'typeA': {},325 'typeB': {},326 'typeC': {327 'fulfills': ['typeB', 'typeA'],328 },329 'typeD': {330 'fulfills': ['typeB', 'typeA'],331 },332 'typeE': {},333 'typeF': {},334 'typeG': {335 'fulfills': ['typeE', 'typeF'],336 },337 'typeH': {338 'fulfills': ['typeE', 'typeF'],339 },340 };341 const types = Object.keys(hierarchy);342 this.multiTypeBlocks = createBlockDefs(types);343 Blockly.defineBlocksWithJsonArray(this.multiTypeBlocks);344 this.checker.init(hierarchy);345 });346 teardown(function() {347 for (const block of this.multiTypeBlocks) {348 delete Blockly.Blocks[block.type];349 }350 });351 clearSiblingTests();352 siblingTest('Multi main, compat inner', function() {353 const t = this.getMain('t');354 const typeCOut = this.getInnerOutput('typeC');355 const typeDOut = this.getInnerOutput('typeD');356 const typeAOut = this.getInnerOutput('typeA');357 this.bindConnection(t.out, 'typeA');358 t.in1.connect(typeCOut);359 t.in2.connect(typeDOut);360 this.unbindConnection(t.out);361 this.assertCanConnect(t.in3, typeAOut);362 });363 siblingTest('Multi main, incompat inner', function() {364 const t = this.getMain('t');365 const typeCOut = this.getInnerOutput('typeC');366 const typeDOut = this.getInnerOutput('typeD');367 const typeEOut = this.getInnerOutput('typeE');368 this.bindConnection(t.out, 'typeA');369 t.in1.connect(typeCOut);370 t.in2.connect(typeDOut);371 this.unbindConnection(t.out);372 this.assertCannotConnect(t.in3, typeEOut);373 });374 siblingTest('Compat outer, multi main', function() {375 const typeAIn = this.getOuterInput('typeA');376 const t = this.getMain('t');377 const typeCOut = this.getInnerOutput('typeC');378 const typeDOut = this.getInnerOutput('typeD');379 t.in1.connect(typeCOut);380 t.in2.connect(typeDOut);381 this.assertCanConnect(typeAIn, t.out);382 });383 siblingTest('Incompat outer, multi main', function() {384 const typeEIn = this.getOuterInput('typeE');385 const t = this.getMain('t');386 const typeCOut = this.getInnerOutput('typeC');387 const typeDOut = this.getInnerOutput('typeD');388 t.in1.connect(typeCOut);389 t.in2.connect(typeDOut);390 this.assertCannotConnect(typeEIn, t.out);391 });392 siblingTest('2 multi mains, compatible', function() {393 const t1 = this.getMain('t', 't1');394 const t2 = this.getMain('t', 't2');395 const typeCOut1 = this.getInnerOutput('typeC');396 const typeDOut1 = this.getInnerOutput('typeD');397 const typeCOut2 = this.getInnerOutput('typeC');398 const typeDOut2 = this.getInnerOutput('typeD');399 t1.in1.connect(typeCOut1);400 t1.in2.connect(typeDOut1);401 t2.in1.connect(typeCOut2);402 t2.in2.connect(typeDOut2);403 this.assertCanConnect(t1.in3, t2.out);404 });405 siblingTest('2 multi mains, incompatible', function() {406 const t1 = this.getMain('t', 't1');407 const t2 = this.getMain('t', 't2');408 const typeCOut = this.getInnerOutput('typeC');409 const typeDOut = this.getInnerOutput('typeD');410 const typeGOut = this.getInnerOutput('typeG');411 const typeHOut = this.getInnerOutput('typeH');412 t1.in1.connect(typeCOut);413 t1.in2.connect(typeDOut);414 t2.in1.connect(typeGOut);415 t2.in2.connect(typeHOut);416 this.assertCannotConnect(t1.in3, t2.out);417 });418 runSiblingTests();419 });420 });421 suite('bindType', function() {422 suite('Disconnect connections', function() {423 setup(function() {424 this.assertIsConnected = function(conn) {425 chai.assert.isTrue(conn.isConnected(),426 'Expected ' + conn.name + ' to be connected.');427 };428 this.assertIsNotConnected = function(conn) {429 chai.assert.isFalse(conn.isConnected(),430 'Expected ' + conn.name + ' to be unconnected.');431 };432 });433 clearThreeBlockTests();434 threeBlockTest('Outer valid, inner valid', function() {435 const dogIn = this.getOuterInput('dog');436 const t = this.getMain('t');437 const dogOut = this.getInnerOutput('dog');438 dogIn.connect(t.out);439 t.in.connect(dogOut);440 this.bindConnection(t.out, 'dog');441 this.assertIsConnected(t.out);442 this.assertIsConnected(t.in);443 });444 threeBlockTest('Outer valid, inner invalid', function() {445 const mammalIn = this.getOuterInput('mammal');446 const t = this.getMain('t');447 const catOut = this.getInnerOutput('cat');448 mammalIn.connect(t.out);449 t.in.connect(catOut);450 this.bindConnection(t.out, 'dog');451 this.assertIsConnected(t.out);452 this.assertIsNotConnected(t.in);453 });454 threeBlockTest('Outer invalid, inner valid', function() {455 const mammalIn = this.getOuterInput('mammal');456 const t = this.getMain('t');457 const batOut = this.getInnerOutput('bat');458 mammalIn.connect(t.out);459 t.in.connect(batOut);460 this.bindConnection(t.out, 'flyinganimal');461 this.assertIsNotConnected(t.out);462 this.assertIsConnected(t.in);463 });464 threeBlockTest('Outer invalid, inner invalid', function() {465 const dogIn = this.getOuterInput('dog');466 const t = this.getMain('t');467 const dogOut = this.getInnerOutput('dog');468 dogIn.connect(t.out);469 t.in.connect(dogOut);470 this.bindConnection(t.out, 'cat');471 this.assertIsNotConnected(t.out);472 this.assertIsNotConnected(t.in);473 });474 runThreeBlockTests();475 clearSiblingTests();476 siblingTest('Some inners valid', function() {477 const t = this.getMain('t');478 const dogOut = this.getInnerOutput('dog');479 const catOut = this.getInnerOutput('cat');480 this.bindConnection(t.out, 'mammal');481 t.in1.connect(dogOut);482 t.in2.connect(catOut);483 this.unbindConnection(t.out);484 this.bindConnection(t.out, 'dog');485 this.assertIsConnected(t.in1);486 this.assertIsNotConnected(t.in2);487 });488 runSiblingTests();489 });490 });491 suite('getExplicitTypes', function() {492 setup(function() {493 this.assertNoType = function(conn) {494 const explicitTypes = this.checker.getExplicitTypes(495 conn.getSourceBlock(), 'T');496 chai.assert.isArray(explicitTypes,497 'Expected getExplicitTypes to return an array.');498 chai.assert.isEmpty(explicitTypes,499 'Expected ' + conn.name + ' to not have a type.');500 };501 this.assertHasType = function(conn, type) {502 const explicitTypes = this.checker.getExplicitTypes(503 conn.getSourceBlock(), 'T');504 chai.assert.include(explicitTypes, type,505 'Expected ' + conn.name + ' to have type ' + type + '.');506 };507 });508 suite('Single blocks', function() {509 clearTwoBlockTests();510 twoBlockTest('Outer explicit', function() {511 const dogIn = this.getOuterInput('dog');512 this.assertNoType(dogIn);513 });514 twoBlockTest('Inner explicit', function() {515 const dogOut = this.getInnerOutput('dog');516 this.assertNoType(dogOut);517 });518 twoBlockTest('Outer unbound', function() {519 const tIn = this.getOuterInput('t');520 this.assertNoType(tIn);521 });522 twoBlockTest('Inner unbound', function() {523 const tOut = this.getInnerOutput('t');524 this.assertNoType(tOut);525 });526 twoBlockTest('Outer bound programmatically', function() {527 const tIn = this.getOuterInput('t');528 this.bindConnection(tIn, 'dog');529 this.assertHasType(tIn, 'dog');530 });531 twoBlockTest('Inner bound programmatically', function() {532 const tOut = this.getInnerOutput('t');533 this.bindConnection(tOut, 'dog');534 this.assertHasType(tOut, 'dog');535 });536 runTwoBlockTests();537 });538 suite('Flow through connections', function() {539 clearTwoBlockTests();540 twoBlockTest('From parent, explicit', function() {541 const dogIn = this.getOuterInput('dog');542 const tOut = this.getInnerOutput('t');543 dogIn.connect(tOut);544 this.assertHasType(tOut, 'dog');545 });546 twoBlockTest('From parent, bound', function() {547 const tIn = this.getOuterInput('t');548 const tOut = this.getInnerOutput('t');549 this.bindConnection(tIn, 'dog');550 tIn.connect(tOut);551 this.assertHasType(tOut, 'dog');552 });553 twoBlockTest('From child, explicit', function() {554 const tIn = this.getOuterInput('t');555 const dogOut = this.getInnerOutput('dog');556 tIn.connect(dogOut);557 this.assertHasType(tIn, 'dog');558 });559 twoBlockTest('From child, bound', function() {560 const tIn = this.getOuterInput('t');561 const tOut = this.getInnerOutput('t');562 this.bindConnection(tOut, 'dog');563 tIn.connect(tOut);564 this.assertHasType(tIn, 'dog');565 });566 runTwoBlockTests();567 clearThreeBlockTests();568 threeBlockTest('From grandparent', function() {569 const dogIn = this.getOuterInput('dog');570 const t = this.getMain('t');571 const tOut = this.getInnerOutput('t');572 dogIn.connect(t.out);573 t.in.connect(tOut);574 this.assertHasType(tOut, 'dog');575 });576 threeBlockTest('From grandchild', function() {577 const tIn = this.getOuterInput('t');578 const t = this.getMain('t');579 const dogOut = this.getInnerOutput('dog');580 tIn.connect(t.out);581 t.in.connect(dogOut);582 this.assertHasType(tIn, 'dog');583 });584 runThreeBlockTests();585 clearSiblingTests();586 siblingTest('From ancestor', function() {587 const dogIn = this.getOuterInput('dog');588 const tOut = this.getInnerOutput('t');589 let t = {in1: dogIn};590 for (let i = 0; i < 10; i++) {591 const newT = this.getMain('t');592 t.in1.connect(newT.out);593 t = newT;594 }595 t.in1.connect(tOut);596 this.assertHasType(tOut, 'dog');597 });598 siblingTest('From descendant', function() {599 const tIn = this.getOuterInput('t');600 const dogOut = this.getInnerOutput('dog');601 let t = {in1: tIn};602 for (let i = 0; i < 10; i++) {603 const newT = this.getMain('t');604 t.in1.connect(newT.out);605 t = newT;606 }607 t.in1.connect(dogOut);608 this.assertHasType(tIn, 'dog');609 });610 siblingTest('From parsib', function() {611 const t1 = this.getMain('t', 't1');612 const t2 = this.getMain('t', 't2');613 const dogOut = this.getInnerOutput('dog');614 const tOut = this.getInnerOutput('t');615 t1.in1.connect(dogOut);616 t1.in2.connect(t2.out);617 t2.in1.connect(tOut);618 this.assertHasType(tOut, 'dog');619 });620 siblingTest('From ancestor parsib', function() {621 const t1 = this.getMain('t', 't1');622 const dogOut = this.getInnerOutput('dog');623 const tOut = this.getInnerOutput('t');624 let tNext = t1;625 for (let i = 0; i < 10; i++) {626 const tNew = this.getMain('t');627 tNext.in1.connect(tNew.out);628 tNext = tNew;629 }630 tNext.in1.connect(tOut);631 t1.in2.connect(dogOut);632 this.assertHasType(tOut, 'dog');633 });634 siblingTest('From sibling', function() {635 const t = this.getMain('t');636 const dogOut = this.getInnerOutput('dog');637 const tOut = this.getInnerOutput('t');638 t.in1.connect(dogOut);639 t.in2.connect(tOut);640 this.assertHasType(tOut, 'dog');641 });642 siblingTest('From cousin', function() {643 const t1 = this.getMain('t', 't1');644 const t2 = this.getMain('t', 't2');645 const t3 = this.getMain('t', 't3');646 const dogOut = this.getInnerOutput('dog');647 const tOut = this.getInnerOutput('t');648 t1.in1.connect(t2.out);649 t1.in2.connect(t3.out);650 t2.in1.connect(dogOut);651 t3.in1.connect(tOut);652 this.assertHasType(tOut, 'dog');653 });654 siblingTest('From second cousin', function() {655 const t1 = this.getMain('t', 't1');656 const t2 = this.getMain('t', 't2');657 const t3 = this.getMain('t', 't3');658 const t4 = this.getMain('t', 't4');659 const t5 = this.getMain('t', 't5');660 const dogOut = this.getInnerOutput('dog');661 const tOut = this.getInnerOutput('t');662 t1.in1.connect(t2.out);663 t1.in2.connect(t3.out);664 t2.in1.connect(t4.out);665 t3.in1.connect(t5.out);666 t4.in1.connect(dogOut);667 t5.in1.connect(tOut);668 this.assertHasType(tOut, 'dog');669 });670 siblingTest('From first cousin once removed', function() {671 const t1 = this.getMain('t', 't1');672 const t2 = this.getMain('t', 't2');673 const t3 = this.getMain('t', 't3');674 const t4 = this.getMain('t', 't4');675 const dogOut = this.getInnerOutput('dog');676 const tOut = this.getInnerOutput('t');677 t1.in1.connect(t2.out);678 t1.in2.connect(t3.out);679 t2.in1.connect(dogOut);680 t3.in1.connect(t4.out);681 t4.in1.connect(tOut);682 this.assertHasType(tOut, 'dog');683 });684 siblingTest('From nibling', function() {685 const t1 = this.getMain('t', 't1');686 const t2 = this.getMain('t', 't2');687 const dogOut = this.getInnerOutput('dog');688 const tOut = this.getInnerOutput('t');689 t1.in1.connect(t2.out);690 t1.in2.connect(tOut);691 t2.in1.connect(dogOut);692 this.assertHasType(tOut, 'dog');693 });694 siblingTest('From grandnibling', function() {695 const t1 = this.getMain('t', 't1');696 const t2 = this.getMain('t', 't2');697 const t3 = this.getMain('t', 't3');698 const dogOut = this.getInnerOutput('dog');699 const tOut = this.getInnerOutput('t');700 t1.in1.connect(t2.out);701 t1.in2.connect(tOut);702 t2.in1.connect(t3.out);703 t3.in1.connect(dogOut);704 this.assertHasType(tOut, 'dog');705 });706 runSiblingTests();707 });708 suite('Flow through different generics', function() {709 clearSiblingTests();710 setup(function() {711 const types = ['T', 'a', 'b', '1', '*'];712 this.genericBlocks = createBlockDefs(types);713 Blockly.defineBlocksWithJsonArray(this.genericBlocks);714 });715 teardown(function() {716 for (const block of this.genericBlocks) {717 delete Blockly.Blocks[block.type];718 }719 });720 siblingTest('Differently cased generics - explicit outer', function() {721 const dogIn = this.getOuterInput('dog');722 let t = {in1: dogIn};723 for (let i = 0; i < 3; i++) {724 const TNew = this.getMain('T');725 t.in1.connect(TNew.out);726 const tNew = this.getMain('t');727 TNew.in1.connect(tNew.out);728 t = tNew;729 }730 this.assertHasType(t.out, 'dog');731 });732 siblingTest('Differently cased generic - explicit inner', function() {733 const tIn = this.getOuterInput('t');734 const dogOut = this.getInnerOutput('dog');735 let t = {in1: tIn};736 for (let i = 0; i < 3; i++) {737 const TNew = this.getMain('T');738 t.in1.connect(TNew.out);739 const tNew = this.getMain('t');740 TNew.in1.connect(tNew.out);741 t = tNew;742 }743 t.in1.connect(dogOut);744 this.assertHasType(tIn, 'dog');745 });746 siblingTest('Different generics - explicit outer', function() {747 const dogIn = this.getOuterInput('dog');748 const connT = this.getMain('T', 'connT');749 const conna = this.getMain('a', 'conna');750 const connb = this.getMain('b', 'connb');751 const conn1 = this.getMain('1', 'conn1');752 const connStar = this.getMain('*', 'connStar');753 const tOut = this.getInnerOutput('t');754 dogIn.connect(connT.out);755 connT.in1.connect(conna.out);756 conna.in1.connect(connb.out);757 connb.in1.connect(conn1.out);758 conn1.in1.connect(connStar.out);759 connStar.in1.connect(tOut);760 this.assertHasType(tOut, 'dog');761 });762 siblingTest('Different generics - explicit inner', function() {763 const tIn = this.getOuterInput('t');764 const connT = this.getMain('T', 'connT');765 const conna = this.getMain('a', 'conna');766 const connb = this.getMain('b', 'connb');767 const conn1 = this.getMain('1', 'conn1');768 const connStar = this.getMain('*', 'connStar');769 const dogOut = this.getInnerOutput('dog');770 tIn.connect(connT.out);771 connT.in1.connect(conna.out);772 conna.in1.connect(connb.out);773 connb.in1.connect(conn1.out);774 conn1.in1.connect(connStar.out);775 connStar.in1.connect(dogOut);776 this.assertHasType(tIn, 'dog');777 });778 runSiblingTests();779 test('Differently cased on same block - lowercase input', function() {780 Blockly.defineBlocksWithJsonArray([{781 'type': 'different_cases',782 'message0': '%1',783 'args0': [784 {785 'type': 'input_value',786 'name': 'INPUT1',787 'check': ['t'],788 },789 ],790 'output': ['T'],791 }]);792 const dog = this.workspace.newBlock('static_dog_outer_value');793 const differentCases1 = this.workspace.newBlock('different_cases');794 const differentCases2 = this.workspace.newBlock('different_cases');795 const dogIn = dog.getInput('INPUT1').connection;796 const differentCases1Out = differentCases1.outputConnection;797 const differentCases1In = differentCases1.getInput('INPUT1').connection;798 const differentCases2Out = differentCases2.outputConnection;799 // For logging.800 dogIn.name = 'dogIn';801 differentCases1Out.name = 'differentCases1Out';802 differentCases1In.name = 'differentCases1In';803 differentCases2Out.name = 'differentCases2Out';804 dogIn.connect(differentCases1Out);805 differentCases1In.connect(differentCases2Out);806 this.assertHasType(differentCases2Out, 'dog');807 delete Blockly.Blocks['different_cases'];808 });809 test('Differently cased on same block - lowercase output', function() {810 Blockly.defineBlocksWithJsonArray([{811 'type': 'different_cases',812 'message0': '%1',813 'args0': [814 {815 'type': 'input_value',816 'name': 'INPUT1',817 'check': ['T'],818 },819 ],820 'output': ['t'],821 }]);822 const dog = this.workspace.newBlock('static_dog_outer_value');823 const differentCases1 = this.workspace.newBlock('different_cases');824 const differentCases2 = this.workspace.newBlock('different_cases');825 const dogIn = dog.getInput('INPUT1').connection;826 const differentCases1Out = differentCases1.outputConnection;827 const differentCases1In = differentCases1.getInput('INPUT1').connection;828 const differentCases2Out = differentCases2.outputConnection;829 // For logging.830 dogIn.name = 'dogIn';831 differentCases1Out.name = 'differentCases1Out';832 differentCases1In.name = 'differentCases1In';833 differentCases2Out.name = 'differentCases2Out';834 dogIn.connect(differentCases1Out);835 differentCases1In.connect(differentCases2Out);836 this.assertHasType(differentCases2Out, 'dog');837 });838 });839 suite('Update on connect', function() {840 suite('Single explicit', function() {841 clearThreeBlockTests();842 threeBlockTest('Connect inner explicit last', function() {843 const tIn = this.getOuterInput('t');844 const t = this.getMain('t');845 const dogOut = this.getInnerOutput('dog');846 tIn.connect(t.out);847 t.in.connect(dogOut);848 this.assertHasType(tIn, 'dog');849 });850 threeBlockTest('Connect inner explicit first', function() {851 const tIn = this.getOuterInput('t');852 const t = this.getMain('t');853 const dogOut = this.getInnerOutput('dog');854 t.in.connect(dogOut);855 tIn.connect(t.out);856 this.assertHasType(tIn, 'dog');857 });858 threeBlockTest('Connect outer explicit last', function() {859 const dogIn = this.getOuterInput('dog');860 const t = this.getMain('t');861 const tOut = this.getInnerOutput('t');862 t.in.connect(tOut);863 dogIn.connect(t.out);864 this.assertHasType(tOut, 'dog');865 });866 threeBlockTest('Connect outer explicit first', function() {867 const dogIn = this.getOuterInput('dog');868 const t = this.getMain('t');869 const tOut = this.getInnerOutput('t');870 dogIn.connect(t.out);871 t.in.connect(tOut);872 this.assertHasType(tOut, 'dog');873 });874 runThreeBlockTests();875 });876 suite('Outer and inner explicit', function() {877 clearThreeBlockTests();878 threeBlockTest('Connect inner last', function() {879 const mammalIn = this.getOuterInput('mammal');880 const t = this.getMain('t');881 const dogOut = this.getInnerOutput('dog');882 mammalIn.connect(t.out);883 t.in.connect(dogOut);884 this.assertHasType(t.out, 'mammal');885 });886 threeBlockTest('Connect outer last', function() {887 const mammalIn = this.getOuterInput('mammal');888 const t = this.getMain('t');889 const dogOut = this.getInnerOutput('dog');890 t.in.connect(dogOut);891 mammalIn.connect(t.out);892 this.assertHasType(t.out, 'mammal');893 });894 runThreeBlockTests();895 });896 });897 suite('Update on disconnect', function() {898 suite('Single explicit', function() {899 clearThreeBlockTests();900 threeBlockTest('Inner explicit', function() {901 const tIn = this.getOuterInput('t');902 const t = this.getMain('t');903 const dogOut = this.getInnerOutput('dog');904 tIn.connect(t.out);905 t.in.connect(dogOut);906 t.in.disconnect();907 this.assertNoType(tIn);908 });909 threeBlockTest('Outer explicit', function() {910 const dogIn = this.getOuterInput('dog');911 const t = this.getMain('t');912 const tOut = this.getInnerOutput('t');913 t.in.connect(tOut);914 dogIn.connect(t.out);915 dogIn.disconnect();916 this.assertNoType(tOut);917 });918 runThreeBlockTests();919 });920 suite('Outer and inner explicit', function() {921 clearThreeBlockTests();922 threeBlockTest('Disconnect inner', function() {923 this.mammalIn = this.getOuterInput('mammal');924 this.t = this.getMain('t');925 this.dogOut = this.getInnerOutput('dog');926 this.mammalIn.connect(this.t.out);927 this.t.in.connect(this.dogOut);928 this.mammalIn.disconnect();929 this.assertHasType(this.t.out, 'dog');930 });931 threeBlockTest('Disconnect outer', function() {932 this.mammalIn = this.getOuterInput('mammal');933 this.t = this.getMain('t');934 this.dogOut = this.getInnerOutput('dog');935 this.mammalIn.connect(this.t.out);936 this.t.in.connect(this.dogOut);937 this.dogOut.disconnect();938 this.assertHasType(this.t.out, 'mammal');939 });940 runThreeBlockTests();941 });942 });943 suite('Update on bind', function() {944 clearThreeBlockTests();945 threeBlockTest('Override outer', function() {946 const mammalIn = this.getOuterInput('mammal');947 const t = this.getMain('t');948 mammalIn.connect(t.out);949 this.bindConnection(t.out, 'dog');950 this.assertHasType(t.out, 'dog');951 });952 threeBlockTest('Override outer w/ inner', function() {953 const mammalIn = this.getOuterInput('mammal');954 const t = this.getMain('t');955 const dogOut = this.getInnerOutput('dog');956 mammalIn.connect(t.out);957 t.in.connect(dogOut);958 this.bindConnection(t.out, 'dog');959 this.assertHasType(t.out, 'dog');960 });961 threeBlockTest('Override inner', function() {962 const t = this.getMain('t');963 const dogOut = this.getInnerOutput('dog');964 t.in.connect(dogOut);965 this.bindConnection(t.out, 'mammal');966 this.assertHasType(t.out, 'mammal');967 });968 runThreeBlockTests();969 });970 suite('Update on unbind', function() {971 clearThreeBlockTests();972 threeBlockTest('Stop overriding outer', function() {973 const mammalIn = this.getOuterInput('mammal');974 const t = this.getMain('t');975 mammalIn.connect(t.out);976 this.bindConnection(t.out, 'dog');977 this.unbindConnection(t.out);978 this.assertHasType(t.out, 'mammal');979 });980 threeBlockTest('Stop overriding outer w/ inner', function() {981 const mammalIn = this.getOuterInput('mammal');982 const t = this.getMain('t');983 const dogOut = this.getInnerOutput('dog');984 mammalIn.connect(t.out);985 t.in.connect(dogOut);986 this.bindConnection(t.out, 'dog');987 this.unbindConnection(t.out);988 this.assertHasType(t.out, 'mammal');989 });990 threeBlockTest('Stop overriding inner', function() {991 const t = this.getMain('t');992 const dogOut = this.getInnerOutput('dog');993 t.in.connect(dogOut);994 this.bindConnection(t.out, 'mammal');995 this.unbindConnection(t.out);996 this.assertHasType(t.out, 'dog');997 });998 runThreeBlockTests();999 });1000 suite('Unification', function() {1001 suite('Inputs', function() {1002 clearSiblingTests();1003 siblingTest('Direct children', function() {1004 const t = this.getMain('t');1005 const dogOut = this.getInnerOutput('dog');1006 const catOut = this.getInnerOutput('cat');1007 this.bindConnection(t.out, 'mammal');1008 t.in1.connect(dogOut);1009 t.in2.connect(catOut);1010 this.unbindConnection(t.out);1011 this.assertHasType(t.out, 'mammal');1012 });1013 siblingTest('Grandchildren', function() {1014 const t1 = this.getMain('t', 't1');1015 const t2 = this.getMain('t', 't2');1016 const t3 = this.getMain('t', 't3');1017 const dogOut = this.getInnerOutput('dog');1018 const catOut = this.getInnerOutput('cat');1019 this.bindConnection(t1.out, 'mammal');1020 t1.in1.connect(t2.out);1021 t1.in2.connect(t3.out);1022 t2.in1.connect(dogOut);1023 t3.in1.connect(catOut);1024 this.unbindConnection(t1.out);1025 this.assertHasType(t1.out, 'mammal');1026 });1027 siblingTest('Children and grandchildren', function() {1028 const t1 = this.getMain('t', 't1');1029 const t2 = this.getMain('t', 't2');1030 const dogOut = this.getInnerOutput('dog');1031 const catOut = this.getInnerOutput('cat');1032 this.bindConnection(t1.out, 'mammal');1033 t1.in1.connect(t2.out);1034 t1.in2.connect(catOut);1035 t2.in1.connect(dogOut);1036 this.unbindConnection(t1.out);1037 this.assertHasType(t1.out, 'mammal');1038 });1039 runSiblingTests();1040 });1041 suite('Outputs', function() {1042 clearSiblingTests();1043 siblingTest('Siblings', function() {1044 const t = this.getMain('t');1045 const dogOut = this.getInnerOutput('dog');1046 const catOut = this.getInnerOutput('cat');1047 const tOut = this.getInnerOutput('t');1048 this.bindConnection(t.out, 'mammal');1049 t.in1.connect(dogOut);1050 t.in2.connect(catOut);1051 t.in3.connect(tOut);1052 this.unbindConnection(t.out);1053 this.assertHasType(tOut, 'mammal');1054 });1055 siblingTest('Parsibs', function() {1056 const t1 = this.getMain('t', 't1');1057 const t2 = this.getMain('t', 't2');1058 const dogOut = this.getInnerOutput('dog');1059 const catOut = this.getInnerOutput('cat');1060 const tOut = this.getInnerOutput('t');1061 this.bindConnection(t1.out, 'mammal');1062 t1.in1.connect(dogOut);1063 t1.in2.connect(catOut);1064 t1.in3.connect(t2.out);1065 t2.in1.connect(tOut);1066 this.unbindConnection(t1.out);1067 this.assertHasType(tOut, 'mammal');1068 });1069 siblingTest('Siblings and parsibs', function() {1070 const t1 = this.getMain('t', 't1');1071 const t2 = this.getMain('t', 't2');1072 const dogOut = this.getInnerOutput('dog');1073 const catOut = this.getInnerOutput('cat');1074 const tOut = this.getInnerOutput('t');1075 this.bindConnection(t1.out, 'mammal');1076 t1.in1.connect(dogOut);1077 t1.in2.connect(t2.out);1078 t2.in1.connect(catOut);1079 t2.in2.connect(tOut);1080 this.unbindConnection(t1.out);1081 this.assertHasType(tOut, 'mammal');1082 });1083 runSiblingTests();1084 });1085 });...
FollowMedia.js
Source:FollowMedia.js
...74 sinaadToolkit.event.on(mainCloseBtn, 'click', this.getCloseMainHandler());75 76 var mainContent = this.mainContent = document.createElement('div');77 var miniContent = this.miniContent = document.createElement('div');78 main.getMain().appendChild(mainContent);79 main.getMain().appendChild(mainCloseBtn);80 mini.getMain().appendChild(miniContent);81 mini.getMain().appendChild(miniReplayBtn);82 mini.getMain().appendChild(miniCloseBtn);83 // document.body.insertBefore(main.getMain(), document.body.firstChild);84 // document.body.insertBefore(mini.getMain(), document.body.firstChild);85 if (this.delay) {86 setTimeout(function () {87 THIS.show();88 }, this.delay * 1000);89 } else {90 this.show();91 }92 }93 FollowMedia.prototype = {94 timer : null,95 show : function () {96 var THIS = this,97 config = this.config;98 clearTimeout(this.timer);...
getters.js
Source:getters.js
1import { formatAMPM, formatTempKtoC, formatTempKtoF, formatTextPascal, convertHPatoKPa } from '../utility'2export const getters = {3 getMain: (state) => {4 return (state.data) ? state.data.main : {}5 },6 getTempUnit: (state) => {7 return state.tempUnit8 },9 getTemp: (state, getters) => {10 if (getters.getTempUnit === 'C') {11 return (getters.getMain) ? formatTempKtoC(getters.getMain.temp) : ''12 }13 return (getters.getMain) ? formatTempKtoF(getters.getMain.temp) : ''14 },15 getSys: (state) => {16 return (state.data) ? state.data.sys : {}17 },18 getSunriseSunset: (state, getters) => {19 return (getters.getSys && getters.getSys.sunrise && getters.getSys.sunset)20 ? {sunrise: formatAMPM(getters.getSys.sunrise * 1000), sunset: formatAMPM(getters.getSys.sunset * 1000)} : {sunrise: '-', sunset: '-'}21 },22 getMaxMinTemp: (state, getters) => {23 if (getters.getTempUnit === 'C') {24 return (getters.getMain)25 ? {tempMax: formatTempKtoC(getters.getMain.temp_max), tempMin: formatTempKtoC(getters.getMain.temp_min)} : {tempMax: '', tempMin: ''}26 }27 return (getters.getMain)28 ? {tempMax: formatTempKtoF(getters.getMain.temp_max), tempMin: formatTempKtoF(getters.getMain.temp_min)} : {tempMax: '', tempMin: ''}29 },30 getHumidity: (state, getters) => {31 return (getters.getMain) ? getters.getMain.humidity : ''32 },33 getPressure: (state, getters) => {34 return (getters.getMain) ? convertHPatoKPa(getters.getMain.pressure) : ''35 },36 getWeather: (state) => {37 return (state.data && state.data.weather && state.data.weather.length > 0) ? state.data.weather[0] : null38 },39 getTempDescr: (state, getters) => {40 return (getters && getters.getWeather) ? formatTextPascal(getters.getWeather.description) : ''41 },42 getTempId: (state, getters) => {43 return (getters && getters.getWeather) ? getters.getWeather.id : ''44 },45 getWindSpeed: (state) => {46 return (state.data && state.data.wind) ? state.data.wind.speed : ''47 },48 getDefaultCity: (state) => {49 return state.defaultCity50 },51 getCityName: (state) => {52 return (state.data) ? state.data.name : ''53 },54 getForecastWeather: (state) => {55 return (state.forecast && state.forecast.list) ? state.forecast.list : []56 },57 getSelectedDate: (state) => {58 return state.selectedDate59 }...
main.js
Source:main.js
1const getmain =Ñ => {2 return document.getElementById(Ñ);3 }4 5 var arrow = getmain('turn-arrow');6 var turn = true;7 var effectTime = 500;8 9 const pikachu = {10 name: 'pikachu',11 baseHP: 100,12 realHP: 100,13 textHP: getmain('health-character'),14 barHP: getmain('progressbar-character'),15 container: document.getElementsByClassName('character')[0]16 }17 18 const charmander = {19 name: 'charmander',20 baseHP: 100,21 realHP: 100,22 textHP: getmain('health-enemy'),23 barHP: getmain('progressbar-enemy'),24 container: document.getElementsByClassName('enemy')[0]25 }26 getmain('enemy-btn-kick').addEventListener('click', function() {27 if (!turn) {28 dealDamage(pikachu);29 } 30 });31 getmain('character-btn-kick').addEventListener('click', function() {32 if (turn) {33 dealDamage(charmander);34 } 35 });36 37 38 const updateHP = player => {39 player.textHP.innerText = player.realHP + ' / ' + player.baseHP;40 player.barHP.style.width = player.realHP + '%';41 player.container.prepend(arrow);42 }43 44 const victory = () => {45 var player;46 if (!turn) player = pikachu;47 else player = charmander;48 getmain('victory-screen').style.display = 'flex';49 getmain('victory-text').innerText = 'Winner ' + player.name;50 getmain('victory-image').src = player.container.getElementsByClassName('sprite')[0].src;51 }52 53 const dealDamage = player => {54 turn = !turn;55 var damage = getRand(100);56 player.realHP = player.realHP - damage;57 var dmgEl = document.createElement('span');58 dmgEl.innerText = '-' + damage;59 dmgEl.classList.add('damage');60 player.container.parentElement.appendChild(dmgEl);61 setTimeout(function()62 {63 player.container.style.animation = "";64 dmgEl.remove();65 }, effectTime);66 if (player.realHP < 0) {67 player.realHP = 0;68 victory();69 }70 updateHP(player);71 }72 73 74 75 getmain('restart').addEventListener('click', function() 76 {77 turn = true;78 pikachu.realHP = pikachu.baseHP;79 charmander.realHP = charmander.baseHP;80 updateHP(charmander); updateHP(pikachu);81 getmain('victory-screen').style.display = 'none';82 });83 const getRand = max => {84 return Math.floor(Math.random()*max);...
PANE.js
Source:PANE.js
...17 this.implement(new Swipeable(this, parseInt(getComputedStyle(this.el).width) * 0.85, true, true));18 this.swipeUp = () => {19 console.log('swipeUp');20 let { navheader } = this.getContainer();21 this.getMain().focusBody().then(() => {22 if (this.getContainer().body.hasClass('active')) {23 if (navheader.hasClass('active')) {24 navheader.el.dispatchEvent(new Deactivate(navheader));25 }26 }27 });28 };29 this.swipeDown = () => {30 console.log('swipeDown');31 let { navheader } = this.getContainer();32 this.getMain().focusBody().then(() => {33 if (this.getContainer().body.hasClass('active')) {34 if (!navheader.hasClass('active')) {35 navheader.el.dispatchEvent(new Activate(navheader));36 }37 }38 });39 };40 this.swipeLeft = () => {41 let [navMenu] = this.getMain().navheader.tabs.get('sidebar-user', 'NAVITEMICON');42 navMenu.el.dispatchEvent(new Activate(navMenu));43 };44 this.swipeRight = () => {45 let [navMenu] = this.getMain().navheader.tabs.get('document-map', 'NAVITEMICON');46 navMenu.el.dispatchEvent(new Activate(navMenu));47 };48 }49}...
openweather_data_model.js
Source:openweather_data_model.js
...23 }24 getWeatherDescription() {25 return this.getWeather().description;26 }27 getMain() {28 return this.json.main;29 }30 getMainTemp() {31 return Math.round((this.getMain().temp - 273.15) * 100) / 100;32 }33 getMainMin() {34 return Math.round((this.getMain().temp_min - 273.15) * 100) / 100;35 }36 getMainMax() {37 return Math.round((this.getMain().temp_max - 273.15) * 100) / 100;38 }39 getMainHumidity() {40 return this.getMain().humidity;41 }42 getWind() {43 return this.json.wind;44 }45 getWindSpeed() {46 return this.getWind().speed;47 }48 getCloudVisibility() {49 return this.json.clouds.all;50 }51 getDateTimeOfMeasurement() {52 let time = new Date(this.json.dt * 1000);53 return Time_formatter.getTimeOnly(time);54 }...
ui.js
Source:ui.js
1function getMain(){2 return chrome.extension.getBackgroundPage().main;3}4function onClick(ind,event){5 window_close();6 if(event){7 if(event.button==0&&event.shiftKey||event.button==1){8 getMain().onClickAccount(ind,true);9 return;10 }11 }12 getMain().onClickAccount(ind);13}14function checkNow(){15 window_close();16 getMain().checkNow();17}18function openAll(){19 window_close();20 getMain().openViews();21}22function openOptions(){23 var url=chrome.extension.getURL("options.html");24 chrome.tabs.query({}, function (tabs) {25 for(var i in tabs){26 var tab=tabs[i];27 if(tab.url==url){28 chrome.tabs.update(tab.id, {selected: true});29 return;30 }31 }32 chrome.tabs.create({url: "options.html",selected: true});33 });34}35function openHelp(){36 window_close();37 if(event.ctrlKey&&event.shiftKey){38 getMain().toggleDebug();39 return;40 }41 chrome.tabs.create({url: "about.html",selected: true});42}43function window_close(){44 window.close();45}46function restart(){47 chrome.extension.getBackgroundPage().init();48 document.location.reload();...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!