|
| 1 | +/** |
| 2 | + * @file Tests for scriptlets rules |
| 3 | + */ |
| 4 | + |
| 5 | +import { type AdblockTokenizer, getAdblockTokenizer } from '../common/get-adblock-tokenizer'; |
| 6 | +import { expectTokens } from '../common/token-expectation'; |
| 7 | + |
| 8 | +let tokenize: AdblockTokenizer; |
| 9 | + |
| 10 | +// Before running any tests, we should load the grammar and get the tokenizer |
| 11 | +beforeAll(async () => { |
| 12 | + tokenize = await getAdblockTokenizer(); |
| 13 | +}); |
| 14 | + |
| 15 | +describe('scriptlet rules', () => { |
| 16 | + describe('valid', () => { |
| 17 | + test('blocking', () => { |
| 18 | + // single quoted arguments |
| 19 | + expectTokens( |
| 20 | + tokenize, |
| 21 | + "#%#//scriptlet('foo', 'bar')", |
| 22 | + [ |
| 23 | + { fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 24 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 25 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 26 | + { fragment: "'foo'", scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 27 | + { fragment: ', ', scopes: ['text.adblock', 'keyword.operator.adblock'] }, |
| 28 | + { fragment: "'bar'", scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 29 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 30 | + ], |
| 31 | + ); |
| 32 | + |
| 33 | + // double quoted arguments |
| 34 | + expectTokens( |
| 35 | + tokenize, |
| 36 | + '#%#//scriptlet("foo", "bar")', |
| 37 | + [ |
| 38 | + { fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 39 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 40 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 41 | + { fragment: '"foo"', scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 42 | + { fragment: ', ', scopes: ['text.adblock', 'keyword.operator.adblock'] }, |
| 43 | + { fragment: '"bar"', scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 44 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 45 | + ], |
| 46 | + ); |
| 47 | + |
| 48 | + // should handle if the argument contain a different quote type |
| 49 | + expectTokens( |
| 50 | + tokenize, |
| 51 | + '#%#//scriptlet("foo\'bar")', |
| 52 | + [ |
| 53 | + { fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 54 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 55 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 56 | + { fragment: '"foo\'bar"', scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 57 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 58 | + ], |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('exception for specific scriptlet', () => { |
| 63 | + expectTokens( |
| 64 | + tokenize, |
| 65 | + "#@%#//scriptlet('foo')", |
| 66 | + [ |
| 67 | + { fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 68 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 69 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 70 | + { fragment: "'foo'", scopes: ['text.adblock', 'string.quoted.adblock'] }, |
| 71 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 72 | + ], |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test('exception for all scriptlets', () => { |
| 77 | + expectTokens( |
| 78 | + tokenize, |
| 79 | + '#@%#//scriptlet()', |
| 80 | + [ |
| 81 | + { fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 82 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 83 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 84 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 85 | + ], |
| 86 | + ); |
| 87 | + |
| 88 | + expectTokens( |
| 89 | + tokenize, |
| 90 | + '#@%#//scriptlet( )', |
| 91 | + [ |
| 92 | + { fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 93 | + { fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] }, |
| 94 | + { fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 95 | + { fragment: ' ', scopes: ['text.adblock', 'entity.name.section.adblock.empty-scriptlet'] }, |
| 96 | + { fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] }, |
| 97 | + ], |
| 98 | + ); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('invalid', () => { |
| 103 | + test('blocking', () => { |
| 104 | + expectTokens( |
| 105 | + tokenize, |
| 106 | + // blocking scriptlet rule cannot be used without any arguments |
| 107 | + '#%#//scriptlet()', |
| 108 | + [ |
| 109 | + { fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] }, |
| 110 | + { fragment: '//scriptlet()', scopes: ['text.adblock', 'invalid.illegal'] }, |
| 111 | + ], |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments