|
| 1 | +import type { SFCScriptBlock } from '@vue/compiler-sfc' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { checkAmountOfComments, reportAmountOfComments } from './amountOfComments' |
| 4 | + |
| 5 | +describe('checkAmountOfComments', () => { |
| 6 | + it('should not report files with comments less than 30% of total content', () => { |
| 7 | + const script = { |
| 8 | + content: ` |
| 9 | + const x = 1; |
| 10 | + const y = 2; |
| 11 | + const z = 3; |
| 12 | + // This is a short comment |
| 13 | + const result = x + y + z; |
| 14 | + `, |
| 15 | + } as SFCScriptBlock |
| 16 | + const fileName = 'amounOfComments.vue' |
| 17 | + checkAmountOfComments(script, fileName) |
| 18 | + const result = reportAmountOfComments() |
| 19 | + expect(result.length).toBe(0) |
| 20 | + expect(result).toStrictEqual([]) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should not report files with no comments', () => { |
| 24 | + const script = { |
| 25 | + content: ` |
| 26 | + const x = 1; |
| 27 | + const y = 2; |
| 28 | + const z = x + y; |
| 29 | + `, |
| 30 | + } as SFCScriptBlock |
| 31 | + const fileName = 'noComments.vue' |
| 32 | + checkAmountOfComments(script, fileName) |
| 33 | + const result = reportAmountOfComments() |
| 34 | + expect(result.length).toBe(0) |
| 35 | + expect(result).toStrictEqual([]) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should report files with comments more than 30% of total content', () => { |
| 39 | + const script = { |
| 40 | + content: ` |
| 41 | + const x = 1; |
| 42 | + // This is a long comment |
| 43 | + // that spans multiple lines |
| 44 | + // and takes up more than 30% |
| 45 | + // of the total content |
| 46 | + // in this script block |
| 47 | + const y = 2; |
| 48 | + `, |
| 49 | + } as SFCScriptBlock |
| 50 | + const fileName = 'amounOfComments-problem.vue' |
| 51 | + checkAmountOfComments(script, fileName) |
| 52 | + const result = reportAmountOfComments() |
| 53 | + expect(result.length).toBe(1) |
| 54 | + expect(result).toStrictEqual([ |
| 55 | + { |
| 56 | + file: fileName, |
| 57 | + rule: `<text_info>rrd ~ amount of comments</text_info>`, |
| 58 | + description: `👉 <text_warn>lots of comments found</text_warn> See: https://vue-mess-detector.webmania.cc/rules/rrd/amount-of-comments.html`, |
| 59 | + message: `The script block contains 62.14% comments, which exceeds the limit of 30%. 🚨`, |
| 60 | + }, |
| 61 | + ]) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should report files with comments more than 50% of total content', () => { |
| 65 | + const script = { |
| 66 | + content: ` |
| 67 | + <script setup> |
| 68 | + /* |
| 69 | + Emiting a Policy (3/4) |
| 70 | + Third, just run the necessary endpoints... |
| 71 | + and trigger policy's action modal |
| 72 | + */ |
| 73 | + function finallyEmiting() { |
| 74 | + if (quote.value.isEmitted) { |
| 75 | + isPolicyActionsOpen.value = true; |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + /* |
| 81 | + Emiting a Policy (4/4) |
| 82 | + Lastly, download your certificates and decide where to go next |
| 83 | + */ |
| 84 | + function afterParty() {} |
| 85 | + </script> |
| 86 | + `, |
| 87 | + } as SFCScriptBlock |
| 88 | + const fileName = 'amounOfComments-problem.vue' |
| 89 | + checkAmountOfComments(script, fileName) |
| 90 | + const result = reportAmountOfComments() |
| 91 | + expect(result.length).toBe(1) |
| 92 | + expect(result).toStrictEqual([ |
| 93 | + { |
| 94 | + file: fileName, |
| 95 | + rule: `<text_info>rrd ~ amount of comments</text_info>`, |
| 96 | + description: `👉 <text_warn>lots of comments found</text_warn> See: https://vue-mess-detector.webmania.cc/rules/rrd/amount-of-comments.html`, |
| 97 | + message: `The script block contains 51.47% comments, which exceeds the limit of 30%. 🚨`, |
| 98 | + }, |
| 99 | + ]) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should report files with only comments', () => { |
| 103 | + const script = { |
| 104 | + content: ` |
| 105 | + // This file contains |
| 106 | + // only comments |
| 107 | + /* |
| 108 | + Multi-line comment |
| 109 | + test case |
| 110 | + */ |
| 111 | + `, |
| 112 | + } as SFCScriptBlock |
| 113 | + const fileName = 'onlyComments.vue' |
| 114 | + checkAmountOfComments(script, fileName) |
| 115 | + const result = reportAmountOfComments() |
| 116 | + expect(result.length).toBe(1) |
| 117 | + expect(result).toStrictEqual([ |
| 118 | + { |
| 119 | + file: fileName, |
| 120 | + rule: `<text_info>rrd ~ amount of comments</text_info>`, |
| 121 | + description: `👉 <text_warn>lots of comments found</text_warn> See: https://vue-mess-detector.webmania.cc/rules/rrd/amount-of-comments.html`, |
| 122 | + message: `The script block contains 84.75% comments, which exceeds the limit of 30%. 🚨`, |
| 123 | + }, |
| 124 | + ]) |
| 125 | + }) |
| 126 | +}) |
0 commit comments