import { splitCommandBatch } from './string-utils'; import * as StringUtils from './string-utils'; describe('string-utils', () => { test.each([ [[], 'test', 'a', 'b', null, 'test'], [['test'], 'test', 'a', 'b', null, 'atestb'], [['test'], 'Test', 'a', 'b', null, 'aTestb'], [['te[]st'], 'Te[]st', 'a', 'b', null, 'aTe[]stb'], // [['test1', 'test2'], 'bla test1 blabla test1 bla test2 not this one - test22', 'a', 'b', 'bla atest1b blabla atest1b bla atest2b not this one - test22'], [['test1', 'test2'], 'bla test1 test1 bla test2', '', '', null, 'bla test1 test1 bla test2'], // [[{ type:'regex', value:'test.*?'}], 'bla test1 test1 bla test2 test tttest', 'a', 'b', 'bla atest1b atest1b bla atest2b atestb tttest'], [['test'], 'testTest', 'a', 'b', { escapeHtml: true }, 'atestbaTestb'], [['test'], 'test test Test', 'a', 'b', { escapeHtml: true }, 'atestb atestb aTestb'], [['d'], 'dfasdf', '[', ']', { escapeHtml: true }, '[d]fas[d]f'], [ [{ scriptType: 'en', type: 'regex', value: 'd*', valueRegex: 'd[^ \t\n\r,\\.,\\+\\-\\*\\?\\!\\=\\{\\}\\<\\>\\|\\:"\'\\(\\)\\[\\]]*?', } as StringUtils.KeywordObjectType], 'dfasdf', '[', ']', { escapeHtml: true }, '[d]fas[d]f', ], [['zzz'], 'zzz', 'a', 'b', { escapeHtml: true }, 'azzzb<img src=q onerror=eval("require('child_process').exec('mate-calc');");>'], [['a'], 'non-latin-chars-éão', '', '', { escapeHtml: true }, 'non-latin-chars-éão'], [['o'], 'Abrir diretório de perfis > (openProfileDirectory)', '', '', { escapeHtml: true }, 'Abrir diretório de perfis > (openProfileDirectory)'], [[], '<>"\'&', 'a', 'b', { escapeHtml: true }, '<>"'&'], [[], '<>"\'&', 'a', 'b', { escapeHtml: false }, '<>"\'&'], [['a'], 'non-latin-chars-éão', '<<<', '>>>', { escapeHtml: false }, 'non-l<<>>tin-ch<<>>rs-é<<<ã>>>o'], ])('should surround keywords with strings (case %#)', (async (keywords, input, prefix, suffix, options, expected) => { const actual = StringUtils.surroundKeywords(keywords, input, prefix, suffix, options); expect(actual).toBe(expected); })); test.each([ ['', [[0, 0]]], ['Joplin', [[0, 6], [3, 6], [6, 6]]], ['Joplin is a free, open source\n note taking and *to-do* application', [[0, 6], [12, 17], [23, 29], [48, 54]]], ])('should find the next whitespace character in string %s', (async (str, testCases) => { for (const range of testCases) { const begin = range[0]; const expected = range[1]; const actual = StringUtils.nextWhitespaceIndex(str, begin); expect(actual).toBe(expected); } })); const eol = '\n'; test.each([ ['', ['']], ['command1', ['command1']], ['command1 arg1 arg2 arg3', ['command1 arg1 arg2 arg3']], [`command1 arg1 'arg2${eol}continue' arg3`, [`command1 arg1 'arg2${eol}continue' arg3`]], [`command1 arg1 'arg2${eol}continue'${eol}command2${eol}command3 'arg1${eol}continue${eol}continue' arg2 arg3`, [`command1 arg1 'arg2${eol}continue'`, 'command2', `command3 'arg1${eol}continue${eol}continue' arg2 arg3`]], [`command1 arg\\1 'arg2${eol}continue\\'continue' arg3`, [`command1 arg\\1 'arg2${eol}continue\\'continue' arg3`]], ])('should split the command batch by newlines not inside quotes (case %#)', (async (batch, expected) => { expect(splitCommandBatch(batch)).toEqual(expected); })); });