|
| 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: |
| 5 | +classCoveredClass{ |
| 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 | +returnthis.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 | +returnthis.opts.special.x+this.opts.special.y; |
| 19 | +} |
| 20 | +returnadd(); |
| 21 | +} |
| 22 | +mult(){ |
| 23 | +returnthis.x*this.y; |
| 24 | +} |
| 25 | +multSpecial(){ |
| 26 | +if(this.opts?.special&&this.opts?.special?.x&&this.opts?.special?.y){ |
| 27 | +returnthis.opts.special.x*this.opts.special.y; |
| 28 | +} |
| 29 | +returnmult(); |
| 30 | +} |
| 31 | +} |
| 32 | + |
| 33 | +// Excercise coverage of functions: |
| 34 | +functionadd(x,y){ |
| 35 | +constmt=newCoveredClass(x,y); |
| 36 | +returnmt.add(); |
| 37 | +} |
| 38 | + |
| 39 | +functionaddSpecial(x,y){ |
| 40 | +letmt; |
| 41 | +if(Math.random()>0.5){ |
| 42 | +mt=newCoveredClass(x,y); |
| 43 | +}else{ |
| 44 | +mt=newCoveredClass(x,y,{ |
| 45 | +special: { |
| 46 | +x: Math.random()*x, |
| 47 | +y: Math.random()*y |
| 48 | +} |
| 49 | +}); |
| 50 | +} |
| 51 | +returnmt.addSpecial(); |
| 52 | +} |
| 53 | + |
| 54 | +functionmult(x,y){ |
| 55 | +constmt=newCoveredClass(x,y); |
| 56 | +returnmt.mult(); |
| 57 | +} |
| 58 | + |
| 59 | +functionmultSpecial(x,y){ |
| 60 | +letmt; |
| 61 | +if(Math.random()>0.5){ |
| 62 | +mt=newCoveredClass(x,y); |
| 63 | +}else{ |
| 64 | +mt=newCoveredClass(x,y,{ |
| 65 | +special: { |
| 66 | +x: Math.random()*x, |
| 67 | +y: Math.random()*y |
| 68 | +} |
| 69 | +}); |
| 70 | +} |
| 71 | +returnmt.multSpecial(); |
| 72 | +} |
| 73 | + |
| 74 | +for(leti=0;i<parseInt(process.env.N);i++){ |
| 75 | +constoperations=['add','addSpecial','mult','multSpecial']; |
| 76 | +for(constoperationofoperations){ |
| 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 | +letr=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 | +letr=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 | +letr=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 | +} |
0 commit comments