@@ -497,4 +497,148 @@ describe('Section range tests', () => {
497497 assert . equal ( luresource . Sections [ 1 ] . Range . End . Line , 11 )
498498 assert . equal ( luresource . Sections [ 1 ] . Range . End . Character , 7 )
499499 } ) ;
500- } )
500+ } )
501+
502+ describe ( 'Section CRUD tests for insert and update sections with newline' , ( ) => {
503+ let luresource = undefined ;
504+
505+ let fileContent =
506+ `# ? who is CEO of Microsoft
507+ - Microsoft CEO
508+
509+ \`\`\`
510+ Satya Nadella
511+ \`\`\`` ;
512+
513+ let updatedQnAConent =
514+ `# ? who is CEO of Facebook
515+ - Facebook CEO
516+
517+ \`\`\`
518+ Mark Zuckerberg
519+ \`\`\`` ;
520+
521+ let insertQnAContent =
522+ `# ? how to greet
523+
524+ \`\`\`
525+ hello
526+ \`\`\``
527+
528+ let insertQnAContent2 =
529+ `# ? how to cancel
530+
531+ \`\`\`
532+ cancel that
533+ \`\`\``
534+
535+ let insertLuContent =
536+ `# welcome
537+ - welcome here`
538+
539+ let insertLuContent2 =
540+ `# stop
541+ - stop that`
542+
543+ it ( 'update qna section test' , ( ) => {
544+ luresource = luparser . parse ( fileContent ) ;
545+
546+ luresource = new SectionOperator ( luresource ) . updateSection ( luresource . Sections [ 0 ] . Id , `${ NEWLINE } ${ updatedQnAConent } ${ NEWLINE } ` ) ;
547+
548+ assert . equal ( luresource . Errors . length , 0 ) ;
549+ assert . equal ( luresource . Sections . length , 1 ) ;
550+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . QNASECTION ) ;
551+ assert . equal ( luresource . Sections [ 0 ] . Body . replace ( / \r \n / g, "\n" ) , `${ updatedQnAConent } \n` ) ;
552+ } ) ;
553+
554+ it ( 'insert qna section at begining test' , ( ) => {
555+ luresource = new SectionOperator ( luresource ) . insertSection ( luresource . Sections [ 0 ] . Id , `${ NEWLINE } ${ insertQnAContent } ` ) ;
556+
557+ assert . equal ( luresource . Errors . length , 0 ) ;
558+ assert . equal ( luresource . Sections . length , 2 ) ;
559+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . QNASECTION ) ;
560+ assert . equal ( luresource . Sections [ 1 ] . SectionType , LUSectionTypes . QNASECTION ) ;
561+ assert . equal ( luresource . Sections [ 0 ] . Body . replace ( / \r \n / g, "\n" ) , `${ insertQnAContent } ` ) ;
562+ assert . equal ( luresource . Sections [ 1 ] . Body . replace ( / \r \n / g, "\n" ) , `${ updatedQnAConent } \n` ) ;
563+ } ) ;
564+
565+ it ( 'insert qna section at middle test' , ( ) => {
566+ luresource = new SectionOperator ( luresource ) . insertSection ( luresource . Sections [ 1 ] . Id , `${ NEWLINE } ${ NEWLINE } ${ insertQnAContent2 } ${ NEWLINE } ` ) ;
567+
568+ assert . equal ( luresource . Errors . length , 0 ) ;
569+ assert . equal ( luresource . Sections . length , 3 ) ;
570+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . QNASECTION ) ;
571+ assert . equal ( luresource . Sections [ 1 ] . SectionType , LUSectionTypes . QNASECTION ) ;
572+ assert . equal ( luresource . Sections [ 2 ] . SectionType , LUSectionTypes . QNASECTION ) ;
573+ assert . equal ( luresource . Sections [ 0 ] . Body . replace ( / \r \n / g, "\n" ) , `${ insertQnAContent } \n\n` ) ;
574+ assert . equal ( luresource . Sections [ 1 ] . Body . replace ( / \r \n / g, "\n" ) , `${ insertQnAContent2 } \n` ) ;
575+ assert . equal ( luresource . Sections [ 2 ] . Body . replace ( / \r \n / g, "\n" ) , `${ updatedQnAConent } \n` ) ;
576+ assert . equal ( luresource . Content . replace ( / \r \n / g, "\n" ) , `\n\n${ insertQnAContent } \n\n\n${ insertQnAContent2 } \n\n${ updatedQnAConent } \n` ) ;
577+ } ) ;
578+
579+ it ( 'insert lu section at begining test' , ( ) => {
580+ luresource = new SectionOperator ( luresource ) . insertSection ( luresource . Sections [ 0 ] . Id , `${ NEWLINE } ${ insertLuContent } ${ NEWLINE } ${ NEWLINE } ` ) ;
581+
582+ assert . equal ( luresource . Errors . length , 0 ) ;
583+ assert . equal ( luresource . Sections . length , 4 ) ;
584+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . SIMPLEINTENTSECTION ) ;
585+ assert . equal ( `# welcome\n${ luresource . Sections [ 0 ] . Body . replace ( / \r \n / g, "\n" ) } ` , `${ insertLuContent } \n\n` ) ;
586+ } ) ;
587+
588+ it ( 'update qna section with lu section test' , ( ) => {
589+ luresource = new SectionOperator ( luresource ) . updateSection ( luresource . Sections [ 3 ] . Id , `${ NEWLINE } ${ insertLuContent2 } ${ NEWLINE } ` ) ;
590+
591+ assert . equal ( luresource . Errors . length , 0 ) ;
592+ assert . equal ( luresource . Sections . length , 4 ) ;
593+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . SIMPLEINTENTSECTION ) ;
594+ assert . equal ( luresource . Sections [ 1 ] . SectionType , LUSectionTypes . QNASECTION ) ;
595+ assert . equal ( luresource . Sections [ 2 ] . SectionType , LUSectionTypes . QNASECTION ) ;
596+ assert . equal ( luresource . Sections [ 3 ] . SectionType , LUSectionTypes . SIMPLEINTENTSECTION ) ;
597+ assert . equal ( `# welcome\n${ luresource . Sections [ 0 ] . Body . replace ( / \r \n / g, "\n" ) } ` , `${ insertLuContent } \n\n` ) ;
598+ assert . equal ( luresource . Sections [ 1 ] . Body . replace ( / \r \n / g, "\n" ) , `${ insertQnAContent } \n\n` ) ;
599+ assert . equal ( luresource . Sections [ 2 ] . Body . replace ( / \r \n / g, "\n" ) , `${ insertQnAContent2 } \n\n` ) ;
600+ assert . equal ( `# stop\n${ luresource . Sections [ 3 ] . Body . replace ( / \r \n / g, "\n" ) } ` , `${ insertLuContent2 } \n` ) ;
601+ assert . equal ( luresource . Content . replace ( / \r \n / g, "\n" ) , `\n\n\n${ insertLuContent } \n\n\n${ insertQnAContent } \n\n\n${ insertQnAContent2 } \n\n\n${ insertLuContent2 } \n` ) ;
602+ } ) ;
603+ } ) ;
604+
605+ describe ( 'Section CRUD tests for import section' , ( ) => {
606+ let luresource = undefined ;
607+
608+ let importContent1 = `[import](howto.source.qna)` ;
609+ let importContent2 = ` [import](guide.source.qna)` ;
610+
611+ let qnaContent =
612+ `# ? who is CEO of Microsoft
613+ - Microsoft CEO
614+
615+ \`\`\`
616+ Satya Nadella
617+ \`\`\`` ;
618+
619+ let insertImportConent = ` [import](windows.source.qna)` ;
620+
621+ it ( 'insert qna section test' , ( ) => {
622+ luresource = luparser . parse ( `${ importContent1 } ${ NEWLINE } ${ importContent2 } ${ NEWLINE } ${ NEWLINE } ${ qnaContent } ` ) ;
623+ luresource = new SectionOperator ( luresource ) . insertSection ( luresource . Sections [ 2 ] . Id , `${ NEWLINE } ${ insertImportConent } ` ) ;
624+
625+ assert . equal ( luresource . Errors . length , 0 ) ;
626+ assert . equal ( luresource . Sections . length , 4 ) ;
627+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . IMPORTSECTION ) ;
628+ assert . equal ( luresource . Sections [ 1 ] . SectionType , LUSectionTypes . IMPORTSECTION ) ;
629+ assert . equal ( luresource . Sections [ 2 ] . SectionType , LUSectionTypes . IMPORTSECTION ) ;
630+ assert . equal ( luresource . Sections [ 3 ] . SectionType , LUSectionTypes . QNASECTION ) ;
631+ assert . equal ( luresource . Content . replace ( / \r \n / g, "\n" ) , `${ importContent1 } \n${ importContent2 } \n\n\n${ insertImportConent } \n${ qnaContent } ` ) ;
632+ } ) ;
633+
634+ it ( 'delete qna section test' , ( ) => {
635+ luresource = new SectionOperator ( luresource ) . deleteSection ( luresource . Sections [ 1 ] . Id ) ;
636+
637+ assert . equal ( luresource . Errors . length , 0 ) ;
638+ assert . equal ( luresource . Sections . length , 3 ) ;
639+ assert . equal ( luresource . Sections [ 0 ] . SectionType , LUSectionTypes . IMPORTSECTION ) ;
640+ assert . equal ( luresource . Sections [ 1 ] . SectionType , LUSectionTypes . IMPORTSECTION ) ;
641+ assert . equal ( luresource . Sections [ 2 ] . SectionType , LUSectionTypes . QNASECTION ) ;
642+ assert . equal ( luresource . Content . replace ( / \r \n / g, "\n" ) , `${ importContent1 } \n\n\n${ insertImportConent } \n${ qnaContent } ` ) ;
643+ } ) ;
644+ } ) ;
0 commit comments