1'use strict'; 2 3// Exercise coverage of a class. Note, this logic is silly and exists solely 4// to generate branch coverage code paths: 5class CoveredClass { 6 constructor(x, y, opts) { 7 this.x = x; 8 this.y = y; 9 // Exercise coverage of nullish coalescing: 10 this.opts = opts ?? (Math.random() > 0.5 ? {} : undefined); 11 } 12 add() { 13 return this.x + this.y; 14 } 15 addSpecial() { 16 // Exercise coverage of optional chains: 17 if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) { 18 return this.opts.special.x + this.opts.special.y; 19 } 20 return add(); 21 } 22 mult() { 23 return this.x * this.y; 24 } 25 multSpecial() { 26 if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) { 27 return this.opts.special.x * this.opts.special.y; 28 } 29 return mult(); 30 } 31} 32 33// Excercise coverage of functions: 34function add(x, y) { 35 const mt = new CoveredClass(x, y); 36 return mt.add(); 37} 38 39function addSpecial(x, y) { 40 let mt; 41 if (Math.random() > 0.5) { 42 mt = new CoveredClass(x, y); 43 } else { 44 mt = new CoveredClass(x, y, { 45 special: { 46 x: Math.random() * x, 47 y: Math.random() * y 48 } 49 }); 50 } 51 return mt.addSpecial(); 52} 53 54function mult(x, y) { 55 const mt = new CoveredClass(x, y); 56 return mt.mult(); 57} 58 59function multSpecial(x, y) { 60 let mt; 61 if (Math.random() > 0.5) { 62 mt = new CoveredClass(x, y); 63 } else { 64 mt = new CoveredClass(x, y, { 65 special: { 66 x: Math.random() * x, 67 y: Math.random() * y 68 } 69 }); 70 } 71 return mt.multSpecial(); 72} 73 74for (let i = 0; i < parseInt(process.env.N); i++) { 75 const operations = ['add', 'addSpecial', 'mult', 'multSpecial']; 76 for (const operation of operations) { 77 // Exercise coverage of switch statements: 78 switch (operation) { 79 case 'add': 80 if (add(Math.random() * 10, Math.random() * 10) > 10) { 81 // Exercise coverage of ternary operations: 82 let r = addSpecial(Math.random() * 10, Math.random() * 10) > 10 ? 83 mult(Math.random() * 10, Math.random() * 10) : 84 add(Math.random() * 10, Math.random() * 10); 85 // Exercise && and || 86 if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; 87 } 88 break; 89 case 'addSpecial': 90 if (addSpecial(Math.random() * 10, Math.random() * 10) > 10 && 91 add(Math.random() * 10, Math.random() * 10) > 10) { 92 let r = mult(Math.random() * 10, Math.random() * 10) > 10 ? 93 add(Math.random() * 10, Math.random() * 10) > 10 : 94 mult(Math.random() * 10, Math.random() * 10); 95 if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; 96 } 97 break; 98 case 'mult': 99 if (mult(Math.random() * 10, Math.random() * 10) > 10) { 100 let r = multSpecial(Math.random() * 10, Math.random() * 10) > 10 ? 101 add(Math.random() * 10, Math.random() * 10) : 102 mult(Math.random() * 10, Math.random() * 10); 103 if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; 104 } 105 break; 106 case 'multSpecial': 107 while (multSpecial(Math.random() * 10, Math.random() * 10) < 10) { 108 mult(Math.random() * 10, Math.random() * 10); 109 } 110 break; 111 default: 112 break; 113 } 114 } 115} 116