2019-07-30 09:35:42 +02:00
/* eslint-disable no-unused-vars */
2021-10-16 13:19:53 +02:00
const { splitCommandBatch } = require ( './string-utils' ) ;
2021-05-21 15:17:21 +02:00
const StringUtils = require ( './string-utils' ) ;
2018-12-29 19:24:02 +02:00
2023-02-20 17:02:29 +02:00
describe ( 'StringUtils' , ( ) => {
2018-12-29 19:24:02 +02:00
2022-11-15 12:23:50 +02:00
2018-12-29 19:24:02 +02:00
2020-12-01 20:05:24 +02:00
it ( 'should surround keywords with strings' , ( async ( ) => {
2018-12-29 19:24:02 +02:00
const testCases = [
2023-02-08 16:20:54 +02:00
[ [ ] , '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' ] ,
2019-01-31 11:48:48 +02:00
// [['test1', 'test2'], 'bla test1 blabla test1 bla test2 not this one - test22', 'a', 'b', 'bla atest1b blabla atest1b bla atest2b not this one - test22'],
2023-02-08 16:20:54 +02:00
[ [ 'test1' , 'test2' ] , 'bla test1 test1 bla test2' , '<span class="highlighted-keyword">' , '</span>' , null , 'bla <span class="highlighted-keyword">test1</span> <span class="highlighted-keyword">test1</span> bla <span class="highlighted-keyword">test2</span>' ] ,
2019-01-31 11:48:48 +02:00
// [[{ type:'regex', value:'test.*?'}], 'bla test1 test1 bla test2 test tttest', 'a', 'b', 'bla atest1b atest1b bla atest2b atestb tttest'],
2023-02-08 16:20:54 +02:00
[ [ '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,\\.,\\+\\-\\*\\?\\!\\=\\{\\}\\<\\>\\|\\:"\'\\(\\)\\[\\]]*?' } ] , 'dfasdf' , '[' , ']' , { escapeHtml : true } , '[d]fas[d]f' ] ,
[ [ 'zzz' ] , 'zzz<img src=q onerror=eval("require(\'child_process\').exec(\'mate-calc\');");>' , 'a' , 'b' , { escapeHtml : true } , 'azzzb<img src=q onerror=eval("require('child_process').exec('mate-calc');");>' ] ,
2018-12-29 19:24:02 +02:00
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const t = testCases [ i ] ;
const keywords = t [ 0 ] ;
const input = t [ 1 ] ;
const prefix = t [ 2 ] ;
const suffix = t [ 3 ] ;
2023-02-08 16:20:54 +02:00
const options = t [ 4 ] ;
const expected = t [ 5 ] ;
2018-12-29 19:24:02 +02:00
2023-02-08 16:20:54 +02:00
const actual = StringUtils . surroundKeywords ( keywords , input , prefix , suffix , options ) ;
2018-12-29 19:24:02 +02:00
2019-09-19 23:51:18 +02:00
expect ( actual ) . toBe ( expected , ` Test case ${ i } ` ) ;
2018-12-29 19:24:02 +02:00
}
2019-09-24 00:23:10 +02:00
} ) ) ;
2018-12-29 19:24:02 +02:00
2020-12-01 20:05:24 +02:00
it ( 'should find the next whitespace character' , ( async ( ) => {
2020-03-28 15:05:00 +02:00
const testCases = [
[ '' , [ [ 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 ] ] ] ,
] ;
2023-06-30 10:39:21 +02:00
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
2020-03-28 15:05:00 +02:00
testCases . forEach ( ( t , i ) => {
const str = t [ 0 ] ;
2023-06-30 10:39:21 +02:00
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
2020-03-28 15:05:00 +02:00
t [ 1 ] . forEach ( ( pair , j ) => {
const begin = pair [ 0 ] ;
const expected = pair [ 1 ] ;
const actual = StringUtils . nextWhitespaceIndex ( str , begin ) ;
expect ( actual ) . toBe ( expected , ` Test string ${ i } - case ${ j } ` ) ;
} ) ;
} ) ;
} ) ) ;
2021-10-16 13:19:53 +02:00
it ( 'should split the command batch by newlines not inside quotes' , ( async ( ) => {
const eol = '\n' ;
const testCases = [
[ '' ,
[ '' ] ] ,
[ '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 ` ] ] ,
] ;
2023-06-30 10:39:21 +02:00
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
2021-10-16 13:19:53 +02:00
testCases . forEach ( ( t ) => {
expect ( splitCommandBatch ( t [ 0 ] ) ) . toEqual ( t [ 1 ] ) ;
} ) ;
} ) ) ;
2019-07-30 09:35:42 +02:00
} ) ;