|
| 1 | +/** |
| 2 | + * @fileoverview Tests for no-unescaped-entities |
| 3 | + * @author Patrick Hayes |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/no-unescaped-entities'); |
| 12 | +var RuleTester = require('eslint').RuleTester; |
| 13 | +var parserOptions = { |
| 14 | + ecmaFeatures: { |
| 15 | + jsx: true |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | +// Tests |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +var ruleTester = new RuleTester(); |
| 24 | +ruleTester.run('no-unescaped-entities', rule, { |
| 25 | + |
| 26 | + valid: [ |
| 27 | + { |
| 28 | + code: [ |
| 29 | + 'var Hello = React.createClass({', |
| 30 | + ' render: function() {', |
| 31 | + ' return (', |
| 32 | + ' <div/>', |
| 33 | + ' );', |
| 34 | + ' }', |
| 35 | + '});' |
| 36 | + ].join('\n'), |
| 37 | + parserOptions: parserOptions |
| 38 | + }, { |
| 39 | + code: [ |
| 40 | + 'var Hello = React.createClass({', |
| 41 | + ' render: function() {', |
| 42 | + ' return <div>Here is some text!</div>;', |
| 43 | + ' }', |
| 44 | + '});' |
| 45 | + ].join('\n'), |
| 46 | + parserOptions: parserOptions |
| 47 | + }, { |
| 48 | + code: [ |
| 49 | + 'var Hello = React.createClass({', |
| 50 | + ' render: function() {', |
| 51 | + ' return <div>I’ve escaped some entities: > < &</div>;', |
| 52 | + ' }', |
| 53 | + '});' |
| 54 | + ].join('\n'), |
| 55 | + parserOptions: parserOptions |
| 56 | + }, { |
| 57 | + code: [ |
| 58 | + 'var Hello = React.createClass({', |
| 59 | + ' render: function() {', |
| 60 | + ' return <div>first line is ok', |
| 61 | + ' so is second', |
| 62 | + ' and here are some escaped entities: > < &</div>;', |
| 63 | + ' }', |
| 64 | + '});' |
| 65 | + ].join('\n'), |
| 66 | + parserOptions: parserOptions |
| 67 | + }, { |
| 68 | + code: [ |
| 69 | + 'var Hello = React.createClass({', |
| 70 | + ' render: function() {', |
| 71 | + ' return <div>{">" + "<" + "&" + \'"\'}</div>;', |
| 72 | + ' },', |
| 73 | + '});' |
| 74 | + ].join('\n'), |
| 75 | + parserOptions: parserOptions |
| 76 | + } |
| 77 | + ], |
| 78 | + |
| 79 | + invalid: [ |
| 80 | + { |
| 81 | + code: [ |
| 82 | + 'var Hello = React.createClass({', |
| 83 | + ' render: function() {', |
| 84 | + ' return <div>></div>;', |
| 85 | + ' }', |
| 86 | + '});' |
| 87 | + ].join('\n'), |
| 88 | + parserOptions: parserOptions, |
| 89 | + errors: [{message: 'HTML entities must be escaped.'}] |
| 90 | + }, { |
| 91 | + code: [ |
| 92 | + 'var Hello = React.createClass({', |
| 93 | + ' render: function() {', |
| 94 | + ' return <div>first line is ok', |
| 95 | + ' so is second', |
| 96 | + ' and here are some bad entities: ></div>', |
| 97 | + ' }', |
| 98 | + '});' |
| 99 | + ].join('\n'), |
| 100 | + parserOptions: parserOptions, |
| 101 | + errors: [{message: 'HTML entities must be escaped.'}] |
| 102 | + }, { |
| 103 | + code: [ |
| 104 | + 'var Hello = React.createClass({', |
| 105 | + ' render: function() {', |
| 106 | + ' return <div>\'</div>;', |
| 107 | + ' }', |
| 108 | + '});' |
| 109 | + ].join('\n'), |
| 110 | + parserOptions: parserOptions, |
| 111 | + errors: [{message: 'HTML entities must be escaped.'}] |
| 112 | + }, { |
| 113 | + code: [ |
| 114 | + 'var Hello = React.createClass({', |
| 115 | + ' render: function() {', |
| 116 | + ' return <div>Multiple errors: \'>></div>;', |
| 117 | + ' }', |
| 118 | + '});' |
| 119 | + ].join('\n'), |
| 120 | + parserOptions: parserOptions, |
| 121 | + errors: [ |
| 122 | + {message: 'HTML entities must be escaped.'}, |
| 123 | + {message: 'HTML entities must be escaped.'}, |
| 124 | + {message: 'HTML entities must be escaped.'} |
| 125 | + ] |
| 126 | + }, { |
| 127 | + code: [ |
| 128 | + 'var Hello = React.createClass({', |
| 129 | + ' render: function() {', |
| 130 | + ' return <div>{"Unbalanced braces"}}</div>;', |
| 131 | + ' }', |
| 132 | + '});' |
| 133 | + ].join('\n'), |
| 134 | + parserOptions: parserOptions, |
| 135 | + errors: [{message: 'HTML entities must be escaped.'}] |
| 136 | + } |
| 137 | + ] |
| 138 | +}); |
0 commit comments