|
| 1 | +/** |
| 2 | + * @fileoverview enforce a given pattern for comments |
| 3 | + * @author Clément P. |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../rules/comments-sentences'); |
| 12 | +const RuleTester = require("eslint").RuleTester; |
| 13 | + |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +// Helpers |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +const SPACE_ERROR_MESSAGE = 'Not starting by a single space'; |
| 20 | +const UPPERCASE_ERROR_MESSAGE = 'Not starting by an uppercase letter while it should'; |
| 21 | +const LOWERCASE_ERROR_MESSAGE = 'Starting by an uppercase letter while it should not'; |
| 22 | +const PUNCTUATION_ERROR_MESSAGE = 'Line does not end by a punctuation'; |
| 23 | + |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | +// Tests |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | + |
| 28 | +const ruleTester = new RuleTester(); |
| 29 | +ruleTester.run('comments-sentences', rule, { |
| 30 | + valid: [ |
| 31 | + '// This is a valid comment.', |
| 32 | + '// 29 is a valid comment.', |
| 33 | + '// @description This is a valid comment.', |
| 34 | + '//@description This is a valid comment.', |
| 35 | + '// @description This is a valid comment', |
| 36 | + '// "This" is a valid comment.', |
| 37 | + '// \'This\' is a valid comment.', |
| 38 | + '// +42 is a valid comment.', |
| 39 | + '// _myVar is a valid comment.', |
| 40 | + '/*\n * This is a valid comment.\n */', |
| 41 | + '/*\n * This is a valid comment.\n * That has multiple sentences.\n */', |
| 42 | + '/**\n * This is a valid comment.\n */', |
| 43 | + '/**\n * This is a valid comment.\n * That has multiple sentences.\n */', |
| 44 | + '/**\n * This is a valid comment,\n * with one long sentence.\n */', |
| 45 | + '/*\n * This is a valid comment,\n * with one long sentence.\n */', |
| 46 | + ], |
| 47 | + |
| 48 | + invalid: [{ |
| 49 | + code: '// this is not a valid comment.', |
| 50 | + errors: [{ |
| 51 | + message: UPPERCASE_ERROR_MESSAGE, |
| 52 | + }], |
| 53 | + }, { |
| 54 | + code: '// This is not a valid comment', |
| 55 | + errors: [{ |
| 56 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 57 | + }], |
| 58 | + }, { |
| 59 | + code: '//This is not a valid comment.', |
| 60 | + errors: [{ |
| 61 | + message: SPACE_ERROR_MESSAGE, |
| 62 | + }], |
| 63 | + }, { |
| 64 | + code: '//this is not a valid comment', |
| 65 | + errors: [{ |
| 66 | + message: SPACE_ERROR_MESSAGE, |
| 67 | + }], |
| 68 | + }, { |
| 69 | + code: '// this is not a valid comment', |
| 70 | + errors: [{ |
| 71 | + message: UPPERCASE_ERROR_MESSAGE, |
| 72 | + }], |
| 73 | + }, { |
| 74 | + code: '// 29 is not a valid comment', |
| 75 | + errors: [{ |
| 76 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 77 | + }], |
| 78 | + }, { |
| 79 | + code: '// +42 is not a valid comment', |
| 80 | + errors: [{ |
| 81 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 82 | + }], |
| 83 | + }, { |
| 84 | + code: '//_myVar is not a valid comment', |
| 85 | + errors: [{ |
| 86 | + message: SPACE_ERROR_MESSAGE, |
| 87 | + }], |
| 88 | + }, { |
| 89 | + code: '//"This" is not a valid comment', |
| 90 | + errors: [{ |
| 91 | + message: SPACE_ERROR_MESSAGE, |
| 92 | + }], |
| 93 | + }, { |
| 94 | + code: '// \'This\' is not a valid comment', |
| 95 | + errors: [{ |
| 96 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 97 | + }], |
| 98 | + }, { |
| 99 | + code: '// \'This\' is not a valid comment,\n// that spans on multi-line.', |
| 100 | + errors: [{ |
| 101 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 102 | + }, { |
| 103 | + message: UPPERCASE_ERROR_MESSAGE, |
| 104 | + }], |
| 105 | + }, { |
| 106 | + code: '/* 42 is not a valid comment,\n * That spans on multi-line.\n */', |
| 107 | + errors: [{ |
| 108 | + message: LOWERCASE_ERROR_MESSAGE, |
| 109 | + }], |
| 110 | + }, { |
| 111 | + code: '/* _myVar is not a valid comment.\n * that spans on multi-line.\n */', |
| 112 | + errors: [{ |
| 113 | + message: UPPERCASE_ERROR_MESSAGE, |
| 114 | + }], |
| 115 | + }, { |
| 116 | + code: '/* This is not a valid comment.\n * It spans on multi-line\n */', |
| 117 | + errors: [{ |
| 118 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 119 | + }], |
| 120 | + }, { |
| 121 | + code: '/* This is not a valid comment,\n * it spans on multi-line\n */', |
| 122 | + errors: [{ |
| 123 | + message: PUNCTUATION_ERROR_MESSAGE, |
| 124 | + }], |
| 125 | + }, { |
| 126 | + code: '/* This is not a valid comment,\n *it spans on multi-line.\n */', |
| 127 | + errors: [{ |
| 128 | + message: SPACE_ERROR_MESSAGE, |
| 129 | + }], |
| 130 | + }, { |
| 131 | + code: '/* This is not a valid comment.\n *Tt spans on multi-line.\n */', |
| 132 | + errors: [{ |
| 133 | + message: SPACE_ERROR_MESSAGE, |
| 134 | + }], |
| 135 | + }, ], |
| 136 | +}); |
0 commit comments